| 1 | -- LuaStateInit.lua |
|---|
| 2 | |
|---|
| 3 | -- Note: 'luaState' is a pointer to the LuaState instance that created this lua state |
|---|
| 4 | |
|---|
| 5 | -- Redirect print to the C++ print function |
|---|
| 6 | original_print = print |
|---|
| 7 | function print(s) |
|---|
| 8 | luaState:luaPrint(s) |
|---|
| 9 | end |
|---|
| 10 | |
|---|
| 11 | -- Create function to log text like COUT, but always prints a line! |
|---|
| 12 | function logMessage(level, message) |
|---|
| 13 | luaState:luaLog(level, message) |
|---|
| 14 | end |
|---|
| 15 | cout = logMessage |
|---|
| 16 | |
|---|
| 17 | -- Redirect dofile in order to load with the resource manager |
|---|
| 18 | original_dofile = dofile |
|---|
| 19 | function dofile(filename) |
|---|
| 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 |
|---|
| 24 | return LuaStateReturnValue -- C-injected global variable |
|---|
| 25 | end |
|---|
| 26 | doFile = dofile |
|---|
| 27 | |
|---|
| 28 | -- Create includeFile function that preparses the file according |
|---|
| 29 | -- to a function provided to the LuaState constructor (in C++) |
|---|
| 30 | function include(filename) |
|---|
| 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 |
|---|
| 35 | return LuaStateReturnValue -- C-injected global variable |
|---|
| 36 | end |
|---|
| 37 | |
|---|
| 38 | -- Replace require function with almost similar behaviour |
|---|
| 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 |
|---|
| 41 | _LOADED = {} |
|---|
| 42 | _LOADED_RETURN_VALUES = {} |
|---|
| 43 | _REQUIREDNAME = nil |
|---|
| 44 | original_require = require |
|---|
| 45 | function require(moduleName) |
|---|
| 46 | if not luaState:fileExists(moduleName .. ".lua") then |
|---|
| 47 | logMessage(2, "Warning: Lua function require() could not find file '" .. moduleName .. ".lua' ") |
|---|
| 48 | return nil |
|---|
| 49 | end |
|---|
| 50 | |
|---|
| 51 | if not _LOADED[moduleName] then |
|---|
| 52 | -- save old value for the required name |
|---|
| 53 | local _REQUIREDNAME_OLD = _REQUIREDNAME |
|---|
| 54 | _REQUIREDNAME = moduleName |
|---|
| 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 | |
|---|
| 64 | -- restore old value |
|---|
| 65 | _REQUIREDNAME = _REQUIREDNAME_OLD |
|---|
| 66 | end |
|---|
| 67 | return _LOADED_RETURN_VALUES[moduleName] |
|---|
| 68 | end |
|---|
| 69 | |
|---|
| 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 |
|---|
| 79 | function pause() |
|---|
| 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 |
|---|
| 86 | function errorHandler(err) |
|---|
| 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 | |
|---|
| 107 | -- Convenience function for console commands |
|---|
| 108 | orxonox.execute = function(command) |
|---|
| 109 | orxonox.CommandExecutor:execute(command) |
|---|
| 110 | end |
|---|
| 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 |
|---|