diff --git a/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.cs b/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.cs index ca771c34c..3fc87b241 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/DebugConsole.cs @@ -3224,7 +3224,14 @@ namespace Barotrauma return; } - GameMain.LuaCs.Lua.DoString(string.Join(" ", args)); + try + { + GameMain.LuaCs.Lua.DoString(string.Join(" ", args)); + } + catch(Exception ex) + { + GameMain.LuaCs.HandleException(ex); + } })); commands.Add(new Command("cl_cs", "cs_cl: runs a string on the client", (string[] args) => { diff --git a/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs b/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs index d2a737eda..643561f2c 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs @@ -1243,7 +1243,14 @@ namespace Barotrauma commands.Add(new Command("lua", "lua: runs a string", (string[] args) => { - GameMain.LuaCs.Lua.DoString(string.Join(" ", args)); + try + { + GameMain.LuaCs.Lua.DoString(string.Join(" ", args)); + } + catch (Exception ex) + { + GameMain.LuaCs.HandleException(ex); + } })); commands.Add(new Command("cs", "cs: runs a string", (string[] args) => { diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/CsLua.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/CsLua.cs index 12d984339..956342c72 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/CsLua.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/CsLua.cs @@ -16,7 +16,7 @@ namespace Barotrauma Globals = setup.lua.Globals; } - public DynValue DoString(string code) => setup.DoString(code); + public DynValue DoString(string code) => setup.lua.DoString(code); } } } \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaRequire.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaRequire.cs index 55591db92..282185732 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaRequire.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaRequire.cs @@ -7,113 +7,114 @@ using MoonSharp.Interpreter; namespace Barotrauma { - class LuaRequire { - private Script lua { get; set; } - private Dictionary loadedModules { get; set; } + class LuaRequire + { + private Script lua { get; set; } + private Dictionary loadedModules { get; set; } - private bool getExistingReturnValue(string moduleName, ref DynValue returnValue) - { - return loadedModules.TryGetValue( - moduleName, - out 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)) + private bool GetExistingReturnValue(string moduleName, ref DynValue returnValue) { - return contentPackagePath; + return loadedModules.TryGetValue( + moduleName, + out 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 = null; + Table environment = globalContext ?? lua.Globals; + + 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) + { + this.lua = lua; + loadedModules = new Dictionary(); } - } - - // 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 = null; - Table environment = globalContext ?? lua.Globals; - - 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) - { - this.lua = lua; - loadedModules = new Dictionary(); - } - } } \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs index 6f11aee09..6f86c07e4 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs @@ -245,80 +245,36 @@ namespace Barotrauma public static void PrintCsMessage(object message) => PrintMessageBase("[CS] ", message, "Null"); public static void PrintLogMessage(object message) => PrintMessageBase("[LuaCs LOG] ", message, "Null"); - private DynValue DoString(string code, Table globalContext = null, string codeStringFriendly = null) - { - try - { - return lua.DoString(code, globalContext, codeStringFriendly); - } - catch (Exception e) - { - HandleException(e); - } - - return null; - } - private DynValue DoFile(string file, Table globalContext = null, string codeStringFriendly = null) { - if (!LuaCsFile.IsPathAllowedLuaException(file, false)) return null; + if (!LuaCsFile.CanReadFromPath(file)) + { + throw new ScriptRuntimeException($"dofile: File access to {file} not allowed."); + } + if (!LuaCsFile.Exists(file)) { - HandleException(new Exception($"dofile: File {file} not found.")); - return null; + throw new ScriptRuntimeException($"dofile: File {file} not found."); } - try - { - return lua.DoFile(file, globalContext, codeStringFriendly); - - } - catch (Exception e) - { - HandleException(e); - } - - return null; - } - - - private DynValue LoadString(string file, Table globalContext = null, string codeStringFriendly = null) - { - try - { - return lua.LoadString(file, globalContext, codeStringFriendly); - - } - catch (Exception e) - { - HandleException(e); - } - - return null; + return lua.DoFile(file, globalContext, codeStringFriendly); } private DynValue LoadFile(string file, Table globalContext = null, string codeStringFriendly = null) { - if (!LuaCsFile.IsPathAllowedLuaException(file, false)) return null; + if (!LuaCsFile.CanReadFromPath(file)) + { + throw new ScriptRuntimeException($"loadfile: File access to {file} not allowed."); + } + if (!LuaCsFile.Exists(file)) { - HandleException(new Exception($"loadfile: File {file} not found.")); - return null; + throw new ScriptRuntimeException($"loadfile: File {file} not found."); } - try - { - return lua.LoadFile(file, globalContext, codeStringFriendly); - - } - catch (Exception e) - { - HandleException(e); - } - - return null; + return lua.LoadFile(file, globalContext, codeStringFriendly); } - + public object CallLuaFunction(object function, params object[] arguments) { try @@ -435,8 +391,8 @@ namespace Barotrauma lua.Globals["loadfile"] = (Func)LoadFile; lua.Globals["require"] = (Func)require.Require; - lua.Globals["dostring"] = (Func)DoString; - lua.Globals["load"] = (Func)LoadString; + lua.Globals["dostring"] = (Func)lua.DoString; + lua.Globals["load"] = (Func)lua.LoadFile; lua.Globals["CsScript"] = CsScript; lua.Globals["LuaUserData"] = UserData.CreateStatic(); diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsUtility.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsUtility.cs index ee0cbfa0a..0b9a53ada 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsUtility.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsUtility.cs @@ -116,16 +116,24 @@ namespace Barotrauma if (write) { if (CanWriteToPath(path)) + { return true; + } else + { GameMain.LuaCs.HandleException(new Exception("File access to \"" + path + "\" not allowed.")); + } } else { if (CanReadFromPath(path)) + { return true; + } else + { GameMain.LuaCs.HandleException(new Exception("File access to \"" + path + "\" not allowed.")); + } } return false;