using System; using System.Linq; using System.Collections.Generic; using Newtonsoft.Json; using Oxide.Core; using Rust; #region Changelogs and ToDo /********************************************************************** * * 1.0.0 : - Beta Release * 1.0.1 : - Optimised delays **********************************************************************/ #endregion namespace Oxide.Plugins { [Info("ExecuteAfterStartup" , "Krungh Crow" , "1.0.1")] [Description("Execute listed commands after a server restart")] class ExecuteAfterStartup : RustPlugin { #region Variables Timer executeTimer; List commandsToExecute = new List(); #endregion #region Configuration void Init() { if (!LoadConfigVariables()) { PrintError("Config file issue detected. Please delete file, or check syntax and fix."); return; } } private ConfigData configData; class ConfigData { [JsonProperty(PropertyName = "Command settings")] public Settings CommandSettings = new Settings(); } class Settings { [JsonProperty(PropertyName = "Time After Startup (seconds)")] public float StartR = 20f; [JsonProperty(PropertyName = "Time between Command Execution (seconds)")] public float StartBC = 2f; [JsonProperty(PropertyName = "Commands to Execute")] public List CommandList = new List(); } private bool LoadConfigVariables() { try { configData = Config.ReadObject(); if (configData == null) { PrintError("Config file is empty or in an invalid format."); return false; } } catch (JsonException ex) { PrintError($"Error loading configuration: {ex.Message}"); return false; } SaveConfig(); return true; } protected override void LoadDefaultConfig() { PrintWarning("Fresh install detected. Creating a new config file."); configData = new ConfigData(); SaveConfig(); } void SaveConfig() => Config.WriteObject(configData , true); #endregion #region Hooks void OnServerInitialized(bool initial) { if (initial) { CheckInitialization(); return; } Puts("[Debugging] Server is already initialized. Skipping new execution sequence"); } #endregion #region Core enum ExecutionState { Initializing, Executing, Finished } ExecutionState executionState; void CheckInitialization() { timer.Once(10f , () => { if (Rust.Application.isLoading) { Puts("[Debugging] Server still loading...."); CheckInitialization(); return; } ExecuteCommands(); }); } void ExecuteCommands() { float timeBetweenCommands = configData.CommandSettings.StartBC; float timeAfterStartup = configData.CommandSettings.StartR; commandsToExecute = configData.CommandSettings.CommandList.ToList(); timer.Once(timeAfterStartup , () => { ExecuteCommandsStateMachine(timeBetweenCommands); }); } void ExecuteCommandsStateMachine(float timeBetweenCommands) { switch (executionState) { case ExecutionState.Initializing: Puts("Starting Command Execution sequence...."); executeTimer = timer.Repeat(timeBetweenCommands , commandsToExecute.Count , () => { if (commandsToExecute.Count > 0) { var command = commandsToExecute[0]; Puts($"Executing command: {command}"); rust.RunServerCommand(command); commandsToExecute.RemoveAt(0); } else { executeTimer.Destroy(); executionState = ExecutionState.Finished; Puts($"Command Execution Finished for {configData.CommandSettings.CommandList.Count} command(s)...."); } }); executionState = ExecutionState.Executing; break; case ExecutionState.Executing: break; case ExecutionState.Finished: break; } } #endregion } }