-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdater.cs
137 lines (123 loc) · 4.8 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace RConControl {
public class Updater {
//*************************************************
// Variables
//*************************************************
public string XmlUri { get; set; }
private WebClient myWebClient = null;
private XmlDocument xdoc = null;
private Language mLangMan = Language.Instance;
private bool Loaded {
get { return xdoc != null; }
}
//*************************************************
// Initialization
//*************************************************
public Updater(string xmlUri) {
this.XmlUri = xmlUri;
myWebClient = new WebClient();
Load();
}
//*************************************************
// Methods
//*************************************************
private void Load() {
byte[] bytes = myWebClient.DownloadData(XmlUri);
string xml;
int codepage;
int offset = 0;
if (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
codepage = 65001; // UTF-8
offset = 3;
} else if (bytes[0] == 0xFF && bytes[1] == 0xFE) {
codepage = 1200; // UTF-16LE
offset = 2;
} else if (bytes[0] == 0xFE && bytes[1] == 0xFF) {
codepage = 1201; // UTF-16BE
offset = 2;
} else {
codepage = 28591; // ISO-8859-1
}
xml = Encoding.GetEncoding(codepage).GetString(bytes, offset, bytes.Length - offset);
xdoc = new XmlDocument();
try {
xdoc.LoadXml(xml);
} catch (XmlException ex) {
xdoc = null;
throw new ApplicationException(mLangMan.GetString("Updater_ErrorLoadingXML"), ex);
}
if (xdoc.DocumentElement == null) {
xdoc = null;
throw new ApplicationException(mLangMan.GetString("Updater_XMLIncomplete"));
}
}
/// <summary>
/// Returns the app name out of the XML file
/// </summary>
public string AppName {
get {
if (xdoc == null || xdoc.DocumentElement == null) return "";
XmlNode appNode = xdoc.DocumentElement.SelectSingleNode("appname");
if (appNode == null) return "";
return appNode.InnerText;
}
}
/// <summary>
/// Returns the actual program version
/// </summary>
public string LatestVersion {
get {
if (xdoc == null || xdoc.DocumentElement == null) return "";
XmlNode latestNode = xdoc.DocumentElement.SelectSingleNode("latest");
if (latestNode == null) return "";
return latestNode.InnerText;
}
}
/// <summary>
/// Returns true, if a newer version is available
/// </summary>
public bool NewerAvailable {
get {
Assembly me = Assembly.GetCallingAssembly();
return me.GetName().Version.CompareTo(new Version(LatestVersion)) < 0;
}
}
/// <summary>
/// Compare a given version string with the latest available version and indicate whether
/// the latest available is newer than the given compare string.
/// </summary>
public bool IsNewer(string compareTo) {
if (LatestVersion == "") return false;
if (compareTo == "") return true;
return new Version(compareTo).CompareTo(new Version(LatestVersion)) < 0;
}
/// <summary>
/// Returns the website out of the XML file
/// </summary>
public string Website {
get {
if (xdoc == null || xdoc.DocumentElement == null) return "";
XmlNode websiteNode = xdoc.DocumentElement.SelectSingleNode("website");
if (websiteNode == null) return "";
return websiteNode.InnerText;
}
}
/// <summary>
/// Open the specific website
/// </summary>
public void OpenWebsite() {
string uri = Website;
if (uri.StartsWith("http://") || uri.StartsWith("https://"))
Process.Start(uri);
else
throw new ApplicationException(mLangMan.GetString("Updater_InvalidWebsite") + ": " + uri);
}
}
}