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; using Barotrauma.Items.Components; using System.Diagnostics; 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 PrintMessageNoLog(object message) { Console.WriteLine(message.ToString()); } public DynValue DoString(string code, Table globalContext = null, string codeStringFriendly = null) { try { return lua.DoString(code, globalContext, codeStringFriendly); } catch (Exception e) { HandleLuaException(e); } return null; } public DynValue DoFile(string file, Table globalContext=null, string codeStringFriendly = null) { try { return lua.DoFile(file, globalContext, codeStringFriendly); } catch (Exception e) { HandleLuaException(e); } return null; } public DynValue LoadString(string file, Table globalContext = null, string codeStringFriendly = null) { try { return lua.LoadString(file, globalContext, codeStringFriendly); } catch (Exception e) { HandleLuaException(e); } return null; } public DynValue LoadFile(string file, Table globalContext = null, string codeStringFriendly = null) { try { return lua.LoadFile(file, globalContext, codeStringFriendly); } catch (Exception e) { HandleLuaException(e); } return null; } public DynValue Require(string modname, Table globalContext) { try { return lua.Call(lua.RequireModule(modname, globalContext)); } catch (Exception e) { HandleLuaException(e); } return null; } public static DynValue CreateUserDataSafe(object o) { if(o == null) return DynValue.Nil; return UserData.Create(o); } public LuaSetup() { PrintMessage("Lua!"); LuaScriptLoader luaScriptLoader = new LuaScriptLoader(this); luaScriptLoader.ModulePaths = new string[] { }; 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(); 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(); 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); lua.Options.DebugPrint = PrintMessage; lua.Options.ScriptLoader = luaScriptLoader; hook = new LuaHook(this); game = new LuaGame(this); lua.Globals["printNoLog"] = (Action)PrintMessageNoLog; lua.Globals["dofile"] = (Func)DoFile; lua.Globals["loadfile"] = (Func)LoadFile; lua.Globals["require"] = (Func)Require; lua.Globals["dostring"] = (Func)DoString; lua.Globals["load"] = (Func)LoadString; 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["File"] = UserData.CreateStatic(); lua.Globals["Networking"] = new LuaNetworking(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["AfflictionPrefab"] = UserData.CreateStatic(); lua.Globals["CharacterTeamType"] = 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; lua.Globals["ChatMessage"] = UserData.CreateStatic(); lua.Globals["Hull"] = UserData.CreateStatic(); lua.Globals["InvSlotType"] = UserData.CreateStatic(); lua.Globals["Gap"] = UserData.CreateStatic(); lua.Globals["ContentPackage"] = UserData.CreateStatic(); lua.Globals["ClientPermissions"] = UserData.CreateStatic(); List modulePaths = new List(); foreach (string d in Directory.GetDirectories("Mods")) { modulePaths.Add(d + "/Lua/?"); if (Directory.Exists(d + "/Lua/Autorun")) { luaScriptLoader.RunFolder(d + "/Lua/Autorun"); } } luaScriptLoader.ModulePaths = modulePaths.ToArray(); } } }