diff --git a/src/MBA.Cli/Program.cs b/src/MBA.Cli/Program.cs index b94ba74..4de4ac9 100644 --- a/src/MBA.Cli/Program.cs +++ b/src/MBA.Cli/Program.cs @@ -41,6 +41,15 @@ static int Main(string[] args) } Core.Main.Current.Start(); + + if (VersionManager.Released) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("New version released, available for download at https://github.com/MaaAssistantArknights/MBA/releases/latest."); + Console.ForegroundColor = ConsoleColor.White; + } + + Console.WriteLine("按任意键关闭此窗口. . ."); Console.ReadKey(); return 0; } diff --git a/src/MBA.Core/Data/Config.cs b/src/MBA.Core/Data/Config.cs index ea76102..198f7dd 100644 --- a/src/MBA.Core/Data/Config.cs +++ b/src/MBA.Core/Data/Config.cs @@ -20,6 +20,12 @@ public class UIConfig { public bool FirstStartUp { get; set; } = true; public bool DebugMode { get; set; } = false; + public string Proxy { get; set; } = string.Empty; + + [JsonIgnore] + public Uri? ProxyUri => string.IsNullOrEmpty(Proxy) + ? null + : new Uri(Proxy.Contains("://") ? Proxy : $"http://{Proxy}"); } public class CoreConfig @@ -96,6 +102,7 @@ public class ConfigDoc public string FirstStartUp { get; } = "是否第一次启动"; public string DebugMode { get; } = "是否开启 Debug Mode (生成的文件会占用大量空间)"; + public string Proxy { get; } = "代理地址,例如 http://127.0.0.1:7890"; #endregion diff --git a/src/MBA.Core/Managers/LogManager.cs b/src/MBA.Core/Managers/LogManager.cs index b46e200..4fbc762 100644 --- a/src/MBA.Core/Managers/LogManager.cs +++ b/src/MBA.Core/Managers/LogManager.cs @@ -60,20 +60,11 @@ private static void LogStart(string env, bool enableDebugMode) if (_logStarted) return; Log.Information("==================================="); - Log.Information(" MBA {UI} v{Version} started", GlobalInfo.IsCli ? "CLI" : "GUI", GetInformationalVersion()); + Log.Information(" MBA {UI} v{Version} started", GlobalInfo.IsCli ? "CLI" : "GUI", VersionManager.InformationalVersion); Log.Information(" Environment: {env}", env); Log.Information(" Debug Mode: {DebugMode}", enableDebugMode); /* Duplicate in famework log */ // Log.Information(" User Dir: {CurrentDirectory}", Directory.GetCurrentDirectory()); Log.Information("==================================="); } - - private static string GetInformationalVersion() - { - var attr = Attribute.GetCustomAttribute( - Assembly.GetExecutingAssembly(), - typeof(AssemblyInformationalVersionAttribute)) - as AssemblyInformationalVersionAttribute; - return attr?.InformationalVersion ?? " "; - } } diff --git a/src/MBA.Core/Managers/VersionManager.cs b/src/MBA.Core/Managers/VersionManager.cs new file mode 100644 index 0000000..177ad0e --- /dev/null +++ b/src/MBA.Core/Managers/VersionManager.cs @@ -0,0 +1,101 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Reflection; +using System.Text.Json.Nodes; +using MBA.Core.Data; + +namespace MBA.Core.Managers; + +public static class VersionManager +{ + private static Serilog.ILogger Log => LogManager.Logger; + + public static string AssemblyVersion { get; private set; } + public static string InformationalVersion { get; private set; } + public static bool Released { get; private set; } = false; + + static VersionManager() + { + AssemblyVersion = GetAssemblyVersion(); + InformationalVersion = GetInformationalVersion(); + _ = SetReleasedAsync(); + } + + private static string GetAssemblyVersion() + { + return Assembly.GetExecutingAssembly().GetName().Version?.ToString(3) + ?? ""; + } + + private static string GetInformationalVersion() + { + var attr = Attribute.GetCustomAttribute( + Assembly.GetExecutingAssembly(), + typeof(AssemblyInformationalVersionAttribute)) + as AssemblyInformationalVersionAttribute; + return attr?.InformationalVersion + ?? AssemblyVersion + "-"; + } + + private static async Task SetReleasedAsync() + { + if (InformationalVersion.EndsWith("-dev") + || InformationalVersion.Contains("Preview")) + return; + + try + { + var latest = await GetLatestReleaseVersionAsync(); + if (latest != AssemblyVersion && latest != InformationalVersion) + Released = true; + } + catch (Exception e) + { + Log.Warning("VersionManager.{Func} failed: {Message}", + nameof(GetLatestReleaseVersionAsync), + e.Message); + Log.Warning("Setting the {Proxy} in {Path} may be useful.", + nameof(ConfigManager.Config.UI.Proxy), + GlobalInfo.ConfigFileFullPath); + } + } + + private static async Task GetLatestReleaseVersionAsync() + { + var url = "https://api.github.com/repos/MaaAssistantArknights/MBA/releases/latest"; + var handler = new HttpClientHandler + { + AllowAutoRedirect = true, + }; + if (ConfigManager.Config.UI.ProxyUri != null) + { + handler.UseProxy = true; + handler.Proxy = new WebProxy + { + Address = ConfigManager.Config.UI.ProxyUri, + BypassProxyOnLocal = false, + UseDefaultCredentials = false, + }; + } + + var client = new HttpClient(handler); + client.DefaultRequestHeaders.Add("Accept", "application/vnd.github+json"); + client.DefaultRequestHeaders.Add("X-GitHub-Api-Version", "2022-11-28"); + client.DefaultRequestHeaders.UserAgent.Add(ProductInfoHeaderValue.Parse("request")); + var jsonString = await client.GetStringAsync(url); + + var jsonNode = JsonNode.Parse(jsonString); + if (jsonNode == null) + return string.Empty; + + jsonNode = jsonNode["tag_name"]; + if (jsonNode == null) + return string.Empty; + + var tag = jsonNode.ToString(); + if (!tag.StartsWith('v')) + return string.Empty; + + return tag.TrimStart('v'); + } +}