Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource2/data/lua/LuaStateInit.lua @ 5768

Last change on this file since 5768 was 5661, checked in by rgrieder, 15 years ago

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.

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1-- Note: luaState is a pointer to the LuaState instance that created this lua state
2
3-- Save original print function in debug
4debug = print
5
6-- Redirect print to the C++ print function
7print = function(s)
8  luaState:luaPrint(s)
9end
10
11-- Create function to log text like COUT, but always prints a line!
12logMessage = function(level, message)
13  luaState:luaLog(level, message)
14end
15
16-- Redirect dofile in order to load with the resource manager
17-- Note: The function does not behave exactly like LuaState::doFile because the
18--       default argument here for the group is not "General" but
19--       "NoResourceGroupProvided". This resolves to the resource group used to
20--       do the current file.
21doFile = function(filename, resourceGroup)
22  local bSearchOtherPaths = (resourceGroup == nil) or false
23  resourceGroup = resourceGroup or "NoResourceGroupProvided"
24  luaState:doFile(filename, resourceGroup, bSearchOtherPaths)
25  -- Required because the C++ function cannot return whatever might be on the stack
26  return LuaStateReturnValue
27end
28original_dofile = dofile
29dofile = doFile
30
31-- Create includeFile function that preparses the file according
32-- to a function provided to the LuaState constructor (in C++)
33-- Note: See the same notes as for doFile
34include = function(filename, resourceGroup)
35  local bSearchOtherPaths = (resourceGroup == nil) or false
36  resourceGroup = resourceGroup or "NoResourceGroupProvided"
37  luaState:includeFile(filename, resourceGroup, bSearchOtherPaths)
38  -- Required because the C++ function cannot return whatever might be on the stack
39  return LuaStateReturnValue
40end
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 TracBrowser for help on using the repository browser.