From f076e1f51226895f0fbd6ae82ed0261beb9959f0 Mon Sep 17 00:00:00 2001 From: ORelio Date: Thu, 25 Jun 2015 12:12:59 +0200 Subject: [PATCH] Add argument passing for C# scripts script files with spaces in filename will need double quotes when calling them eg /script "my script.txt" instead of /script my script.txt --- MinecraftClient/ChatBots/Script.cs | 56 +++++++++++++++++-- MinecraftClient/ChatBots/ScriptScheduler.cs | 2 +- .../config/sample-script-extended.cs | 13 +++-- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/MinecraftClient/ChatBots/Script.cs b/MinecraftClient/ChatBots/Script.cs index 6fc8573d..0e1894d9 100644 --- a/MinecraftClient/ChatBots/Script.cs +++ b/MinecraftClient/ChatBots/Script.cs @@ -16,6 +16,7 @@ namespace MinecraftClient.ChatBots { private string file; private string[] lines = new string[0]; + private string[] args = new string[0]; private int sleepticks = 10; private int nextline = 0; private string owner; @@ -25,7 +26,7 @@ namespace MinecraftClient.ChatBots public Script(string filename) { - file = filename; + ParseArguments(filename); } public Script(string filename, string ownername) @@ -35,7 +36,52 @@ namespace MinecraftClient.ChatBots owner = ownername; } - public static bool lookForScript(ref string filename) + private void ParseArguments(string argstr) + { + List args = new List(); + StringBuilder str = new StringBuilder(); + + bool escape = false; + bool quotes = false; + + foreach (char c in argstr) + { + if (escape) + { + if (c != '"') + str.Append('\\'); + str.Append(c); + escape = false; + } + else + { + if (c == '\\') + escape = true; + else if (c == '"') + quotes = !quotes; + else if (c == ' ' && !quotes) + { + if (str.Length > 0) + args.Add(str.ToString()); + str.Clear(); + } + else str.Append(c); + } + } + + if (str.Length > 0) + args.Add(str.ToString()); + + if (args.Count > 0) + { + file = args[0]; + args.RemoveAt(0); + this.args = args.ToArray(); + } + else file = ""; + } + + public static bool LookForScript(ref string filename) { //Automatically look in subfolders and try to add ".txt" file extension char dir_slash = Program.isUsingMono ? '/' : '\\'; @@ -67,7 +113,7 @@ namespace MinecraftClient.ChatBots public override void Initialize() { //Load the given file from the startup parameters - if (lookForScript(ref file)) + if (LookForScript(ref file)) { lines = System.IO.File.ReadAllLines(file); csharp = file.EndsWith(".cs"); @@ -199,7 +245,7 @@ namespace MinecraftClient.ChatBots "using MinecraftClient;", "namespace ScriptLoader {", "public class Script : ChatBot {", - "public void __run(ChatBot master, ManualResetEvent tpause) {", + "public void __run(ChatBot master, ManualResetEvent tpause, string[] args) {", "SetMaster(master);", String.Join("\n", script), "}", @@ -229,7 +275,7 @@ namespace MinecraftClient.ChatBots //Run the compiled script with exception handling object compiledScript = result.CompiledAssembly.CreateInstance("ScriptLoader.Script"); - try { compiledScript.GetType().GetMethod("__run").Invoke(compiledScript, new object[] { this, tpause }); } + try { compiledScript.GetType().GetMethod("__run").Invoke(compiledScript, new object[] { this, tpause, args }); } catch (Exception e) { LogToConsole("Runtime error for '" + file + "':\n" + e); diff --git a/MinecraftClient/ChatBots/ScriptScheduler.cs b/MinecraftClient/ChatBots/ScriptScheduler.cs index d1783cb7..7605fc40 100644 --- a/MinecraftClient/ChatBots/ScriptScheduler.cs +++ b/MinecraftClient/ChatBots/ScriptScheduler.cs @@ -95,7 +95,7 @@ namespace MinecraftClient.ChatBots if (current_task != null) { //Check if we built a valid task before adding it - if (current_task.script_file != null && Script.lookForScript(ref current_task.script_file) //Check if file exists + if (current_task.script_file != null && Script.LookForScript(ref current_task.script_file) //Check if file exists && (current_task.triggerOnLogin || (current_task.triggerOnTime && current_task.triggerOnTime_Times.Count > 0)) || (current_task.triggerOnInterval && current_task.triggerOnInterval_Interval > 0)) //Look for a valid trigger diff --git a/MinecraftClient/config/sample-script-extended.cs b/MinecraftClient/config/sample-script-extended.cs index be6694da..68ff8cd1 100644 --- a/MinecraftClient/config/sample-script-extended.cs +++ b/MinecraftClient/config/sample-script-extended.cs @@ -1,12 +1,17 @@ //MCCScript 1.0 -/* This script demonstrates how to add fields and methods */ +/* This script demonstrates how to use methods and arguments */ +string text = "hello"; + +if (args.Length > 0) + text = args[0]; + for (int i = 0; i < 5; i++) { int count = GetVarAsInt("test") + 1; SetVar("test", count); - SendHelloWorld(count); + SendHelloWorld(count, text); SleepBetweenSends(); } @@ -14,13 +19,13 @@ for (int i = 0; i < 5; i++) /* Here you can define methods for use into your script */ -void SendHelloWorld(int count) +void SendHelloWorld(int count, string text) { /* Warning: Do not make more than one server-related call into a method * defined as a script extension eg SendText or switching servers, * as execution flow is not managed in the Extensions section */ - SendText("Hello World no. " + count); + SendText("Hello World no. " + count + ": " + text); } void SleepBetweenSends()