From 994f6b991a56382f530e3d43ad1f3a56c195559d Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 19 Feb 2017 20:19:00 -0500 Subject: SFT making utilities --- ShiftOS_TheReturn/Scripting.cs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'ShiftOS_TheReturn/Scripting.cs') diff --git a/ShiftOS_TheReturn/Scripting.cs b/ShiftOS_TheReturn/Scripting.cs index cfa1549..3ecf9d9 100644 --- a/ShiftOS_TheReturn/Scripting.cs +++ b/ShiftOS_TheReturn/Scripting.cs @@ -63,6 +63,12 @@ namespace ShiftOS.Engine.Scripting public dynamic Lua = new DynamicLua.DynamicLua(); public bool Running = true; + public static string CreateSft(string lua) + { + byte[] bytes = Encoding.UTF8.GetBytes(lua); + return Convert.ToBase64String(bytes); + } + public static void RunSft(string sft) { if (Utils.FileExists(sft)) @@ -72,6 +78,7 @@ namespace ShiftOS.Engine.Scripting string b64 = Utils.ReadAllText(sft); byte[] bytes = Convert.FromBase64String(b64); string lua = Encoding.UTF8.GetString(bytes); + CurrentDirectory = sft.Replace(Utils.GetFileInfo(sft).Name, ""); new LuaInterpreter().Execute(lua); } catch @@ -81,6 +88,8 @@ namespace ShiftOS.Engine.Scripting } } + public static string CurrentDirectory { get; private set; } + public LuaInterpreter() { Lua(@"function totable(clrlist) @@ -101,6 +110,7 @@ end"); public void SetupAPIs() { + Lua.currentdir = (string.IsNullOrWhiteSpace(CurrentDirectory)) ? "0:" : CurrentDirectory; Lua.random = new Func((min, max) => { return new Random().Next(min, max); @@ -208,6 +218,7 @@ end"); { if (Utils.FileExists(file)) { + CurrentDirectory = file.Replace(Utils.GetFileInfo(file).Name, ""); Execute(Utils.ReadAllText(file)); } else @@ -248,6 +259,37 @@ end"); } } + [Exposed("sft")] + public class SFTFunctions + { + public string make(string lua) + { + return LuaInterpreter.CreateSft(lua); + } + + public void makefile(string lua, string outpath) + { + Utils.WriteAllText(outpath, make(lua)); + } + + public void run(string inpath) + { + LuaInterpreter.RunSft(inpath); + } + + public string unmake(string sft) + { + if (Utils.FileExists(sft)) + { + string b64 = Utils.ReadAllText(sft); + byte[] bytes = Convert.FromBase64String(b64); + string lua = Encoding.UTF8.GetString(bytes); + return lua; + } + return ""; + } + } + [Exposed("net")] public class NetFunctions { -- cgit v1.2.3