mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Merge ScriptScheduler's config
This commit is contained in:
parent
e4952dbee0
commit
e0678ea7a5
3 changed files with 141 additions and 172 deletions
|
|
@ -3,7 +3,9 @@ using System.Collections.Generic;
|
|||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Tomlet.Attributes;
|
||||
using static MinecraftClient.ChatBots.ScriptScheduler.Configs;
|
||||
|
||||
namespace MinecraftClient.ChatBots
|
||||
{
|
||||
|
|
@ -23,155 +25,126 @@ namespace MinecraftClient.ChatBots
|
|||
|
||||
public bool Enabled = false;
|
||||
|
||||
public string Tasks_File = @"tasks.ini";
|
||||
public TaskConfig[] TaskList = new TaskConfig[] { new() };
|
||||
|
||||
public void OnSettingUpdate()
|
||||
{
|
||||
Tasks_File ??= string.Empty;
|
||||
|
||||
if (!Enabled) return;
|
||||
|
||||
string Tasks_File_Full = Settings.Config.AppVar.ExpandVars(Tasks_File);
|
||||
if (!File.Exists(Tasks_File_Full))
|
||||
foreach (TaskConfig task in TaskList)
|
||||
{
|
||||
//Look for a valid action
|
||||
if (!String.IsNullOrWhiteSpace(task.Action))
|
||||
{
|
||||
//Look for a valid trigger
|
||||
if (task.Trigger_On_Login
|
||||
|| task.Trigger_On_First_Login
|
||||
|| (task.Trigger_On_Times.Enable && task.Trigger_On_Times.Times.Length > 0)
|
||||
|| (task.Trigger_On_Interval.Enable && task.Trigger_On_Interval.MinTime > 0))
|
||||
{
|
||||
if (Settings.Config.Logging.DebugMessages)
|
||||
LogToConsole(BotName, Translations.TryGet("bot.scriptScheduler.loaded_task", Task2String(task)));
|
||||
task.Trigger_On_Interval_Countdown = task.Trigger_On_Interval.MinTime; //Init countdown for interval
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Settings.Config.Logging.DebugMessages)
|
||||
LogToConsole(BotName, Translations.TryGet("bot.scriptScheduler.no_trigger", Task2String(task)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
task.Action ??= string.Empty;
|
||||
if (Settings.Config.Logging.DebugMessages)
|
||||
LogToConsole(BotName, Translations.TryGet("bot.scriptScheduler.no_action", Task2String(task)));
|
||||
}
|
||||
}
|
||||
|
||||
if (Enabled && TaskList.Length == 0)
|
||||
{
|
||||
LogToConsole(BotName, Translations.TryGet("bot.scriptScheduler.not_found", Path.GetFullPath(Tasks_File_Full)));
|
||||
LogToConsole(BotName, Translations.TryGet("general.bot_unload"));
|
||||
Enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
public class TaskConfig
|
||||
{
|
||||
public string Task_Name = "Task Name (Can be empty)";
|
||||
public bool Trigger_On_First_Login = false;
|
||||
public bool Trigger_On_Login = false;
|
||||
public TriggerOnTimeConfig Trigger_On_Times = new(false, new TimeSpan[] { new(23, 59, 59) });
|
||||
public TriggerOnIntervalConfig Trigger_On_Interval = new(false, 1, 10);
|
||||
public string Action = "send /hello";
|
||||
|
||||
[NonSerialized]
|
||||
public bool Trigger_On_Time_Already_Triggered = false;
|
||||
|
||||
[NonSerialized]
|
||||
public int Trigger_On_Interval_Countdown = 0;
|
||||
|
||||
public struct TriggerOnTimeConfig
|
||||
{
|
||||
public bool Enable = false;
|
||||
public TimeSpan[] Times;
|
||||
|
||||
public TriggerOnTimeConfig(bool Enable, TimeSpan[] Time)
|
||||
{
|
||||
this.Enable = Enable;
|
||||
this.Times = Time;
|
||||
}
|
||||
|
||||
public TriggerOnTimeConfig(TimeSpan[] Time)
|
||||
{
|
||||
this.Enable = true;
|
||||
this.Times = Time;
|
||||
}
|
||||
}
|
||||
|
||||
public struct TriggerOnIntervalConfig
|
||||
{
|
||||
public bool Enable = false;
|
||||
public int MinTime, MaxTime;
|
||||
|
||||
public TriggerOnIntervalConfig(int value)
|
||||
{
|
||||
this.Enable = true;
|
||||
value = Math.Max(value, 10);
|
||||
MinTime = MaxTime = value;
|
||||
}
|
||||
|
||||
public TriggerOnIntervalConfig(bool Enable, int value)
|
||||
{
|
||||
this.Enable = Enable;
|
||||
value = Math.Max(value, 10);
|
||||
MinTime = MaxTime = value;
|
||||
}
|
||||
|
||||
public TriggerOnIntervalConfig(int min, int max)
|
||||
{
|
||||
min = Math.Max(min, 10);
|
||||
max = Math.Max(max, 10);
|
||||
this.MinTime = min;
|
||||
this.MaxTime = max;
|
||||
}
|
||||
|
||||
public TriggerOnIntervalConfig(bool Enable, int min, int max)
|
||||
{
|
||||
this.Enable = Enable;
|
||||
min = Math.Max(min, 10);
|
||||
max = Math.Max(max, 10);
|
||||
this.MinTime = min;
|
||||
this.MaxTime = max;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class TaskDesc
|
||||
{
|
||||
public string? action = null;
|
||||
public bool triggerOnFirstLogin = false;
|
||||
public bool triggerOnLogin = false;
|
||||
public bool triggerOnTime = false;
|
||||
public bool triggerOnInterval = false;
|
||||
public int triggerOnInterval_Interval = 0;
|
||||
public int triggerOnInterval_Interval_Max = 0;
|
||||
public int triggerOnInterval_Interval_Countdown = 0;
|
||||
public List<DateTime> triggerOnTime_Times = new();
|
||||
public bool triggerOnTime_alreadyTriggered = false;
|
||||
}
|
||||
private Random random = new();
|
||||
|
||||
private static bool firstlogin_done = false;
|
||||
|
||||
private bool serverlogin_done = false;
|
||||
private readonly List<TaskDesc> tasks = new();
|
||||
private int verifytasks_timeleft = 10;
|
||||
private readonly int verifytasks_delay = 10;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
//Load the given file from the startup parameters
|
||||
string Tasks_File_Full = Settings.Config.AppVar.ExpandVars(Config.Tasks_File);
|
||||
if (File.Exists(Tasks_File_Full))
|
||||
{
|
||||
LogDebugToConsoleTranslated("bot.scriptScheduler.loading", System.IO.Path.GetFullPath(Tasks_File_Full));
|
||||
TaskDesc? current_task = null;
|
||||
string[] lines = System.IO.File.ReadAllLines(Tasks_File_Full, Encoding.UTF8);
|
||||
foreach (string lineRAW in lines)
|
||||
{
|
||||
string line = lineRAW.Split('#')[0].Trim();
|
||||
if (line.Length > 0)
|
||||
{
|
||||
if (line[0] == '[' && line[^1] == ']')
|
||||
{
|
||||
switch (line[1..^1].ToLower())
|
||||
{
|
||||
case "task":
|
||||
CheckAddTask(current_task);
|
||||
current_task = new TaskDesc(); //Create a blank task
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (current_task != null)
|
||||
{
|
||||
string argName = line.Split('=')[0];
|
||||
if (line.Length > (argName.Length + 1))
|
||||
{
|
||||
string argValue = line[(argName.Length + 1)..];
|
||||
switch (argName.ToLower())
|
||||
{
|
||||
case "triggeronfirstlogin": current_task.triggerOnFirstLogin = bool.Parse(argValue); break;
|
||||
case "triggeronlogin": current_task.triggerOnLogin = bool.Parse(argValue); break;
|
||||
case "triggerontime": current_task.triggerOnTime = bool.Parse(argValue); break;
|
||||
case "triggeroninterval": current_task.triggerOnInterval = bool.Parse(argValue); break;
|
||||
case "timevalue": try { current_task.triggerOnTime_Times.Add(DateTime.ParseExact(argValue, "HH:mm", CultureInfo.InvariantCulture)); } catch { } break;
|
||||
case "timeinterval":
|
||||
int interval;
|
||||
int intervalMax = 0;
|
||||
|
||||
if (argValue.Contains('-'))
|
||||
{
|
||||
string[] parts = argValue.Split("-");
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
interval = int.Parse(parts[0].Trim(), NumberStyles.Any, CultureInfo.CurrentCulture);
|
||||
intervalMax = int.Parse(parts[1].Trim(), NumberStyles.Any, CultureInfo.CurrentCulture);
|
||||
}
|
||||
else
|
||||
{
|
||||
interval = 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
interval = int.Parse(argValue, NumberStyles.Any, CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
current_task.triggerOnInterval_Interval = interval;
|
||||
current_task.triggerOnInterval_Interval_Max = intervalMax;
|
||||
|
||||
break;
|
||||
case "script": current_task.action = "script " + argValue; break; //backward compatibility with older tasks.ini
|
||||
case "action": current_task.action = argValue; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
CheckAddTask(current_task);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogToConsoleTranslated("bot.scriptScheduler.not_found", Path.GetFullPath(Tasks_File_Full));
|
||||
UnloadBot(); //No need to keep the bot active
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckAddTask(TaskDesc? current_task)
|
||||
{
|
||||
//Check if we built a valid task before adding it
|
||||
if (current_task != null)
|
||||
{
|
||||
//Look for a valid action
|
||||
if (!String.IsNullOrWhiteSpace(current_task.action))
|
||||
{
|
||||
//Look for a valid trigger
|
||||
if (current_task.triggerOnLogin
|
||||
|| current_task.triggerOnFirstLogin
|
||||
|| (current_task.triggerOnTime && current_task.triggerOnTime_Times.Count > 0)
|
||||
|| (current_task.triggerOnInterval && current_task.triggerOnInterval_Interval > 0))
|
||||
{
|
||||
|
||||
LogDebugToConsoleTranslated("bot.scriptScheduler.loaded_task", Task2String(current_task));
|
||||
current_task.triggerOnInterval_Interval_Countdown = current_task.triggerOnInterval_Interval; //Init countdown for interval
|
||||
tasks.Add(current_task);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogDebugToConsoleTranslated("bot.scriptScheduler.no_trigger", Task2String(current_task));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LogDebugToConsoleTranslated("bot.scriptScheduler.no_action", Task2String(current_task));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
if (verifytasks_timeleft <= 0)
|
||||
|
|
@ -179,55 +152,50 @@ namespace MinecraftClient.ChatBots
|
|||
verifytasks_timeleft = verifytasks_delay;
|
||||
if (serverlogin_done)
|
||||
{
|
||||
foreach (TaskDesc task in tasks)
|
||||
foreach (TaskConfig task in Config.TaskList)
|
||||
{
|
||||
if (task.triggerOnTime)
|
||||
if (task.Trigger_On_Times.Enable)
|
||||
{
|
||||
bool matching_time_found = false;
|
||||
|
||||
foreach (DateTime time in task.triggerOnTime_Times)
|
||||
foreach (TimeSpan time in task.Trigger_On_Times.Times)
|
||||
{
|
||||
if (time.Hour == DateTime.Now.Hour && time.Minute == DateTime.Now.Minute)
|
||||
if (time.Hours == DateTime.Now.Hour && time.Minutes == DateTime.Now.Minute)
|
||||
{
|
||||
matching_time_found = true;
|
||||
if (!task.triggerOnTime_alreadyTriggered)
|
||||
if (!task.Trigger_On_Time_Already_Triggered)
|
||||
{
|
||||
task.triggerOnTime_alreadyTriggered = true;
|
||||
LogDebugToConsoleTranslated("bot.scriptScheduler.running_time", task.action);
|
||||
PerformInternalCommand(task.action!);
|
||||
task.Trigger_On_Time_Already_Triggered = true;
|
||||
LogDebugToConsoleTranslated("bot.scriptScheduler.running_time", task.Action);
|
||||
PerformInternalCommand(task.Action);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!matching_time_found)
|
||||
task.triggerOnTime_alreadyTriggered = false;
|
||||
task.Trigger_On_Time_Already_Triggered = false;
|
||||
}
|
||||
|
||||
if (task.triggerOnInterval)
|
||||
if (task.Trigger_On_Interval.Enable)
|
||||
{
|
||||
if (task.triggerOnInterval_Interval_Countdown == 0)
|
||||
if (task.Trigger_On_Interval_Countdown == 0)
|
||||
{
|
||||
int time = task.triggerOnInterval_Interval;
|
||||
|
||||
if (task.triggerOnInterval_Interval_Max != 0)
|
||||
time = new Random().Next(task.triggerOnInterval_Interval, task.triggerOnInterval_Interval_Max);
|
||||
|
||||
task.triggerOnInterval_Interval_Countdown = time;
|
||||
LogDebugToConsoleTranslated("bot.scriptScheduler.running_inverval", task.action);
|
||||
PerformInternalCommand(task.action!);
|
||||
task.Trigger_On_Interval_Countdown = random.Next(task.Trigger_On_Interval.MinTime, task.Trigger_On_Interval.MaxTime);
|
||||
LogDebugToConsoleTranslated("bot.scriptScheduler.running_inverval", task.Action);
|
||||
PerformInternalCommand(task.Action);
|
||||
}
|
||||
else task.triggerOnInterval_Interval_Countdown--;
|
||||
else task.Trigger_On_Interval_Countdown--;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (TaskDesc task in tasks)
|
||||
foreach (TaskConfig task in Config.TaskList)
|
||||
{
|
||||
if (task.triggerOnLogin || (firstlogin_done == false && task.triggerOnFirstLogin))
|
||||
if (task.Trigger_On_Login || (firstlogin_done == false && task.Trigger_On_First_Login))
|
||||
{
|
||||
LogDebugToConsoleTranslated("bot.scriptScheduler.running_login", task.action);
|
||||
PerformInternalCommand(task.action!);
|
||||
LogDebugToConsoleTranslated("bot.scriptScheduler.running_login", task.Action);
|
||||
PerformInternalCommand(task.Action);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -244,17 +212,17 @@ namespace MinecraftClient.ChatBots
|
|||
return false;
|
||||
}
|
||||
|
||||
private static string Task2String(TaskDesc task)
|
||||
private static string Task2String(TaskConfig task)
|
||||
{
|
||||
return Translations.Get(
|
||||
"bot.scriptScheduler.task",
|
||||
task.triggerOnFirstLogin,
|
||||
task.triggerOnLogin,
|
||||
task.triggerOnTime,
|
||||
task.triggerOnInterval,
|
||||
String.Join(", ", task.triggerOnTime_Times),
|
||||
task.triggerOnInterval_Interval,
|
||||
task.action
|
||||
task.Trigger_On_First_Login,
|
||||
task.Trigger_On_Login,
|
||||
task.Trigger_On_Times.Enable,
|
||||
task.Trigger_On_Interval.Enable,
|
||||
task.Trigger_On_Times.Times,
|
||||
task.Trigger_On_Interval.MinTime + '-' + task.Trigger_On_Interval.MaxTime,
|
||||
task.Action
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -452,7 +452,7 @@ namespace MinecraftClient
|
|||
|
||||
if (timeoutdetector != null)
|
||||
{
|
||||
if (Thread.CurrentThread != timeoutdetector.Item1)
|
||||
if (timeoutdetector != null && Thread.CurrentThread != timeoutdetector.Item1)
|
||||
timeoutdetector.Item2.Cancel();
|
||||
timeoutdetector = null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -290,6 +290,7 @@ namespace MinecraftClient
|
|||
return;
|
||||
}
|
||||
}
|
||||
InternalConfig.Username = Config.Main.General.Account.Login;
|
||||
if (string.IsNullOrEmpty(Config.Main.General.Account.Password) && !useBrowser &&
|
||||
(Config.Main.Advanced.SessionCache == CacheType.none || !SessionCache.Contains(Settings.ToLowerIfNeed(Config.Main.General.Account.Login))))
|
||||
{
|
||||
|
|
@ -333,7 +334,7 @@ namespace MinecraftClient
|
|||
Translations.WriteLineFormatted("mcc.offline");
|
||||
result = ProtocolHandler.LoginResult.Success;
|
||||
session.PlayerID = "0";
|
||||
session.PlayerName = Config.Main.General.Account.Login;
|
||||
session.PlayerName = InternalConfig.Username;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -372,14 +373,14 @@ namespace MinecraftClient
|
|||
Translations.WriteLine("mcc.connecting", Config.Main.General.AccountType == LoginType.mojang ? "Minecraft.net" : "Microsoft");
|
||||
result = ProtocolHandler.GetLogin(Config.Main.General.Account.Login, InternalConfig.Password, Config.Main.General.AccountType, out session);
|
||||
}
|
||||
|
||||
if (result == ProtocolHandler.LoginResult.Success && Config.Main.Advanced.SessionCache != CacheType.none)
|
||||
SessionCache.Store(loginLower, session);
|
||||
|
||||
if (result == ProtocolHandler.LoginResult.Success)
|
||||
session.SessionPreCheckTask = Task.Factory.StartNew(() => session.SessionPreCheck());
|
||||
}
|
||||
|
||||
if (result == ProtocolHandler.LoginResult.Success && Config.Main.Advanced.SessionCache != CacheType.none)
|
||||
SessionCache.Store(loginLower, session);
|
||||
|
||||
if (result == ProtocolHandler.LoginResult.Success)
|
||||
session.SessionPreCheckTask = Task.Factory.StartNew(() => session.SessionPreCheck());
|
||||
|
||||
if (result == ProtocolHandler.LoginResult.Success)
|
||||
{
|
||||
if (Config.Main.General.AccountType == LoginType.microsoft && InternalConfig.Password != "-" && Config.Signature.LoginWithSecureProfile)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue