2024-05-06 23:47:15 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
2024-01-19 14:56:27 +08:00
|
|
|
|
using System.Text.Json;
|
2024-05-06 23:47:15 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using Community.PowerToys.Run.Plugin.Everything.Properties;
|
2022-12-11 02:20:09 +08:00
|
|
|
|
|
2023-01-07 02:20:22 +08:00
|
|
|
|
namespace Community.PowerToys.Run.Plugin.Everything
|
|
|
|
|
{
|
2023-03-30 00:22:49 +08:00
|
|
|
|
internal sealed class Update
|
2022-12-11 02:20:09 +08:00
|
|
|
|
{
|
2024-04-18 17:04:25 +08:00
|
|
|
|
private readonly CompositeFormat updateAvailable = CompositeFormat.Parse(Resources.UpdateAvailable);
|
2024-01-19 14:56:27 +08:00
|
|
|
|
internal async Task UpdateAsync(Version v, Settings s)
|
2022-12-11 02:20:09 +08:00
|
|
|
|
{
|
2024-01-19 14:56:27 +08:00
|
|
|
|
string apiUrl = "https://api.github.com/repos/lin-ycv/EverythingPowerToys/releases/latest";
|
2024-10-13 01:04:46 +08:00
|
|
|
|
#if DEBUG
|
2024-05-25 21:11:41 +08:00
|
|
|
|
if (s.Log > LogLevel.None)
|
|
|
|
|
Debugger.Write("1.Checking Update...");
|
2024-10-13 01:04:46 +08:00
|
|
|
|
#endif
|
2022-12-11 02:20:09 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-01-19 14:56:27 +08:00
|
|
|
|
using HttpClient httpClient = new();
|
|
|
|
|
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0");
|
|
|
|
|
|
|
|
|
|
HttpResponseMessage response = await httpClient.GetAsync(apiUrl);
|
2024-10-13 01:04:46 +08:00
|
|
|
|
#if DEBUG
|
2024-05-25 21:11:41 +08:00
|
|
|
|
if (s.Log == LogLevel.Verbose) Debugger.Write($"\tResponse: {response.StatusCode}");
|
2024-10-13 01:04:46 +08:00
|
|
|
|
#endif
|
2024-01-19 14:56:27 +08:00
|
|
|
|
if (response.IsSuccessStatusCode)
|
2022-12-11 02:20:09 +08:00
|
|
|
|
{
|
2024-01-19 14:56:27 +08:00
|
|
|
|
using JsonDocument jsonDocument = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
|
|
|
|
|
JsonElement root = jsonDocument.RootElement;
|
|
|
|
|
Version latest = Version.TryParse(root.GetProperty("tag_name").GetString().AsSpan(1), out var vNumber)
|
|
|
|
|
? vNumber
|
|
|
|
|
: Version.Parse(root.GetProperty("tag_name").GetString());
|
|
|
|
|
if (latest > v && latest.ToString() != s.Skip)
|
2022-12-11 02:20:09 +08:00
|
|
|
|
{
|
2024-04-18 17:04:25 +08:00
|
|
|
|
MessageBoxResult mbox = MessageBox.Show(string.Format(CultureInfo.InvariantCulture, updateAvailable, v, latest), "Updater", MessageBoxButton.YesNoCancel);
|
2024-01-19 14:56:27 +08:00
|
|
|
|
if (mbox == MessageBoxResult.Yes && root.TryGetProperty("assets", out JsonElement assets))
|
2022-12-11 02:20:09 +08:00
|
|
|
|
{
|
2024-01-19 14:56:27 +08:00
|
|
|
|
string[] nameUrl = [string.Empty, string.Empty];
|
|
|
|
|
foreach (JsonElement asset in assets.EnumerateArray())
|
|
|
|
|
{
|
2024-07-03 22:00:35 +08:00
|
|
|
|
#if X64
|
|
|
|
|
if (asset.TryGetProperty("browser_download_url", out JsonElement downUrl) && downUrl.ToString().EndsWith("x64.exe", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
#elif ARM64
|
|
|
|
|
if (asset.TryGetProperty("browser_download_url", out JsonElement downUrl) && downUrl.ToString().EndsWith("ARM64.exe", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
#endif
|
2024-01-19 14:56:27 +08:00
|
|
|
|
{
|
|
|
|
|
nameUrl[0] = asset.GetProperty("name").ToString();
|
|
|
|
|
nameUrl[1] = downUrl.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 15:37:15 +08:00
|
|
|
|
if (nameUrl[0].Length > 0)
|
|
|
|
|
{
|
|
|
|
|
byte[] fileContent = await httpClient.GetByteArrayAsync(nameUrl[1]);
|
|
|
|
|
string fileName = Path.Combine(Path.GetTempPath(), nameUrl[0]);
|
|
|
|
|
File.WriteAllBytes(fileName, fileContent);
|
|
|
|
|
Process.Start(fileName);
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-01-19 14:56:27 +08:00
|
|
|
|
{
|
|
|
|
|
ProcessStartInfo p = new("https://github.com/lin-ycv/EverythingPowerToys/releases/latest")
|
|
|
|
|
{
|
|
|
|
|
UseShellExecute = true,
|
|
|
|
|
Verb = "Open",
|
|
|
|
|
};
|
|
|
|
|
Process.Start(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (mbox == MessageBoxResult.No)
|
|
|
|
|
{
|
|
|
|
|
s.Skip = latest.ToString();
|
|
|
|
|
}
|
2023-11-01 08:52:14 +08:00
|
|
|
|
}
|
2022-12-11 02:20:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-13 01:04:46 +08:00
|
|
|
|
#if RELEASE
|
|
|
|
|
catch
|
|
|
|
|
{ }
|
|
|
|
|
#else
|
2024-05-25 21:11:41 +08:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
if (s.Log > LogLevel.None)
|
|
|
|
|
Debugger.Write($"\r\nERROR: {e.Message}\r\n{e.StackTrace}\r\n");
|
2024-10-13 01:04:46 +08:00
|
|
|
|
|
2024-05-25 21:11:41 +08:00
|
|
|
|
}
|
2024-07-03 22:00:35 +08:00
|
|
|
|
if (s.Log > LogLevel.None)
|
|
|
|
|
Debugger.Write(" Checking Update...Done");
|
2024-10-13 01:04:46 +08:00
|
|
|
|
#endif
|
2022-12-11 02:20:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|