Implement thread-safe ChatBot API (#1510, #1579)

+ Rework task scheduling in chatbots
+ Switch back terrain processing to tasks
This commit is contained in:
ORelio 2021-05-15 17:36:16 +02:00
parent c1cfaf520d
commit 95d6318350
7 changed files with 517 additions and 461 deletions

View file

@ -24,7 +24,6 @@ namespace MinecraftClient.ChatBots
private string owner;
private bool csharp;
private Thread thread;
private ManualResetEvent tpause;
private Dictionary<string, object> localVars;
public Script(string filename)
@ -157,12 +156,11 @@ namespace MinecraftClient.ChatBots
//Initialize thread on first update
if (thread == null)
{
tpause = new ManualResetEvent(false);
thread = new Thread(() =>
{
try
{
CSharpRunner.Run(this, tpause, lines, args, localVars);
CSharpRunner.Run(this, lines, args, localVars);
}
catch (CSharpException e)
{
@ -173,16 +171,14 @@ namespace MinecraftClient.ChatBots
LogToConsole(e.InnerException);
}
});
thread.Name = "MCC Script - " + file;
thread.Start();
}
//Let the thread run for a short span of time
if (thread != null)
//Unload bot once the thread has finished running
if (thread != null && !thread.IsAlive)
{
tpause.Set();
tpause.Reset();
if (!thread.IsAlive)
UnloadBot();
UnloadBot();
}
}
else //Classic MCC script interpreter