-- Note: luaState is a pointer to the LuaState instance that created this lua state -- Redirect print to the C++ print function original_print = print print = function(s) luaState:luaPrint(s) end -- Create function to log text like COUT, but always prints a line! logMessage = function(level, message) luaState:luaLog(level, message) end cout = logMessage -- Redirect dofile in order to load with the resource manager original_dofile = dofile dofile = function(filename) luaState:doFile(filename) -- Required because if the file returns a table, it cannot be passed through the C++ function return LuaStateReturnValue -- C-injected global variable end doFile = dofile -- Create includeFile function that preparses the file according -- to a function provided to the LuaState constructor (in C++) include = function(filename) luaState:includeFile(filename) -- Required because if the file returns a table, it cannot be passed through the C++ function return LuaStateReturnValue -- C-injected global variable end -- Replace require function with almost similar behaviour -- The loaded modules are then stored with their names (where name has no .lua extension) -- Furthermore the ".lua" extension is appended to the moduleName parameter when looking for the file original_require = require _REQUIREDNAME = "" LuaStateReturnValue = true require = function(moduleName) if not luaState:fileExists(moduleName .. ".lua") then logMessage(2, "Warning: Lua function require() could not find file '" .. moduleName .. ".lua' ") return nil end if not _LOADED then _LOADED = {} end if _LOADED[moduleName] == nil then -- save old value local _REQUIREDNAME_OLD = _REQUIREDNAME _REQUIREDNAME = moduleName luaState:doFile(moduleName .. ".lua") -- LuaStateReturnValue is required because if the file returns a table, -- it cannot be passed through the C++ function if LuaStateReturnValue == nil then -- C-injected global variable LuaStateReturnValue = true end _LOADED[moduleName] = LuaStateReturnValue -- This entry must never be nil -- restore old value _REQUIREDNAME = _REQUIREDNAME_OLD end return _LOADED[moduleName] end -- Include command line debugger for lua 5.1 -- Note: It doesn't work if the IOConsole was started. Then we replace pause() with a warning if _VERSION ~= "Lua 5.0" and not luaState:usingIOConsole() then require("Debugger") else -- Fallback pause function pause = function() logMessage(2, [["Warning: debug() called in Lua, but Debugger is not active. Do you have the IOConsole disabled and are you using Lua version 5.1?"]]) end end -- General error handler that gets called whenever an error happens at runtime errorHandler = function(err) -- Display the error message if type(err) == "string" then logMessage(1, "Lua runtime error: "..err) end -- Start debugger if possible if _LOADED and _LOADED["Debugger"] ~= nil then pause() else -- Fallback: print stack trace logMessage(1, debug.traceback(2)) end return err -- Hello Lua debugger user! Please type 'set 2' to get to the -- actual position in the stack where the error occurred end -- Convenience function for console commands orxonox.execute = function(command) orxonox.CommandExecutor:execute(command) end -- Convenience function for config values orxonox.getConfig = function(section, entry) return orxonox.SettingsConfigFile:getInstance():getConfig(section, entry) end orxonox.config = function(section, entry, value) return orxonox.SettingsConfigFile:getInstance():config(section, entry, value) end orxonox.tconfig = function(section, entry, value) return orxonox.SettingsConfigFile:getInstance():tconfig(section, entry, value) end