diff --git a/Barotrauma/BarotraumaShared/Lua/ModLoader.lua b/Barotrauma/BarotraumaShared/Lua/ModLoader.lua index 1dc3d28c0..62d94fe56 100644 --- a/Barotrauma/BarotraumaShared/Lua/ModLoader.lua +++ b/Barotrauma/BarotraumaShared/Lua/ModLoader.lua @@ -65,7 +65,10 @@ local function ExecutionQueue() while queue[1] ~= nil do RunFolder( table.unpack( - table.remove(queue) + table.remove( + queue, + 1 + ) ) ) end @@ -83,7 +86,7 @@ local function ExecutionQueue() return queueExecutionFIFO, processQueueFIFO end -local queueAutorun, processAutorun = ExecutionQueue() +local queueAutorun, processAutorun = ExecutionQueue() local queueForcedAutorun, processForcedAutorun = ExecutionQueue() local function processPackages(packages, fn) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaRequire.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaRequire.cs index 0509bc0a6..55591db92 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaRequire.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaRequire.cs @@ -2,40 +2,118 @@ using System; using System.Collections.Generic; +using System.IO; using MoonSharp.Interpreter; namespace Barotrauma { class LuaRequire { private Script lua { get; set; } - private Dictionary, DynValue> LoadedModules { get; set; } + private Dictionary loadedModules { get; set; } - private bool GetExistingReturnValue(Tuple key, ref DynValue returnValue) { - return LoadedModules.TryGetValue(key, out returnValue); + private bool getExistingReturnValue(string moduleName, ref DynValue returnValue) + { + return loadedModules.TryGetValue( + moduleName, + out returnValue + ); } - private void ExecuteModule(Tuple key, ref DynValue returnValue) { - string moduleName = key.Item1; - Table globalContext = key.Item2; - returnValue = lua.Call(lua.RequireModule(moduleName, globalContext)); - LoadedModules[key] = returnValue; + private string fixContentPackagePath(string contentPackagePath) + { + contentPackagePath = Path.TrimEndingDirectorySeparator( + new FileInfo(contentPackagePath) // filelist.xml + .Directory + .FullName + .CleanUpPathCrossPlatform() + ); + + return contentPackagePath; + } + private string getContentPackagePath(string path) + { + IEnumerable allContentPackages = ContentPackageManager.AllPackages; + foreach (ContentPackage contentPackage in allContentPackages) + { + string contentPackagePath = fixContentPackagePath(contentPackage.Path); + if (path.StartsWith(contentPackagePath)) + { + return contentPackagePath; + } + } + + // Return null if we can't find a content package that + // this module belongs to. + return null; + } + + private string getContentPackagePath(string moduleName, Table environment) + { + string filePath = lua.Options + .ScriptLoader + .ResolveModuleName( + moduleName, + environment + ); + filePath = Path.TrimEndingDirectorySeparator( + new FileInfo(filePath) + .Directory + .FullName + .CleanUpPathCrossPlatform() + ); + + return getContentPackagePath(filePath); + } + + private void saveReturnValue(string moduleName, DynValue returnValue) + { + loadedModules[moduleName] = returnValue; + } + + private void executeModule(string moduleName, Table environment, ref DynValue returnValue) + { + DynValue loadFunc = lua.RequireModule( + moduleName, + environment + ); + string packagePath = getContentPackagePath( + moduleName, + environment + ); + + returnValue = lua.Call( + loadFunc, + packagePath + ); + } // Lua modules that have been previously loaded by require() will // not be loaded again; instead, their initial return value is // preserved and returned again on subsequent attempts. - public DynValue Require(string moduleName, Table globalContext) { - DynValue returnValue = DynValue.Nil; - var key = new Tuple(moduleName, globalContext); + public DynValue Require(string moduleName, Table globalContext) + { + DynValue returnValue = null; + Table environment = globalContext ?? lua.Globals; - if (GetExistingReturnValue(key, ref returnValue)) return returnValue; - ExecuteModule(key, ref returnValue); + if (getExistingReturnValue(moduleName, ref returnValue)) + return returnValue; + + executeModule(moduleName, environment, ref returnValue); + if ( + returnValue == null + || returnValue.IsNil() + || returnValue.IsVoid() + ) + returnValue = DynValue.NewBoolean(true); + saveReturnValue(moduleName, returnValue); return returnValue; } - public LuaRequire(Script lua) { + public LuaRequire(Script lua) + { this.lua = lua; - LoadedModules = new Dictionary, DynValue>(); + loadedModules = new Dictionary(); } } } \ No newline at end of file