Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 31, 2010, 1:05:28 AM (14 years ago)
Author:
rgrieder
Message:

Merged revisions 6621-6661 to gamestates2.

Location:
code/branches/gamestates2
Files:
2 edited
2 copied

Legend:

Unmodified
Added
Removed
  • code/branches/gamestates2

  • code/branches/gamestates2/data/lua/LuaStateInit.lua

    r6536 r6662  
    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)
     16original_dofile = dofile
     17dofile = function(filename)
    1818  luaState:doFile(filename)
    19   -- Required because the C++ function cannot return whatever might be on the stack
     19  -- Required because if the file returns a table, it cannot be passed through the C++ function
    2020  return LuaStateReturnValue -- C-injected global variable
    2121end
    22 original_dofile = dofile
    23 dofile = doFile
     22doFile = dofile
    2423
    2524-- Create includeFile function that preparses the file according
     
    2726include = function(filename)
    2827  luaState:includeFile(filename)
    29   -- Required because the C++ function cannot return whatever might be on the stack
     28  -- Required because if the file returns a table, it cannot be passed through the C++ function
    3029  return LuaStateReturnValue -- C-injected global variable
    3130end
     
    3433-- The loaded modules are then stored with their names (where name has no .lua extension)
    3534-- Furthermore the ".lua" extension is appended to the moduleName parameter when looking for the file
    36 old_require = require
     35original_require = require
     36_REQUIREDNAME = ""
     37LuaStateReturnValue = true
    3738require = function(moduleName)
    3839  if not luaState:fileExists(moduleName .. ".lua") then
     40    logMessage(2, "Warning: Lua function require() could not find file '" .. moduleName .. ".lua' ")
    3941    return nil
    4042  end
     
    4244    _LOADED = {}
    4345  end
    44   if not _LOADED[moduleName] then
     46  if _LOADED[moduleName] == nil then
    4547    -- save old value
    46     _REQUIREDNAME_OLD = _REQUIREDNAME
     48    local _REQUIREDNAME_OLD = _REQUIREDNAME
    4749    _REQUIREDNAME = moduleName
    4850    luaState:doFile(moduleName .. ".lua")
    49     _LOADED[moduleName] = LuaStateReturnValue or true
     51    -- LuaStateReturnValue is required because if the file returns a table,
     52    -- it cannot be passed through the C++ function
     53    if LuaStateReturnValue == nil then -- C-injected global variable
     54        LuaStateReturnValue = true
     55    end
     56    _LOADED[moduleName] = LuaStateReturnValue -- This entry must never be nil
    5057    -- restore old value
    5158    _REQUIREDNAME = _REQUIREDNAME_OLD
     
    5360  return _LOADED[moduleName]
    5461end
     62
     63
     64-- Include command line debugger for lua 5.1
     65-- Note: It doesn't work if the IOConsole was started. Then we replace pause() with a warning
     66if _VERSION ~= "Lua 5.0"  and not luaState:usingIOConsole() then
     67  require("Debugger")
     68else
     69  -- Fallback pause function
     70  pause = function()
     71    logMessage(2, [["Warning: debug() called in Lua, but Debugger is not active.
     72Do you have the IOConsole disabled and are you using Lua version 5.1?"]])
     73  end
     74end
     75
     76-- General error handler that gets called whenever an error happens at runtime
     77errorHandler = function(err)
     78  -- Display the error message
     79  if type(err) == "string" then
     80    logMessage(1, "Lua runtime error: "..err)
     81  end
     82
     83  -- Start debugger if possible
     84  if _LOADED and _LOADED["Debugger"] ~= nil then
     85    pause()
     86  else
     87    -- Fallback: print stack trace
     88    logMessage(1, debug.traceback(2))
     89  end
     90  return err -- Hello Lua debugger user! Please type 'set 2' to get to the
     91             -- actual position in the stack where the error occurred
     92end
     93
    5594
    5695-- Convenience function for console commands
Note: See TracChangeset for help on using the changeset viewer.