[5654] | 1 | -- Note: luaState is a pointer to the LuaState instance that created this lua state |
---|
| 2 | |
---|
[5661] | 3 | -- Save original print function in debug |
---|
[5654] | 4 | debug = print |
---|
| 5 | |
---|
| 6 | -- Redirect print to the C++ print function |
---|
| 7 | print = function(s) |
---|
| 8 | luaState:luaPrint(s) |
---|
| 9 | end |
---|
| 10 | |
---|
| 11 | -- Create function to log text like COUT, but always prints a line! |
---|
| 12 | logMessage = function(level, message) |
---|
| 13 | luaState:luaLog(level, message) |
---|
| 14 | end |
---|
| 15 | |
---|
| 16 | -- Redirect dofile in order to load with the resource manager |
---|
[6417] | 17 | doFile = function(filename) |
---|
| 18 | luaState:doFile(filename) |
---|
[5661] | 19 | -- Required because the C++ function cannot return whatever might be on the stack |
---|
[6417] | 20 | return LuaStateReturnValue -- C-injected global variable |
---|
[5654] | 21 | end |
---|
[5661] | 22 | original_dofile = dofile |
---|
[5654] | 23 | dofile = doFile |
---|
| 24 | |
---|
| 25 | -- Create includeFile function that preparses the file according |
---|
| 26 | -- to a function provided to the LuaState constructor (in C++) |
---|
[6417] | 27 | include = function(filename) |
---|
| 28 | luaState:includeFile(filename) |
---|
[5661] | 29 | -- Required because the C++ function cannot return whatever might be on the stack |
---|
[6417] | 30 | return LuaStateReturnValue -- C-injected global variable |
---|
[5654] | 31 | end |
---|
[5661] | 32 | |
---|
| 33 | -- Replace require function with almost similar behaviour |
---|
[6417] | 34 | -- The loaded modules are then stored with their names (where name has no .lua extension) |
---|
| 35 | -- Furthermore the ".lua" extension is appended to the moduleName parameter when looking for the file |
---|
[5661] | 36 | old_require = require |
---|
[6417] | 37 | require = function(moduleName) |
---|
| 38 | if not luaState:fileExists(moduleName .. ".lua") then |
---|
[5661] | 39 | return nil |
---|
| 40 | end |
---|
| 41 | if not _LOADED then |
---|
| 42 | _LOADED = {} |
---|
| 43 | end |
---|
| 44 | if not _LOADED[moduleName] then |
---|
| 45 | -- save old value |
---|
| 46 | _REQUIREDNAME_OLD = _REQUIREDNAME |
---|
| 47 | _REQUIREDNAME = moduleName |
---|
[6417] | 48 | luaState:doFile(moduleName .. ".lua") |
---|
[5661] | 49 | _LOADED[moduleName] = LuaStateReturnValue or true |
---|
| 50 | -- restore old value |
---|
| 51 | _REQUIREDNAME = _REQUIREDNAME_OLD |
---|
| 52 | end |
---|
| 53 | return _LOADED[moduleName] |
---|
| 54 | end |
---|
[6417] | 55 | |
---|
| 56 | -- Convenience function for console commands |
---|
| 57 | orxonox.execute = function(command) |
---|
| 58 | orxonox.CommandExecutor:execute(command) |
---|
| 59 | end |
---|