From 585a629c6fc5e65933304e79671c60a82604463d Mon Sep 17 00:00:00 2001 From: Evil Factory <36804725+evilfactory@users.noreply.github.com> Date: Fri, 12 Feb 2021 19:26:18 -0300 Subject: [PATCH] more --- .../BarotraumaClient/WindowsClient.csproj | 1 + .../BarotraumaServer/ServerSource/GameMain.cs | 4 +- .../ServerSource/Lua/LuaScriptLoader.cs | 17 +++- .../ServerSource/Lua/LuaSetup.cs | 87 ++++++++++++++++--- .../ServerSource/Networking/ChatMessage.cs | 18 +++- .../ServerSource/Networking/GameServer.cs | 7 +- .../BarotraumaServer/WindowsServer.csproj | 1 + 7 files changed, 116 insertions(+), 19 deletions(-) diff --git a/Barotrauma/BarotraumaClient/WindowsClient.csproj b/Barotrauma/BarotraumaClient/WindowsClient.csproj index fc4f285b7..b1eb76668 100644 --- a/Barotrauma/BarotraumaClient/WindowsClient.csproj +++ b/Barotrauma/BarotraumaClient/WindowsClient.csproj @@ -124,6 +124,7 @@ + diff --git a/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs b/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs index 51451964b..591074f82 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs @@ -12,6 +12,7 @@ using System.Reflection; using System.Threading; using System.Xml.Linq; using MoonSharp.Interpreter; +using MoonSharp.VsCodeDebugger; namespace Barotrauma { @@ -135,6 +136,7 @@ namespace Barotrauma CheckContentPackage(); Lua = new LuaSetup(); + } @@ -361,7 +363,7 @@ namespace Barotrauma TaskPool.Update(); CoroutineManager.Update((float)Timing.Step, (float)Timing.Step); - GameMain.Lua.hook.Call("think", new DynValue[] { DynValue.NewNumber(elapsedTime) }); + GameMain.Lua.hook.Call("think", new DynValue[] { DynValue.NewNumber(elapsedTime), DynValue.NewNumber(Timing.TotalTime) }); Timing.Accumulator -= Timing.Step; } diff --git a/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaScriptLoader.cs b/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaScriptLoader.cs index eb3aa669f..73e984458 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaScriptLoader.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaScriptLoader.cs @@ -29,7 +29,22 @@ namespace Barotrauma { Console.WriteLine(s); - script.DoFile(s); // i hate windows + try + { + script.DoFile(s); // i hate windows + } + catch (Exception e) + { + if (e is InterpreterException) + { + + Console.WriteLine(((InterpreterException)e).DecoratedMessage); + } + else + { + Console.WriteLine(e.ToString()); + } + } } } diff --git a/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaSetup.cs b/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaSetup.cs index 0201b5908..f1bb62a0b 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaSetup.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Lua/LuaSetup.cs @@ -6,6 +6,7 @@ using Barotrauma.Networking; using MoonSharp.Interpreter; using Microsoft.Xna.Framework; using System.Threading.Tasks; +using MoonSharp.VsCodeDebugger; namespace Barotrauma { @@ -15,6 +16,7 @@ namespace Barotrauma public Script lua; public Hook hook; + public bool overrideTraitors = false; public void DoString(string code) { @@ -24,7 +26,36 @@ namespace Barotrauma } catch (Exception e) { - Console.WriteLine(e.ToString()); + if (e is InterpreterException) + { + + Console.WriteLine(((InterpreterException)e).DecoratedMessage); + } + else + { + Console.WriteLine(e.ToString()); + } + } + } + + + public void RunFunction(DynValue func) + { + try + { + lua.Call(func); + } + catch (Exception e) + { + if (e is InterpreterException) + { + + Console.WriteLine(((InterpreterException)e).DecoratedMessage); + } + else + { + Console.WriteLine(e.ToString()); + } } } @@ -86,11 +117,23 @@ namespace Barotrauma private class Game { + LuaSetup env; + + public Game(LuaSetup e) + { + env = e; + } + public static void SendMessage(string msg, int messageType = 0, Client sender = null, Character character = null) { GameMain.Server.SendChatMessage(msg, (ChatMessageType)messageType, sender, character); } + public static void SendTraitorMessage(Client client, string msg, int type) + { + GameMain.Server.SendTraitorMessage(client, msg, "", (TraitorMessageType)type); + } + public static void SendDirectChatMessage(string sendername, string text, Character sender, int messageType = 0, Client client = null) { @@ -100,6 +143,11 @@ namespace Barotrauma } + public void OverrideTraitors(bool o) + { + env.overrideTraitors = o; + } + public static void Log(string message, int type) { GameServer.Log(message, (ServerLog.MessageType)type); @@ -128,16 +176,22 @@ namespace Barotrauma private class LuaTimer { - public Script env; + public LuaSetup env; - public LuaTimer(Script e) + public LuaTimer(LuaSetup e) { env = e; } public void Simple(int time, DynValue function) { - Task.Delay(new TimeSpan(0, 0, 0, 0, time)).ContinueWith(o => { env.Call(function); }); + + Task.Delay(time).ContinueWith(o => { env.RunFunction(function); }); + } + + public static double GetTime() + { + return Timing.TotalTime; } @@ -207,7 +261,7 @@ namespace Barotrauma hookFunctions.Add(new HookFunction(name, hookName, function)); } - public void Call(string name, DynValue[] args) + public DynValue Call(string name, DynValue[] args) { foreach(HookFunction hf in hookFunctions) { @@ -215,22 +269,31 @@ namespace Barotrauma { try { - env.Call(hf.function, args); - }catch(Exception e) + return env.Call(hf.function, args); + } + catch (Exception e) { - Console.WriteLine(e.ToString()); + if (e is InterpreterException) + { + + Console.WriteLine(((InterpreterException)e).DecoratedMessage); + } + else + { + Console.WriteLine(e.ToString()); + } } } } + + return null; } } public LuaSetup() { - Console.WriteLine("Lua!"); - LuaCustomConverters.RegisterAll(); LuaScriptLoader luaScriptLoader = new LuaScriptLoader(); @@ -253,10 +316,10 @@ namespace Barotrauma hook = new Hook(lua); lua.Globals["Player"] = new Player(); - lua.Globals["Game"] = new Game(); + lua.Globals["Game"] = new Game(this); lua.Globals["Hook"] = hook; lua.Globals["Random"] = new LuaRandom(); - lua.Globals["Timer"] = new LuaTimer(lua); + lua.Globals["Timer"] = new LuaTimer(this); luaScriptLoader.RunFolder("Lua/autorun", lua); diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs index 1370f16fe..0ecbec4fc 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs @@ -174,6 +174,21 @@ namespace Barotrauma.Networking return; } + var should = GameMain.Lua.hook.Call("chatMessage", new DynValue[] { DynValue.NewString(txt), UserData.Create(c) }); + + + if(should != null) + { + if (should.CastToBool()) + { + return; + } + else + { + + } + } + if (type == ChatMessageType.Order) { if (c.Character == null || c.Character.SpeechImpediment >= 100.0f || c.Character.IsDead) { return; } @@ -225,9 +240,6 @@ namespace Barotrauma.Networking } - GameMain.Lua.hook.Call("chatMessage", new DynValue[] { DynValue.NewString(txt), UserData.Create(c) }); - - } public int EstimateLengthBytesServer(Client c) diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs index 288780f32..8163d6a84 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs @@ -2395,8 +2395,11 @@ namespace Barotrauma.Networking { if (!(GameMain.GameSession?.GameMode is CampaignMode)) { - TraitorManager = new TraitorManager(); - TraitorManager.Start(this); + if (!GameMain.Lua.overrideTraitors) + { + TraitorManager = new TraitorManager(); + TraitorManager.Start(this); + } } } diff --git a/Barotrauma/BarotraumaServer/WindowsServer.csproj b/Barotrauma/BarotraumaServer/WindowsServer.csproj index 2d0a14086..ba178ceac 100644 --- a/Barotrauma/BarotraumaServer/WindowsServer.csproj +++ b/Barotrauma/BarotraumaServer/WindowsServer.csproj @@ -82,6 +82,7 @@ +