Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 19, 2009, 12:19:11 AM (15 years ago)
Author:
rgrieder
Message:

Cleaned out the lua script files for the GUI.
Also replaced "require" function to support resources.
Fixed a problem with the return value of doFile, includeFile and require being discarded because the tolua binding is for a C++ function returning void.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/resource2/data/lua/LuaStateInit.lua

    r5654 r5661  
    11-- Note: luaState is a pointer to the LuaState instance that created this lua state
    22
    3 -- Redirect debug to print
     3-- Save original print function in debug
    44debug = print
    55
     
    2323  resourceGroup = resourceGroup or "NoResourceGroupProvided"
    2424  luaState:doFile(filename, resourceGroup, bSearchOtherPaths)
     25  -- Required because the C++ function cannot return whatever might be on the stack
     26  return LuaStateReturnValue
    2527end
     28original_dofile = dofile
    2629dofile = doFile
    2730
     
    3336  resourceGroup = resourceGroup or "NoResourceGroupProvided"
    3437  luaState:includeFile(filename, resourceGroup, bSearchOtherPaths)
     38  -- Required because the C++ function cannot return whatever might be on the stack
     39  return LuaStateReturnValue
    3540end
     41
     42-- Replace require function with almost similar behaviour
     43-- The difference is that you need to provide a resource group
     44-- Default value there is the current one (if present) or else "General"
     45-- But the loaded modules are then stored with only with the name (where name has no .lua extension)
     46-- CAUTION: That also means that you need to take care of conflicting filenames among groups
     47-- Furthermore the moduleName parameters is appended with the .lua extension when looking for the file
     48old_require = require
     49require = function(moduleName, resourceGroup)
     50  local bSearchOtherPaths = (resourceGroup == nil) or false
     51  resourceGroup = resourceGroup or "NoResourceGroupProvided"
     52  if not luaState:fileExists(moduleName .. ".lua", resourceGroup, bSearchOtherPaths) then
     53    return nil
     54  end
     55  if not _LOADED then
     56    _LOADED = {}
     57  end
     58  if not _LOADED[moduleName] then
     59    -- save old value
     60    _REQUIREDNAME_OLD = _REQUIREDNAME
     61    _REQUIREDNAME = moduleName
     62    luaState:doFile(moduleName .. ".lua", resourceGroup, bSearchOtherPaths)
     63    _LOADED[moduleName] = LuaStateReturnValue or true
     64    -- restore old value
     65    _REQUIREDNAME = _REQUIREDNAME_OLD
     66  end
     67  return _LOADED[moduleName]
     68end
Note: See TracChangeset for help on using the changeset viewer.