/
ReleaseInstaller.cs
260 lines (216 loc) · 8.98 KB
/
ReleaseInstaller.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using GitHubReleases;
using IWshRuntimeLibrary;
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace iRacingApplicationVersionManger
{
internal class PluginInstaller
{
private readonly string downloadPath;
private string pluginRepo;
private string repo;
private readonly string tmpPath;
private string user;
private readonly string pluginPath;
public PluginInstaller(string user, string repo, string pluginRepo)
{
this.user = user;
this.repo = repo;
this.pluginRepo = pluginRepo;
var programFilesPath = Environment.GetEnvironmentVariable("PROGRAMFILES");
var appPath = Path.Combine(programFilesPath, "iRacing Application Version Manager", user, repo, "current");
tmpPath = Path.Combine(programFilesPath, "iRacing Application Version Manager", user, repo, "tmp");
downloadPath = Path.Combine(programFilesPath, "iRacing Application Version Manager", user, repo);
pluginPath = Path.Combine(appPath, "plugins", "overlay");
}
internal async Task Install(string versionStamp, Action<int> progress)
{
var downloadFilePath = GetDownloadReleaseZipFilePath(versionStamp);
await download(versionStamp, progress);
RemoveCurrentRelease();
InstallNewRelease(downloadFilePath);
progress(100);
await Task.Delay(500);
}
string GetDownloadReleaseZipFilePath(string versionStamp)
{
return Path.Combine(downloadPath, pluginRepo + "-version-" + versionStamp + ".release.zip");
}
async Task download(string versionStamp, Action<int> progress)
{
var downloadFilePath = GetDownloadReleaseZipFilePath(versionStamp);
CreateReleaseDirectory();
var releaseDownloadUrl = await GetReleaseDownloadUrl(versionStamp);
await DownloadReleaseZip(progress, downloadFilePath, releaseDownloadUrl);
}
void CreateReleaseDirectory()
{
Directory.CreateDirectory(pluginPath);
}
Task<string> GetReleaseDownloadUrl(string versionStamp)
{
return GitHubAccess.GetReleaseDownloadUrl(user, pluginRepo, versionStamp);
}
static async Task DownloadReleaseZip(Action<int> progress, string downloadFilePath, string releaseDownloadUrl)
{
using (var webClient = new WebClient())
{
webClient.DownloadProgressChanged += (s, ee) => progress(ee.ProgressPercentage);
await webClient.DownloadFileTaskAsync(new Uri(releaseDownloadUrl), downloadFilePath);
}
}
void RemoveCurrentRelease()
{
var di = new DirectoryInfo(pluginPath);
if (di.Exists)
di.Delete(true);
}
void InstallNewRelease(string downloadFilePath)
{
var di = new DirectoryInfo(tmpPath);
if (di.Exists)
di.Delete(true);
ZipFile.ExtractToDirectory(downloadFilePath, tmpPath);
di.MoveTo(pluginPath);
}
}
internal class ReleaseInstaller
{
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
readonly string user;
readonly string repo;
readonly string appPath;
readonly string shortCutPath;
readonly string mainExePath;
readonly string pluginTesterExePath;
readonly string downloadPath;
readonly string tmpPath;
public ReleaseInstaller(string user, string repo)
{
this.user = user;
this.repo = repo;
var programFilesPath = Environment.GetEnvironmentVariable("PROGRAMFILES");
appPath = Path.Combine(programFilesPath, "iRacing Application Version Manager", user, repo, "current");
tmpPath = Path.Combine(programFilesPath, "iRacing Application Version Manager", user, repo, "tmp");
downloadPath = Path.Combine(programFilesPath, "iRacing Application Version Manager", user, repo);
mainExePath = appPath + "\\iRacingReplayOverlay.exe";
pluginTesterExePath = appPath + "\\iRacingDirector.Plugin.Tester.exe";
shortCutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "iRacing Applications");
}
internal PluginInstaller ForPlugin(string pluginRepo)
{
return new PluginInstaller(user, repo, pluginRepo);
}
public async Task Install(string versionStamp, Action<int> progress) {
var downloadFilePath = GetDownloadReleaseZipFilePath(versionStamp);
if (!System.IO.File.Exists(downloadFilePath))
await download(versionStamp, progress);
RemoveCurrentRelease();
InstallNewRelease(downloadFilePath);
createShortCut(versionStamp);
CreateTesterShortCut(versionStamp);
}
public async Task download(string versionStamp, Action<int> progress)
{
var downloadFilePath = GetDownloadReleaseZipFilePath(versionStamp);
CreateReleaseDirectory();
var releaseDownloadUrl = await GetReleaseDownloadUrl(versionStamp);
await DownloadReleaseZip(progress, downloadFilePath, releaseDownloadUrl);
}
public async Task<VersionItem[]> AvailableVersions()
{
return await GitHubAccess.GetVersions(user, repo).ConfigureAwait(true);
}
public void Run()
{
var processes = Process.GetProcessesByName("iRacingReplayOverlay");
if (processes.Length > 0)
SetForegroundWindow(processes.First().MainWindowHandle);
else
DeElevatedProcess.Start(mainExePath);
}
public string CurrentInstalledVersion
{
get
{
if (System.IO.File.Exists(mainExePath))
return AssemblyName.GetAssemblyName(mainExePath).Version.ToString();
return null;
}
}
public bool IsVersionDownloaded(string versionStamp)
{
return System.IO.File.Exists(GetDownloadReleaseZipFilePath(versionStamp));
}
void CreateReleaseDirectory()
{
Directory.CreateDirectory(appPath);
}
void InstallNewRelease(string downloadFilePath)
{
var di = new DirectoryInfo(tmpPath);
if( di.Exists)
di.Delete(true);
ZipFile.ExtractToDirectory(downloadFilePath, tmpPath);
di.MoveTo(appPath);
}
void RemoveCurrentRelease()
{
var di = new DirectoryInfo(appPath);
if( di.Exists)
di.Delete(true);
di = new DirectoryInfo(shortCutPath);
if (di.Exists)
di.Delete(true);
}
static async Task DownloadReleaseZip(Action<int> progress, string downloadFilePath, string releaseDownloadUrl)
{
using (var webClient = new WebClient())
{
webClient.DownloadProgressChanged += (s, ee) => progress(ee.ProgressPercentage);
await webClient.DownloadFileTaskAsync(new Uri(releaseDownloadUrl), downloadFilePath);
}
}
Task<string> GetReleaseDownloadUrl( string versionStamp)
{
return GitHubAccess.GetReleaseDownloadUrl(user, repo, versionStamp);
}
void createShortCut(string versionStamp)
{
var name = "iRacing Replay Director (" + versionStamp + ")";
Directory.CreateDirectory(shortCutPath);
var shortCutFilePath = Path.Combine(shortCutPath, name + ".lnk");
var shell = new WshShell();
var shortcut = (IWshShortcut)shell.CreateShortcut(shortCutFilePath);
shortcut.Description = name;
shortcut.TargetPath = mainExePath;
shortcut.Save();
}
void CreateTesterShortCut(string versionStamp)
{
if (System.IO.File.Exists(pluginTesterExePath))
{
var name = "iRacing Overlay Plugin Tester (" + versionStamp + ")";
Directory.CreateDirectory(shortCutPath);
var shortCutFilePath = Path.Combine(shortCutPath, name + ".lnk");
var shell = new WshShell();
var shortcut = (IWshShortcut)shell.CreateShortcut(shortCutFilePath);
shortcut.Description = name;
shortcut.TargetPath = pluginTesterExePath;
shortcut.Save();
}
}
private string GetDownloadReleaseZipFilePath(string versionStamp)
{
return Path.Combine(downloadPath, "version-" + versionStamp + ".release.zip");
}
}
}