Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestates2/data/lua/LuaStateInit.lua @ 6665

Last change on this file since 6665 was 6665, checked in by rgrieder, 14 years ago

Fixed problems with the return value of require()

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1-- Note: luaState is a pointer to the LuaState instance that created this lua state
2
3-- Redirect print to the C++ print function
4original_print = print
5print = function(s)
6  luaState:luaPrint(s)
7end
8
9-- Create function to log text like COUT, but always prints a line!
10logMessage = function(level, message)
11  luaState:luaLog(level, message)
12end
13cout = logMessage
14
15-- Redirect dofile in order to load with the resource manager
16original_dofile = dofile
17dofile = function(filename)
18  luaState:doFile(filename)
19  -- Required because if the file returns a table, it cannot be passed through the C++ function
20  return LuaStateReturnValue -- C-injected global variable
21end
22doFile = dofile
23
24-- Create includeFile function that preparses the file according
25-- to a function provided to the LuaState constructor (in C++)
26include = function(filename)
27  luaState:includeFile(filename)
28  -- Required because if the file returns a table, it cannot be passed through the C++ function
29  return LuaStateReturnValue -- C-injected global variable
30end
31
32-- Replace require function with almost similar behaviour
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
35original_require = require
36_REQUIREDNAME = ""
37LuaStateReturnValue = true
38require = function(moduleName)
39  if not luaState:fileExists(moduleName .. ".lua") then
40    logMessage(2, "Warning: Lua function require() could not find file '" .. moduleName .. ".lua' ")
41    return nil
42  end
43
44  if not _LOADED then
45    _LOADED = {}
46  end
47  if not _LOADED_RETURN_VALUES then
48      _LOADED_RETURN_VALUES = {}
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    luaState:doFile(moduleName .. ".lua")
57    -- LuaStateReturnValue is required because if the file returns a table,
58    -- it cannot be passed through the C++ function
59    _LOADED_RETURN_VALUES[moduleName] = LuaStateReturnValue
60    _LOADED[moduleName] = true
61
62    -- restore old value
63    _REQUIREDNAME = _REQUIREDNAME_OLD
64  end
65  local asdf = _LOADED_RETURN_VALUES[moduleName]
66  return asdf
67end
68
69
70-- Include command line debugger for lua 5.1
71-- Note: It doesn't work if the IOConsole was started. Then we replace pause() with a warning
72if _VERSION ~= "Lua 5.0"  and not luaState:usingIOConsole() then
73  require("Debugger")
74else
75  -- Fallback pause function
76  pause = function()
77    logMessage(2, [["Warning: debug() called in Lua, but Debugger is not active.
78Do you have the IOConsole disabled and are you using Lua version 5.1?"]])
79  end
80end
81
82-- General error handler that gets called whenever an error happens at runtime
83errorHandler = function(err)
84  -- Display the error message
85  if type(err) == "string" then
86    logMessage(1, "Lua runtime error: "..err)
87  end
88
89  -- Start debugger if possible
90  if _LOADED and _LOADED["Debugger"] ~= nil then
91    pause()
92  else
93    -- Fallback: print stack trace
94    logMessage(1, debug.traceback(2))
95  end
96  return err -- Hello Lua debugger user! Please type 'set 2' to get to the
97             -- actual position in the stack where the error occurred
98end
99
100
101-- Convenience function for console commands
102orxonox.execute = function(command)
103  orxonox.CommandExecutor:execute(command)
104end
105
106-- Convenience function for config values
107orxonox.getConfig = function(section, entry)
108  return orxonox.SettingsConfigFile:getInstance():getConfig(section, entry)
109end
110orxonox.config = function(section, entry, value)
111  return orxonox.SettingsConfigFile:getInstance():config(section, entry, value)
112end
113orxonox.tconfig = function(section, entry, value)
114  return orxonox.SettingsConfigFile:getInstance():tconfig(section, entry, value)
115end
Note: See TracBrowser for help on using the repository browser.