Skip to content

Commit

Permalink
1.8.0 | Auto Update, Backup old plugin, remove old stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Vretu-Dev committed Nov 28, 2024
1 parent 2d019eb commit 81b7fde
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 21 deletions.
5 changes: 3 additions & 2 deletions UsefulHints/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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; } = "<color=yellow>Light Zone</color> has been decontaminated";
public string BroadcastWarningLcz { get; set; } = "<color=yellow>Light Zone</color> will be decontaminated in 5 minutes!";
}
}
2 changes: 1 addition & 1 deletion UsefulHints/EventHandlers/Items/Hints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private static void OnSCP268ChangedItem(ChangedItemEventArgs ev)
}
private static IEnumerator<float> Scp268Timer(Player player)
{
float duration = UsefulHints.Instance.Config.Scp268Duration;
float duration = 15f;

while (duration > 0)
{
Expand Down
17 changes: 0 additions & 17 deletions UsefulHints/EventHandlers/Modules/Maps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, () =>
Expand Down
159 changes: 159 additions & 0 deletions UsefulHints/Update.cs
Original file line number Diff line number Diff line change
@@ -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}");
}
}
}
}
4 changes: 3 additions & 1 deletion UsefulHints/UsefulHints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ public class UsefulHints : Plugin<Config>
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(); }
Expand All @@ -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(); }
Expand Down
1 change: 1 addition & 0 deletions UsefulHints/UsefulHints.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="EventHandlers\Modules\Maps.cs" />
<Compile Include="EventHandlers\Modules\RoundSummary.cs" />
<Compile Include="EventHandlers\Modules\Teammates.cs" />
<Compile Include="Update.cs" />
<Compile Include="UsefulHints.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down

0 comments on commit 81b7fde

Please sign in to comment.