Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutoriallevel/data/lua/LuaStateInit.lua @ 8487

Last change on this file since 8487 was 6746, checked in by rgrieder, 15 years ago

Merged gamestates2 branch back to trunk.
This brings in some heavy changes in the GUI framework.
It should also fix problems with triggered asserts in the InputManager.

Note: PickupInventory does not seem to work —> Segfault when showing because before, the owner in GUIOverlay::setGUIName is already NULL.
I haven't tested it before, so I can't tell whether it's my changes.

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