diff --git a/UsefulHints/Config.cs b/UsefulHints/Config.cs index aa21e90..a0cb6cf 100644 --- a/UsefulHints/Config.cs +++ b/UsefulHints/Config.cs @@ -9,10 +9,12 @@ public class Config : IConfig { public bool IsEnabled { get; set; } = true; public bool Debug { get; set; } = false; + [Description("Auto Update Plugin:")] + public bool AutoUpdate { get; set; } = true; + public bool EnableBackup { get; set; } = false; [Description("Hint Settings:")] public bool EnableHints { get; set; } = true; public string Scp096LookMessage { get; set; } = "You looked at SCP-096!"; - public float Scp268Duration { get; set; } = 15f; public string Scp268TimeLeftMessage { get; set; } = "Remaining: {0}s"; public string Scp2176TimeLeftMessage { get; set; } = "Remaining: {0}s"; public string Scp1576TimeLeftMessage { get; set; } = "Remaining: {0}s"; @@ -61,7 +63,6 @@ public class Config : IConfig public bool IgnoreTutorialRole { get; set; } = true; [Description("Map Broadcast:")] public bool EnableMapBroadcast { get; set; } = true; - public string BroadcastDecontaminatedLcz { get; set; } = "Light Zone has been decontaminated"; public string BroadcastWarningLcz { get; set; } = "Light Zone will be decontaminated in 5 minutes!"; } } \ No newline at end of file diff --git a/UsefulHints/EventHandlers/Items/Hints.cs b/UsefulHints/EventHandlers/Items/Hints.cs index 98152d0..43889bd 100644 --- a/UsefulHints/EventHandlers/Items/Hints.cs +++ b/UsefulHints/EventHandlers/Items/Hints.cs @@ -241,7 +241,7 @@ private static void OnSCP268ChangedItem(ChangedItemEventArgs ev) } private static IEnumerator Scp268Timer(Player player) { - float duration = UsefulHints.Instance.Config.Scp268Duration; + float duration = 15f; while (duration > 0) { diff --git a/UsefulHints/EventHandlers/Modules/Maps.cs b/UsefulHints/EventHandlers/Modules/Maps.cs index 6d557fd..21b48b9 100644 --- a/UsefulHints/EventHandlers/Modules/Maps.cs +++ b/UsefulHints/EventHandlers/Modules/Maps.cs @@ -8,29 +8,12 @@ public static class Maps { public static void RegisterEvents() { - Exiled.Events.Handlers.Map.Decontaminating += OnDecontaminated; Exiled.Events.Handlers.Server.RoundStarted += OnRoundStarted; } public static void UnregisterEvents() { - Exiled.Events.Handlers.Map.Decontaminating -= OnDecontaminated; Exiled.Events.Handlers.Server.RoundStarted -= OnRoundStarted; } - private static void OnDecontaminated(DecontaminatingEventArgs ev) - { - if (ev.IsAllowed) - { - string message = string.Format(UsefulHints.Instance.Config.BroadcastDecontaminatedLcz); - - foreach (var player in Player.List) - { - if (player.IsAlive) - { - player.Broadcast(7, message); - } - } - } - } private static void OnRoundStarted() { Timing.CallDelayed(445f, () => diff --git a/UsefulHints/Update.cs b/UsefulHints/Update.cs new file mode 100644 index 0000000..87d2f19 --- /dev/null +++ b/UsefulHints/Update.cs @@ -0,0 +1,159 @@ +using System; +using System.IO; +using System.Net.Http; +using System.Threading.Tasks; +using Exiled.API.Features; + +namespace UsefulHints +{ + public static class UpdateChecker + { + private static readonly string RepositoryUrl = "https://api.github.com/repos/Vretu-Dev/UsefulHints/releases/latest"; + private static readonly string PluginPath = "/home/container/.config/EXILED/Plugins/UsefulHints.dll"; + private static readonly string CurrentVersion = "1.8.0"; + public static void RegisterEvents() + { + Task.Run(async () => await CheckForUpdates(true)); + Exiled.Events.Handlers.Server.WaitingForPlayers += WaitingForPlayers; + } + public static void UnregisterEvents() + { + Exiled.Events.Handlers.Server.WaitingForPlayers -= WaitingForPlayers; + } + private static void WaitingForPlayers() + { + Log.Info("Checking for updates..."); + Task.Run(async () => await CheckForUpdates(true)); + } + private static async Task CheckForUpdates(bool autoUpdate) + { + using (var client = new HttpClient()) + { + client.DefaultRequestHeaders.Add("User-Agent", "UpdateChecker"); + try + { + // Dowload info about latest version + var response = await client.GetAsync(RepositoryUrl); + if (!response.IsSuccessStatusCode) + { + Log.Error($"Failed to check for updates: {response.StatusCode} - {response.ReasonPhrase}"); + return; + } + + var content = await response.Content.ReadAsStringAsync(); + var latestVersion = ExtractLatestVersion(content); + var downloadUrl = ExtractDownloadUrl(content); + + if (latestVersion == null || downloadUrl == null) + { + Log.Error("Failed to parse update information."); + return; + } + + // Check Version + if (IsNewerVersion(CurrentVersion, latestVersion)) + { + Log.Warn($"A new version is available: {latestVersion} (current: {CurrentVersion})"); + + if (autoUpdate) + { + Log.Info("Automatic update is enabled. Downloading and applying the update..."); + UpdatePlugin(downloadUrl); + + + RestartRound(); + } + else + { + Log.Warn("Automatic update is disabled. Please download the update manually."); + } + } + else + { + Log.Info("You are using the latest version."); + } + } + catch (Exception ex) + { + Log.Error($"Error while checking for updates: {ex.Message}"); + } + } + } + private static void RestartRound() + { + try + { + // Restart Command + string command = "rnr"; + Server.ExecuteCommand(command); + Log.Info("Round restart initiated."); + } + catch (Exception ex) + { + Log.Error($"Error while restarting the round: {ex.Message}"); + } + } + private static string ExtractLatestVersion(string json) + { + var startIndex = json.IndexOf("\"tag_name\":\"") + 12; + if (startIndex == -1) return null; + + var endIndex = json.IndexOf("\"", startIndex); + return endIndex == -1 ? null : json.Substring(startIndex, endIndex - startIndex); + } + private static string ExtractDownloadUrl(string json) + { + var startIndex = json.IndexOf("\"browser_download_url\":\"") + 24; + if (startIndex == -1) return null; + + var endIndex = json.IndexOf("\"", startIndex); + return endIndex == -1 ? null : json.Substring(startIndex, endIndex - startIndex); + } + private static bool IsNewerVersion(string currentVersion, string latestVersion) + { + var currentParts = currentVersion.Split('.'); + var latestParts = latestVersion.Split('.'); + + for (int i = 0; i < Math.Min(currentParts.Length, latestParts.Length); i++) + { + if (int.Parse(currentParts[i]) < int.Parse(latestParts[i])) + return true; + + if (int.Parse(currentParts[i]) > int.Parse(latestParts[i])) + return false; + } + + return latestParts.Length > currentParts.Length; + } + private static void UpdatePlugin(string downloadUrl) + { + try + { + using (var client = new HttpClient()) + { + var pluginData = client.GetByteArrayAsync(downloadUrl).Result; + + if (UsefulHints.Instance.Config.EnableBackup) + { + // Create backup current plugin + string backupPath = PluginPath + ".backup"; + + if (File.Exists(PluginPath)) + { + File.Copy(PluginPath, backupPath, overwrite: true); + Log.Warn($"Backup created: {backupPath}"); + } + } + + // Override plugin + File.WriteAllBytes(PluginPath, pluginData); + Log.Info("Plugin updated successfully. Restart the server to apply changes."); + } + } + catch (Exception ex) + { + Log.Error($"Error during plugin update: {ex.Message}"); + } + } + } +} \ No newline at end of file diff --git a/UsefulHints/UsefulHints.cs b/UsefulHints/UsefulHints.cs index 7449721..5c45947 100644 --- a/UsefulHints/UsefulHints.cs +++ b/UsefulHints/UsefulHints.cs @@ -9,13 +9,14 @@ public class UsefulHints : Plugin public override string Name => "Useful Hints"; public override string Author => "Vretu"; public override string Prefix { get; } = "UH"; - public override Version Version => new Version(1, 7, 6); + public override Version Version => new Version(1, 8, 0); public override Version RequiredExiledVersion { get; } = new Version(8, 9, 8); public override PluginPriority Priority { get; } = PluginPriority.Low; public static UsefulHints Instance { get; private set; } public override void OnEnabled() { Instance = this; + if(Config.AutoUpdate){ UpdateChecker.RegisterEvents(); } if(Config.EnableHints){ EventHandlers.Entities.SCP096.RegisterEvents(); } if(Config.EnableHints){ EventHandlers.Items.Hints.RegisterEvents(); } if(Config.EnableWarnings){ EventHandlers.Items.WarningHints.RegisterEvents(); } @@ -30,6 +31,7 @@ public override void OnEnabled() public override void OnDisabled() { Instance = null; + if(Config.AutoUpdate){ UpdateChecker.UnregisterEvents(); } if(Config.EnableHints){ EventHandlers.Entities.SCP096.UnregisterEvents(); } if(Config.EnableHints){ EventHandlers.Items.Hints.UnregisterEvents(); } if(Config.EnableWarnings){ EventHandlers.Items.WarningHints.UnregisterEvents(); } diff --git a/UsefulHints/UsefulHints.csproj b/UsefulHints/UsefulHints.csproj index 3926a8f..ad643df 100644 --- a/UsefulHints/UsefulHints.csproj +++ b/UsefulHints/UsefulHints.csproj @@ -66,6 +66,7 @@ +