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