forked from wyday/wyupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientFile.cs
599 lines (496 loc) · 21.8 KB
/
ClientFile.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Text;
using Ionic.Zip;
namespace wyUpdate.Common
{
//also used in settings.cs
public enum ImageAlign { Left = 0, Right = 1, Fill = 2 }
public enum UpdateOn
{
DownloadingSelfUpdate = 0, FullSelfUpdate = 1, ExtractSelfUpdate = 2, InstallSelfUpdate = 3,
DownloadingUpdate = 4, Extracting = 5, ClosingProcesses = 6,
PreExecute = 7, BackUpInstalling = 8, ModifyReg = 9,
OptimizeExecute = 10, WriteClientFile = 11, DeletingTemp = 12, Uninstalling = 13
}
public enum ClientFileType { PreRC2, RC2, Final }
public class ClientFile
{
#if CLIENT_READER
public List<string> Languages = new List<string>();
public bool ContainsUninstallFile;
#else
public Hashtable Languages = new Hashtable();
#endif
public string InstalledVersion;
public UpdateOn CurrentlyUpdating = UpdateOn.DownloadingUpdate;
public List<string> ServerFileSites = new List<string>(1);
public List<string> ClientServerSites = new List<string>(1);
public string CompanyName;
public string ProductName;
string m_GUID;
public string GUID
{
get
{
#if !CLIENT_READER
if (string.IsNullOrEmpty(m_GUID))
{
// generate a GUID from the product name
char[] invalidChars = Path.GetInvalidFileNameChars();
if (ProductName != null && ProductName.IndexOfAny(invalidChars) != -1)
{
List<char> invalidFilenameChars = new List<char>(invalidChars);
// there are bad filename characters
//make a new string builder (with at least one bad character)
StringBuilder newText = new StringBuilder(ProductName.Length - 1);
//remove the bad characters
for (int i = 0; i < ProductName.Length; i++)
{
if (invalidFilenameChars.IndexOf(ProductName[i]) == -1)
newText.Append(ProductName[i]);
}
return newText.ToString();
}
return ProductName;
}
#endif
return m_GUID;
}
set
{
m_GUID = value;
}
}
public ImageAlign HeaderImageAlign = ImageAlign.Left;
public string HeaderTextColorName;
public int HeaderTextIndent = -1;
public bool HideHeaderDivider;
public Image TopImage;
public Image SideImage;
public string TopImageFilename;
public string SideImageFilename;
public bool CloseOnSuccess;
public string CustomWyUpdateTitle;
public string PublicSignKey;
public string UpdatePassword;
#if CLIENT
#if !CLIENT_READER
//Open Pre-RC2 client files
public void OpenObsoleteClientFile(string fileName)
{
FileStream fs = null;
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
}
catch (Exception ex)
{
if (fs != null)
fs.Close();
throw new ArgumentException("The client data file (client.wyc) failed to open.\n\nFull details:\n\n" + ex.Message);
}
// Read back the file identification data, if any
if (!ReadFiles.IsHeaderValid(fs, "IUCDFV2"))
{
//free up the file so it can be deleted
fs.Close();
throw new ArgumentException("The client file does not have the correct identifier - this is usually caused by file corruption. \n\nA possible solution is to replace the following file by reinstalling:\n\n" + fileName);
}
byte bType = (byte)fs.ReadByte();
while (!ReadFiles.ReachedEndByte(fs, bType, 0xFF))
{
switch (bType)
{
case 0x01: // Read Company Name
CompanyName = ReadFiles.ReadDeprecatedString(fs);
break;
case 0x02: // Product Name
ProductName = ReadFiles.ReadDeprecatedString(fs);
break;
case 0x03: // Read Installed Version
InstalledVersion = ReadFiles.ReadDeprecatedString(fs);
break;
case 0x04: // Add server file site
AddUniqueString(ReadFiles.ReadDeprecatedString(fs), ServerFileSites);
break;
case 0x09: // Add client server file site
AddUniqueString(ReadFiles.ReadDeprecatedString(fs), ClientServerSites);
break;
case 0x11: // Header image alignment
try
{
HeaderImageAlign = (ImageAlign)Enum.Parse(typeof(ImageAlign), ReadFiles.ReadDeprecatedString(fs));
}
catch { }
break;
case 0x12: // Header text indent
HeaderTextIndent = ReadFiles.ReadInt(fs);
break;
case 0x13: // Header text color
HeaderTextColorName = ReadFiles.ReadDeprecatedString(fs);
break;
case 0x06: // top Image
TopImage = ReadFiles.ReadImage(fs);
break;
case 0x07: // side Image
SideImage = ReadFiles.ReadImage(fs);
break;
default:
ReadFiles.SkipField(fs, bType);
break;
}
bType = (byte)fs.ReadByte();
}
fs.Close();
}
#endif
void LoadClientData(Stream ms, string updatePathVar, string customUrlArgs)
{
ms.Position = 0;
// Read back the file identification data, if any
if (!ReadFiles.IsHeaderValid(ms, "IUCDFV2"))
{
//free up the file so it can be deleted
ms.Close();
throw new Exception("The client file does not have the correct identifier - this is usually caused by file corruption.");
}
#if !CLIENT_READER
LanguageCulture lastLanguage = null;
#endif
string serverSite;
byte bType = (byte)ms.ReadByte();
while (!ReadFiles.ReachedEndByte(ms, bType, 0xFF))
{
switch (bType)
{
case 0x01://Read Company Name
CompanyName = ReadFiles.ReadDeprecatedString(ms);
break;
case 0x02://Product Name
ProductName = ReadFiles.ReadDeprecatedString(ms);
break;
case 0x0A: // GUID
m_GUID = ReadFiles.ReadString(ms);
break;
case 0x03://Read Installed Version
InstalledVersion = ReadFiles.ReadDeprecatedString(ms);
break;
case 0x04://Add server file site
serverSite = ReadFiles.ReadDeprecatedString(ms);
if (updatePathVar != null)
serverSite = serverSite.Replace("%updatepath%", updatePathVar);
if (customUrlArgs != null)
serverSite = serverSite.Replace("%urlargs%", customUrlArgs);
AddUniqueString(serverSite, ServerFileSites);
break;
case 0x09://Add client server file site
serverSite = ReadFiles.ReadDeprecatedString(ms);
if (updatePathVar != null)
serverSite = serverSite.Replace("%updatepath%", updatePathVar);
if (customUrlArgs != null)
serverSite = serverSite.Replace("%urlargs%", customUrlArgs);
AddUniqueString(serverSite, ClientServerSites);
break;
case 0x11://Header image alignment
try
{
HeaderImageAlign = (ImageAlign)Enum.Parse(typeof(ImageAlign), ReadFiles.ReadDeprecatedString(ms));
}
catch { }
break;
case 0x12://Header text indent
HeaderTextIndent = ReadFiles.ReadInt(ms);
break;
case 0x13://Header text color
HeaderTextColorName = ReadFiles.ReadDeprecatedString(ms);
break;
case 0x14: //header image filename
TopImageFilename = ReadFiles.ReadDeprecatedString(ms);
break;
case 0x15: //side image filename
SideImageFilename = ReadFiles.ReadDeprecatedString(ms);
break;
#if CLIENT_READER
case 0x18: // language culture
Languages.Add(ReadFiles.ReadDeprecatedString(ms));
break;
#else
case 0x18: // language culture
lastLanguage = new LanguageCulture(ReadFiles.ReadDeprecatedString(ms));
Languages.Add(lastLanguage.Culture, lastLanguage);
break;
case 0x16: //language filename
if (lastLanguage != null)
lastLanguage.Filename = ReadFiles.ReadDeprecatedString(ms);
else
Languages.Add(string.Empty, new LanguageCulture(null) { Filename = ReadFiles.ReadDeprecatedString(ms) });
break;
#endif
case 0x17: //hide the header divider
HideHeaderDivider = ReadFiles.ReadBool(ms);
break;
case 0x19:
CloseOnSuccess = ReadFiles.ReadBool(ms);
break;
case 0x1A:
CustomWyUpdateTitle = ReadFiles.ReadString(ms);
break;
case 0x1B:
PublicSignKey = ReadFiles.ReadString(ms);
break;
case 0x1C:
UpdatePassword = ReadFiles.ReadString(ms);
break;
default:
ReadFiles.SkipField(ms, bType);
break;
}
bType = (byte)ms.ReadByte();
}
ms.Close();
}
public void LoadClientData(string filename)
{
try
{
using (Stream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
LoadClientData(fs, null, null);
}
}
catch { }
}
#if CLIENT_READER
public void OpenClientFile(string m_Filename)
{
using (ZipFile zip = ZipFile.Read(m_Filename))
{
// load the client details (image filenames, languages, etc.)
using (MemoryStream ms = new MemoryStream())
{
zip["iuclient.iuc"].Extract(ms);
//read in the client data
LoadClientData(ms, null, null);
}
ContainsUninstallFile = zip["uninstall.dat"] != null;
// load the top image
if (!string.IsNullOrEmpty(TopImageFilename))
{
using (MemoryStream ms = new MemoryStream())
{
zip[TopImageFilename].Extract(ms);
// convert the bytes to an images
TopImage = Image.FromStream(ms, true);
}
}
// load the side image
if (!string.IsNullOrEmpty(SideImageFilename))
{
using (MemoryStream ms = new MemoryStream())
{
zip[SideImageFilename].Extract(ms);
// convert the bytes to an images
SideImage = Image.FromStream(ms, true);
}
}
}
}
#else
public void OpenClientFile(string m_Filename, ClientLanguage lang, string forcedCulture, string updatePathVar, string customUrlArgs)
{
using (ZipFile zip = ZipFile.Read(m_Filename))
{
// load the client details (image filenames, languages, etc.)
using (MemoryStream ms = new MemoryStream())
{
zip["iuclient.iuc"].Extract(ms);
//read in the client data
LoadClientData(ms, updatePathVar, customUrlArgs);
}
// load the top image
if (!string.IsNullOrEmpty(TopImageFilename))
{
using (MemoryStream ms = new MemoryStream())
{
zip[TopImageFilename].Extract(ms);
// convert the bytes to an images
TopImage = Image.FromStream(ms, true);
}
}
// load the side image
if (!string.IsNullOrEmpty(SideImageFilename))
{
using (MemoryStream ms = new MemoryStream())
{
zip[SideImageFilename].Extract(ms);
// convert the bytes to an images
SideImage = Image.FromStream(ms, true);
}
}
// Backwards compatability with pre-v1.3 of wyUpdate:
// if the languages has a culture with a null name, load that file
if (Languages.Count == 1 && Languages.Contains(string.Empty))
{
using (MemoryStream ms = new MemoryStream())
{
zip[((LanguageCulture)Languages[string.Empty]).Filename].Extract(ms);
lang.Open(ms);
}
}
else if (Languages.Count > 0)
{
LanguageCulture useLang = null;
// use a forced culture
if (!string.IsNullOrEmpty(forcedCulture))
useLang = (LanguageCulture) Languages[forcedCulture];
// try to find the current culture
if (useLang == null)
useLang = (LanguageCulture)Languages[CultureInfo.CurrentUICulture.Name];
// if current culture isn't available, use the default culture (english)
if (useLang == null)
useLang = (LanguageCulture) Languages["en-US"];
// if the default culture isn't available, use the first available language
if (useLang == null)
{
foreach (LanguageCulture l in Languages.Values)
{
useLang = l;
break;
}
}
if (useLang != null && !string.IsNullOrEmpty(useLang.Filename))
{
using (MemoryStream ms = new MemoryStream())
{
zip[useLang.Filename].Extract(ms);
lang.Open(ms);
}
}
}
}
}
#endif
#endif
#if !CLIENT_READER
public void SaveClientFile(List<UpdateFile> files, string outputFilename)
{
try
{
if (File.Exists(outputFilename))
File.Delete(outputFilename);
using (ZipFile zip = new ZipFile(outputFilename))
{
zip.AlternateEncoding = Encoding.UTF8;
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
// 0 (store only) to 9 (best compression)
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.Level7;
ZipEntry entry;
for (int i = 0; i < files.Count; i++)
{
entry = zip.AddFile(files[i].Filename, "");
entry.FileName = files[i].RelativePath;
entry.LastModified = File.GetLastWriteTime(files[i].Filename);
}
using (Stream clientDets = SaveClientFile())
{
//add the client file
entry = zip.AddEntry("iuclient.iuc", clientDets);
entry.LastModified = DateTime.Now;
zip.Save();
}
}
}
catch (Exception ex)
{
//send back the error message
throw new Exception(Path.GetFileName(outputFilename) + ". \r\n\r\n" + ex.Message);
}
}
Stream SaveClientFile()
{
MemoryStream ms = new MemoryStream();
try
{
// file-identification data
WriteFiles.WriteHeader(ms, "IUCDFV2");
//Company Name
WriteFiles.WriteDeprecatedString(ms, 0x01, CompanyName);
//Product Name
WriteFiles.WriteDeprecatedString(ms, 0x02, ProductName);
// GUID
if (m_GUID != null)
WriteFiles.WriteString(ms, 0x0A, m_GUID);
//Installed Version
WriteFiles.WriteDeprecatedString(ms, 0x03, InstalledVersion);
foreach (string site in ServerFileSites)
{
//Server File Site
WriteFiles.WriteDeprecatedString(ms, 0x04, site);
}
foreach (string site in ClientServerSites)
{
//Client Server File Site
WriteFiles.WriteDeprecatedString(ms, 0x09, site);
}
//Header image alignment
WriteFiles.WriteDeprecatedString(ms, 0x11, HeaderImageAlign.ToString());
//Header text indent
WriteFiles.WriteInt(ms, 0x12, HeaderTextIndent);
//Header text color
if (!string.IsNullOrEmpty(HeaderTextColorName))
WriteFiles.WriteDeprecatedString(ms, 0x13, HeaderTextColorName);
//Top image filename
if (!string.IsNullOrEmpty(TopImageFilename))
WriteFiles.WriteDeprecatedString(ms, 0x14, TopImageFilename);
//Side image filename
if (!string.IsNullOrEmpty(SideImageFilename))
WriteFiles.WriteDeprecatedString(ms, 0x15, SideImageFilename);
foreach (DictionaryEntry dLang in Languages)
{
LanguageCulture lang = (LanguageCulture)dLang.Value;
//Language culture
WriteFiles.WriteDeprecatedString(ms, 0x18, lang.Culture);
//Language filename
if (!string.IsNullOrEmpty(lang.Filename))
WriteFiles.WriteDeprecatedString(ms, 0x16, lang.Filename);
}
//Hide the header divider
if (HideHeaderDivider)
WriteFiles.WriteBool(ms, 0x17, true);
if (CloseOnSuccess)
WriteFiles.WriteBool(ms, 0x19, true);
if (!string.IsNullOrEmpty(CustomWyUpdateTitle))
WriteFiles.WriteString(ms, 0x1A, CustomWyUpdateTitle);
if (!string.IsNullOrEmpty(PublicSignKey))
WriteFiles.WriteString(ms, 0x1B, PublicSignKey);
if (!string.IsNullOrEmpty(UpdatePassword))
WriteFiles.WriteString(ms, 0x1C, UpdatePassword);
ms.WriteByte(0xFF);
ms.Position = 0;
}
catch (Exception)
{
ms.Dispose();
throw;
}
return ms;
}
#endif
#if CLIENT
public static void AddUniqueString(string newString, List<string> list)
{
// if the string already exists, bail out
foreach (string site in list)
if (string.Equals(newString, site, StringComparison.OrdinalIgnoreCase))
return;
// add the string
list.Add(newString);
}
#endif
}
}