From 79aaa0477551b252fb9681ffbf677fbaba7e3731 Mon Sep 17 00:00:00 2001 From: ORelio Date: Tue, 8 May 2018 19:27:19 +0200 Subject: [PATCH] ScriptScheduler: Add debug messages for issue #431 These messages will only apprear if debugmessages=true in config. --- MinecraftClient/ChatBots/Script.cs | 15 ++++++ MinecraftClient/ChatBots/ScriptScheduler.cs | 52 ++++++++++++++++--- MinecraftClient/ChatBots/TestBot.cs | 4 +- .../Protocol/SessionCache/SessionCache.cs | 1 + 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/MinecraftClient/ChatBots/Script.cs b/MinecraftClient/ChatBots/Script.cs index 4e464f1a..df8d8a49 100644 --- a/MinecraftClient/ChatBots/Script.cs +++ b/MinecraftClient/ChatBots/Script.cs @@ -6,6 +6,7 @@ using System.Threading; using Microsoft.CSharp; using System.CodeDom.Compiler; using System.Reflection; +using System.Diagnostics; namespace MinecraftClient.ChatBots { @@ -108,6 +109,20 @@ namespace MinecraftClient.ChatBots } } + if (Settings.DebugMessages) + { + string caller = "Script"; + try + { + StackFrame frame = new StackFrame(1); + MethodBase method = frame.GetMethod(); + Type type = method.DeclaringType; + caller = type.Name; + } + catch { } + ConsoleIO.WriteLineFormatted(String.Format("ยง8[MCC] [{0}] Cannot find script file: {1}", caller, filename)); + } + return false; } diff --git a/MinecraftClient/ChatBots/ScriptScheduler.cs b/MinecraftClient/ChatBots/ScriptScheduler.cs index cb06fc3e..7fc5d41f 100644 --- a/MinecraftClient/ChatBots/ScriptScheduler.cs +++ b/MinecraftClient/ChatBots/ScriptScheduler.cs @@ -44,6 +44,8 @@ namespace MinecraftClient.ChatBots //Load the given file from the startup parameters if (System.IO.File.Exists(tasksfile)) { + if (Settings.DebugMessages) + LogToConsole("Loading tasks from '" + tasksfile + "'"); TaskDesc current_task = null; String[] lines = System.IO.File.ReadAllLines(tasksfile); foreach (string lineRAW in lines) @@ -95,14 +97,27 @@ 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 - && (current_task.triggerOnLogin - || current_task.triggerOnFirstLogin - || (current_task.triggerOnTime && current_task.triggerOnTime_Times.Count > 0)) - || (current_task.triggerOnInterval && current_task.triggerOnInterval_Interval > 0)) //Look for a valid trigger + if (Script.LookForScript(ref current_task.script_file)) //Check if file exists { - current_task.triggerOnInterval_Interval_Countdown = current_task.triggerOnInterval_Interval; //Init countdown for interval - tasks.Add(current_task); + if (current_task.script_file != null + && (current_task.triggerOnLogin + || current_task.triggerOnFirstLogin + || (current_task.triggerOnTime && current_task.triggerOnTime_Times.Count > 0)) + || (current_task.triggerOnInterval && current_task.triggerOnInterval_Interval > 0)) //Look for a valid trigger + { + if (Settings.DebugMessages) + LogToConsole("Loaded task:\n" + Task2String(current_task)); + current_task.triggerOnInterval_Interval_Countdown = current_task.triggerOnInterval_Interval; //Init countdown for interval + tasks.Add(current_task); + } + else if (Settings.DebugMessages) + { + LogToConsole("This task will never trigger:\n" + Task2String(current_task)); + } + } + else if (Settings.DebugMessages) + { + LogToConsole("No valid script for task:\n" + Task2String(current_task)); } } } @@ -128,6 +143,8 @@ namespace MinecraftClient.ChatBots if (!task.triggerOnTime_alreadyTriggered) { task.triggerOnTime_alreadyTriggered = true; + if (Settings.DebugMessages) + LogToConsole("Time / Running script: " + task.script_file); RunScript(task.script_file); } } @@ -142,6 +159,8 @@ namespace MinecraftClient.ChatBots if (task.triggerOnInterval_Interval_Countdown == 0) { task.triggerOnInterval_Interval_Countdown = task.triggerOnInterval_Interval; + if (Settings.DebugMessages) + LogToConsole("Interval / Running script: " + task.script_file); RunScript(task.script_file); } else task.triggerOnInterval_Interval_Countdown--; @@ -153,7 +172,11 @@ namespace MinecraftClient.ChatBots foreach (TaskDesc task in tasks) { if (task.triggerOnLogin || (firstlogin_done == false && task.triggerOnFirstLogin)) + { + if (Settings.DebugMessages) + LogToConsole("Login / Running script: " + task.script_file); RunScript(task.script_file); + } } firstlogin_done = true; @@ -162,5 +185,20 @@ namespace MinecraftClient.ChatBots } else verifytasks_timeleft--; } + + private static string Task2String(TaskDesc task) + { + return String.Format( + "triggeronfirstlogin = {0}\n triggeronlogin = {1}\n triggerontime = {2}\n " + + "triggeroninterval = {3}\n timevalue = {4}\n timeinterval = {5}\n script = {6}", + task.triggerOnFirstLogin, + task.triggerOnLogin, + task.triggerOnTime, + task.triggerOnInterval, + String.Join(", ", task.triggerOnTime_Times), + task.triggerOnInterval_Interval, + task.script_file + ); + } } } diff --git a/MinecraftClient/ChatBots/TestBot.cs b/MinecraftClient/ChatBots/TestBot.cs index ce6ef8f7..e8afcaa6 100644 --- a/MinecraftClient/ChatBots/TestBot.cs +++ b/MinecraftClient/ChatBots/TestBot.cs @@ -19,11 +19,11 @@ namespace MinecraftClient.ChatBots if (IsPrivateMessage(text, ref message, ref username)) { - ConsoleIO.WriteLine("Bot: " + username + " told me : " + message); + LogToConsole("Bot: " + username + " told me : " + message); } else if (IsChatMessage(text, ref message, ref username)) { - ConsoleIO.WriteLine("Bot: " + username + " said : " + message); + LogToConsole("Bot: " + username + " said : " + message); } } } diff --git a/MinecraftClient/Protocol/SessionCache/SessionCache.cs b/MinecraftClient/Protocol/SessionCache/SessionCache.cs index 474cae06..6e65a4fb 100644 --- a/MinecraftClient/Protocol/SessionCache/SessionCache.cs +++ b/MinecraftClient/Protocol/SessionCache/SessionCache.cs @@ -260,6 +260,7 @@ namespace MinecraftClient.Protocol.SessionCache { List sessionCacheLines = new List(); sessionCacheLines.Add("# Generated by MCC v" + Program.Version + " - Edit at own risk!"); + sessionCacheLines.Add("# Login=SessionID,PlayerName,UUID,ClientID"); foreach (KeyValuePair entry in sessions) sessionCacheLines.Add(entry.Key + '=' + entry.Value.ToString()); File.WriteAllLines(SessionCacheFilePlaintext, sessionCacheLines);