forked from wyday/wyupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerFile.Load.cs
187 lines (150 loc) · 7.13 KB
/
ServerFile.Load.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
using System;
using System.IO;
using System.Text;
using Ionic.Zip;
namespace wyUpdate.Common
{
public partial class ServerFile
{
public static ServerFile Load(string fileName, string updatePathVar, string customUrlArgs)
{
ServerFile serv = new ServerFile();
byte[] fileIDBytes = new byte[7];
Stream fs = null;
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
// Read the first 7 bytes of identification data
fs.Read(fileIDBytes, 0, 7);
}
catch (Exception)
{
if (fs != null)
fs.Close();
throw;
}
// check for compression (see if PKZip header is there)
if (fileIDBytes[0] == 0x50 && fileIDBytes[1] == 0x4B && fileIDBytes[2] == 0x03 && fileIDBytes[3] == 0x04)
{
// decompress the "actual" server file to memory
fs.Close();
using (ZipFile zip = ZipFile.Read(fileName))
{
fs = new MemoryStream();
zip["0"].Extract(fs);
}
fs.Position = 0;
// Read the first 7 bytes of identification data
fs.Read(fileIDBytes, 0, 7);
}
// see if the file is in the correct server format
string fileID = Encoding.UTF8.GetString(fileIDBytes);
if (fileID != "IUSDFV2")
{
//free up the file so it can be deleted
fs.Close();
throw new Exception("The downloaded server file does not have the correct identifier. This is usually caused by file corruption.");
}
serv.VersionChoices.Add(new VersionChoice());
byte bType = (byte)fs.ReadByte();
while (!ReadFiles.ReachedEndByte(fs, bType, 0xFF))
{
switch (bType)
{
case 0x01://Read New Version
serv.NewVersion = ReadFiles.ReadDeprecatedString(fs);
break;
case 0x07: //Min Client version
serv.MinClientVersion = ReadFiles.ReadDeprecatedString(fs);
break;
case 0x0B: //The version to update from
if (serv.VersionChoices.Count > 1 || serv.VersionChoices[0].Version != null)
serv.VersionChoices.Add(new VersionChoice());
serv.VersionChoices[serv.VersionChoices.Count - 1].Version = ReadFiles.ReadDeprecatedString(fs);
break;
case 0x03://Add update file site
string updateSite = ReadFiles.ReadDeprecatedString(fs);
if (updatePathVar != null)
updateSite = updateSite.Replace("%updatepath%", updatePathVar);
updateSite = updateSite.Replace("%urlargs%", customUrlArgs ?? string.Empty);
ClientFile.AddUniqueString(updateSite, serv.VersionChoices[serv.VersionChoices.Count - 1].FileSites);
break;
case 0x80: //the changes text is in RTF format
serv.VersionChoices[serv.VersionChoices.Count - 1].RTFChanges = true;
break;
case 0x04://Read Changes
serv.VersionChoices[serv.VersionChoices.Count - 1].Changes = ReadFiles.ReadDeprecatedString(fs);
break;
case 0x09://update's filesize
serv.VersionChoices[serv.VersionChoices.Count - 1].FileSize = ReadFiles.ReadLong(fs);
break;
case 0x08://update's Adler32 checksum
serv.VersionChoices[serv.VersionChoices.Count - 1].Adler32 = ReadFiles.ReadLong(fs);
break;
case 0x14: // signed SHA1 hash
serv.VersionChoices[serv.VersionChoices.Count - 1].SignedSHA1Hash = ReadFiles.ReadByteArray(fs);
break;
case 0x0A: //Installing to which directories?
serv.VersionChoices[serv.VersionChoices.Count - 1].InstallingTo = (InstallingTo)ReadFiles.ReadInt(fs);
break;
case 0x8E: //the RegChanges (built with wyBuid 2.6.11.4 and below)
if (RegChange.ReadFromStream(fs).RegBasekey != RegBasekeys.HKEY_CURRENT_USER)
serv.VersionChoices[serv.VersionChoices.Count - 1].InstallingTo |= InstallingTo.NonCurrentUserReg;
break;
case 0x20:
serv.NoUpdateToLatestLinkText = ReadFiles.ReadDeprecatedString(fs);
break;
case 0x21:
serv.NoUpdateToLatestLinkURL = ReadFiles.ReadDeprecatedString(fs);
break;
case 0x0F:
//skip over the integer (4 bytes) length
//this is just used to trick pre-1.0 Final versions
//of wyUpdate to correctly read the server file correctly
fs.Position += 4;
break;
default:
ReadFiles.SkipField(fs, bType);
break;
}
bType = (byte)fs.ReadByte();
}
fs.Close();
return serv;
}
#if !SERVER_READER
public VersionChoice GetVersionChoice(string installedVersion)
{
VersionChoice updateFrom = null;
for (int i = 0; i < VersionChoices.Count; i++)
{
// select the correct delta-patch version choice
if (VersionTools.Compare(VersionChoices[i].Version, installedVersion) == 0)
{
updateFrom = VersionChoices[i];
break;
}
}
// if no delta-patch update has been selected, use the catch-all update (if it exists)
if (updateFrom == null && CatchAllUpdateExists)
updateFrom = VersionChoices[VersionChoices.Count - 1];
if (updateFrom == null)
throw new NoUpdatePathToNewestException();
return updateFrom;
}
bool? catchAllExists;
public bool CatchAllUpdateExists
{
get
{
if (catchAllExists == null)
{
catchAllExists = VersionChoices.Count > 0 &&
VersionTools.Compare(VersionChoices[VersionChoices.Count - 1].Version, NewVersion) == 0;
}
return catchAllExists.Value;
}
}
#endif
}
}