forked from Montellese/xbmc-on-imon
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Updating.cs
147 lines (116 loc) · 5.02 KB
/
Updating.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
138
139
140
141
142
143
144
145
146
147
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Windows.Forms;
namespace iMon.XBMC
{
internal static class Updating
{
# region Constants
private const string ChangelogFile = "CHANGELOG";
private const string ChangelogUrl = "https://raw.github.com/BlackMan82/xbmc-on-imon/master/";
private const string UpdateBaseUrl = "https://github.com/downloads/BlackMan82/";
private const string ProjectName = "xbmc-on-imon";
//private const string VersionNumberIndicator = "*";
private const string VersionNumberIndicator = "v";
//private const int VersionNumberPosition = 1;
private const int VersionNumberPosition = 0;
private const string LoggingArea = "Update";
# endregion
# region Private variables
private static Version currentVersion;
private static Version newestVersion;
# endregion
# region Constructor
static Updating()
{
Updating.currentVersion = new Version(Assembly.GetExecutingAssembly().GetName().Version.ToString());
// For testing purposes only
//Updating.currentVersion = new Version("0.1.8.0");
}
# endregion
# region Private functions
private static string getDownloadUrl()
{
return UpdateBaseUrl + ProjectName + "/" + ProjectName + "-v" + newestVersion.ToString() + ".zip";
}
private static bool getNewestVersionNumber()
{
WebResponse response;
string sourceUrl = ChangelogUrl + ChangelogFile;
string line;
bool result = false;
WebRequest request = WebRequest.Create(sourceUrl);
Logging.Log(LoggingArea, "Source for updates: " + sourceUrl);
try
{
response = request.GetResponse();
}
catch (WebException ex)
{
Logging.Error(LoggingArea, ex.Message);
return result;
}
Logging.Log(LoggingArea, "CHANGELOG found");
StreamReader reader = new StreamReader(response.GetResponseStream());
while (!reader.EndOfStream)
{
line = reader.ReadLine();
if (line.StartsWith(VersionNumberIndicator))
{
Logging.Log(LoggingArea, "Version number found, trying to parse: " + line);
try
{
string versionNumber = line.Split(' ')[VersionNumberPosition].Substring(1);
Updating.newestVersion = new Version(versionNumber);
//Version.TryParse(versionNumber, out Updating.newestVersion);
}
catch (Exception)
{
result = false;
Logging.Error(LoggingArea, "Version number parsing failed");
continue;
}
result = true;
Logging.Log(LoggingArea, "Latest version number parsed: " + newestVersion);
break;
}
}
if (!result)
Logging.Error(LoggingArea, "Version number not found!");
return result;
//Updating.newestVersion = new Version(reader.ReadLine()); // the first line in the file is a version number
//Updating.dNewestVersion = double.Parse(Updating.sNewestVersion, CultureInfo.InvariantCulture);
//string Link = reader.ReadLine(); // the second line in the file is a link to the latest update
}
# endregion
# region Public functions
public static bool update(bool automaticUpdate)
{
bool updateIsAvailable = false;
Logging.Log(LoggingArea, "Checking for update");
if (!getNewestVersionNumber())
return false;
if (Updating.newestVersion > Updating.currentVersion)
{
updateIsAvailable = true;
Logging.Log(LoggingArea, "Update available");
if (MessageBox.Show("An update is available! (" + Updating.newestVersion.ToString() + ")\nDo you wish to download it?", Assembly.GetExecutingAssembly().GetName().Name + ": Update available", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
Process.Start(Updating.getDownloadUrl());
}
}
else
{
updateIsAvailable = false;
Logging.Log(LoggingArea, "Update not available");
if (!automaticUpdate)
MessageBox.Show("You are using the most up to date version!", Assembly.GetExecutingAssembly().GetName().Name + ": Update not available");
}
return updateIsAvailable;
}
# endregion
}
}