forked from AscensionGameDev/Intersect-Engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdates.patch
5643 lines (5468 loc) · 336 KB
/
updates.patch
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From 304a49a72d99f1ef0921131be46778162d0aeaa7 Mon Sep 17 00:00:00 2001
From: JC Snider <[email protected]>
Date: Mon, 20 Apr 2020 21:39:47 -0400
Subject: [PATCH 1/8] Actually makes party xp range and common event options
work (#162)
* Actually makes party xp range and common event options work
* Fixing exp distribution amounts between members, includes pandas suggested cleanups
---
Intersect.Server/Entities/Player.cs | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/Intersect.Server/Entities/Player.cs b/Intersect.Server/Entities/Player.cs
index 71815bab..2c140f1d 100644
--- a/Intersect.Server/Entities/Player.cs
+++ b/Intersect.Server/Entities/Player.cs
@@ -861,15 +861,17 @@ public override void KilledEntity(Entity entity)
// If in party, split the exp.
if (Party != null && Party.Count > 0)
{
- foreach (var partyMember in Party)
- {
- //TODO: Only share experience with party members on the 9 surrounding maps....
- partyMember.GiveExperience(descriptor.Experience / Party.Count);
+ var partyMembersInXpRange = Party.Where(partyMember => partyMember.InRangeOf(this, Options.PartySharedXpRange));
+ var partyExperience = descriptor.Experience / partyMembersInXpRange.Count();
+ foreach (var partyMember in partyMembersInXpRange) {
+ partyMember.GiveExperience(partyExperience);
partyMember.UpdateQuestKillTasks(entity);
- if (partyEvent != null)
- {
- if (!(playerEvent != null && partyMember == this))
- {
+ }
+
+ if (partyEvent != null)
+ {
+ foreach (var partyMember in Party) {
+ if (partyMember.InRangeOf(this, Options.PartyStartCommonEventRange) && !(partyMember == this && playerEvent != null)) {
partyMember.StartCommonEvent(partyEvent);
}
}
From 26e0a3c053818115c0f6d601e2f40f4194a4784c Mon Sep 17 00:00:00 2001
From: JC Snider <[email protected]>
Date: Mon, 20 Apr 2020 21:40:02 -0400
Subject: [PATCH 2/8] Provide a client and editor autoupdate system (#157)
* Editor update creation and client autoupdater wip
* Editor updater wip and some bug fixes
* Fixed editor trying to include version.json within updates
* Remove non-existent files from exclude list and allow custom scripts to generate config
* WIP download streaming, much faster, but not fully tested.
* Fixed up editor update creation logic so that it is more user friendly and less destructive
* Fix updater not showing the smaller font/info when downloaded from the host
* Allow the streaming url to be provided in the update configuration.
* Undoing unintended formatting changes to graphics.cs
* Capatilizing strings
* Fixes #161 the base dir check returning true when trying to save update in parent of editors folder
---
.../Configuration/ClientConfiguration.cs | 2 +
Intersect (Core)/Intersect (Core).csproj | 4 +
Intersect (Core)/Updater/Update.cs | 16 +
Intersect (Core)/Updater/UpdateFile.cs | 30 +
Intersect (Core)/Updater/UpdateStatus.cs | 18 +
Intersect (Core)/Updater/Updater.cs | 699 ++++++
Intersect.Client/Core/Graphics.cs | 2 +-
Intersect.Client/Localization/Strings.cs | 21 +
.../MonoGame/Graphics/MonoRenderer.cs | 1 -
Intersect.Client/MonoGame/IntersectGame.cs | 276 ++-
Intersect.Editor/Core/Program.cs | 3 +-
Intersect.Editor/Forms/frmLogin.Designer.cs | 3 +-
Intersect.Editor/Forms/frmMain.Designer.cs | 29 +-
Intersect.Editor/Forms/frmMain.cs | 203 ++
Intersect.Editor/Forms/frmMain.resx | 18 +-
Intersect.Editor/Forms/frmUpdate.Designer.cs | 153 ++
Intersect.Editor/Forms/frmUpdate.cs | 124 +
Intersect.Editor/Forms/frmUpdate.resx | 2151 +++++++++++++++++
Intersect.Editor/General/Globals.cs | 4 +
Intersect.Editor/Intersect.Editor.csproj | 9 +
Intersect.Editor/Localization/Strings.cs | 46 +
Intersect.Network/Lidgren/LidgrenInterface.cs | 4 +
22 files changed, 3777 insertions(+), 39 deletions(-)
create mode 100644 Intersect (Core)/Updater/Update.cs
create mode 100644 Intersect (Core)/Updater/UpdateFile.cs
create mode 100644 Intersect (Core)/Updater/UpdateStatus.cs
create mode 100644 Intersect (Core)/Updater/Updater.cs
create mode 100644 Intersect.Editor/Forms/frmUpdate.Designer.cs
create mode 100644 Intersect.Editor/Forms/frmUpdate.cs
create mode 100644 Intersect.Editor/Forms/frmUpdate.resx
diff --git a/Intersect (Core)/Configuration/ClientConfiguration.cs b/Intersect (Core)/Configuration/ClientConfiguration.cs
index 4bba076c..893914d2 100644
--- a/Intersect (Core)/Configuration/ClientConfiguration.cs
+++ b/Intersect (Core)/Configuration/ClientConfiguration.cs
@@ -125,6 +125,8 @@ public void Validate()
// TODO: What is this for?
public List<string> IntroImages { get; set; } = new List<string>();
+ public string UpdateUrl { get; set; } = "";
+
#endregion
#region Serialization Hooks
diff --git a/Intersect (Core)/Intersect (Core).csproj b/Intersect (Core)/Intersect (Core).csproj
index faa4aba3..d1dbe3f5 100644
--- a/Intersect (Core)/Intersect (Core).csproj
+++ b/Intersect (Core)/Intersect (Core).csproj
@@ -477,6 +477,10 @@
<Compile Include="Threading\ConcurrentInstance.cs" />
<Compile Include="Threading\LockingActionQueue.cs" />
<Compile Include="Threading\Threaded.cs" />
+ <Compile Include="Updater\Update.cs" />
+ <Compile Include="Updater\UpdateFile.cs" />
+ <Compile Include="Updater\Updater.cs" />
+ <Compile Include="Updater\UpdateStatus.cs" />
<Compile Include="Utilities\AlphanumComparator.cs" />
<Compile Include="Utilities\DatabaseUtils.cs" />
<Compile Include="Utilities\ExperienceCurve.cs" />
diff --git a/Intersect (Core)/Updater/Update.cs b/Intersect (Core)/Updater/Update.cs
new file mode 100644
index 00000000..0ba71d9b
--- /dev/null
+++ b/Intersect (Core)/Updater/Update.cs
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Intersect.Updater
+{
+ public class Update
+ {
+ public List<UpdateFile> Files { get; set; } = new List<UpdateFile>();
+ public bool TrustCache { get; set; } = true;
+ public string StreamingUrl { get; set; }
+
+ }
+}
diff --git a/Intersect (Core)/Updater/UpdateFile.cs b/Intersect (Core)/Updater/UpdateFile.cs
new file mode 100644
index 00000000..bc26ad3b
--- /dev/null
+++ b/Intersect (Core)/Updater/UpdateFile.cs
@@ -0,0 +1,30 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Intersect.Updater
+{
+ public class UpdateFile
+ {
+ public string Path { get; set; }
+ public string Hash { get; set; }
+ public long Size { get; set; }
+ public bool ClientIgnore { get; set; }
+ public bool EditorIgnore { get; set; }
+
+ public UpdateFile()
+ {
+
+ }
+
+ public UpdateFile(string path, string hash, long size)
+ {
+ Path = path;
+ Hash = hash;
+ Size = size;
+ }
+
+ }
+}
diff --git a/Intersect (Core)/Updater/UpdateStatus.cs b/Intersect (Core)/Updater/UpdateStatus.cs
new file mode 100644
index 00000000..fbf7cb8a
--- /dev/null
+++ b/Intersect (Core)/Updater/UpdateStatus.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Intersect.Updater
+{
+ public enum UpdateStatus
+ {
+ Checking = 0,
+ Updating = 1,
+ Restart,
+ Done,
+ Error,
+ None
+ }
+}
diff --git a/Intersect (Core)/Updater/Updater.cs b/Intersect (Core)/Updater/Updater.cs
new file mode 100644
index 00000000..773402e1
--- /dev/null
+++ b/Intersect (Core)/Updater/Updater.cs
@@ -0,0 +1,699 @@
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Net.Http;
+using System.Reflection;
+using System.Security.Cryptography;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+using Intersect.Configuration;
+using Intersect.Logging;
+
+using Newtonsoft.Json;
+
+namespace Intersect.Updater
+{
+ public class Updater
+ {
+
+ private Thread mUpdateThread;
+ private Update mUpdate;
+ private Update mCachedVersion;
+ private Update mCurrentVersion;
+ private string mCurrentVersionPath;
+ private Thread[] mDownloadThreads;
+ private readonly int mDownloadThreadCount = 1;
+ private ConcurrentStack<UpdateFile> mDownloadQueue = new ConcurrentStack<UpdateFile>();
+ private ConcurrentDictionary<UpdateFile, long> mFailedDownloads = new ConcurrentDictionary<UpdateFile, long>();
+ private ConcurrentBag<UpdateFile> mCompletedDownloads = new ConcurrentBag<UpdateFile>();
+ private ConcurrentDictionary<UpdateFile, long> mActiveDownloads = new ConcurrentDictionary<UpdateFile, long>();
+ private long mDownloadedBytes;
+ private bool mFailed;
+ private bool mStopping;
+ private bool mUpdaterContentLoaded;
+ private bool mIsClient;
+ private string mConfigUrl;
+ private string mBaseUrl;
+
+ public float Progress => ((float)BytesDownloaded / (float)SizeTotal) * 100f;
+
+ public int FilesRemaining => mDownloadQueue.Count +
+ mActiveDownloads.Count;
+
+ public long SizeRemaining => SizeTotal -
+ BytesDownloaded;
+
+ public int FilesTotal => mDownloadQueue.Count +
+ mActiveDownloads.Count +
+ mCompletedDownloads.Count;
+
+ public long BytesDownloaded => mDownloadedBytes +
+ mActiveDownloads.Values.Sum();
+
+ public long SizeTotal { get; private set; }
+
+ public UpdateStatus Status { get; private set; } = UpdateStatus.Checking;
+
+ public Exception Exception { get; private set; }
+
+ public bool ReplacedSelf { get; private set; }
+
+
+
+ public Updater(string updateUrl, string currentVersionPath, bool isClient, int maxDownloadThreads = 10)
+ {
+ if (string.IsNullOrWhiteSpace(updateUrl))
+ {
+ Status = UpdateStatus.None;
+ return;
+ }
+
+ mDownloadThreadCount = maxDownloadThreads;
+ mCurrentVersionPath = currentVersionPath;
+ mIsClient = isClient;
+
+ mUpdateThread = new Thread(RunUpdates);
+ mUpdateThread.Start();
+ }
+
+
+ private async void RunUpdates()
+ {
+ DeleteOldFiles();
+
+ //Download Update Config
+ using (WebClient wc = new WebClient())
+ {
+ try
+ {
+ mConfigUrl = ClientConfiguration.Instance.UpdateUrl.TrimEnd(new char[] { '/' });
+ mBaseUrl = ClientConfiguration.Instance.UpdateUrl.TrimEnd(new char[] { '/' });
+ var uri = new Uri(ClientConfiguration.Instance.UpdateUrl);
+
+ //Specifying update.json themselves or some other file that generates the config... base url needs to be the folder containing it
+ if (Path.HasExtension(uri.AbsolutePath))
+ {
+ mBaseUrl = uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments.Last().Length).TrimEnd(new char[] { '/' });
+ }
+ else
+ {
+ mConfigUrl = ClientConfiguration.Instance.UpdateUrl.TrimEnd(new char[] { '/' }) + "/update.json";
+ }
+ var json = wc.DownloadString(mConfigUrl + "?token=" + Environment.TickCount);
+ mUpdate = JsonConvert.DeserializeObject<Update>(json);
+
+ var downloadFirst = new List<UpdateFile>();
+
+ var updateRequired = true;
+
+ if (File.Exists(mCurrentVersionPath))
+ {
+ mCachedVersion = JsonConvert.DeserializeObject<Update>(
+ File.ReadAllText(mCurrentVersionPath)
+ );
+
+
+ updateRequired = false;
+ foreach (var file in mUpdate.Files.Where(f => f.ClientIgnore == false))
+ {
+ var checkFile = mCachedVersion.Files.FirstOrDefault(f => f.Path == file.Path);
+ if (checkFile == null || checkFile.Size != file.Size || checkFile.Hash != file.Hash)
+ {
+ updateRequired = true;
+ }
+ else
+ {
+ if (!File.Exists(file.Path) || !mUpdate.TrustCache)
+ {
+ updateRequired = true;
+ }
+ }
+ }
+
+ }
+
+ //If we are doing a forced full check or if we don't have a current version file then we will start from scratch
+ if (updateRequired)
+ {
+ //Remove Deleted Files
+ if (mCachedVersion != null)
+ {
+ foreach (var file in mCachedVersion.Files)
+ {
+ if (!mUpdate.Files.Any(f => f.Path == file.Path))
+ {
+ if (File.Exists(file.Path))
+ {
+ try
+ {
+ File.Delete(file.Path);
+ }
+ catch
+ {
+ }
+ }
+ }
+ }
+ }
+
+ //Copy Over
+ mCurrentVersion = new Update();
+ foreach (var file in mUpdate.Files)
+ {
+ if ((mIsClient && file.ClientIgnore || !mIsClient && file.EditorIgnore) && mCachedVersion != null)
+ {
+ var ignoredFile = mCachedVersion.Files.FirstOrDefault(f => f.Path == file.Path);
+ if (ignoredFile != null)
+ {
+ mCurrentVersion.Files.Add(ignoredFile);
+ }
+ continue;
+ }
+
+ if (File.Exists(file.Path))
+ {
+ //If json we will still trust the cache, this might be wrong but given that the client is constantly updating json files we really can't expect the hash to always match
+ if (mCachedVersion != null && Path.GetExtension(file.Path) == ".json")
+ {
+ var cacheCompare = mCachedVersion.Files.FirstOrDefault(f => f.Path == file.Path);
+ if (cacheCompare != null)
+ {
+ if (cacheCompare.Size == file.Size &&
+ cacheCompare.Hash == file.Hash)
+ {
+ mCurrentVersion.Files.Add(file);
+
+ continue;
+ }
+ }
+ }
+
+ //Otherwise let's compare hashes and potentially add it to the update list
+ var md5Hash = "";
+ using (var md5 = MD5.Create())
+ {
+ using (var fs = File.OpenRead(file.Path))
+ {
+ if (fs.Length != file.Size)
+ {
+ AddToUpdateList(file, downloadFirst);
+ }
+ else
+ {
+ using (var stream = new BufferedStream(fs, 1200000))
+ {
+ md5Hash = BitConverter.ToString(md5.ComputeHash(stream))
+ .Replace("-", "")
+ .ToLowerInvariant();
+ }
+
+ if (md5Hash != file.Hash)
+ {
+ AddToUpdateList(file, downloadFirst);
+ }
+ else
+ {
+ mCurrentVersion.Files.Add(file);
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ AddToUpdateList(file, downloadFirst);
+ }
+ }
+ }
+
+ foreach (var file in downloadFirst)
+ {
+ mDownloadQueue.Push(file);
+ }
+
+ if (mDownloadQueue.Count == 0)
+ {
+ Status = UpdateStatus.None;
+ return;
+ }
+ }
+ catch (Exception ex)
+ {
+ //Failed to fetch update info or deserialize!
+ Status = UpdateStatus.Error;
+ Exception = new Exception("[Update Check Failed!] - " + ex.Message, ex);
+ return;
+ }
+ }
+
+ //Got our update list!
+ foreach (var file in mDownloadQueue)
+ SizeTotal += file.Size;
+
+
+ Status = UpdateStatus.Updating;
+
+ var streamingSuccess = false;
+
+ if (!string.IsNullOrWhiteSpace(mUpdate.StreamingUrl))
+ {
+ streamingSuccess = await StreamDownloads();
+ }
+
+ if (!streamingSuccess)
+ {
+ //Spawn Download Threads
+ var threadCount = Math.Min(mDownloadThreadCount, FilesTotal);
+ mDownloadThreads = new Thread[threadCount];
+
+ for (int i = 0; i < threadCount; i++)
+ {
+ mDownloadThreads[i] = new Thread(DownloadUpdates);
+ mDownloadThreads[i].Start();
+ }
+ }
+
+ while (Updating())
+ {
+ Thread.Sleep(10);
+ }
+
+ //Success or failure we will save the current version info here
+ foreach (var file in mCompletedDownloads)
+ mCurrentVersion.Files.Add(file);
+
+
+ File.WriteAllText(
+ mCurrentVersionPath,
+ JsonConvert.SerializeObject(
+ mCurrentVersion, Formatting.Indented,
+ new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }
+ )
+ );
+
+
+ if (mStopping)
+ {
+ return;
+ }
+
+ if (!mFailed)
+ {
+ if (ReplacedSelf)
+ {
+ Status = UpdateStatus.Restart;
+ }
+ else
+ {
+ Status = UpdateStatus.Done;
+ }
+ }
+ else
+ {
+ Status = UpdateStatus.Error;
+ }
+
+ }
+
+ private async Task<bool> StreamDownloads()
+ {
+ var client = new HttpClient();
+
+ var files = new List<string>();
+
+ while (!mDownloadQueue.IsEmpty)
+ {
+ if (mDownloadQueue.TryPop(out UpdateFile file))
+ {
+ files.Add(file.Path);
+ mActiveDownloads.TryAdd(file,0);
+ }
+ }
+ var msg = new HttpRequestMessage(HttpMethod.Post, mUpdate.StreamingUrl + "?token=" + Environment.TickCount)
+ {
+ Content = new StringContent(JsonConvert.SerializeObject(files), Encoding.UTF8, "application/json"),
+ };
+ var response = await client.SendAsync(msg, HttpCompletionOption.ResponseHeadersRead);
+
+ using (var str = await response.Content.ReadAsStreamAsync())
+ {
+ try
+ {
+ using (var br = new BinaryReader(str))
+ {
+ while (true)
+ {
+ var name = br.ReadString();
+
+ var file = mActiveDownloads.Keys.FirstOrDefault(f => f.Path == name);
+ var size = (int)br.ReadInt64();
+
+ var dataStream = new MemoryStream(size);
+ var downloaded = 0;
+ while (downloaded < size)
+ {
+ var chunk = 1024 * 1024;
+ if (downloaded + chunk > size)
+ {
+ chunk = size - downloaded;
+ }
+ dataStream.Write(br.ReadBytes(chunk),0,chunk);
+ downloaded += chunk;
+ if (file != null)
+ {
+ mActiveDownloads[file] = downloaded;
+ }
+ }
+
+ var data = dataStream.ToArray();
+ dataStream.Close();
+ dataStream.Dispose();
+
+ if (file != null)
+ {
+ try
+ {
+ BeforeFileDownload(file);
+ CheckFileData(file, data);
+ BeforeReplaceFile(file);
+
+
+ //Save New File
+ File.WriteAllBytes(file.Path, data);
+
+ lock (mUpdate)
+ {
+ mCompletedDownloads.Add(file);
+ mActiveDownloads.TryRemove(file, out long val);
+ mDownloadedBytes += file.Size;
+
+ if (IsUpdaterFile(file.Path))
+ {
+ mUpdaterContentLoaded = true;
+ }
+ }
+
+
+ }
+ catch (EndOfStreamException eof)
+ {
+ return mDownloadQueue.IsEmpty && mActiveDownloads.IsEmpty;
+ }
+ catch (Exception ex)
+ {
+ lock (mUpdate)
+ {
+
+ mActiveDownloads.TryRemove(file, out long val);
+
+ if (mFailedDownloads.ContainsKey(file))
+ {
+ mFailedDownloads[file]++;
+ }
+ else
+ {
+ mFailedDownloads.TryAdd(file, 1);
+ }
+
+ if (mFailedDownloads[file] > 2)
+ {
+ Exception = new Exception("[" + file.Path + "] - " + ex.Message, ex);
+ mFailed = true;
+ }
+ else
+ {
+ mDownloadQueue.Push(file);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ catch (EndOfStreamException eof)
+ {
+ //Good to go?
+ //TODO Check if any files are missing, if so return false and let the basic downloader code try to fetch them.
+ return true;
+ }
+ catch (Exception ex)
+ {
+ //Errored
+ Log.Error("Failed to download streamed files, failure occured on " );
+ return false;
+ }
+ }
+ }
+
+ private bool Updating()
+ {
+ lock (mUpdate)
+ {
+ return (mDownloadQueue.Count > 0 || mActiveDownloads.Count > 0) && !mFailed && !mStopping;
+ }
+ }
+
+ private async void DownloadUpdates()
+ {
+ while (mDownloadQueue.Count > 0 && !mFailed)
+ {
+ UpdateFile file = null;
+ var streamDl = false;
+ lock (mUpdate)
+ {
+ if (mDownloadQueue.TryPop(out file))
+ {
+ mActiveDownloads.TryAdd(file, 0);
+ }
+ }
+
+ if (file != null)
+ {
+ //Download File
+ BeforeFileDownload(file);
+
+ try
+ {
+ //Use WebClient to Download File To Memory
+ var wc = new WebClient();
+ wc.DownloadProgressChanged += ((sender, args) => mActiveDownloads[file] = args.BytesReceived);
+ var fileData = await wc.DownloadDataTaskAsync(new Uri(mBaseUrl + "/" + file.Path + "?token=" + Environment.TickCount));
+ wc.Dispose();
+
+ CheckFileData(file, fileData);
+
+ BeforeReplaceFile(file);
+
+ //Save New File
+ File.WriteAllBytes(file.Path, fileData);
+
+ lock (mUpdate)
+ {
+ mCompletedDownloads.Add(file);
+ mActiveDownloads.TryRemove(file, out long val);
+ mDownloadedBytes += file.Size;
+
+ if (IsUpdaterFile(file.Path))
+ {
+ mUpdaterContentLoaded = true;
+ }
+ }
+
+
+ }
+ catch (Exception ex)
+ {
+ lock (mUpdate)
+ {
+ mActiveDownloads.TryRemove(file, out long val);
+
+
+ if (mFailedDownloads.ContainsKey(file))
+ {
+ mFailedDownloads[file]++;
+ }
+ else
+ {
+ mFailedDownloads.TryAdd(file, 1);
+ }
+
+ if (mFailedDownloads[file] > 2)
+ {
+ Exception = new Exception("[" + file.Path + "] - " + ex.Message, ex);
+ mFailed = true;
+ }
+ else
+ {
+ mDownloadQueue.Push(file);
+ }
+ }
+ }
+
+ }
+ Thread.Sleep(10);
+ }
+ }
+
+ private void BeforeFileDownload(UpdateFile file)
+ {
+ //Create any parent directories for this file
+ var dir = Path.GetDirectoryName(file.Path);
+ if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
+ {
+ Directory.CreateDirectory(dir);
+ }
+ }
+
+ private void CheckFileData(UpdateFile file, byte[] fileData)
+ {
+ if (fileData.Length != file.Size)
+ {
+ throw new Exception("[File Length Mismatch - Got " + fileData.Length + " bytes, Expected " + file.Size + "]");
+ }
+
+ //Check MD5
+ var md5Hash = "";
+ using (var md5 = MD5.Create())
+ {
+ using (var stream = new MemoryStream(fileData))
+ {
+ md5Hash = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLowerInvariant();
+ }
+ }
+
+ if (md5Hash != file.Hash)
+ {
+ throw new Exception("File Hash Mismatch");
+ }
+ }
+
+ private void BeforeReplaceFile(UpdateFile file)
+ {
+ //Delete .old first if exists
+ if (File.Exists(file.Path + ".old"))
+ {
+ try
+ {
+ File.Delete(file.Path + ".old");
+ }
+ catch { }
+ }
+
+ //Delete Existing File
+ if (File.Exists(file.Path))
+ {
+ try
+ {
+ File.Delete(file.Path);
+ }
+ catch
+ {
+ try
+ {
+ File.Move(file.Path, file.Path + ".old");
+ }
+ catch
+ {
+ throw new Exception("Failed to delete or move existing file!");
+ }
+ }
+ }
+
+ if (file.Path == Path.GetFileName(Assembly.GetEntryAssembly().Location))
+ {
+ ReplacedSelf = true;
+ }
+ }
+
+ private void AddToUpdateList(UpdateFile file, List<UpdateFile> downloadFirst)
+ {
+ if (IsUpdaterFile(file.Path.ToLower()))
+ {
+ downloadFirst.Add(file);
+ }
+ else
+ {
+ mDownloadQueue.Push(file);
+ }
+ }
+
+ private void DeleteOldFiles()
+ {
+ foreach (var file in Directory.GetFiles(Directory.GetCurrentDirectory(), "*.old", SearchOption.AllDirectories))
+ {
+ try
+ {
+ File.Delete(file);
+ }
+ catch { }
+ }
+ }
+
+ private bool IsUpdaterFile(string path)
+ {
+ switch (path)
+ {
+ case "resources/updater/background.png":
+ return true;
+ case "resources/updater/font.xnb":
+ return true;
+ case "resources/updater/fontsmall.xnb":
+ return true;
+ case "resources/updater/progressbar.png":
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ public string GetHumanReadableFileSize(long size)
+ {
+ string[] sizes = { "B", "KB", "MB", "GB", "TB" };
+ double len = size;
+ int order = 0;
+ while (len >= 1024 && order < sizes.Length - 1)
+ {
+ order++;
+ len = len / 1024;
+ }
+ return String.Format("{0:0.##} {1} Left", len, sizes[order]);
+ }
+
+ public bool CheckUpdaterContentLoaded()
+ {
+ if (mUpdaterContentLoaded)
+ {
+ mUpdaterContentLoaded = false;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ public void Stop()
+ {
+ if (mDownloadThreads != null)
+ {
+ foreach (var dlThread in mDownloadThreads)
+ {
+ try
+ {
+ dlThread?.Abort();
+ }
+ catch
+ {
+ }
+ }
+ }
+
+ mStopping = true;
+ }
+ }
+}
diff --git a/Intersect.Client/Core/Graphics.cs b/Intersect.Client/Core/Graphics.cs
index 3fb9cfeb..fa564aa1 100644
--- a/Intersect.Client/Core/Graphics.cs
+++ b/Intersect.Client/Core/Graphics.cs
@@ -1245,4 +1245,4 @@ public static void UpdatePlayerLight()
}
-}
+}
\ No newline at end of file
diff --git a/Intersect.Client/Localization/Strings.cs b/Intersect.Client/Localization/Strings.cs
index a1b0296f..b556e802 100644
--- a/Intersect.Client/Localization/Strings.cs
+++ b/Intersect.Client/Localization/Strings.cs
@@ -1513,6 +1513,27 @@ public struct Numbers
}
+ public struct Update
+ {
+
+ public static LocalizedString Checking = @"Checking for updates, please wait!";
+
+ public static LocalizedString Updating = @"Downloading updates, please wait!";
+
+ public static LocalizedString Restart = @"Update complete! Relaunch {00} to play!";
+
+ public static LocalizedString Done = @"Update complete! Launching game!";
+
+ public static LocalizedString Error = @"Update Error! Check logs for more info!";
+
+ public static LocalizedString Files = @"{00} Files Remaining";
+
+ public static LocalizedString Size = @"{00} Left";
+
+ public static LocalizedString Percent = @"{00}%";
+
+ }
+
}
}
diff --git a/Intersect.Client/MonoGame/Graphics/MonoRenderer.cs b/Intersect.Client/MonoGame/Graphics/MonoRenderer.cs
index 8c861612..2440ad6c 100644
--- a/Intersect.Client/MonoGame/Graphics/MonoRenderer.cs
+++ b/Intersect.Client/MonoGame/Graphics/MonoRenderer.cs
@@ -97,7 +97,6 @@ public MonoRenderer(GraphicsDeviceManager graphics, ContentManager contentManage
mGame = monoGame;
mGraphics = graphics;
mContentManager = contentManager;
- mGraphics.PreferHalfPixelOffset = true;
mNormalState = new BlendState()
{
diff --git a/Intersect.Client/MonoGame/IntersectGame.cs b/Intersect.Client/MonoGame/IntersectGame.cs
index 970e5a8a..bb46d116 100644
--- a/Intersect.Client/MonoGame/IntersectGame.cs
+++ b/Intersect.Client/MonoGame/IntersectGame.cs
@@ -1,4 +1,7 @@
using System;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
using System.Reflection;
using Intersect.Client.Core;
@@ -16,6 +19,7 @@
using Intersect.Client.MonoGame.System;
using Intersect.Configuration;
using Intersect.Logging;
+using Intersect.Updater;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
@@ -30,9 +34,21 @@ namespace Intersect.Client.MonoGame
/// </summary>
public class IntersectGame : Game
{
-
+ private bool mInitialized;
private double mLastUpdateTime = 0;