Skip to content

Commit c3e8142

Browse files
committed
Fixed issue where reloading scripts wouldn't correctly clear packages
1 parent 481892c commit c3e8142

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

Source/Managers/LuaMan.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,24 +234,25 @@ void LuaStateWrapper::Initialize() {
234234

235235
luaL_dostring(m_State,
236236
// Add cls() as a shortcut to ConsoleMan:Clear().
237-
"cls = function() ConsoleMan:Clear(); end"
238-
"\n"
237+
"cls = function() ConsoleMan:Clear(); end\n"
239238
// Override "print" in the lua state to output to the console.
240-
"print = function(stringToPrint) ConsoleMan:PrintString(\"PRINT: \" .. tostring(stringToPrint)); end"
241-
"\n"
239+
"print = function(stringToPrint) ConsoleMan:PrintString(\"PRINT: \" .. tostring(stringToPrint)); end\n"
242240
// Override random functions to appear global instead of under LuaMan
243241
"SelectRand = function(lower, upper) return LuaMan:SelectRand(lower, upper); end;\n"
244242
"RangeRand = function(lower, upper) return LuaMan:RangeRand(lower, upper); end;\n"
245243
"PosRand = function() return LuaMan:PosRand(); end;\n"
246244
"NormalRand = function() return LuaMan:NormalRand(); end;\n"
247245
// Override "math.random" in the lua state to use RTETools MT19937 implementation. Preserve return types of original to not break all the things.
248-
"math.random = function(lower, upper) if lower ~= nil and upper ~= nil then return LuaMan:SelectRand(lower, upper); elseif lower ~= nil then return LuaMan:SelectRand(1, lower); else return LuaMan:PosRand(); end end"
249-
"\n"
246+
"math.random = function(lower, upper) if lower ~= nil and upper ~= nil then return LuaMan:SelectRand(lower, upper); elseif lower ~= nil then return LuaMan:SelectRand(1, lower); else return LuaMan:PosRand(); end end\n"
250247
// Override "dofile"/"loadfile" to be able to account for Data/ or Mods/ directory.
251-
"OriginalDoFile = dofile; dofile = function(filePath) filePath = PresetMan:GetFullModulePath(filePath); if filePath ~= '' then return OriginalDoFile(filePath); end end;"
252-
"OriginalLoadFile = loadfile; loadfile = function(filePath) filePath = PresetMan:GetFullModulePath(filePath); if filePath ~= '' then return OriginalLoadFile(filePath); end end;"
248+
"OriginalDoFile = dofile; dofile = function(filePath) filePath = PresetMan:GetFullModulePath(filePath); if filePath ~= '' then return OriginalDoFile(filePath); end end;\n"
249+
"OriginalLoadFile = loadfile; loadfile = function(filePath) filePath = PresetMan:GetFullModulePath(filePath); if filePath ~= '' then return OriginalLoadFile(filePath); end end;\n"
250+
// Override "require" to be able to track loaded packages so we can clear them when scripts are reloaded.
251+
"_RequiredPackages = {};\n"
252+
"OriginalRequire = require; require = function(filePath) _RequiredPackages[filePath] = true; return OriginalRequire(filePath); end;\n"
253+
"_ClearRequiredPackages = function() for k, v in pairs(_RequiredPackages) do print(\"clearing: \" .. k); package.loaded[k] = nil; end; _RequiredPackages = {}; end;\n"
253254
// Internal helper functions to add callbacks for async pathing requests
254-
"_AsyncPathCallbacks = {};"
255+
"_AsyncPathCallbacks = {};\n"
255256
"_AddAsyncPathCallback = function(id, callback) _AsyncPathCallbacks[id] = callback; end\n"
256257
"_TriggerAsyncPathCallback = function(id, param) if _AsyncPathCallbacks[id] ~= nil then _AsyncPathCallbacks[id](param); _AsyncPathCallbacks[id] = nil; end end\n");
257258
}
@@ -415,7 +416,7 @@ void LuaMan::Destroy() {
415416
}
416417

417418
void LuaStateWrapper::ClearUserModuleCache() {
418-
luaL_dostring(m_State, "for m, n in pairs(package.loaded) do if type(n) == \"boolean\" then package.loaded[m] = nil; end; end;");
419+
luaL_dostring(m_State, "_ClearRequiredPackages();");
419420
}
420421

421422
void LuaStateWrapper::ClearLuaScriptCache() {

0 commit comments

Comments
 (0)