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

View File

@@ -55,8 +55,6 @@ namespace Barotrauma
private Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>> hookPrefixMethods;
private Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>> hookPostfixMethods;
private Queue<(float, LuaCsAction, object[])> queuedFunctionCalls;
private static LuaCsHook instance;
public LuaCsHook() {
@@ -66,8 +64,6 @@ namespace Barotrauma
hookPrefixMethods = new Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>>();
hookPostfixMethods = new Dictionary<long, HashSet<(string, LuaCsPatch, ACsMod)>>();
queuedFunctionCalls = new Queue<(float, LuaCsAction, object[])>();
}
public void Initialize()
@@ -339,20 +335,6 @@ namespace Barotrauma
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)
{
name = name.ToLower();
@@ -382,29 +364,13 @@ namespace Barotrauma
hookPrefixMethods.Clear();
hookPostfixMethods.Clear();
queuedFunctionCalls.Clear();
harmony?.UnpatchAll();
}
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]

View File

@@ -56,6 +56,9 @@ namespace Barotrauma
public LuaScriptLoader LuaScriptLoader { get; private set; }
public LuaCsHook Hook { get; private set; }
public LuaCsTimer Timer { get; private set; }
public LuaCsNetworking Networking { get; private set; }
public LuaCsModStore ModStore { get; private set; }
private LuaRequire require { get; set; }
@@ -297,7 +300,7 @@ namespace Barotrauma
public void Update()
{
Hook?.Update();
LuaCsTimer.Update();
Timer?.Update();
}
public void Stop()
@@ -313,7 +316,6 @@ namespace Barotrauma
if (Thread.CurrentThread == GameMain.MainThread)
{
Hook?.Call("stop");
LuaCsTimer.Clear();
}
Game?.Stop();
@@ -322,6 +324,7 @@ namespace Barotrauma
ModStore.Clear();
Game = new LuaGame();
Networking = new LuaCsNetworking();
Timer = new LuaCsTimer();
LuaScriptLoader = null;
lua = null;
Lua = null;
@@ -369,6 +372,7 @@ namespace Barotrauma
Game = new LuaGame();
Networking = new LuaCsNetworking();
Timer = new LuaCsTimer();
Hook.Initialize();
ModStore.Initialize();
@@ -401,7 +405,7 @@ namespace Barotrauma
lua.Globals["Game"] = Game;
lua.Globals["Hook"] = Hook;
lua.Globals["ModStore"] = ModStore;
lua.Globals["Timer"] = new LuaCsTimer();
lua.Globals["Timer"] = Timer;
lua.Globals["File"] = UserData.CreateStatic<LuaCsFile>();
lua.Globals["Networking"] = Networking;
@@ -418,7 +422,7 @@ namespace Barotrauma
Config.FirstTimeCsWarning = false;
UpdateConfig();
LuaCsTimer.Wait((args) => PrintCsError(@"
Timer.Wait((args) => PrintCsError(@"
----==== ====----
WARNING!

View File

@@ -9,6 +9,8 @@ namespace Barotrauma
public static long LastUpdateTime = 0;
public static double Time => Timing.TotalTime;
public static double GetTime() => Time;
public static float GetUsageMemory()
{
@@ -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());
@@ -67,7 +69,7 @@ namespace Barotrauma
timedActions.Insert(insertionPoint, timedAction);
}
public static void Update()
public void Update()
{
while (timedActions.Count > 0)
{
@@ -84,12 +86,12 @@ namespace Barotrauma
}
}
public static void Clear()
public void Clear()
{
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);
AddTimer(timedAction);

View File

@@ -315,18 +315,21 @@ namespace Barotrauma
{
var httpResponse = httpWebRequest.EndGetResponse(result);
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)
{
GameMain.LuaCs.Hook.Enqueue(callback, e.ToString());
GameMain.LuaCs.Timer.Wait((object[] par) => { callback(e.Message); }, 0);
}
}), null);
}
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);
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)
{
GameMain.LuaCs.Hook.Enqueue(callback, e.ToString());
GameMain.LuaCs.Timer.Wait((object[] par) => { callback(e.Message); }, 0);
}
}), null);
}
catch (Exception e)
{
GameMain.LuaCs.Hook.Enqueue(callback, e.ToString());
GameMain.LuaCs.Timer.Wait((object[] par) => { callback(e.Message); }, 0);
}
}