diff --git a/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs b/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs index 60cf0e8a9..dfb871e76 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs @@ -1017,6 +1017,7 @@ namespace Barotrauma SoundManager?.Update(); + GameMain.Lua.Update(); GameMain.Lua.hook.Call("think", new object[] { }); Timing.Accumulator -= Timing.Step; diff --git a/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs b/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs index d4ce0ca29..579ba3550 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs @@ -394,6 +394,7 @@ namespace Barotrauma TaskPool.Update(); CoroutineManager.Update((float)Timing.Step, (float)Timing.Step); + GameMain.Lua.Update(); GameMain.Lua.hook.Call("think", new object[] { }); performanceMeasurement.Stop(); LuaSetup.LuaTimer.LastUpdateTime = performanceMeasurement.ElapsedMilliseconds; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses.cs index 6e636c05b..dcabc221f 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses.cs @@ -427,6 +427,11 @@ namespace Barotrauma } } + public void Wait(object function, int millisecondDelay) + { + Task.Delay(millisecondDelay).ContinueWith(t => env.hook.EnqueueFunction(function)); + } + public static double GetTime() { return Timing.TotalTime; @@ -661,16 +666,16 @@ namespace Barotrauma { var httpResponse = httpWebRequest.EndGetResponse(result); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) - env.CallFunction(callback, new object[] { streamReader.ReadToEnd() }); + env.hook.EnqueueFunction(callback, streamReader.ReadToEnd() ); }catch(Exception e) { - env.CallFunction(callback, new object[] { e.ToString() }); + env.hook.EnqueueFunction(callback, e.ToString()); } }), null); }catch(Exception e) { - env.CallFunction(callback, new object[] { e.ToString() }); + env.hook.EnqueueFunction(callback, e.ToString()); } } @@ -686,16 +691,16 @@ namespace Barotrauma { var httpResponse = httpWebRequest.EndGetResponse(result); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) - env.CallFunction(callback, new object[] { streamReader.ReadToEnd() }); + env.hook.EnqueueFunction(callback, streamReader.ReadToEnd()); }catch (Exception e) { - env.CallFunction(callback, new object[] { e.ToString() }); + env.hook.EnqueueFunction(callback, e.ToString()); } - }), null); + }), null); } catch(Exception e) { - env.CallFunction(callback, new object[] { e.ToString() }); + env.hook.EnqueueFunction(callback, e.ToString()); } } } @@ -728,6 +733,8 @@ namespace Barotrauma private static Dictionary methodNameToHookName; + private Queue> queuedFunctionCalls = new Queue>(); + public enum HookMethodType { Before, After @@ -818,6 +825,11 @@ namespace Barotrauma methodNameToHookName.Add(methodName, hookName); } + public void EnqueueFunction(object function, params object[] args) + { + queuedFunctionCalls.AddItem(new Tuple(function, args)); + } + public void Add(string name, string hookName, object function) { if (name == null && hookName == null && function == null) return; @@ -839,6 +851,14 @@ namespace Barotrauma hookFunctions[name].Remove(hookName); } + public void Update() + { + if (queuedFunctionCalls.TryDequeue(out Tuple result)) + { + env.CallFunction(result.Item1, result.Item2); + } + } + public object Call(string name, params object[] args) { if (env == null) return null; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs index e3fe2d9e9..ffe2d497f 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs @@ -36,6 +36,11 @@ namespace Barotrauma public LuaScriptLoader luaScriptLoader; + public void Update() + { + hook?.Update(); + } + public void HandleLuaException(Exception ex, string extra = "") { if (!string.IsNullOrWhiteSpace(extra))