Client LuaCsForBarotrauma
LuaPlatformAccessor.cs
1 using MoonSharp.Interpreter.Platforms;
2 using MoonSharp.Interpreter;
3 using System;
4 using System.IO;
5 using System.Text;
6 
7 namespace Barotrauma
8 {
9  public class LuaPlatformAccessor : PlatformAccessorBase
10  {
11  public static FileMode ParseFileMode(string mode)
12  {
13  mode = mode.Replace("b", "");
14 
15  if (mode == "r")
16  return FileMode.Open;
17  else if (mode == "r+")
18  return FileMode.OpenOrCreate;
19  else if (mode == "w")
20  return FileMode.Create;
21  else if (mode == "w+")
22  return FileMode.Truncate;
23  else
24  return FileMode.Append;
25  }
26 
27  public static FileAccess ParseFileAccess(string mode)
28  {
29  mode = mode.Replace("b", "");
30 
31  if (mode == "r")
32  return FileAccess.Read;
33  else if (mode == "r+")
34  return FileAccess.ReadWrite;
35  else if (mode == "w")
36  return FileAccess.ReadWrite;
37  else if (mode == "w+")
38  return FileAccess.ReadWrite;
39  else
40  return FileAccess.Write;
41  }
42 
43  public override string GetEnvironmentVariable(string envvarname)
44  {
45  return null;
46  }
47 
48  public override CoreModules FilterSupportedCoreModules(CoreModules module)
49  {
50  return module;
51  }
52 
53  public override Stream IO_OpenFile(Script script, string filename, Encoding encoding, string mode)
54  {
55  if (!LuaCsFile.IsPathAllowedLuaException(filename)) { return Stream.Null; }
56 
57  FileStream stream = new FileStream(filename, ParseFileMode(mode), ParseFileAccess(mode), FileShare.ReadWrite | FileShare.Delete);
58  return stream;
59  }
60 
61  public override Stream IO_GetStandardStream(StandardFileType type)
62  {
63  switch (type)
64  {
65  case StandardFileType.StdIn:
66  return Console.OpenStandardInput();
67  case StandardFileType.StdOut:
68  return Console.OpenStandardOutput();
69  case StandardFileType.StdErr:
70  return Console.OpenStandardError();
71  default:
72  throw new ArgumentException("type");
73  }
74  }
75 
76  public override string IO_OS_GetTempFilename()
77  {
78  return "LocalMods/temp.txt";
79  }
80 
81  public override void OS_ExitFast(int exitCode)
82  {
83  throw new ScriptRuntimeException("usage of os.exit is not allowed.");
84  }
85 
86  public override bool OS_FileExists(string file)
87  {
88  return LuaCsFile.Exists(file);
89  }
90 
91  public override void OS_FileDelete(string file)
92  {
93  LuaCsFile.Delete(file);
94  }
95 
96  public override void OS_FileMove(string src, string dst)
97  {
98  LuaCsFile.Move(src, dst);
99  }
100 
101  public override int OS_Execute(string cmdline)
102  {
103  throw new ScriptRuntimeException("usage of os.execute is not allowed.");
104  }
105 
106  public override string GetPlatformNamePrefix()
107  {
108  return "lua";
109  }
110 
111  public override void DefaultPrint(string content)
112  {
113  System.Diagnostics.Debug.WriteLine(content);
114  }
115  }
116 }
static void Move(string path, string destination)
static bool IsPathAllowedLuaException(string path, bool write=true)
static void Delete(string path)
static bool Exists(string path)
override Stream IO_OpenFile(Script script, string filename, Encoding encoding, string mode)
override void OS_FileDelete(string file)
override bool OS_FileExists(string file)
override void OS_FileMove(string src, string dst)
static FileAccess ParseFileAccess(string mode)
static FileMode ParseFileMode(string mode)
override Stream IO_GetStandardStream(StandardFileType type)
override CoreModules FilterSupportedCoreModules(CoreModules module)
override int OS_Execute(string cmdline)
override void DefaultPrint(string content)
override void OS_ExitFast(int exitCode)
override string GetEnvironmentVariable(string envvarname)