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
|
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using UnityEngine;
using XUtliPoolLib;
namespace XUpdater
{
public sealed class XCaching : XSingleton<XCaching>
{
public string VersionServer
{
get
{
return this._version_server;
}
}
public string HostUrl
{
get
{
return this._host_url;
}
}
public string UpdatePath
{
get
{
return this._update_path;
}
}
public XDownloader Downloader
{
get
{
return this._down_loader;
}
}
private string _version_server = null;
private string _host_url = null;
internal static readonly string UPDATE_DIRECTORY = "/update/";
private string _update_path = null;
private StringBuilder _log = new StringBuilder();
private XDownloader _down_loader = null;
private MD5CryptoServiceProvider _md5Generator = null;
private AsyncExtractRequest _aer = null;
private AsyncWriteRequest _meta_awr = null;
internal string GetLocalPath(XBundleData data)
{
return string.Format("{0}{1}.assetbundle", this._update_path, data.Name);
}
internal string GetLocalUrl(XBundleData data)
{
bool flag = XSingleton<XUpdater>.singleton.RunTimePlatform == BuildTarget.Standalone;
string arg;
if (flag)
{
arg = "file:///";
}
else
{
arg = "file://";
}
string text = string.Format("{0}{1}{2}.assetbundle", arg, this._update_path, data.Name);
XSingleton<XDebug>.singleton.AddLog("LocalURL: ", text, null, null, null, null, XDebugColor.XDebug_None);
return text;
}
public string GetLoginServerAddress(string loginType)
{
IPlatform xplatform = XSingleton<XUpdater>.singleton.XPlatform;
return xplatform.GetHostWithHttpDns(xplatform.GetLoginServer(loginType));
}
internal string GetDownloadUrl(XBundleData data)
{
return this.MakeToken(string.Format("{0}{1}/{2}.assetbundle", this.HostUrl, XSingleton<XUpdater>.singleton.Platform, data.Name));
}
internal string MakeToken(string url)
{
return string.Format("{0}?token={1}", url, DateTime.Now.Ticks);
}
public override bool Init()
{
IPlatform xplatform = XSingleton<XUpdater>.singleton.XPlatform;
this._version_server = xplatform.GetHostWithHttpDns(xplatform.GetVersionServer());
this._host_url = xplatform.GetHostUrl();
this._md5Generator = new MD5CryptoServiceProvider();
this._down_loader = XUpdater.XGameRoot.AddComponent<XDownloader>();
this._update_path = Application.persistentDataPath + XCaching.UPDATE_DIRECTORY;
return true;
}
internal bool EnableCache()
{
bool flag = !Directory.Exists(this._update_path);
if (flag)
{
XSingleton<XDebug>.singleton.AddLog("Create new path " + this._update_path, null, null, null, null, null, XDebugColor.XDebug_None);
try
{
Directory.CreateDirectory(this._update_path);
return Directory.Exists(this._update_path);
}
catch (Exception ex)
{
XSingleton<XDebug>.singleton.AddErrorLog(string.Format("Error ", ex.Message), null, null, null, null, null);
return false;
}
}
XSingleton<XDebug>.singleton.AddLog(string.Format("Path {0} exists.", this._update_path), null, null, null, null, null, XDebugColor.XDebug_None);
return true;
}
internal AsyncCachedRequest IsBundleCached(XBundleData bundle, uint size)
{
string fullpath = this.GetLocalPath(bundle);
AsyncCachedRequest req = new AsyncCachedRequest();
bool flag = bundle.Size < size;
if (flag)
{
new Thread((ThreadStart) delegate
{
bool flag2 = File.Exists(fullpath);
if (flag2)
{
byte[] bundle2 = this.LoadFile(fullpath);
string a = this.CalculateMD5(bundle2);
req.Cached = (a == bundle.MD5);
req.MaybeCached = true;
}
req.IsDone = true;
}).Start();
}
else
{
req.MaybeCached = File.Exists(fullpath);
req.IsDone = true;
}
return req;
}
internal bool CleanCache()
{
string path = ((int)Application.platform == 8) ? ("/private" + this._update_path) : this._update_path;
bool result;
try
{
bool flag = Directory.Exists(path);
if (flag)
{
DirectoryInfo directoryInfo = new DirectoryInfo(path);
directoryInfo.Delete(true);
bool flag2 = !Directory.Exists(path);
bool flag3 = flag2;
if (flag3)
{
result = this.EnableCache();
}
else
{
result = false;
}
}
else
{
result = true;
}
}
catch (Exception ex)
{
XSingleton<XDebug>.singleton.AddErrorLog("CleanCache error: ", ex.Message, null, null, null, null);
result = false;
}
return result;
}
internal byte[] LoadFile(string fullpath)
{
return File.ReadAllBytes(fullpath);
}
private AsyncReadRequest LoadFileAsync(string fullpath)
{
AsyncReadRequest arr = new AsyncReadRequest();
new Thread((ThreadStart) delegate
{
arr.bytes = File.ReadAllBytes(fullpath);
arr.IsDone = true;
}).Start();
return arr;
}
internal void Download(XBundleData bundle, HandleFetchBundle callback, float percent)
{
this._down_loader.GetBundle(bundle, this.GetDownloadUrl(bundle), new HandleBundleDownload(this.OnBundleDownload), callback, percent);
}
internal AsyncWriteRequest Download(string meta, uint size, float percent)
{
this._meta_awr = new AsyncWriteRequest();
this._meta_awr.Size = size;
this._meta_awr.Location = meta;
this._meta_awr.Name = meta.Substring(meta.LastIndexOf('/') + 1);
this._meta_awr.HasError = false;
string name = meta.Substring(meta.LastIndexOf("/") + 1);
meta = this.MakeToken(this.HostUrl + XSingleton<XUpdater>.singleton.Platform + meta);
this._down_loader.GetMeta(meta, name, new XDownloader.HandleBytesDownload(this.OnMetaDownload), percent);
return this._meta_awr;
}
internal bool Extract(XBundleData bundle, HandleFetchBundle callback, float percent)
{
bool flag =(int) Application.platform == 8;
bool result;
if (flag)
{
bool flag2 = this._aer == null;
if (flag2)
{
this._aer = new AsyncExtractRequest();
new Thread((ThreadStart)delegate
{
string localPath = this.GetLocalPath(bundle);
this._aer.Data = File.ReadAllBytes(localPath);
this._aer.IsDone = true;
}).Start();
}
bool isDone = this._aer.IsDone;
if (isDone)
{
callback(null, this._aer.Data, bundle, false);
this._aer.Data = null;
this._aer = null;
result = true;
}
else
{
result = false;
}
}
else
{
this._down_loader.GetBundle(bundle, this.GetLocalUrl(bundle), null, callback, percent);
result = true;
}
return result;
}
private void OnMetaDownload(WWW www, string error)
{
bool flag = string.IsNullOrEmpty(error);
if (flag)
{
byte[] bs = www.bytes;
new Thread((ThreadStart)delegate
{
try
{
bool needCheckFile = XSingleton<XUpdater>.singleton.NeedCheckFile;
if (needCheckFile)
{
bool flag2 = Path.GetExtension(this._meta_awr.Location).Contains("ab");
if (flag2)
{
bool flag3 = bs[0] != 85 || bs[1] != 110 || bs[2] != 105 || bs[3] != 116 || bs[4] != 121 || bs[5] != 70 || bs[6] != 83;
if (flag3)
{
throw new Exception("Meta head check failed.");
}
}
}
string text = Path.Combine(this._update_path, "AssetBundles");
string fileName = Path.GetFileName(this._meta_awr.Location);
bool flag4 = !Directory.Exists(text);
if (flag4)
{
Directory.CreateDirectory(text);
}
string text2 = Path.Combine(text, fileName);
File.WriteAllBytes(text2, bs);
bool needCheckFile2 = XSingleton<XUpdater>.singleton.NeedCheckFile;
if (needCheckFile2)
{
Thread.Sleep(1);
bool flag5 = this.CheckFileSize(text2, (long)((ulong)this._meta_awr.Size));
if (!flag5)
{
throw new Exception("Meta File size " + this._meta_awr.Size + " not match.");
}
XSingleton<XUpdater>.singleton.XPlatform.SetNoBackupFlag(text2);
this._meta_awr.IsDone = true;
}
else
{
XSingleton<XUpdater>.singleton.XPlatform.SetNoBackupFlag(text2);
this._meta_awr.IsDone = true;
}
}
catch (Exception ex)
{
this.OnDownloadFailed(ex.Message, this._meta_awr);
}
}).Start();
}
else
{
this.OnDownloadFailed(error, this._meta_awr);
}
}
private void OnDownloadFailed(string error, AsyncWriteRequest awr)
{
XSingleton<XDebug>.singleton.AddErrorLog("Download Meta ", awr.Name, " error: ", error, null, null);
awr.HasError = true;
}
private AsyncWriteRequest OnBundleDownload(WWW www, XBundleData bundle, string error)
{
AsyncWriteRequest req = new AsyncWriteRequest();
bool flag = string.IsNullOrEmpty(error);
if (flag)
{
byte[] bs = www.bytes;
new Thread((ThreadStart)delegate
{
req.Location = this.GetLocalPath(bundle);
try
{
File.WriteAllBytes(req.Location, bs);
req.IsDone = true;
}
catch (Exception ex)
{
this.OnDownloadFailed(ex.Message, req);
}
}).Start();
}
else
{
this.OnDownloadFailed(error, req);
}
return req;
}
internal string CalculateMD5(byte[] bundle)
{
byte[] value = this._md5Generator.ComputeHash(bundle);
return BitConverter.ToString(value);
}
internal string CalculateMD5(byte[] bundle, int offset, int count)
{
byte[] value = this._md5Generator.ComputeHash(bundle, offset, count);
return BitConverter.ToString(value);
}
public bool CheckFileSize(string filePath, long fileSize)
{
bool result;
try
{
bool flag = !File.Exists(filePath);
if (flag)
{
result = false;
}
else
{
FileInfo fileInfo = new FileInfo(filePath);
result = (fileSize == fileInfo.Length);
}
}
catch (Exception ex)
{
XSingleton<XDebug>.singleton.AddErrorLog("XCaching.CheckFileSize: " + ex.Message, null, null, null, null, null);
result = false;
}
return result;
}
public override void Uninit()
{
}
}
}
|