Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6746 for code/trunk/data/lua


Ignore:
Timestamp:
Apr 16, 2010, 2:50:16 PM (15 years ago)
Author:
rgrieder
Message:

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.

Location:
code/trunk
Files:
2 edited
3 copied

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/data/lua/LuaStateInit.lua

    r6536 r6746  
    11-- Note: luaState is a pointer to the LuaState instance that created this lua state
    22
    3 -- Save original print function in debug
    4 debug = print
    5 
    63-- Redirect print to the C++ print function
     4original_print = print
    75print = function(s)
    86  luaState:luaPrint(s)
     
    1311  luaState:luaLog(level, message)
    1412end
     13cout = logMessage
    1514
    1615-- Redirect dofile in order to load with the resource manager
    17 doFile = function(filename)
    18   luaState:doFile(filename)
    19   -- Required because the C++ function cannot return whatever might be on the stack
     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
    2022  return LuaStateReturnValue -- C-injected global variable
    2123end
    22 original_dofile = dofile
    23 dofile = doFile
     24doFile = dofile
    2425
    2526-- Create includeFile function that preparses the file according
    2627-- to a function provided to the LuaState constructor (in C++)
    2728include = function(filename)
    28   luaState:includeFile(filename)
    29   -- Required because the C++ function cannot return whatever might be on the stack
     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
    3033  return LuaStateReturnValue -- C-injected global variable
    3134end
     
    3437-- The loaded modules are then stored with their names (where name has no .lua extension)
    3538-- Furthermore the ".lua" extension is appended to the moduleName parameter when looking for the file
    36 old_require = require
     39original_require = require
     40_REQUIREDNAME = ""
     41LuaStateReturnValue = true
    3742require = function(moduleName)
    3843  if not luaState:fileExists(moduleName .. ".lua") then
     44    logMessage(2, "Warning: Lua function require() could not find file '" .. moduleName .. ".lua' ")
    3945    return nil
    4046  end
     47
    4148  if not _LOADED then
    4249    _LOADED = {}
    4350  end
     51  if not _LOADED_RETURN_VALUES then
     52      _LOADED_RETURN_VALUES = {}
     53  end
     54
    4455  if not _LOADED[moduleName] then
    45     -- save old value
    46     _REQUIREDNAME_OLD = _REQUIREDNAME
     56    -- save old value for the required name
     57    local _REQUIREDNAME_OLD = _REQUIREDNAME
    4758    _REQUIREDNAME = moduleName
    48     luaState:doFile(moduleName .. ".lua")
    49     _LOADED[moduleName] = LuaStateReturnValue or true
     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
    5068    -- restore old value
    5169    _REQUIREDNAME = _REQUIREDNAME_OLD
    5270  end
    53   return _LOADED[moduleName]
     71  local asdf = _LOADED_RETURN_VALUES[moduleName]
     72  return asdf
    5473end
     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
    55114
    56115-- Convenience function for console commands
Note: See TracChangeset for help on using the changeset viewer.