-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kalle Minkner
committed
Nov 8, 2024
1 parent
d14c502
commit 915d1ab
Showing
4 changed files
with
93 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,17 @@ | |
using CounterStrikeSharp.API.Core; | ||
using YamlDotNet.Serialization; | ||
using YamlDotNet.Serialization.NamingConventions; | ||
using System.IO.Compression; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace UpdateManager | ||
{ | ||
public partial class UpdateManager : BasePlugin | ||
{ | ||
public override string ModuleName => "Update Manager"; | ||
public override string ModuleAuthor => "Jon-Mailes Graeffe <[email protected]> / Kalle <[email protected]>"; | ||
public override string ModuleVersion => "0.0.1"; | ||
public override string ModuleVersion => "0.1.0"; | ||
|
||
private string _pluginPath = ""; | ||
private List<Tuple<string, string, string>> _plugins = new(); | ||
|
@@ -53,6 +56,7 @@ private void OnServerHibernationUpdate(bool isHibernating) | |
|
||
private void getPluginList() | ||
{ | ||
_plugins.Clear(); | ||
var directories = Directory.GetDirectories(_pluginPath); | ||
foreach (var dir in directories) | ||
{ | ||
|
@@ -87,13 +91,90 @@ private void checkForUpdates() | |
var pluginConfig = Config.Plugins[pluginName]; | ||
if (pluginConfig == null || !pluginConfig.Enabled) continue; | ||
// check github api /repos/{owner}/{repo}/releases/latest | ||
try { | ||
var client = new HttpClient(); | ||
client.DefaultRequestHeaders.Add("User-Agent", "CounterStrikeSharp"); | ||
if (!string.IsNullOrEmpty(pluginConfig.GithubToken)) | ||
client.DefaultRequestHeaders.Add("Authorization", $"token {pluginConfig.GithubToken}"); | ||
var repoPath = new Uri(pluginRepoURL).AbsolutePath.Trim('/'); | ||
var response = client.GetAsync($"https://api.github.com/repos/{repoPath}/releases/latest").Result; | ||
Console.WriteLine(response); | ||
// check if response is successful | ||
if (!response.IsSuccessStatusCode) { | ||
Console.WriteLine(Localizer["update.error"].Value | ||
.Replace("{error}", response.ReasonPhrase)); | ||
continue; | ||
} | ||
// parse response | ||
var responseString = response.Content.ReadAsStringAsync().Result; | ||
// get download url for latest .zip | ||
var release = JsonSerializer.Deserialize<Dictionary<string, object>>(responseString); | ||
if (release == null) | ||
{ | ||
Console.WriteLine(Localizer["update.error"].Value | ||
.Replace("{pluginName}", pluginName) | ||
.Replace("{error}", "Release data not found.")); | ||
continue; | ||
} | ||
if (!release.TryGetValue("tag_name", out var tagName)) | ||
{ | ||
Console.WriteLine(Localizer["update.error"].Value | ||
.Replace("{pluginName}", pluginName) | ||
.Replace("{error}", "Tag name not found in release data.")); | ||
continue; | ||
} | ||
var latestVersion = tagName.ToString(); | ||
if (latestVersion == pluginVersion) | ||
{ | ||
Console.WriteLine(Localizer["update.notfound"].Value | ||
.Replace("{pluginName}", pluginName) | ||
.Replace("{pluginVersion}", pluginVersion)); | ||
continue; | ||
} | ||
Console.WriteLine(Localizer["update.available"].Value | ||
.Replace("{pluginName}", pluginName) | ||
.Replace("{pluginVersion}", pluginVersion) | ||
.Replace("{latestVersion}", latestVersion)); | ||
// download and update plugin | ||
if (!release.TryGetValue("assets", out var assets) || assets == null) | ||
{ | ||
Console.WriteLine(Localizer["update.error"].Value | ||
.Replace("{pluginName}", pluginName) | ||
.Replace("{error}", "Assets not found in release data.")); | ||
continue; | ||
} | ||
var assetList = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(assets?.ToString() ?? string.Empty); | ||
var zipAsset = assetList?.FirstOrDefault(a => | ||
{ | ||
if (a == null) return false; | ||
return a.TryGetValue("name", out var name) && name?.ToString().EndsWith(".zip") == true; | ||
}); | ||
if (zipAsset == null || !zipAsset.TryGetValue("browser_download_url", out var browserDownloadUrl)) | ||
{ | ||
Console.WriteLine(Localizer["update.error"].Value | ||
.Replace("{pluginName}", pluginName) | ||
.Replace("{error}", "Download URL for .zip file not found in assets.")); | ||
continue; | ||
} | ||
var downloadURL = browserDownloadUrl.ToString(); | ||
var downloadPath = Path.Combine(_pluginPath, $"{pluginName}.zip"); | ||
var downloadStream = client.GetStreamAsync(downloadURL).Result; | ||
using (var fileStream = File.Create(downloadPath)) | ||
{ | ||
downloadStream.CopyTo(fileStream); | ||
} | ||
// extract zip | ||
ZipFile.ExtractToDirectory(downloadPath, _pluginPath, true); | ||
// remove zip | ||
File.Delete(downloadPath); | ||
Console.WriteLine(Localizer["update.success"].Value | ||
.Replace("{pluginName}", pluginName) | ||
.Replace("{pluginVersion}", pluginVersion) | ||
.Replace("{latestVersion}", latestVersion)); | ||
} catch (Exception e) { | ||
Console.WriteLine(Localizer["update.error"].Value | ||
.Replace("{pluginName}", pluginName) | ||
.Replace("{error}", e.Message)); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters