Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/data/lua/LuaStateInit.lua @ 8896

Last change on this file since 8896 was 8858, checked in by landauf, 13 years ago

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

  • orxout levelname message
  • orxout_context levelname context message
  • shortcuts: log message, error message, warning message, status message, info message, debug message
  • Property svn:eol-style set to native
File size: 4.5 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-- 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)
15orxout = 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
23end
24
25-- Redirect dofile in order to load with the resource manager
26original_dofile = dofile
27dofile = 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
33end
34doFile = dofile
35
36-- Create includeFile function that preparses the file according
37-- to a function provided to the LuaState constructor (in C++)
38include = 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
44end
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
49original_require = require
50_REQUIREDNAME = ""
51LuaStateReturnValue = true
52require = 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
83end
84
85
86-- Load useful tool functions (like handleDefaultArgument)
87require("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
92if _VERSION ~= "Lua 5.0"  and not luaState:usingIOConsole() then
93  require("Debugger")
94else
95  -- Fallback pause function
96  pause = function()
97    orxout(orxonox.level.internal_warning, [["Warning: debug() called in Lua, but Debugger is not active.
98Do you have the IOConsole disabled and are you using Lua version 5.1?"]])
99  end
100end
101
102-- General error handler that gets called whenever an error happens at runtime
103errorHandler = 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
122end
123
124
125-- Convenience function for console commands
126orxonox.execute = function(command)
127  orxonox.CommandExecutor:execute(command)
128end
129
130-- Convenience function for config values
131orxonox.getConfig = function(section, entry)
132  return orxonox.SettingsConfigFile:getInstance():getConfig(section, entry)
133end
134orxonox.config = function(section, entry, value)
135  return orxonox.SettingsConfigFile:getInstance():config(section, entry, value)
136end
137orxonox.tconfig = function(section, entry, value)
138  return orxonox.SettingsConfigFile:getInstance():tconfig(section, entry, value)
139end
Note: See TracBrowser for help on using the repository browser.