Fixes for PR #80

This commit is contained in:
Evil Factory
2022-05-06 13:10:50 -03:00
parent e890126d9f
commit 64f0e2f137
4 changed files with 28 additions and 50 deletions
@@ -55,8 +55,6 @@ namespace Barotrauma
private Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>> hookPrefixMethods; private Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>> hookPrefixMethods;
private Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>> hookPostfixMethods; private Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>> hookPostfixMethods;
private Queue<(float, LuaCsAction, object[])> queuedFunctionCalls;
private static LuaCsHook instance; private static LuaCsHook instance;
public LuaCsHook() { public LuaCsHook() {
@@ -66,8 +64,6 @@ namespace Barotrauma
hookPrefixMethods = new Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>>(); hookPrefixMethods = new Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>>();
hookPostfixMethods = new Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>>(); hookPostfixMethods = new Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>>();
queuedFunctionCalls = new Queue<(float, LuaCsAction, object[])>();
} }
public void Initialize() public void Initialize()
@@ -339,20 +335,6 @@ namespace Barotrauma
UnhookMethod(identifier, methodInfo, hookType); UnhookMethod(identifier, methodInfo, hookType);
} }
public void Enqueue(LuaCsAction action, params object[] args)
{
queuedFunctionCalls.Enqueue((0, action, args));
}
public void EnqueueTimed(float time, LuaCsAction action, params object[] args)
{
queuedFunctionCalls.Enqueue((time, action, args));
}
protected void EnqueueFunction(LuaCsAction function, params object[] args) => Enqueue(function, args);
protected void EnqueueTimedFunction(float time, LuaCsAction function, params object[] args) => EnqueueTimed(time, function, args);
public void Add(string name, string hookName, LuaCsFunc hook, ACsMod owner = null) public void Add(string name, string hookName, LuaCsFunc hook, ACsMod owner = null)
{ {
name = name.ToLower(); name = name.ToLower();
@@ -382,29 +364,13 @@ namespace Barotrauma
hookPrefixMethods.Clear(); hookPrefixMethods.Clear();
hookPostfixMethods.Clear(); hookPostfixMethods.Clear();
queuedFunctionCalls.Clear();
harmony?.UnpatchAll(); harmony?.UnpatchAll();
} }
public void Update() public void Update()
{ {
try
{
if (queuedFunctionCalls.TryPeek(out (float, LuaCsAction, object[]) result))
{
if (Timing.TotalTime >= result.Item1)
{
result.Item2(result.Item3);
queuedFunctionCalls.Dequeue();
}
}
}
catch (Exception ex)
{
GameMain.LuaCs.HandleException(ex, $"queuedFunctionCalls was {queuedFunctionCalls}", LuaCsSetup.ExceptionType.Both);
}
} }
[MoonSharpHidden] [MoonSharpHidden]
@@ -56,6 +56,9 @@ namespace Barotrauma
public LuaScriptLoader LuaScriptLoader { get; private set; } public LuaScriptLoader LuaScriptLoader { get; private set; }
public LuaCsHook Hook { get; private set; } public LuaCsHook Hook { get; private set; }
public LuaCsTimer Timer { get; private set; }
public LuaCsNetworking Networking { get; private set; } public LuaCsNetworking Networking { get; private set; }
public LuaCsModStore ModStore { get; private set; } public LuaCsModStore ModStore { get; private set; }
private LuaRequire require { get; set; } private LuaRequire require { get; set; }
@@ -297,7 +300,7 @@ namespace Barotrauma
public void Update() public void Update()
{ {
Hook?.Update(); Hook?.Update();
LuaCsTimer.Update(); Timer?.Update();
} }
public void Stop() public void Stop()
@@ -313,7 +316,6 @@ namespace Barotrauma
if (Thread.CurrentThread == GameMain.MainThread) if (Thread.CurrentThread == GameMain.MainThread)
{ {
Hook?.Call("stop"); Hook?.Call("stop");
LuaCsTimer.Clear();
} }
Game?.Stop(); Game?.Stop();
@@ -322,6 +324,7 @@ namespace Barotrauma
ModStore.Clear(); ModStore.Clear();
Game = new LuaGame(); Game = new LuaGame();
Networking = new LuaCsNetworking(); Networking = new LuaCsNetworking();
Timer = new LuaCsTimer();
LuaScriptLoader = null; LuaScriptLoader = null;
lua = null; lua = null;
Lua = null; Lua = null;
@@ -369,6 +372,7 @@ namespace Barotrauma
Game = new LuaGame(); Game = new LuaGame();
Networking = new LuaCsNetworking(); Networking = new LuaCsNetworking();
Timer = new LuaCsTimer();
Hook.Initialize(); Hook.Initialize();
ModStore.Initialize(); ModStore.Initialize();
@@ -401,7 +405,7 @@ namespace Barotrauma
lua.Globals["Game"] = Game; lua.Globals["Game"] = Game;
lua.Globals["Hook"] = Hook; lua.Globals["Hook"] = Hook;
lua.Globals["ModStore"] = ModStore; lua.Globals["ModStore"] = ModStore;
lua.Globals["Timer"] = new LuaCsTimer(); lua.Globals["Timer"] = Timer;
lua.Globals["File"] = UserData.CreateStatic<LuaCsFile>(); lua.Globals["File"] = UserData.CreateStatic<LuaCsFile>();
lua.Globals["Networking"] = Networking; lua.Globals["Networking"] = Networking;
@@ -418,7 +422,7 @@ namespace Barotrauma
Config.FirstTimeCsWarning = false; Config.FirstTimeCsWarning = false;
UpdateConfig(); UpdateConfig();
LuaCsTimer.Wait((args) => PrintCsError(@" Timer.Wait((args) => PrintCsError(@"
----==== ====---- ----==== ====----
WARNING! WARNING!
@@ -10,6 +10,8 @@ namespace Barotrauma
public static double Time => Timing.TotalTime; public static double Time => Timing.TotalTime;
public static double GetTime() => Time;
public static float GetUsageMemory() public static float GetUsageMemory()
{ {
Process proc = Process.GetCurrentProcess(); Process proc = Process.GetCurrentProcess();
@@ -53,9 +55,9 @@ namespace Barotrauma
} }
} }
private static List<TimedAction> timedActions = new List<TimedAction>(); private List<TimedAction> timedActions = new List<TimedAction>();
private static void AddTimer(TimedAction timedAction) private void AddTimer(TimedAction timedAction)
{ {
int insertionPoint = timedActions.BinarySearch(timedAction, new TimerComparer()); int insertionPoint = timedActions.BinarySearch(timedAction, new TimerComparer());
@@ -67,7 +69,7 @@ namespace Barotrauma
timedActions.Insert(insertionPoint, timedAction); timedActions.Insert(insertionPoint, timedAction);
} }
public static void Update() public void Update()
{ {
while (timedActions.Count > 0) while (timedActions.Count > 0)
{ {
@@ -84,12 +86,12 @@ namespace Barotrauma
} }
} }
public static void Clear() public void Clear()
{ {
timedActions = new List<TimedAction>(); timedActions = new List<TimedAction>();
} }
public static void Wait(LuaCsAction action, int millisecondDelay) public void Wait(LuaCsAction action, int millisecondDelay)
{ {
TimedAction timedAction = new TimedAction(action, millisecondDelay); TimedAction timedAction = new TimedAction(action, millisecondDelay);
AddTimer(timedAction); AddTimer(timedAction);
@@ -315,18 +315,21 @@ namespace Barotrauma
{ {
var httpResponse = httpWebRequest.EndGetResponse(result); var httpResponse = httpWebRequest.EndGetResponse(result);
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
GameMain.LuaCs.Hook.Enqueue(callback, streamReader.ReadToEnd()); {
string responseResult = streamReader.ReadToEnd();
GameMain.LuaCs.Timer.Wait((object[] par) => { callback(responseResult); }, 0);
}
} }
catch (Exception e) catch (Exception e)
{ {
GameMain.LuaCs.Hook.Enqueue(callback, e.ToString()); GameMain.LuaCs.Timer.Wait((object[] par) => { callback(e.Message); }, 0);
} }
}), null); }), null);
} }
catch (Exception e) catch (Exception e)
{ {
GameMain.LuaCs.Hook.Enqueue(callback, e.ToString()); GameMain.LuaCs.Timer.Wait((object[] par) => { callback(e.Message); }, 0);
} }
} }
@@ -342,17 +345,20 @@ namespace Barotrauma
{ {
var httpResponse = httpWebRequest.EndGetResponse(result); var httpResponse = httpWebRequest.EndGetResponse(result);
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
GameMain.LuaCs.Hook.Enqueue(callback, streamReader.ReadToEnd()); {
string responseResult = streamReader.ReadToEnd();
GameMain.LuaCs.Timer.Wait((object[] par) => { callback(responseResult); }, 0);
}
} }
catch (Exception e) catch (Exception e)
{ {
GameMain.LuaCs.Hook.Enqueue(callback, e.ToString()); GameMain.LuaCs.Timer.Wait((object[] par) => { callback(e.Message); }, 0);
} }
}), null); }), null);
} }
catch (Exception e) catch (Exception e)
{ {
GameMain.LuaCs.Hook.Enqueue(callback, e.ToString()); GameMain.LuaCs.Timer.Wait((object[] par) => { callback(e.Message); }, 0);
} }
} }