| 1 | -- Note: luaState is a pointer to the LuaState instance that created this lua state | 
|---|
| 2 |  | 
|---|
| 3 | -- Redirect print to the C++ print function | 
|---|
| 4 | original_print = print | 
|---|
| 5 | print = function(s) | 
|---|
| 6 | luaState:luaPrint(s) | 
|---|
| 7 | end | 
|---|
| 8 |  | 
|---|
| 9 | -- Create function to log text like COUT, but always prints a line! | 
|---|
| 10 | logMessage = function(level, message) | 
|---|
| 11 | luaState:luaLog(level, message) | 
|---|
| 12 | end | 
|---|
| 13 | cout = logMessage | 
|---|
| 14 |  | 
|---|
| 15 | -- Redirect dofile in order to load with the resource manager | 
|---|
| 16 | original_dofile = dofile | 
|---|
| 17 | dofile = function(filename) | 
|---|
| 18 | if not luaState:doFile(filename) then | 
|---|
| 19 | error("Error propagation. Do not display") | 
|---|
| 20 | end | 
|---|
| 21 | -- Required because if the file returns a table, it cannot be passed through the C++ function | 
|---|
| 22 | return LuaStateReturnValue -- C-injected global variable | 
|---|
| 23 | end | 
|---|
| 24 | doFile = dofile | 
|---|
| 25 |  | 
|---|
| 26 | -- Create includeFile function that preparses the file according | 
|---|
| 27 | -- to a function provided to the LuaState constructor (in C++) | 
|---|
| 28 | include = function(filename) | 
|---|
| 29 | if not luaState:includeFile(filename) then | 
|---|
| 30 | error("Error propagation. Do not display") | 
|---|
| 31 | end | 
|---|
| 32 | -- Required because if the file returns a table, it cannot be passed through the C++ function | 
|---|
| 33 | return LuaStateReturnValue -- C-injected global variable | 
|---|
| 34 | end | 
|---|
| 35 |  | 
|---|
| 36 | -- Replace require function with almost similar behaviour | 
|---|
| 37 | -- The loaded modules are then stored with their names (where name has no .lua extension) | 
|---|
| 38 | -- Furthermore the ".lua" extension is appended to the moduleName parameter when looking for the file | 
|---|
| 39 | original_require = require | 
|---|
| 40 | _REQUIREDNAME = "" | 
|---|
| 41 | LuaStateReturnValue = true | 
|---|
| 42 | require = function(moduleName) | 
|---|
| 43 | if not luaState:fileExists(moduleName .. ".lua") then | 
|---|
| 44 | logMessage(2, "Warning: Lua function require() could not find file '" .. moduleName .. ".lua' ") | 
|---|
| 45 | return nil | 
|---|
| 46 | end | 
|---|
| 47 |  | 
|---|
| 48 | if not _LOADED then | 
|---|
| 49 | _LOADED = {} | 
|---|
| 50 | end | 
|---|
| 51 | if not _LOADED_RETURN_VALUES then | 
|---|
| 52 | _LOADED_RETURN_VALUES = {} | 
|---|
| 53 | end | 
|---|
| 54 |  | 
|---|
| 55 | if not _LOADED[moduleName] then | 
|---|
| 56 | -- save old value for the required name | 
|---|
| 57 | local _REQUIREDNAME_OLD = _REQUIREDNAME | 
|---|
| 58 | _REQUIREDNAME = moduleName | 
|---|
| 59 |  | 
|---|
| 60 | if not luaState:doFile(moduleName .. ".lua") then | 
|---|
| 61 | error("Error propagation. Do not display") | 
|---|
| 62 | end | 
|---|
| 63 | -- LuaStateReturnValue is required because if the file returns a table, | 
|---|
| 64 | -- it cannot be passed through the C++ function | 
|---|
| 65 | _LOADED_RETURN_VALUES[moduleName] = LuaStateReturnValue | 
|---|
| 66 | _LOADED[moduleName] = true | 
|---|
| 67 |  | 
|---|
| 68 | -- restore old value | 
|---|
| 69 | _REQUIREDNAME = _REQUIREDNAME_OLD | 
|---|
| 70 | end | 
|---|
| 71 | local asdf = _LOADED_RETURN_VALUES[moduleName] | 
|---|
| 72 | return asdf | 
|---|
| 73 | end | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 | -- Load useful tool functions (like handleDefaultArgument) | 
|---|
| 77 | require("Tools") | 
|---|
| 78 |  | 
|---|
| 79 |  | 
|---|
| 80 | -- Include command line debugger for lua 5.1 | 
|---|
| 81 | -- Note: It doesn't work if the IOConsole was started. Then we replace pause() with a warning | 
|---|
| 82 | if _VERSION ~= "Lua 5.0"  and not luaState:usingIOConsole() then | 
|---|
| 83 | require("Debugger") | 
|---|
| 84 | else | 
|---|
| 85 | -- Fallback pause function | 
|---|
| 86 | pause = function() | 
|---|
| 87 | logMessage(2, [["Warning: debug() called in Lua, but Debugger is not active. | 
|---|
| 88 | Do you have the IOConsole disabled and are you using Lua version 5.1?"]]) | 
|---|
| 89 | end | 
|---|
| 90 | end | 
|---|
| 91 |  | 
|---|
| 92 | -- General error handler that gets called whenever an error happens at runtime | 
|---|
| 93 | errorHandler = function(err) | 
|---|
| 94 | if type(err) == "string" then | 
|---|
| 95 | -- Simply return if the error has already been handled | 
|---|
| 96 | if string.find(err, "Error propagation. Do not display") ~= nil then | 
|---|
| 97 | return err | 
|---|
| 98 | end | 
|---|
| 99 | -- Display the error message | 
|---|
| 100 | logMessage(1, "Lua runtime error: "..err) | 
|---|
| 101 | end | 
|---|
| 102 |  | 
|---|
| 103 | -- Start debugger if possible | 
|---|
| 104 | if _LOADED and _LOADED["Debugger"] ~= nil then | 
|---|
| 105 | pause() | 
|---|
| 106 | else | 
|---|
| 107 | -- Fallback: print stack trace | 
|---|
| 108 | logMessage(3, debug.traceback("")) | 
|---|
| 109 | end | 
|---|
| 110 | return err -- Hello Lua debugger user! Please type 'set 2' to get to the | 
|---|
| 111 | -- actual position in the stack where the error occurred | 
|---|
| 112 | end | 
|---|
| 113 |  | 
|---|
| 114 |  | 
|---|
| 115 | -- Convenience function for console commands | 
|---|
| 116 | orxonox.execute = function(command) | 
|---|
| 117 | orxonox.CommandExecutor:execute(command) | 
|---|
| 118 | end | 
|---|
| 119 |  | 
|---|
| 120 | -- Convenience function for config values | 
|---|
| 121 | orxonox.getConfig = function(section, entry) | 
|---|
| 122 | return orxonox.SettingsConfigFile:getInstance():getConfig(section, entry) | 
|---|
| 123 | end | 
|---|
| 124 | orxonox.config = function(section, entry, value) | 
|---|
| 125 | return orxonox.SettingsConfigFile:getInstance():config(section, entry, value) | 
|---|
| 126 | end | 
|---|
| 127 | orxonox.tconfig = function(section, entry, value) | 
|---|
| 128 | return orxonox.SettingsConfigFile:getInstance():tconfig(section, entry, value) | 
|---|
| 129 | end | 
|---|