Implemented a custom PlatformAccessor and re-added the rest of the io and os libraries
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
using MoonSharp.Interpreter.Platforms;
|
||||
using MoonSharp.Interpreter;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class LuaPlatformAccessor : PlatformAccessorBase
|
||||
{
|
||||
public static FileMode ParseFileMode(string mode)
|
||||
{
|
||||
mode = mode.Replace("b", "");
|
||||
|
||||
if (mode == "r")
|
||||
return FileMode.Open;
|
||||
else if (mode == "r+")
|
||||
return FileMode.OpenOrCreate;
|
||||
else if (mode == "w")
|
||||
return FileMode.Create;
|
||||
else if (mode == "w+")
|
||||
return FileMode.Truncate;
|
||||
else
|
||||
return FileMode.Append;
|
||||
}
|
||||
|
||||
public override string GetEnvironmentVariable(string envvarname)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public override CoreModules FilterSupportedCoreModules(CoreModules module)
|
||||
{
|
||||
return module;
|
||||
}
|
||||
|
||||
public override Stream IO_OpenFile(Script script, string filename, Encoding encoding, string mode)
|
||||
{
|
||||
if (!LuaCsFile.IsPathAllowedLuaException(filename)) { return Stream.Null; }
|
||||
|
||||
return new FileStream(filename, ParseFileMode(mode), FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete);
|
||||
}
|
||||
|
||||
public override Stream IO_GetStandardStream(StandardFileType type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case StandardFileType.StdIn:
|
||||
return Console.OpenStandardInput();
|
||||
case StandardFileType.StdOut:
|
||||
return Console.OpenStandardOutput();
|
||||
case StandardFileType.StdErr:
|
||||
return Console.OpenStandardError();
|
||||
default:
|
||||
throw new ArgumentException("type");
|
||||
}
|
||||
}
|
||||
|
||||
public override string IO_OS_GetTempFilename()
|
||||
{
|
||||
return "LocalMods/temp.txt";
|
||||
}
|
||||
|
||||
public override void OS_ExitFast(int exitCode)
|
||||
{
|
||||
throw new ScriptRuntimeException("usage of os.exit is not allowed.");
|
||||
}
|
||||
|
||||
public override bool OS_FileExists(string file)
|
||||
{
|
||||
return LuaCsFile.Exists(file);
|
||||
}
|
||||
|
||||
public override void OS_FileDelete(string file)
|
||||
{
|
||||
LuaCsFile.Delete(file);
|
||||
}
|
||||
|
||||
public override void OS_FileMove(string src, string dst)
|
||||
{
|
||||
LuaCsFile.Move(src, dst);
|
||||
}
|
||||
|
||||
public override int OS_Execute(string cmdline)
|
||||
{
|
||||
throw new ScriptRuntimeException("usage of os.execute is not allowed.");
|
||||
}
|
||||
|
||||
public override string GetPlatformNamePrefix()
|
||||
{
|
||||
return "lua";
|
||||
}
|
||||
|
||||
public override void DefaultPrint(string content)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,6 +76,8 @@ namespace Barotrauma
|
||||
|
||||
public LuaCsSetup()
|
||||
{
|
||||
Script.GlobalOptions.Platform = new LuaPlatformAccessor();
|
||||
|
||||
Hook = new LuaCsHook(this);
|
||||
ModStore = new LuaCsModStore();
|
||||
|
||||
@@ -339,7 +341,7 @@ namespace Barotrauma
|
||||
|
||||
RegisterLuaConverters();
|
||||
|
||||
Lua = new Script(CoreModules.Preset_SoftSandbox | CoreModules.Debug);
|
||||
Lua = new Script(CoreModules.Preset_SoftSandbox | CoreModules.Debug | CoreModules.IO | CoreModules.OS_System);
|
||||
Lua.Options.DebugPrint = (o) => { LuaCsLogger.LogMessage(o); };
|
||||
Lua.Options.ScriptLoader = LuaScriptLoader;
|
||||
Lua.Options.CheckThreadAccess = false;
|
||||
|
||||
@@ -143,6 +143,14 @@ namespace Barotrauma
|
||||
Directory.Delete(path, true);
|
||||
}
|
||||
|
||||
public static void Move(string path, string destination)
|
||||
{
|
||||
if (!IsPathAllowedException(path))
|
||||
return;
|
||||
|
||||
File.Move(path, destination, true);
|
||||
}
|
||||
|
||||
public static FileStream OpenRead(string path)
|
||||
{
|
||||
if (!IsPathAllowedException(path))
|
||||
|
||||
Reference in New Issue
Block a user