|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace YARDT.Classes |
| 9 | +{ |
| 10 | + class Updater |
| 11 | + { |
| 12 | + private static readonly string APILatestURL = "https://api.github.com/repos/Assistant/ModAssistant/releases/latest"; |
| 13 | + |
| 14 | + private static Update LatestUpdate; |
| 15 | + private static Version CurrentVersion; |
| 16 | + private static Version LatestVersion; |
| 17 | + private static bool NeedsUpdate = false; |
| 18 | + private static string NewExe = Path.Combine(Path.GetDirectoryName(Utils.ExePath), "ModAssistant.exe"); |
| 19 | + |
| 20 | + public static async Task<bool> CheckForUpdate() |
| 21 | + { |
| 22 | + var resp = await HttpClient.GetAsync(APILatestURL); |
| 23 | + var body = await resp.Content.ReadAsStringAsync(); |
| 24 | + LatestUpdate = JsonSerializer.Deserialize<Update>(body); |
| 25 | + |
| 26 | + LatestVersion = new Version(LatestUpdate.tag_name.Substring(1)); |
| 27 | + CurrentVersion = new Version(App.Version); |
| 28 | + |
| 29 | + return (LatestVersion > CurrentVersion); |
| 30 | + } |
| 31 | + |
| 32 | + public static async Task Run() |
| 33 | + { |
| 34 | + if (Path.GetFileName(Utils.ExePath).Equals("ModAssistant.old.exe")) RunNew(); |
| 35 | + try |
| 36 | + { |
| 37 | + NeedsUpdate = await CheckForUpdate(); |
| 38 | + } |
| 39 | + catch |
| 40 | + { |
| 41 | + Console.WriteLine("Updater:CheckFailed"); |
| 42 | + } |
| 43 | + |
| 44 | + if (NeedsUpdate) await StartUpdate(); |
| 45 | + } |
| 46 | + |
| 47 | + public static async Task StartUpdate() |
| 48 | + { |
| 49 | + string OldExe = Path.Combine(Path.GetDirectoryName(Utils.ExePath), "ModAssistant.old.exe"); |
| 50 | + string DownloadLink = null; |
| 51 | + |
| 52 | + foreach (Update.Asset asset in LatestUpdate.assets) |
| 53 | + { |
| 54 | + if (asset.name == "ModAssistant.exe") |
| 55 | + { |
| 56 | + DownloadLink = asset.browser_download_url; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (string.IsNullOrEmpty(DownloadLink)) |
| 61 | + { |
| 62 | + Console.WriteLine("Updater:DownloadFailed"); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + if (File.Exists(OldExe)) |
| 67 | + { |
| 68 | + File.Delete(OldExe); |
| 69 | + } |
| 70 | + |
| 71 | + File.Move(Utils.ExePath, OldExe); |
| 72 | + |
| 73 | + await Utils.Download(DownloadLink, NewExe); |
| 74 | + RunNew(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static void RunNew() |
| 79 | + { |
| 80 | + Process.Start(NewExe); |
| 81 | + Application.Current.Dispatcher.Invoke(() => { Application.Current.Shutdown(); }); |
| 82 | + } |
| 83 | + } |
| 84 | + public class Update |
| 85 | + { |
| 86 | + public string url; |
| 87 | + public string assets_url; |
| 88 | + public string upload_url; |
| 89 | + public string html_url; |
| 90 | + public int id; |
| 91 | + public string node_id; |
| 92 | + public string tag_name; |
| 93 | + public string target_commitish; |
| 94 | + public string name; |
| 95 | + public bool draft; |
| 96 | + public User author; |
| 97 | + public bool prerelease; |
| 98 | + public string created_at; |
| 99 | + public string published_at; |
| 100 | + public Asset[] assets; |
| 101 | + public string tarball_url; |
| 102 | + public string zipball_url; |
| 103 | + public string body; |
| 104 | + |
| 105 | + public class Asset |
| 106 | + { |
| 107 | + public string url; |
| 108 | + public int id; |
| 109 | + public string node_id; |
| 110 | + public string name; |
| 111 | + public string label; |
| 112 | + public User uploader; |
| 113 | + public string content_type; |
| 114 | + public string state; |
| 115 | + public int size; |
| 116 | + public string created_at; |
| 117 | + public string updated_at; |
| 118 | + public string browser_download_url; |
| 119 | + } |
| 120 | + |
| 121 | + public class User |
| 122 | + { |
| 123 | + public string login; |
| 124 | + public int id; |
| 125 | + public string node_id; |
| 126 | + public string avatar_url; |
| 127 | + public string gravatar_id; |
| 128 | + public string url; |
| 129 | + public string html_url; |
| 130 | + public string followers_url; |
| 131 | + public string following_url; |
| 132 | + public string gists_url; |
| 133 | + public string starred_url; |
| 134 | + public string subscriptions_url; |
| 135 | + public string organizations_url; |
| 136 | + public string repos_url; |
| 137 | + public string events_url; |
| 138 | + public string received_events_url; |
| 139 | + public string type; |
| 140 | + public bool site_admin; |
| 141 | + |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments