fix file loading

This commit is contained in:
Evil Factory
2022-04-09 14:40:55 -03:00
parent e1d0b8235f
commit 454a0a22d1
2 changed files with 19 additions and 12 deletions
@@ -4,6 +4,7 @@ using System.Text;
using System.IO; using System.IO;
using MoonSharp.Interpreter; using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Loaders; using MoonSharp.Interpreter.Loaders;
using System.Linq;
namespace Barotrauma namespace Barotrauma
{ {
@@ -11,18 +12,11 @@ namespace Barotrauma
public class LuaScriptLoader : ScriptLoaderBase public class LuaScriptLoader : ScriptLoaderBase
{ {
public LuaSetup lua;
public LuaScriptLoader(LuaSetup l)
{
lua = l;
}
public override object LoadFile(string file, Table globalContext) public override object LoadFile(string file, Table globalContext)
{ {
if (!LuaFile.IsPathAllowedLuaException(file, false)) return null; if (!LuaFile.IsPathAllowedLuaException(file, false)) return null;
return File.ReadAllText(file); return File.ReadAllText(file);
} }
@@ -32,7 +26,6 @@ namespace Barotrauma
return File.Exists(file); return File.Exists(file);
} }
}
}
} }
} }
@@ -126,6 +126,13 @@ namespace Barotrauma
public DynValue DoFile(string file, Table globalContext = null, string codeStringFriendly = null) public DynValue DoFile(string file, Table globalContext = null, string codeStringFriendly = null)
{ {
if (!LuaFile.IsPathAllowedLuaException(file, false)) return null;
if (!LuaFile.Exists(file))
{
HandleLuaException(new Exception($"dofile: File {file} not found."));
return null;
}
try try
{ {
return lua.DoFile(file, globalContext, codeStringFriendly); return lua.DoFile(file, globalContext, codeStringFriendly);
@@ -157,6 +164,13 @@ namespace Barotrauma
public DynValue LoadFile(string file, Table globalContext = null, string codeStringFriendly = null) public DynValue LoadFile(string file, Table globalContext = null, string codeStringFriendly = null)
{ {
if (!LuaFile.IsPathAllowedLuaException(file, false)) return null;
if (!LuaFile.Exists(file))
{
HandleLuaException(new Exception($"loadfile: File {file} not found."));
return null;
}
try try
{ {
return lua.LoadFile(file, globalContext, codeStringFriendly); return lua.LoadFile(file, globalContext, codeStringFriendly);
@@ -223,7 +237,7 @@ namespace Barotrauma
PrintMessage("Lua! Version " + AssemblyInfo.GitRevision); PrintMessage("Lua! Version " + AssemblyInfo.GitRevision);
luaScriptLoader = new LuaScriptLoader(this); luaScriptLoader = new LuaScriptLoader();
luaScriptLoader.ModulePaths = new string[] { }; luaScriptLoader.ModulePaths = new string[] { };
LuaCustomConverters.RegisterAll(); LuaCustomConverters.RegisterAll();