|
| 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 | +} |
0 commit comments