using System; using System.Collections.Generic; using System.Text; using System.IO; using Barotrauma.Networking; using MoonSharp.Interpreter; using Microsoft.Xna.Framework; using System.Threading.Tasks; namespace Barotrauma { partial class LuaSetup { public Script lua; public LuaHook hook; public LuaGame game; public void HandleLuaException(Exception ex) { if(ex is InterpreterException) { PrintMessage(((InterpreterException)ex).DecoratedMessage); } else { PrintMessage(ex.ToString()); } } public void PrintMessage(object message) { Console.WriteLine(message.ToString()); if (GameMain.Server != null) { foreach (var c in GameMain.Server.ConnectedClients) { GameMain.Server.SendDirectChatMessage(message.ToString(), c, ChatMessageType.Console); GameServer.Log("[LUA] " + message.ToString(), ServerLog.MessageType.ServerMessage); } } } public void DoString(string code) { try { lua.DoString(code); } catch (Exception e) { HandleLuaException(e); } } public void RunFunction(DynValue func) { try { lua.Call(func); } catch (Exception e) { HandleLuaException(e); } } public void DoFile(string file) { try { lua.DoFile(file); } catch (Exception e) { HandleLuaException(e); } } public LuaSetup() { PrintMessage("Lua!"); LuaScriptLoader luaScriptLoader = new LuaScriptLoader(this); LuaCustomConverters.RegisterAll(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); lua = new Script(CoreModules.Preset_SoftSandbox | CoreModules.LoadMethods); lua.Options.DebugPrint = PrintMessage; lua.Options.ScriptLoader = luaScriptLoader; hook = new LuaHook(this); game = new LuaGame(this); lua.Globals["Player"] = new LuaPlayer(); lua.Globals["Game"] = game; lua.Globals["Hook"] = hook; lua.Globals["Random"] = new LuaRandom(); lua.Globals["Timer"] = new LuaTimer(this); lua.Globals["WayPoint"] = UserData.CreateStatic(); lua.Globals["SpawnType"] = UserData.CreateStatic(); lua.Globals["ChatMessageType"] = UserData.CreateStatic(); lua.Globals["ServerLog_MessageType"] = UserData.CreateStatic(); lua.Globals["Submarine"] = UserData.CreateStatic(); lua.Globals["Client"] = UserData.CreateStatic(); lua.Globals["Character"] = UserData.CreateStatic(); lua.Globals["Item"] = UserData.CreateStatic(); lua.Globals["Level"] = UserData.CreateStatic(); lua.Globals["PositionType"] = UserData.CreateStatic(); lua.Globals["JobPrefab"] = UserData.CreateStatic(); lua.Globals["TraitorMessageType"] = UserData.CreateStatic(); lua.Globals["CauseOfDeathType"] = UserData.CreateStatic(); lua.Globals["Affliction"] = UserData.CreateStatic(); lua.Globals["Vector2"] = UserData.CreateStatic(); lua.Globals["Vector3"] = UserData.CreateStatic(); lua.Globals["Vector4"] = UserData.CreateStatic(); lua.Globals["CreateVector2"] = (Func)CreateVector2; lua.Globals["CreateVector3"] = (Func)CreateVector3; lua.Globals["CreateVector4"] = (Func)CreateVector4; foreach (string d in Directory.GetDirectories("Mods")) { if (Directory.Exists(d + "/Lua/Autorun")) { luaScriptLoader.RunFolder(d + "/Lua/Autorun"); } if (Directory.Exists(d + "/LuaRaw")) { foreach (string d2 in Directory.GetDirectories(d + "/LuaRaw")) { luaScriptLoader.RunFolder(d2 + "/Autorun"); } } } } } }