forked from nerdunit/androidsideloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Updater.cs
74 lines (67 loc) · 3.03 KB
/
Updater.cs
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
using System;
using System.Text;
using System.Diagnostics;
using JR.Utils.GUI.Forms;
using System.Net;
using System.Windows.Forms;
using System.Net.Http;
using System.IO;
using AndroidSideloader;
namespace AndroidSideloader
{
class Updater
{
public static string AppName { get; set; }
public static string Repostory { get; set; }
private static string RawGitHubUrl;
private static string GitHubUrl;
static readonly public string LocalVersion = "2.0-WIP-SU1";
public static string currentVersion = string.Empty;
public static string changelog = string.Empty;
//Check if there is a new version of the sideloader
private static bool IsUpdateAvailable()
{
HttpClient client = new HttpClient();
try
{
currentVersion = client.GetStringAsync($"{RawGitHubUrl}/master/version").Result;
if (currentVersion.Length > LocalVersion.Length)
currentVersion = currentVersion.Remove(currentVersion.Length - 1);
changelog = client.GetStringAsync($"{RawGitHubUrl}/master/changelog.txt").Result;
client.Dispose();
}
catch { return false; }
return LocalVersion != currentVersion;
}
//Call this to ask the user if they want to update
public static void Update()
{
RawGitHubUrl = $"https://raw.githubusercontent.com/{Repostory}";
GitHubUrl = $"https://github.com/{Repostory}";
if (IsUpdateAvailable())
doUpdate();
}
//If the user wants to update
private static void doUpdate()
{
DialogResult dialogResult = FlexibleMessageBox.Show($"There is a new update you have version {LocalVersion}, do you want to update?\nCHANGELOG\n{changelog}", $"Version {currentVersion} is available", MessageBoxButtons.YesNo);
if (dialogResult != DialogResult.Yes)
return;
//Download new sideloader with version appended to file name so there is no chance of overwriting the current exe
try
{
var fileClient = new WebClient();
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Logger.Log($"Downloading update from {RawGitHubUrl}/releases/download/v{currentVersion}/{AppName}.exe to {AppName} v{currentVersion}.exe");
fileClient.DownloadFile($"{GitHubUrl}/releases/download/v{currentVersion}/{AppName}.exe", $"{AppName} v{currentVersion}.exe");
fileClient.Dispose();
Logger.Log($"Starting {AppName} v{currentVersion}.exe");
Process.Start($"{AppName} v{currentVersion}.exe");
//Delete current version
AndroidSideloader.Utilities.GeneralUtilities.Melt();
}
catch { }
}
}
}