calling lua function inside an async function causes lots of issues (probably caused by the fact that it runs on a separate thread?), solved by creating a queue of functions to be called on the main thread
This commit is contained in:
@@ -1017,6 +1017,7 @@ namespace Barotrauma
|
|||||||
|
|
||||||
SoundManager?.Update();
|
SoundManager?.Update();
|
||||||
|
|
||||||
|
GameMain.Lua.Update();
|
||||||
GameMain.Lua.hook.Call("think", new object[] { });
|
GameMain.Lua.hook.Call("think", new object[] { });
|
||||||
|
|
||||||
Timing.Accumulator -= Timing.Step;
|
Timing.Accumulator -= Timing.Step;
|
||||||
|
|||||||
@@ -394,6 +394,7 @@ namespace Barotrauma
|
|||||||
TaskPool.Update();
|
TaskPool.Update();
|
||||||
CoroutineManager.Update((float)Timing.Step, (float)Timing.Step);
|
CoroutineManager.Update((float)Timing.Step, (float)Timing.Step);
|
||||||
|
|
||||||
|
GameMain.Lua.Update();
|
||||||
GameMain.Lua.hook.Call("think", new object[] { });
|
GameMain.Lua.hook.Call("think", new object[] { });
|
||||||
performanceMeasurement.Stop();
|
performanceMeasurement.Stop();
|
||||||
LuaSetup.LuaTimer.LastUpdateTime = performanceMeasurement.ElapsedMilliseconds;
|
LuaSetup.LuaTimer.LastUpdateTime = performanceMeasurement.ElapsedMilliseconds;
|
||||||
|
|||||||
@@ -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()
|
public static double GetTime()
|
||||||
{
|
{
|
||||||
return Timing.TotalTime;
|
return Timing.TotalTime;
|
||||||
@@ -661,16 +666,16 @@ 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()))
|
||||||
env.CallFunction(callback, new object[] { streamReader.ReadToEnd() });
|
env.hook.EnqueueFunction(callback, streamReader.ReadToEnd() );
|
||||||
}catch(Exception e)
|
}catch(Exception e)
|
||||||
{
|
{
|
||||||
env.CallFunction(callback, new object[] { e.ToString() });
|
env.hook.EnqueueFunction(callback, e.ToString());
|
||||||
}
|
}
|
||||||
}), null);
|
}), null);
|
||||||
|
|
||||||
}catch(Exception e)
|
}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);
|
var httpResponse = httpWebRequest.EndGetResponse(result);
|
||||||
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
|
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
|
||||||
env.CallFunction(callback, new object[] { streamReader.ReadToEnd() });
|
env.hook.EnqueueFunction(callback, streamReader.ReadToEnd());
|
||||||
}catch (Exception e)
|
}catch (Exception e)
|
||||||
{
|
{
|
||||||
env.CallFunction(callback, new object[] { e.ToString() });
|
env.hook.EnqueueFunction(callback, e.ToString());
|
||||||
}
|
}
|
||||||
}), null);
|
}), null);
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
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<string, string> methodNameToHookName;
|
private static Dictionary<string, string> methodNameToHookName;
|
||||||
|
|
||||||
|
private Queue<Tuple<object, object[]>> queuedFunctionCalls = new Queue<Tuple<object, object[]>>();
|
||||||
|
|
||||||
public enum HookMethodType
|
public enum HookMethodType
|
||||||
{
|
{
|
||||||
Before, After
|
Before, After
|
||||||
@@ -818,6 +825,11 @@ namespace Barotrauma
|
|||||||
methodNameToHookName.Add(methodName, hookName);
|
methodNameToHookName.Add(methodName, hookName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void EnqueueFunction(object function, params object[] args)
|
||||||
|
{
|
||||||
|
queuedFunctionCalls.AddItem(new Tuple<object, object[]>(function, args));
|
||||||
|
}
|
||||||
|
|
||||||
public void Add(string name, string hookName, object function)
|
public void Add(string name, string hookName, object function)
|
||||||
{
|
{
|
||||||
if (name == null && hookName == null && function == null) return;
|
if (name == null && hookName == null && function == null) return;
|
||||||
@@ -839,6 +851,14 @@ namespace Barotrauma
|
|||||||
hookFunctions[name].Remove(hookName);
|
hookFunctions[name].Remove(hookName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Update()
|
||||||
|
{
|
||||||
|
if (queuedFunctionCalls.TryDequeue(out Tuple<object, object[]> result))
|
||||||
|
{
|
||||||
|
env.CallFunction(result.Item1, result.Item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public object Call(string name, params object[] args)
|
public object Call(string name, params object[] args)
|
||||||
{
|
{
|
||||||
if (env == null) return null;
|
if (env == null) return null;
|
||||||
|
|||||||
@@ -36,6 +36,11 @@ namespace Barotrauma
|
|||||||
|
|
||||||
public LuaScriptLoader luaScriptLoader;
|
public LuaScriptLoader luaScriptLoader;
|
||||||
|
|
||||||
|
public void Update()
|
||||||
|
{
|
||||||
|
hook?.Update();
|
||||||
|
}
|
||||||
|
|
||||||
public void HandleLuaException(Exception ex, string extra = "")
|
public void HandleLuaException(Exception ex, string extra = "")
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrWhiteSpace(extra))
|
if (!string.IsNullOrWhiteSpace(extra))
|
||||||
|
|||||||
Reference in New Issue
Block a user