1 | -- Note: luaState is a pointer to the LuaState instance that created this lua state |
---|
2 | |
---|
3 | -- Save original print function in debug |
---|
4 | debug = print |
---|
5 | |
---|
6 | -- Redirect print to the C++ print function |
---|
7 | print = function(s) |
---|
8 | luaState:luaPrint(s) |
---|
9 | end |
---|
10 | |
---|
11 | -- Create function to log text like COUT, but always prints a line! |
---|
12 | logMessage = function(level, message) |
---|
13 | luaState:luaLog(level, message) |
---|
14 | end |
---|
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. |
---|
21 | doFile = 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 |
---|
27 | end |
---|
28 | original_dofile = dofile |
---|
29 | dofile = 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 |
---|
34 | include = 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 |
---|
40 | end |
---|
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 |
---|
48 | old_require = require |
---|
49 | require = 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] |
---|
68 | end |
---|