-
Notifications
You must be signed in to change notification settings - Fork 11
/
Updater.cs
83 lines (75 loc) · 2.65 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
75
76
77
78
79
80
81
82
83
using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Reflection;
using LeagueSharp;
namespace SAwareness
{
public static class SUpdater
{
private const int Localmajorversion = 0;
private const int Localversion = 8;
public static void UpdateCheck()
{
var bgw = new BackgroundWorker();
bgw.DoWork += bgw_DoWork;
bgw.RunWorkerAsync();
}
private static void bgw_DoWork(object sender, DoWorkEventArgs e)
{
var myUpdater =
new Updater("https://raw.githubusercontent.com/Screeder/SAwareness/master/Properties/Version",
"https://github.com/Screeder/SAwareness/releases/download/", "SAwareness.exe", Localmajorversion,
Localversion);
if (myUpdater.NeedUpdate)
{
Game.PrintChat("SAwareness Updating ...");
if (myUpdater.Update())
{
Game.PrintChat("SAwareness updated, reload please");
}
}
}
}
internal class Updater
{
private readonly string _updatelink;
private readonly WebClient _wc = new WebClient {Proxy = null};
public bool NeedUpdate = false;
public Updater(string versionlink, string updatelink, String assemblyName, int localmajorversion,
int localversion)
{
_updatelink = updatelink;
String str = _wc.DownloadString(versionlink);
str = str.Trim();
_updatelink = updatelink + "v" + str + "/" + assemblyName;
if (Convert.ToInt32(str.Remove(str.IndexOf("."))) > localmajorversion)
NeedUpdate = true;
if (Convert.ToInt32(str.Remove(0, str.IndexOf(".") + 1)) > localversion)
NeedUpdate = true;
}
public bool Update()
{
try
{
if (
File.Exists(
Path.Combine(Assembly.GetExecutingAssembly().Location) + ".bak"))
{
File.Delete(
Path.Combine(Assembly.GetExecutingAssembly().Location) + ".bak");
}
File.Move(Assembly.GetExecutingAssembly().Location,
Path.Combine(Assembly.GetExecutingAssembly().Location) + ".bak");
_wc.DownloadFile(_updatelink,
Path.Combine(Assembly.GetExecutingAssembly().Location));
return true;
}
catch (Exception)
{
return false;
}
}
}
}