forked from w8tcha/ripper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoUpdater.cs
285 lines (240 loc) · 10.2 KB
/
AutoUpdater.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AutoUpdater.cs" company="The Watcher">
// Copyright (c) The Watcher Partial Rights Reserved.
// This software is licensed under the MIT license. See license.txt for details.
// </copyright>
// <summary>
// Code Named: VG-Ripper
// Function : Extracts Images posted on RiP forums and attempts to fetch them to disk.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
//#if (!RIPRIPPERX)
namespace Ripper.Core.Components
{
#region
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Zip;
#endregion
/// <summary>
/// Class to download Update and restart the app
/// </summary>
public class AutoUpdater
{
#region Public Methods
/// <summary>
/// TryUpdate: Invoke this method when you are ready to run the update checking thread
/// </summary>
/// <param name="programName">Name of the program.</param>
/// <param name="assembly">The assembly.</param>
public static void TryUpdate(string programName, Assembly assembly)
{
var backgroundThread = programName.Equals("Ripper.Services")
? new Thread(() => UpdateServicesDll(assembly)) { IsBackground = true }
: new Thread(() => UpdateProgram(programName, assembly)) { IsBackground = true };
backgroundThread.Start();
}
/// <summary>
/// Cleanup after the update.
/// </summary>
/// <param name="programName">Name of the program.</param>
public static void CleanupAfterUpdate(string programName)
{
var tempFolder = Path.Combine(Application.StartupPath, "temp");
// Delete Backup Files From AutoUpdate
if (File.Exists(Path.Combine(Application.StartupPath, string.Format("{0}.bak", programName))))
{
File.Delete(Path.Combine(Application.StartupPath, string.Format("{0}.bak", programName)));
}
if (File.Exists(Path.Combine(Application.StartupPath, "Ripper.Services.bak")))
{
File.Delete(Path.Combine(Application.StartupPath, "Ripper.Services.bak"));
}
if (File.Exists(Path.Combine(Application.StartupPath, "Ripper.Core.bak")))
{
File.Delete(Path.Combine(Application.StartupPath, "Ripper.Core.bak"));
}
// Finally Delete Temp Directory
DeleteFolder(tempFolder);
}
#endregion
#region Methods
/// <summary>
/// Auto Updater, download latest version and restart it.
/// </summary>
/// <param name="programName">Name of the program.</param>
/// <param name="assembly">The assembly.</param>
private static void UpdateProgram(string programName, Assembly assembly)
{
var tempFolder = Path.Combine(Application.StartupPath, "temp");
try
{
// Download Update
var downloadURL = string.Format(
"http://www.watchersnet.de/rip-ripper/{0}{1}.zip",
programName,
VersionCheck.OnlineVersion);
var tempZIP = Path.Combine(Application.StartupPath, "temp.zip");
var client = new WebClient();
client.DownloadFile(downloadURL, tempZIP);
client.Dispose();
if (!File.Exists(tempZIP))
{
return;
}
// Extract Zip file
var fastZip = new FastZip();
fastZip.ExtractZip(tempZIP, tempFolder, null);
File.Delete(tempZIP);
if (programName.Equals("PG-Ripper"))
{
programName = "PGRipper";
}
if (!File.Exists(Path.Combine(tempFolder, string.Format("{0}.exe", programName))))
{
return;
}
// Check for Microsoft.WindowsAPICodePack.dll
if (!File.Exists(Path.Combine(Application.StartupPath, "Microsoft.WindowsAPICodePack.dll")))
{
File.Copy(
Path.Combine(tempFolder, "Microsoft.WindowsAPICodePack.dll"),
Path.Combine(Application.StartupPath, "Microsoft.WindowsAPICodePack.dll"));
}
// Check for Microsoft.WindowsAPICodePack.Shell.dll
if (!File.Exists(Path.Combine(Application.StartupPath, "Microsoft.WindowsAPICodePack.Shell.dll")))
{
File.Copy(
Path.Combine(tempFolder, "Microsoft.WindowsAPICodePack.Shell.dll"),
Path.Combine(Application.StartupPath, "Microsoft.WindowsAPICodePack.Shell.dll"));
}
// Check for Ripper.Services.dll
if (!File.Exists(Path.Combine(Application.StartupPath, "Ripper.Services.dll")))
{
File.Copy(
Path.Combine(tempFolder, "Ripper.Services.dll"),
Path.Combine(Application.StartupPath, "Ripper.Services.dll"));
}
else
{
File.Replace(
Path.Combine(tempFolder, "Ripper.Services.dll"),
Path.Combine(Application.StartupPath, "Ripper.Services.dll"),
"Ripper.Services.bak");
}
// Check for Ripper.Core.dll
if (!File.Exists(Path.Combine(Application.StartupPath, "Ripper.Core.dll")))
{
File.Copy(
Path.Combine(tempFolder, "Ripper.Core.dll"),
Path.Combine(Application.StartupPath, "Ripper.Core.dll"));
}
else
{
File.Replace(
Path.Combine(tempFolder, "Ripper.Core.dll"),
Path.Combine(Application.StartupPath, "Ripper.Core.dll"),
"Ripper.Core.bak");
}
if (!File.Exists(Path.Combine(Application.StartupPath, string.Format("{0}.exe", programName))))
{
return;
}
// Replace Exe
File.Replace(
Path.Combine(tempFolder, string.Format("{0}.exe", programName)),
assembly.Location,
string.Format("{0}.bak", programName));
DeleteFolder(tempFolder);
var upgradeProcess = new ProcessStartInfo(assembly.Location);
Process.Start(upgradeProcess);
Environment.Exit(0);
}
catch (Exception ex)
{
Utility.SaveOnCrash(ex.Message, ex.StackTrace, null);
}
finally
{
// Finally Delete Temp Directory
DeleteFolder(tempFolder);
}
}
/// <summary>
/// Auto Updater, download latest version and restart it.
/// </summary>
/// <param name="assembly">The assembly.</param>
private static void UpdateServicesDll(Assembly assembly)
{
var tempFolder = Path.Combine(Application.StartupPath, "temp");
try
{
// Download Update
const string DownloadURL = "http://www.watchersnet.de/rip-ripper/Ripper.Services.dll";
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
var client = new WebClient();
client.DownloadFile(DownloadURL, Path.Combine(tempFolder, "Ripper.Services.dll"));
client.Dispose();
if (!File.Exists(Path.Combine(tempFolder, "Ripper.Services.dll")))
{
return;
}
if (!File.Exists(Path.Combine(Application.StartupPath, "Ripper.Services.dll")))
{
File.Copy(
Path.Combine(tempFolder, "Ripper.Services.dll"),
Path.Combine(Application.StartupPath, "Ripper.Services.dll"));
}
else
{
File.Replace(
Path.Combine(tempFolder, "Ripper.Services.dll"),
Path.Combine(Application.StartupPath, "Ripper.Services.dll"),
"Ripper.Services.bak");
}
var upgradeProcess = new ProcessStartInfo(assembly.Location);
Process.Start(upgradeProcess);
Environment.Exit(0);
}
catch (Exception ex)
{
Utility.SaveOnCrash(ex.Message, ex.StackTrace, null);
}
finally
{
// Finally Delete Temp Directory
DeleteFolder(tempFolder);
}
}
/// <summary>
/// Deletes the folder.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>Returns if Folder was deleted</returns>
private static bool DeleteFolder(string path)
{
try
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
}
catch (Exception)
{
return false;
}
return true;
}
#endregion
}
}
//#endif