Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestates3/data/lua/LuaStateInit.lua @ 7542

Last change on this file since 7542 was 6773, checked in by rgrieder, 15 years ago

Eliminated all unnecessary global Lua variables and replaced them either with a local or a instance variable (P.myVar).

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