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