C#:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using Rust;
#region Changelogs and ToDo
/**********************************************************************
*
* 1.1.1 - Fixed command execution for Oxide compatibility
* - Replaced server.Command with ConsoleSystem.Run
* - Keeps Carbon and Oxide command prefixes
* - Added null protection for config usage
*
**********************************************************************/
#endregion
namespace Oxide.Plugins
{
[Info("ReloadAfterStartup" , "Krungh Crow" , "1.1.1")]
[Description("Reload listed plugins after a restart")]
class ReloadAfterStartup : RustPlugin
{
#region Variables
private Timer reloadtimer;
private Timer initCheckTimer;
private Timer startDelayTimer;
private enum ReloadState
{
Idle,
WaitingForLoad,
WaitingDelay,
Reloading,
Finished
}
private ReloadState state = ReloadState.Idle;
#endregion
#region Configuration
private ConfigData configData;
private class ConfigData
{
[JsonProperty(PropertyName = "Plugin settings")]
public Settings PlugSettings = new Settings();
}
private 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<string> ReloadList = new List<string>();
}
void Init()
{
if (!LoadConfigVariables())
{
PrintError("Config file issue detected. Please delete file, or check syntax and fix.");
return;
}
}
private bool LoadConfigVariables()
{
try
{
configData = Config.ReadObject<ConfigData>();
if (configData == null)
{
PrintError("Config file is empty or invalid.");
return false;
}
if (configData.PlugSettings == null)
configData.PlugSettings = new Settings();
if (configData.PlugSettings.ReloadList == null)
configData.PlugSettings.ReloadList = new List<string>();
}
catch (JsonException ex)
{
PrintError($"Error loading configuration: {ex.Message}");
return false;
}
catch (Exception ex)
{
PrintError($"Unexpected config error: {ex.Message}");
return false;
}
Config.WriteObject(configData , true);
return true;
}
protected override void LoadDefaultConfig()
{
PrintWarning("Fresh install detected. Creating a new config file.");
configData = new ConfigData();
Config.WriteObject(configData , true);
}
#endregion
#region Hooks
void OnServerInitialized(bool initial)
{
if (!initial)
{
Puts("Server already initialized. Skipping reload sequence.");
NextTick(UnloadSelf);
return;
}
if (state != ReloadState.Idle)
return;
state = ReloadState.WaitingForLoad;
StopAllTimers();
CheckFullyLoaded();
}
void Unload()
{
StopAllTimers();
state = ReloadState.Idle;
}
#endregion
#region Core
void CheckFullyLoaded()
{
initCheckTimer = timer.Once(10f , () =>
{
if (state != ReloadState.WaitingForLoad)
return;
if (Rust.Application.isLoading)
{
Puts("Server still loading....");
CheckFullyLoaded();
return;
}
StartAfterDelay();
});
}
void StartAfterDelay()
{
if (state != ReloadState.WaitingForLoad)
return;
if (configData == null || configData.PlugSettings == null)
{
PrintError("Configuration was not loaded correctly.");
state = ReloadState.Finished;
NextTick(UnloadSelf);
return;
}
float delay = configData.PlugSettings.StartR;
state = ReloadState.WaitingDelay;
startDelayTimer = timer.Once(delay , () =>
{
if (state != ReloadState.WaitingDelay)
return;
BeginReloading();
});
}
void BeginReloading()
{
if (configData == null || configData.PlugSettings == null || configData.PlugSettings.ReloadList == null)
{
PrintError("Reload list configuration is missing.");
state = ReloadState.Finished;
NextTick(UnloadSelf);
return;
}
List<string> queue = new List<string>(configData.PlugSettings.ReloadList);
if (queue.Count == 0)
{
Puts("No plugins listed to reload.");
state = ReloadState.Finished;
NextTick(UnloadSelf);
return;
}
float interval = configData.PlugSettings.StartBR;
int total = queue.Count;
Puts("Starting Reload sequence....");
state = ReloadState.Reloading;
StopReloadTimer();
reloadtimer = timer.Repeat(interval , 0 , () =>
{
if (state != ReloadState.Reloading)
{
StopReloadTimer();
return;
}
if (queue.Count == 0)
{
StopReloadTimer();
state = ReloadState.Finished;
Puts($"Reload Checks Finished for {total} Plugin(s)....");
NextTick(UnloadSelf);
return;
}
int current = total - queue.Count + 1;
string plugin = queue[0];
Puts($"Reloading {current}/{total}: {plugin}");
ReloadPlugin(plugin);
queue.RemoveAt(0);
});
}
void UnloadSelf()
{
Puts($"Unloading {Name} to save server resources");
#if CARBON
ConsoleSystem.Run(ConsoleSystem.Option.Server, $"c.unload {Name}");
#else
ConsoleSystem.Run(ConsoleSystem.Option.Server , $"o.unload {Name}");
#endif
}
void ReloadPlugin(string name)
{
#if CARBON
ConsoleSystem.Run(ConsoleSystem.Option.Server, $"c.reload {name}");
#else
ConsoleSystem.Run(ConsoleSystem.Option.Server , $"o.reload {name}");
#endif
}
void StopReloadTimer()
{
if (reloadtimer != null && !reloadtimer.Destroyed)
reloadtimer.Destroy();
reloadtimer = null;
}
void StopAllTimers()
{
if (reloadtimer != null && !reloadtimer.Destroyed)
reloadtimer.Destroy();
if (initCheckTimer != null && !initCheckTimer.Destroyed)
initCheckTimer.Destroy();
if (startDelayTimer != null && !startDelayTimer.Destroyed)
startDelayTimer.Destroy();
reloadtimer = null;
initCheckTimer = null;
startDelayTimer = null;
}
#endregion
}
}