Give access to AutoRespond matches inside scripts

Available as %match_u%, %match_1% and so on
See #770 for initial suggestion
See #772 for in-progress implementation
This commit is contained in:
ORelio 2020-03-27 21:14:05 +01:00
parent e4e1f0b9fa
commit 00112e4c6a
25 changed files with 129 additions and 69 deletions

View file

@ -25,10 +25,11 @@ namespace MinecraftClient
/// <param name="tickHandler">Tick handler for waiting after some API calls</param>
/// <param name="lines">Lines of the script file to run</param>
/// <param name="args">Arguments to pass to the script</param>
/// <param name="localVars">Local variables passed along with the script</param>
/// <param name="run">Set to false to compile and cache the script without launching it</param>
/// <exception cref="CSharpException">Thrown if an error occured</exception>
/// <returns>Result of the execution, returned by the script</returns>
public static object Run(ChatBot apiHandler, ManualResetEvent tickHandler, string[] lines, string[] args, bool run = true)
public static object Run(ChatBot apiHandler, ManualResetEvent tickHandler, string[] lines, string[] args, Dictionary<string, object> localVars, bool run = true)
{
//Script compatibility check for handling future versions differently
if (lines.Length < 1 || lines[0] != "//MCCScript 1.0")
@ -132,7 +133,7 @@ namespace MinecraftClient
.GetType()
.GetMethod("__run")
.Invoke(compiledScript,
new object[] { new CSharpAPI(apiHandler, tickHandler), args });
new object[] { new CSharpAPI(apiHandler, tickHandler, localVars), args });
}
catch (Exception e) { throw new CSharpException(CSErrorType.RuntimeError, e); }
}
@ -192,15 +193,22 @@ namespace MinecraftClient
/// </summary>
private ManualResetEvent tickHandler;
/// <summary>
/// Holds local variables passed along with the script
/// </summary>
private Dictionary<string, object> localVars;
/// <summary>
/// Create a new C# API Wrapper
/// </summary>
/// <param name="apiHandler">ChatBot API Handler</param>
/// <param name="tickHandler">ChatBot tick handler</param>
public CSharpAPI(ChatBot apiHandler, ManualResetEvent tickHandler)
/// <param name="localVars">Local variables passed along with the script</param>
public CSharpAPI(ChatBot apiHandler, ManualResetEvent tickHandler, Dictionary<string , object> localVars)
{
SetMaster(apiHandler);
this.tickHandler = tickHandler;
this.localVars = localVars;
}
/* == Wrappers for ChatBot API with public visibility and call limit to one per tick for safety == */
@ -231,10 +239,13 @@ namespace MinecraftClient
/// Perform an internal MCC command (not a server command, use SendText() instead for that!)
/// </summary>
/// <param name="command">The command to process</param>
/// <param name="localVars">Local variables passed along with the script</param>
/// <returns>TRUE if the command was indeed an internal MCC command</returns>
new public bool PerformInternalCommand(string command)
new public bool PerformInternalCommand(string command, Dictionary<string, object> localVars = null)
{
bool result = base.PerformInternalCommand(command);
if (localVars == null)
localVars = this.localVars;
bool result = base.PerformInternalCommand(command, localVars);
tickHandler.WaitOne();
return result;
}
@ -302,6 +313,8 @@ namespace MinecraftClient
/// <returns>Value of the variable or null if no variable</returns>
public object GetVar(string varName)
{
if (localVars != null && localVars.ContainsKey(varName))
return localVars[varName];
return Settings.GetVar(varName);
}
@ -312,6 +325,8 @@ namespace MinecraftClient
/// <param name="varValue">Value of the variable</param>
public bool SetVar(string varName, object varValue)
{
if (localVars != null && localVars.ContainsKey(varName))
localVars.Remove(varName);
return Settings.SetVar(varName, varValue);
}
@ -385,7 +400,7 @@ namespace MinecraftClient
ChatBots.Script.LookForScript(ref script);
try { lines = File.ReadAllLines(script); }
catch (Exception e) { throw new CSharpException(CSErrorType.FileReadError, e); }
return CSharpRunner.Run(this, tickHandler, lines, args);
return CSharpRunner.Run(this, tickHandler, lines, args, localVars);
}
}
}