Skip to content

Commit

Permalink
feat: 添加更新提示
Browse files Browse the repository at this point in the history
  • Loading branch information
Dissectum committed Sep 17, 2023
1 parent cf4e2d0 commit 33e11b2
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/MBA.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,23 @@ static int Main(string[] args)
}

Core.Main.Current.Start();

TipNewVersion();
Console.WriteLine("按任意键关闭此窗口. . .");
Console.ReadKey();
return 0;
}

private static void TipNewVersion()
{
if (!VersionManager.Released)
return;

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("New version released, available for download at https://github.com/MaaAssistantArknights/MBA/releases/latest.");
Console.ForegroundColor = ConsoleColor.White;
}

static string Greeting =>
$@"
MBA.Cli.exe [arg] ...
Expand Down
7 changes: 7 additions & 0 deletions src/MBA.Core/Data/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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
Expand Down Expand Up @@ -128,6 +134,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

Expand Down
11 changes: 1 addition & 10 deletions src/MBA.Core/Managers/LogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? " <Unidentified>";
}
}
101 changes: 101 additions & 0 deletions src/MBA.Core/Managers/VersionManager.cs
Original file line number Diff line number Diff line change
@@ -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)
?? "<Unidentified>";
}

private static string GetInformationalVersion()
{
var attr = Attribute.GetCustomAttribute(
Assembly.GetExecutingAssembly(),
typeof(AssemblyInformationalVersionAttribute))
as AssemblyInformationalVersionAttribute;
return attr?.InformationalVersion
?? AssemblyVersion + "-<Unidentified>";
}

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<string> 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');
}
}

0 comments on commit 33e11b2

Please sign in to comment.