mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
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
This commit is contained in:
parent
e29b4ee545
commit
f076e1f512
3 changed files with 61 additions and 10 deletions
|
|
@ -16,6 +16,7 @@ namespace MinecraftClient.ChatBots
|
||||||
{
|
{
|
||||||
private string file;
|
private string file;
|
||||||
private string[] lines = new string[0];
|
private string[] lines = new string[0];
|
||||||
|
private string[] args = new string[0];
|
||||||
private int sleepticks = 10;
|
private int sleepticks = 10;
|
||||||
private int nextline = 0;
|
private int nextline = 0;
|
||||||
private string owner;
|
private string owner;
|
||||||
|
|
@ -25,7 +26,7 @@ namespace MinecraftClient.ChatBots
|
||||||
|
|
||||||
public Script(string filename)
|
public Script(string filename)
|
||||||
{
|
{
|
||||||
file = filename;
|
ParseArguments(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Script(string filename, string ownername)
|
public Script(string filename, string ownername)
|
||||||
|
|
@ -35,7 +36,52 @@ namespace MinecraftClient.ChatBots
|
||||||
owner = ownername;
|
owner = ownername;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool lookForScript(ref string filename)
|
private void ParseArguments(string argstr)
|
||||||
|
{
|
||||||
|
List<string> args = new List<string>();
|
||||||
|
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
|
//Automatically look in subfolders and try to add ".txt" file extension
|
||||||
char dir_slash = Program.isUsingMono ? '/' : '\\';
|
char dir_slash = Program.isUsingMono ? '/' : '\\';
|
||||||
|
|
@ -67,7 +113,7 @@ namespace MinecraftClient.ChatBots
|
||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
{
|
{
|
||||||
//Load the given file from the startup parameters
|
//Load the given file from the startup parameters
|
||||||
if (lookForScript(ref file))
|
if (LookForScript(ref file))
|
||||||
{
|
{
|
||||||
lines = System.IO.File.ReadAllLines(file);
|
lines = System.IO.File.ReadAllLines(file);
|
||||||
csharp = file.EndsWith(".cs");
|
csharp = file.EndsWith(".cs");
|
||||||
|
|
@ -199,7 +245,7 @@ namespace MinecraftClient.ChatBots
|
||||||
"using MinecraftClient;",
|
"using MinecraftClient;",
|
||||||
"namespace ScriptLoader {",
|
"namespace ScriptLoader {",
|
||||||
"public class Script : ChatBot {",
|
"public class Script : ChatBot {",
|
||||||
"public void __run(ChatBot master, ManualResetEvent tpause) {",
|
"public void __run(ChatBot master, ManualResetEvent tpause, string[] args) {",
|
||||||
"SetMaster(master);",
|
"SetMaster(master);",
|
||||||
String.Join("\n", script),
|
String.Join("\n", script),
|
||||||
"}",
|
"}",
|
||||||
|
|
@ -229,7 +275,7 @@ namespace MinecraftClient.ChatBots
|
||||||
|
|
||||||
//Run the compiled script with exception handling
|
//Run the compiled script with exception handling
|
||||||
object compiledScript = result.CompiledAssembly.CreateInstance("ScriptLoader.Script");
|
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)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
LogToConsole("Runtime error for '" + file + "':\n" + e);
|
LogToConsole("Runtime error for '" + file + "':\n" + e);
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,7 @@ namespace MinecraftClient.ChatBots
|
||||||
if (current_task != null)
|
if (current_task != null)
|
||||||
{
|
{
|
||||||
//Check if we built a valid task before adding it
|
//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.triggerOnLogin
|
||||||
|| (current_task.triggerOnTime && current_task.triggerOnTime_Times.Count > 0))
|
|| (current_task.triggerOnTime && current_task.triggerOnTime_Times.Count > 0))
|
||||||
|| (current_task.triggerOnInterval && current_task.triggerOnInterval_Interval > 0)) //Look for a valid trigger
|
|| (current_task.triggerOnInterval && current_task.triggerOnInterval_Interval > 0)) //Look for a valid trigger
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,17 @@
|
||||||
//MCCScript 1.0
|
//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++)
|
for (int i = 0; i < 5; i++)
|
||||||
{
|
{
|
||||||
int count = GetVarAsInt("test") + 1;
|
int count = GetVarAsInt("test") + 1;
|
||||||
SetVar("test", count);
|
SetVar("test", count);
|
||||||
SendHelloWorld(count);
|
SendHelloWorld(count, text);
|
||||||
SleepBetweenSends();
|
SleepBetweenSends();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -14,13 +19,13 @@ for (int i = 0; i < 5; i++)
|
||||||
|
|
||||||
/* Here you can define methods for use into your script */
|
/* 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
|
/* Warning: Do not make more than one server-related call into a method
|
||||||
* defined as a script extension eg SendText or switching servers,
|
* defined as a script extension eg SendText or switching servers,
|
||||||
* as execution flow is not managed in the Extensions section */
|
* as execution flow is not managed in the Extensions section */
|
||||||
|
|
||||||
SendText("Hello World no. " + count);
|
SendText("Hello World no. " + count + ": " + text);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SleepBetweenSends()
|
void SleepBetweenSends()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue