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