Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7625 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
RevLine 
[6773]1-- LuaStateInit.lua
[5654]2
[6773]3-- Note: 'luaState' is a pointer to the LuaState instance that created this lua state
4
[5654]5-- Redirect print to the C++ print function
[6746]6original_print = print
[6773]7function print(s)
[5654]8  luaState:luaPrint(s)
9end
10
11-- Create function to log text like COUT, but always prints a line!
[6773]12function logMessage(level, message)
[5654]13  luaState:luaLog(level, message)
14end
[6746]15cout = logMessage
[5654]16
17-- Redirect dofile in order to load with the resource manager
[6746]18original_dofile = dofile
[6773]19function dofile(filename)
[6746]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
[6417]24  return LuaStateReturnValue -- C-injected global variable
[5654]25end
[6746]26doFile = dofile
[5654]27
28-- Create includeFile function that preparses the file according
29-- to a function provided to the LuaState constructor (in C++)
[6773]30function include(filename)
[6746]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
[6417]35  return LuaStateReturnValue -- C-injected global variable
[5654]36end
[5661]37
38-- Replace require function with almost similar behaviour
[6417]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
[6773]41_LOADED = {}
42_LOADED_RETURN_VALUES = {}
43_REQUIREDNAME = nil
[6746]44original_require = require
[6773]45function require(moduleName)
[6417]46  if not luaState:fileExists(moduleName .. ".lua") then
[6746]47    logMessage(2, "Warning: Lua function require() could not find file '" .. moduleName .. ".lua' ")
[5661]48    return nil
49  end
[6746]50
[5661]51  if not _LOADED[moduleName] then
[6746]52    -- save old value for the required name
53    local _REQUIREDNAME_OLD = _REQUIREDNAME
[5661]54    _REQUIREDNAME = moduleName
[6746]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
[5661]64    -- restore old value
65    _REQUIREDNAME = _REQUIREDNAME_OLD
66  end
[6773]67  return _LOADED_RETURN_VALUES[moduleName]
[5661]68end
[6417]69
[6746]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
[6773]79  function pause()
[6746]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
[6773]86function errorHandler(err)
[6746]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
[6417]107-- Convenience function for console commands
108orxonox.execute = function(command)
109  orxonox.CommandExecutor:execute(command)
110end
[6536]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.