-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
114 lines (93 loc) · 4.38 KB
/
Program.cs
File metadata and controls
114 lines (93 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Net.Http.Json;
using System.Reflection;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
public static class Updater
{
private const string Owner = "routersys";
private const string Repo = "YMM4-CustomEasing";
public static async Task Main(string[] args)
{
if (args.Length < 2 || !int.TryParse(args[0], out int processId))
{
Console.WriteLine("引数が必要です: <ProcessId> <PluginDirectory>");
return;
}
string pluginDirectory = args[1];
try
{
Process ymmProcess = Process.GetProcessById(processId);
Console.WriteLine($"YMM4 (PID: {processId}) の終了を待っています...");
await ymmProcess.WaitForExitAsync();
}
catch (ArgumentException)
{
Console.WriteLine("YMM4プロセスが見つかりません。アップデートチェックを続行します...");
}
Console.WriteLine("YMM4が終了しました。アップデートを確認しています...");
try
{
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", "YMM4-CustomEasing-Updater");
var latestRelease = await client.GetFromJsonAsync<GitHubRelease>($"https://api.github.com/repos/{Owner}/{Repo}/releases/latest");
if (latestRelease?.Assets == null || latestRelease.Assets.Length == 0)
{
Console.WriteLine("最新のリリースにアセットが見つかりません。");
return;
}
var latestVersion = new Version(latestRelease.TagName.TrimStart('v'));
var pluginDllPath = Path.Combine(pluginDirectory, "ymm-plugin.dll");
var currentVersion = Assembly.LoadFrom(pluginDllPath).GetName().Version ?? new Version("0.0.0");
Console.WriteLine($"現在のバージョン: {currentVersion}, 最新バージョン: {latestVersion}");
if (latestVersion <= currentVersion)
{
Console.WriteLine("最新版を使用しています。");
return;
}
Console.WriteLine("新しいバージョンが見つかりました。アップデートを開始します...");
var asset = latestRelease.Assets[0];
var downloadUrl = asset.BrowserDownloadUrl;
string tempZipPath = Path.GetTempFileName();
string tempExtractPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempExtractPath);
Console.WriteLine($"{downloadUrl} からダウンロードしています...");
var zipBytes = await client.GetByteArrayAsync(downloadUrl);
await File.WriteAllBytesAsync(tempZipPath, zipBytes);
Console.WriteLine("アップデートファイルを展開しています...");
ZipFile.ExtractToDirectory(tempZipPath, tempExtractPath);
Console.WriteLine("プラグインファイルを置き換えています...");
foreach (var file in Directory.GetFiles(tempExtractPath))
{
string destinationFile = Path.Combine(pluginDirectory, Path.GetFileName(file));
File.Copy(file, destinationFile, true);
Console.WriteLine($" - 更新: {Path.GetFileName(file)}");
}
Console.WriteLine("一時ファイルを削除しています...");
File.Delete(tempZipPath);
Directory.Delete(tempExtractPath, true);
Console.WriteLine("アップデートが完了しました!");
}
catch (Exception ex)
{
Console.WriteLine($"エラーが発生しました: {ex.Message}");
File.WriteAllText(Path.Combine(pluginDirectory, "update_error.log"), ex.ToString());
}
}
}
public class GitHubRelease
{
[JsonPropertyName("tag_name")]
public string TagName { get; set; } = "";
[JsonPropertyName("assets")]
public Asset[] Assets { get; set; } = [];
}
public class Asset
{
[JsonPropertyName("browser_download_url")]
public string BrowserDownloadUrl { get; set; } = "";
}