Skip to content

Commit eb5eacc

Browse files
authored
Added auto updater
1 parent 3a155ba commit eb5eacc

5 files changed

Lines changed: 102 additions & 6 deletions

File tree

AutoUpdater.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="Config.cs" company="MySCPStats">
3+
// Copyright (c) joseph_fallen. All rights reserved.
4+
// Licensed under the CC BY-SA 3.0 license.
5+
// </copyright>
6+
// -----------------------------------------------------------------------
7+
using System;
8+
using System.IO;
9+
using System.Net.Http;
10+
using System.Reflection;
11+
using System.Text.Json;
12+
using System.Threading.Tasks;
13+
using Exiled.API.Features;
14+
15+
namespace SCPStats
16+
{
17+
public static class AutoUpdater
18+
{
19+
private const string RepoOwner = "Josephfallen";
20+
private const string RepoName = "MySCPStats";
21+
private const string PluginFileName = "SCPStats.dll";
22+
23+
private static readonly string GithubApiLatestRelease = $"https://api.github.com/repos/{RepoOwner}/{RepoName}/releases/latest";
24+
private static readonly string PluginPath = Path.Combine(Paths.Plugins, PluginFileName);
25+
26+
public static async Task RunAsync()
27+
{
28+
try
29+
{
30+
Log.Info("[AutoUpdater] Checking GitHub for plugin updates...");
31+
32+
using HttpClient client = new();
33+
client.DefaultRequestHeaders.Add("User-Agent", "MySCPStats-AutoUpdater");
34+
35+
var response = await client.GetAsync(GithubApiLatestRelease);
36+
response.EnsureSuccessStatusCode();
37+
38+
var json = await response.Content.ReadAsStringAsync();
39+
var release = JsonDocument.Parse(json).RootElement;
40+
41+
string latestVersion = release.GetProperty("tag_name").GetString()?.TrimStart('v');
42+
string currentVersion = Assembly.GetExecutingAssembly().GetName().Version?.ToString();
43+
44+
if (string.IsNullOrWhiteSpace(latestVersion))
45+
{
46+
Log.Warn("[AutoUpdater] Could not determine latest version tag.");
47+
return;
48+
}
49+
50+
if (string.Equals(latestVersion, currentVersion, StringComparison.OrdinalIgnoreCase))
51+
{
52+
Log.Info("[AutoUpdater] Plugin is up to date.");
53+
return;
54+
}
55+
56+
Log.Warn($"[AutoUpdater] Update available: {latestVersion} (current: {currentVersion})");
57+
58+
foreach (var asset in release.GetProperty("assets").EnumerateArray())
59+
{
60+
string name = asset.GetProperty("name").GetString();
61+
string downloadUrl = asset.GetProperty("browser_download_url").GetString();
62+
63+
if (name.Equals(PluginFileName, StringComparison.OrdinalIgnoreCase))
64+
{
65+
string tempPath = PluginPath + ".update";
66+
67+
Log.Info("[AutoUpdater] Downloading updated plugin...");
68+
69+
using var downloadStream = await client.GetStreamAsync(downloadUrl);
70+
using var fileStream = File.Create(tempPath);
71+
await downloadStream.CopyToAsync(fileStream);
72+
73+
// Replace old file
74+
File.Delete(PluginPath);
75+
File.Move(tempPath, PluginPath);
76+
77+
Log.Info("[AutoUpdater] Update downloaded successfully! Restart the server to apply.");
78+
return;
79+
}
80+
}
81+
82+
Log.Warn("[AutoUpdater] Plugin .dll not found in the release assets.");
83+
}
84+
catch (Exception ex)
85+
{
86+
Log.Error($"[AutoUpdater] Update failed: {ex.Message}");
87+
}
88+
}
89+
}
90+
}

Config.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public class Config : IConfig
1717
public bool Debug { get; set; } = false;
1818
[Description("Should developers get credit if they join the server?")]
1919
public bool ShowDeveloperBadge { get; set; } = true;
20+
[Description("Enable automatic plugin updates.")]
21+
public bool EnableAutoUpdate { get; set; } = true;
22+
2023

2124
}
2225
}

LoaderMessages.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
// </copyright>
66
// -----------------------------------------------------------------------
77

8+
9+
using System;
10+
811
namespace MySCPStats.Loader
912
{
10-
using System;
11-
12-
/// <summary>
13-
/// Contains different loader messages for MySCPStats.
14-
/// </summary>
13+
1514
public static class LoaderMessages
1615
{
1716
/// <summary>

Plugin.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Plugin : Plugin<Config>
1818
{
1919
public override string Name => "MySCPStats";
2020
public override string Author => "Joseph_fallen";
21-
public override Version Version => new(1, 4, 1);
21+
public override Version Version => new(1, 4, 2);
2222
public override Version RequiredExiledVersion => new Version(9, 6, 0);
2323
public static Plugin Instance { get; private set; }
2424
public VerificationManager VerificationManager { get; private set; }
@@ -34,6 +34,8 @@ public override void OnEnabled()
3434
TimerHandler.Initialize();
3535
Log.Info(LoaderMessages.GetMessage());
3636
Exiled.Events.Handlers.Player.Verified += OnVerified;
37+
if (Config.EnableAutoUpdate)
38+
_ = AutoUpdater.RunAsync();
3739
base.OnEnabled();
3840
}
3941

@@ -59,6 +61,7 @@ private void OnVerified(VerifiedEventArgs ev)
5961
}
6062

6163

64+
6265
private void AssignDeveloperBadge(Player player)
6366
{
6467
// Remove any hidden or default badges

scpstats.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
</Reference>
8686
</ItemGroup>
8787
<ItemGroup>
88+
<Compile Include="AutoUpdater.cs" />
8889
<Compile Include="Config.cs" />
8990
<Compile Include="Event Handlers\EventHandler.cs" />
9091
<Compile Include="Helpers\CryptoHelper.cs" />

0 commit comments

Comments
 (0)