using System; using System.Linq; using System.Collections.Generic; using Newtonsoft.Json; using Oxide.Core; using Rust; #region Changelogs and ToDo /********************************************************************** * * 1.0.0 : - Release * 1.0.1 : - Added checks to see if server is fully started (THX @BMGJet for the tips) * 1.0.2 : - Code Optimisation and performance * **********************************************************************/ #endregion namespace Oxide.Plugins { [Info("ReloadAfterStartup" , "Krungh Crow" , "1.0.2")] [Description("Reload listed plugins after a restart")] class ReloadAfterStartup : RustPlugin { #region Variables Timer reloadtimer; #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 = "Plugin settings")] public Settings PlugSettings = new Settings(); } class Settings { [JsonProperty(PropertyName = "Time After Startup (seconds)")] public float StartR = 20f; [JsonProperty(PropertyName = "Time between Reloads (seconds)")] public float StartBR = 2f; [JsonProperty(PropertyName = "Plugins to Reload")] public List ReloadList = 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; } SaveConf(); return true; } protected override void LoadDefaultConfig() { PrintWarning("Fresh install detected. Creating a new config file."); configData = new ConfigData(); SaveConf(); } void SaveConf() => Config.WriteObject(configData , true); #endregion #region Hooks void OnServerInitialized(bool initial) { if (initial) { CheckInit(); return; } Puts("[Debugging] Server is already initialized. Skipping new reload sequence"); } #endregion #region Core enum ReloadState { Initializing, Reloading, Finished } ReloadState reloadState; void CheckInit() { timer.Once(10f , () => { if (Rust.Application.isLoading) { Puts("[Debugging] Server still loading...."); CheckInit(); return; } ReloadStart(); }); } void ReloadStart() { float timeBetween = configData.PlugSettings.StartBR; float timeAfter = configData.PlugSettings.StartR; List pluginsToReload = configData.PlugSettings.ReloadList.ToList(); timer.Once(timeAfter , () => { ReloadStateMachine(pluginsToReload , timeBetween); }); } void ReloadStateMachine(List pluginsToReload , float timeBetween) { switch (reloadState) { case ReloadState.Initializing: Puts("Starting Reload sequence...."); reloadtimer = timer.Repeat(timeBetween , 0 , () => { if (pluginsToReload.Count > 0) { Puts($"Reloading {configData.PlugSettings.ReloadList.Count - pluginsToReload.Count + 1}/{configData.PlugSettings.ReloadList.Count}"); Interface.Oxide.ReloadPlugin(pluginsToReload[0]); pluginsToReload.RemoveAt(0); } else { reloadtimer.Destroy(); reloadState = ReloadState.Finished; Puts($"Reload Checks Finished for {configData.PlugSettings.ReloadList.Count} Plugin(s)...."); } }); reloadState = ReloadState.Reloading; break; case ReloadState.Reloading: // This state is handled inside the timer callback break; case ReloadState.Finished: // This state is handled after all plugins are reloaded break; } } #endregion } }