forked from sarbian/ModuleManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMPatchLoader.cs
More file actions
2245 lines (1924 loc) · 95.2 KB
/
Copy pathMMPatchLoader.cs
File metadata and controls
2245 lines (1924 loc) · 95.2 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using ModuleManager.Collections;
using ModuleManager.Logging;
using ModuleManager.Extensions;
using ModuleManager.Threading;
using ModuleManager.Tags;
using ModuleManager.Patches;
using ModuleManager.Progress;
using NodeStack = ModuleManager.Collections.ImmutableStack<ConfigNode>;
using static ModuleManager.FilePathRepository;
namespace ModuleManager
{
public class MMPatchLoader
{
private const string PHYSICS_NODE_NAME = "PHYSICSGLOBALS";
private const string TECH_TREE_NODE_NAME = "TechTree";
public string status = "";
public string errors = "";
public static bool keepPartDB = false;
private static readonly KeyValueCache<string, Regex> regexCache = new KeyValueCache<string, Regex>();
private string configSha;
private readonly Dictionary<string, string> filesSha = new Dictionary<string, string>();
private string runReportLine = "Execution Report | Outcome: Pending | Scenario: Unknown | Patches Applied: Unknown";
private int lastCachedPatchedNodeCount = -1;
// Represents the current state of the cache after validation
private class CacheStatus
{
public bool IsValid { get; set; }
public List<string> AddedFiles { get; set; } = new List<string>();
public List<string> ModifiedFiles { get; set; } = new List<string>();
public List<string> DeletedFiles { get; set; } = new List<string>();
public string Summary { get; set; } = "metadata unavailable";
public string NextAction { get; set; } = "full patch";
// True if cache can be used as baseline for incremental patching
public bool CanUseIncrementalPatching => IsValid && (AddedFiles.Count > 0 || ModifiedFiles.Count > 0 || DeletedFiles.Count > 0);
// True if there are any changes at all
public bool HasChanges => AddedFiles.Count > 0 || ModifiedFiles.Count > 0 || DeletedFiles.Count > 0;
}
private const int STATUS_UPDATE_INVERVAL_MS = 33;
private readonly IEnumerable<ModListGenerator.ModAddedByAssembly> modsAddedByAssemblies;
private readonly IBasicLogger logger;
public static void AddPostPatchCallback(ModuleManagerPostPatchCallback callback)
{
PostPatchLoader.AddPostPatchCallback(callback);
}
public MMPatchLoader(IEnumerable<ModListGenerator.ModAddedByAssembly> modsAddedByAssemblies, IBasicLogger logger)
{
this.modsAddedByAssemblies = modsAddedByAssemblies ?? throw new ArgumentNullException(nameof(modsAddedByAssemblies));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public IEnumerable<IProtoUrlConfig> Run()
{
Stopwatch patchSw = new Stopwatch();
patchSw.Start();
runReportLine = BuildRunReport("Pending", "Unknown", "Patches Applied: Unknown");
status = "Checking cache";
logger.Info(status);
CacheStatus cacheStatus = new CacheStatus();
try
{
cacheStatus = CheckCache();
status = "Cache check done: " + cacheStatus.Summary + " -> " + cacheStatus.NextAction;
logger.Info(status);
}
catch (Exception ex)
{
logger.Exception("Exception while checking cache", ex);
status = "Cache check failed -> full patch";
logger.Warning(status);
}
#if DEBUG
//cacheStatus.IsValid = false;
#endif
try
{
IEnumerable<IProtoUrlConfig> databaseConfigs = null;
if (!cacheStatus.IsValid)
{
status = "Cache invalid -> full patch";
logger.Info(status);
databaseConfigs = FullPatch();
}
else if (cacheStatus.CanUseIncrementalPatching)
{
status = "Cache valid with file changes -> incremental patch";
logger.Info(status);
databaseConfigs = IncrementalPatch(cacheStatus.AddedFiles, cacheStatus.ModifiedFiles, cacheStatus.DeletedFiles);
}
else
{
status = "Cache valid and unchanged -> loading cache";
logger.Info(status);
databaseConfigs = LoadCache();
if (File.Exists(patchLogPath))
{
logger.Info("Dumping patch log");
logger.Info("\n#### BEGIN PATCH LOG ####\n\n\n" + File.ReadAllText(patchLogPath) + "\n\n\n#### END PATCH LOG ####");
}
else
{
logger.Error("Patch log does not exist: " + patchLogPath);
}
}
if (KSP.Localization.Localizer.Instance != null)
KSP.Localization.Localizer.SwitchToLanguage(KSP.Localization.Localizer.CurrentLanguage);
logger.Info(status + "\n" + errors);
return databaseConfigs;
}
finally
{
// Keep a one-line report in status so it is visible in-game at run end.
status = runReportLine;
logger.Info(runReportLine);
patchSw.Stop();
logger.Info("Ran in " + ((float)patchSw.ElapsedMilliseconds / 1000).ToString("F3") + "s");
}
}
private static string BuildRunReport(string outcome, string scenario, params string[] fields)
{
StringBuilder builder = new StringBuilder("Execution Report");
builder.Append(" | Outcome: ").Append(outcome);
builder.Append(" | Scenario: ").Append(scenario);
if (fields != null)
{
foreach (string field in fields)
{
if (!string.IsNullOrWhiteSpace(field))
{
builder.Append(" | ").Append(field);
}
}
}
return builder.ToString();
}
private static string GetOutcomeLabel(int errors, int exceptions)
{
return (errors > 0 || exceptions > 0) ? "Completed with Errors" : "Success";
}
private IEnumerable<IProtoUrlConfig> FullPatch()
{
if (!Directory.Exists(logsDirPath)) Directory.CreateDirectory(logsDirPath);
MessageQueue<ILogMessage> patchLogQueue = new MessageQueue<ILogMessage>();
QueueLogRunner logRunner = new QueueLogRunner(patchLogQueue);
ITaskStatus loggingThreadStatus = BackgroundTask.Start(delegate
{
using StreamLogger streamLogger = new StreamLogger(new FileStream(patchLogPath, FileMode.Create));
streamLogger.Info("Log started at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
logRunner.Run(streamLogger);
streamLogger.Info("Done!");
});
IBasicLogger patchLogger = new LogSplitter(logger, new QueueLogger(patchLogQueue));
IPatchProgress progress = new PatchProgress(patchLogger);
status = "Full patch: init";
patchLogger.Info(status);
string[] mods = ModListGenerator.GenerateModList(modsAddedByAssemblies, progress, patchLogger).ToArray();
status = "Full patch: mod list has " + mods.Length + " mod" + (mods.Length != 1 ? "s" : "");
patchLogger.Info(status);
// If we don't use the cache then it is best to clean the PartDatabase.cfg
if (!keepPartDB && File.Exists(partDatabasePath))
File.Delete(partDatabasePath);
LoadPhysicsConfig();
#region Sorting Patches
status = "Full patch: extracting patches from loaded configs";
patchLogger.Info(status);
UrlDir gameData = GameDatabase.Instance.root.children.First(dir => dir.type == UrlDir.DirectoryType.GameData && dir.name == "");
INeedsChecker needsChecker = new NeedsChecker(mods, gameData, progress, patchLogger);
ITagListParser tagListParser = new TagListParser(progress);
IProtoPatchBuilder protoPatchBuilder = new ProtoPatchBuilder(progress);
IPatchCompiler patchCompiler = new PatchCompiler();
PatchExtractor extractor = new PatchExtractor(progress, patchLogger, needsChecker, tagListParser, protoPatchBuilder, patchCompiler);
// Have to convert to an array because we will be removing patches
var allConfigs = GameDatabase.Instance.root.AllConfigs.ToArray();
int totalConfigCount = allConfigs.Length;
int extractionProgressStep = Math.Max(1, totalConfigCount / 20);
List<IPatch> extractedPatches = new List<IPatch>(totalConfigCount);
for (int i = 0; i < totalConfigCount; i++)
{
IPatch patch = extractor.ExtractPatch(allConfigs[i]);
if (patch != null)
extractedPatches.Add(patch);
if (i == totalConfigCount - 1 || i % extractionProgressStep == 0)
{
status = "Full patch: extracting patches " + (i + 1) + "/" + totalConfigCount + " (found " + extractedPatches.Count + ")";
}
}
status = "Full patch: extracted " + extractedPatches.Count + " patch" + (extractedPatches.Count != 1 ? "es" : "") + " from " + totalConfigCount + " config" + (totalConfigCount != 1 ? "s" : "");
patchLogger.Info(status);
PatchList patchList = new PatchList(mods, extractedPatches, progress);
#endregion
#region Applying patches
status = "Full patch: applying patches";
patchLogger.Info(status);
IPass currentPass = null;
progress.OnPassStarted.Add(delegate (IPass pass)
{
currentPass = pass;
StatusUpdate(progress, currentPass.Name);
});
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
progress.OnPatchApplied.Add(delegate
{
long timeRemaining = STATUS_UPDATE_INVERVAL_MS - stopwatch.ElapsedMilliseconds;
if (timeRemaining < 0)
{
StatusUpdate(progress, currentPass.Name);
stopwatch.Reset();
stopwatch.Start();
}
});
PatchApplier applier = new PatchApplier(progress, patchLogger);
IEnumerable<IProtoUrlConfig> databaseConfigs = applier.ApplyPatches(patchList);
stopwatch.Stop();
StatusUpdate(progress);
patchLogger.Info("Done patching");
#endregion Applying patches
#region Saving Cache
foreach (KeyValuePair<string, int> item in progress.Counter.warningFiles)
{
patchLogger.Warning(item.Value + " warning" + (item.Value > 1 ? "s" : "") + " related to GameData/" + item.Key);
}
if (progress.Counter.errors > 0 || progress.Counter.exceptions > 0)
{
foreach (KeyValuePair<string, int> item in progress.Counter.errorFiles)
{
errors += item.Value + " error" + (item.Value > 1 ? "s" : "") + " related to GameData/" + item.Key
+ "\n";
}
patchLogger.Warning("Patch errors prevent cache creation");
status = "Full patch done with errors: cache write skipped";
try
{
if (File.Exists(cachePath))
File.Delete(cachePath);
if (File.Exists(shaPath))
File.Delete(shaPath);
}
catch (Exception e)
{
patchLogger.Exception("Exception while deleting stale cache", e);
}
}
else
{
status = "Full patch: writing cache";
patchLogger.Info(status);
CreateCache(databaseConfigs, progress.Counter.patchedNodes, progress.Counter.patchedNodes);
}
StatusUpdate(progress);
#endregion Saving Cache
SaveModdedTechTree(databaseConfigs);
SaveModdedPhysics(databaseConfigs);
logRunner.RequestStop();
while (loggingThreadStatus.IsRunning)
{
System.Threading.Thread.Sleep(100);
}
if (loggingThreadStatus.IsExitedWithError)
{
logger.Error("The patching thread threw an exception");
throw loggingThreadStatus.Exception;
}
runReportLine = BuildRunReport(
GetOutcomeLabel(progress.Counter.errors, progress.Counter.exceptions),
"Full Patch",
"Patches Applied: " + progress.Counter.patchedNodes,
"Warnings: " + progress.Counter.warnings,
"Errors: " + progress.Counter.errors,
"Exceptions: " + progress.Counter.exceptions);
status = "Full patch done";
logger.Info(status);
return databaseConfigs;
}
private IEnumerable<IProtoUrlConfig> IncrementalPatch(List<string> addedFileUrls, List<string> modifiedFileUrls, List<string> deletedFileUrls)
{
status = $"Incremental patch: changes Added={addedFileUrls.Count}, Modified={modifiedFileUrls.Count}, Deleted={deletedFileUrls.Count}";
logger.Info(status);
if (!Directory.Exists(logsDirPath)) Directory.CreateDirectory(logsDirPath);
MessageQueue<ILogMessage> patchLogQueue = new MessageQueue<ILogMessage>();
QueueLogRunner logRunner = new QueueLogRunner(patchLogQueue);
ITaskStatus loggingThreadStatus = BackgroundTask.Start(delegate
{
using StreamLogger streamLogger = new StreamLogger(new FileStream(patchLogPath, FileMode.Append));
streamLogger.Info("\n\n=== Incremental patching started at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " ===");
streamLogger.Info($"Changed files: Added={addedFileUrls.Count}, Modified={modifiedFileUrls.Count}, Deleted={deletedFileUrls.Count}");
logRunner.Run(streamLogger);
streamLogger.Info("Done!");
});
IBasicLogger patchLogger = new LogSplitter(logger, new QueueLogger(patchLogQueue));
try
{
// Load cached database as baseline
status = "Incremental patch: loading cache baseline";
LinkedList<IProtoUrlConfig> databaseConfigs = new LinkedList<IProtoUrlConfig>(LoadCache());
patchLogger.Info(status + $" ({databaseConfigs.Count} cached entries loaded)");
// Remove cached entries from modified and deleted files
HashSet<string> affectedFiles = new HashSet<string>(modifiedFileUrls);
foreach (var deletedFile in deletedFileUrls)
affectedFiles.Add(deletedFile);
status = "Incremental patch: pruning affected cache entries";
patchLogger.Info($"Removing {affectedFiles.Count} cached entries from affected files");
int baselineConfigCount = databaseConfigs.Count;
var nodesToRemove = databaseConfigs.Where(config => affectedFiles.Contains(config.UrlFile.GetUrlWithExtension())).ToList();
foreach (var nodeToRemove in nodesToRemove)
{
var node = databaseConfigs.Find(nodeToRemove);
if (node != null)
databaseConfigs.Remove(node);
}
status = "Incremental patch: pruned " + nodesToRemove.Count + "/" + baselineConfigCount + " cache entries";
patchLogger.Info(status);
patchLogger.Info($"Cache baseline reduced from {baselineConfigCount} to {databaseConfigs.Count} entries");
// Now extract and apply patches from modified and new files
HashSet<string> filesToPatch = new HashSet<string>(addedFileUrls);
foreach (var modFile in modifiedFileUrls)
filesToPatch.Add(modFile);
status = $"Incremental patch: extracting patches from {filesToPatch.Count} changed file" + (filesToPatch.Count != 1 ? "s" : "");
patchLogger.Info(status);
IPatchProgress progress = new PatchProgress(patchLogger);
string[] mods = ModListGenerator.GenerateModList(modsAddedByAssemblies, progress, patchLogger).ToArray();
status = "Incremental patch: mod list has " + mods.Length + " mod" + (mods.Length != 1 ? "s" : "");
patchLogger.Info(status);
UrlDir gameData = GameDatabase.Instance.root.children.First(dir => dir.type == UrlDir.DirectoryType.GameData && dir.name == "");
INeedsChecker needsChecker = new NeedsChecker(mods, gameData, progress, patchLogger);
ITagListParser tagListParser = new TagListParser(progress);
IProtoPatchBuilder protoPatchBuilder = new ProtoPatchBuilder(progress);
IPatchCompiler patchCompiler = new PatchCompiler();
PatchExtractor extractor = new PatchExtractor(progress, patchLogger, needsChecker, tagListParser, protoPatchBuilder, patchCompiler);
// Extract patches only from changed files
patchLogger.Info($"Extracting patches from {filesToPatch.Count} changed files");
var changedConfigs = GameDatabase.Instance.root.AllConfigs
.Where(config => filesToPatch.Contains(config.parent.GetUrlWithExtension()))
.ToArray();
int changedConfigCount = changedConfigs.Length;
int changedExtractionProgressStep = Math.Max(1, changedConfigCount / 20);
List<IPatch> changedPatches = new List<IPatch>(changedConfigCount);
for (int i = 0; i < changedConfigCount; i++)
{
IPatch patch = extractor.ExtractPatch(changedConfigs[i]);
if (patch != null)
changedPatches.Add(patch);
if (i == changedConfigCount - 1 || i % changedExtractionProgressStep == 0)
{
status = "Incremental patch: extracting patches " + (i + 1) + "/" + changedConfigCount + " (found " + changedPatches.Count + ")";
}
}
status = "Incremental patch: extracted " + changedPatches.Count + " patch" + (changedPatches.Count != 1 ? "es" : "") + " from " + changedConfigCount + " changed config" + (changedConfigCount != 1 ? "s" : "");
patchLogger.Info(status);
PatchList patchList = new PatchList(mods, changedPatches, progress);
IPass currentPass = null;
progress.OnPassStarted.Add(delegate (IPass pass)
{
currentPass = pass;
StatusUpdate(progress, currentPass.Name);
});
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
progress.OnPatchApplied.Add(delegate
{
long timeRemaining = STATUS_UPDATE_INVERVAL_MS - stopwatch.ElapsedMilliseconds;
if (timeRemaining < 0)
{
StatusUpdate(progress, currentPass.Name);
stopwatch.Reset();
stopwatch.Start();
}
});
// Apply patches to the filtered database
status = "Incremental patch: applying " + changedPatches.Count + " patch" + (changedPatches.Count != 1 ? "es" : "") + " to " + databaseConfigs.Count + " cached config entr" + (databaseConfigs.Count == 1 ? "y" : "ies");
patchLogger.Info(status);
PatchApplier applier = new PatchApplier(progress, patchLogger);
IEnumerable<IProtoUrlConfig> patchedConfigs = applier.ApplyPatches(patchList, databaseConfigs);
stopwatch.Stop();
StatusUpdate(progress);
patchLogger.Info("Done incremental patching");
status = "Incremental patch: writing updated cache";
patchLogger.Info(status);
// Preserve the full-patch baseline in fullPatchedNodeCount so it stays
// consistent with what a fresh full patch would report.
CreateCache(patchedConfigs, progress.Counter.patchedNodes, lastCachedPatchedNodeCount);
// Save updated tech tree and physics
status = "Incremental patch: saving updated TechTree and Physics";
patchLogger.Info(status);
SaveModdedTechTree(patchedConfigs);
SaveModdedPhysics(patchedConfigs);
logRunner.RequestStop();
while (loggingThreadStatus.IsRunning)
{
System.Threading.Thread.Sleep(100);
}
if (loggingThreadStatus.IsExitedWithError)
{
logger.Error("The incremental patching thread threw an exception");
throw loggingThreadStatus.Exception;
}
runReportLine = BuildRunReport(
GetOutcomeLabel(progress.Counter.errors, progress.Counter.exceptions),
"Incremental Patch",
"Patches Applied: " + (lastCachedPatchedNodeCount >= 0 ? lastCachedPatchedNodeCount.ToString() : "Unknown"),
"This Run: " + progress.Counter.patchedNodes,
"Warnings: " + progress.Counter.warnings,
"Errors: " + progress.Counter.errors,
"Exceptions: " + progress.Counter.exceptions);
status = "Incremental patch done";
logger.Info(status);
return patchedConfigs;
}
catch (Exception ex)
{
logger.Exception("Exception during incremental patching, falling back to full patch", ex);
status = "Incremental patch failed -> full patch";
logger.Warning(status);
runReportLine = BuildRunReport(
"Fallback to Full Patch",
"Incremental Patch",
"Patches Applied: Unknown",
"New Patches: Unknown",
"Warnings: Unknown",
"Errors: Unknown",
"Exceptions: 1");
// On error, fall back to full patch
return FullPatch();
}
}
private void LoadPhysicsConfig()
{
logger.Info("Loading Physics.cfg");
UrlDir gameDataDir = GameDatabase.Instance.root.AllDirectories.First(d => d.path.EndsWith("GameData") && d.name == "" && d.url == "");
// need to use a file with a cfg extension to get the right fileType or you can't AddConfig on it
UrlDir.UrlFile physicsUrlFile = new UrlDir.UrlFile(gameDataDir, new FileInfo(defaultPhysicsPath));
// Since it loaded the default config badly (sub node only) we clear it first
physicsUrlFile.configs.Clear();
// And reload it properly
ConfigNode physicsContent = ConfigNode.Load(defaultPhysicsPath);
physicsContent.name = PHYSICS_NODE_NAME;
physicsUrlFile.AddConfig(physicsContent);
gameDataDir.files.Add(physicsUrlFile);
}
private void SaveModdedPhysics(IEnumerable<IProtoUrlConfig> databaseConfigs)
{
IEnumerable<IProtoUrlConfig> configs = databaseConfigs.Where(config => config.NodeType == PHYSICS_NODE_NAME);
int count = configs.Count();
if (count == 0)
{
logger.Info($"No {PHYSICS_NODE_NAME} node found. No custom Physics config will be saved");
return;
}
if (count > 1)
{
logger.Info($"{count} {PHYSICS_NODE_NAME} nodes found. A patch may be wrong. Using the first one");
}
configs.First().Node.Save(physicsPath);
}
private CacheStatus CheckCache()
{
Stopwatch sw = new Stopwatch();
sw.Start();
using System.Security.Cryptography.SHA256 sha = System.Security.Cryptography.SHA256.Create();
using System.Security.Cryptography.SHA256 filesha = System.Security.Cryptography.SHA256.Create();
UrlDir.UrlFile[] files = GameDatabase.Instance.root.AllConfigFiles.ToArray();
int fileCount = files.Length;
int fileHashProgressStep = Math.Max(1, fileCount / 20);
filesSha.Clear();
for (int i = 0; i < files.Length; i++)
{
string url = files[i].GetUrlWithExtension();
// Hash the file path so the checksum change if files are moved
byte[] pathBytes = Encoding.UTF8.GetBytes(url);
sha.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0);
// hash the file content
byte[] contentBytes = File.ReadAllBytes(files[i].fullPath);
sha.TransformBlock(contentBytes, 0, contentBytes.Length, contentBytes, 0);
filesha.ComputeHash(contentBytes);
if (!filesSha.ContainsKey(url))
{
filesSha.Add(url, BitConverter.ToString(filesha.Hash));
}
else
{
logger.Warning("Duplicate fileSha key. This should not append. The key is " + url);
}
if (i == files.Length - 1 || i % fileHashProgressStep == 0)
{
status = "Checking cache: hashing config files " + (i + 1) + "/" + fileCount;
}
}
// Hash the mods dll path so the checksum change if dlls are moved or removed (impact NEEDS)
AssemblyLoader.LoadedAssembly[] loadedAssemblies = AssemblyLoader.loadedAssemblies.ToArray();
int loadedAssemblyCount = loadedAssemblies.Length;
int assemblyProgressStep = Math.Max(1, loadedAssemblyCount / 10);
for (int i = 0; i < loadedAssemblyCount; i++)
{
AssemblyLoader.LoadedAssembly dll = loadedAssemblies[i];
string path = dll.url + "/" + dll.name;
byte[] pathBytes = Encoding.UTF8.GetBytes(path);
sha.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0);
if (i == loadedAssemblyCount - 1 || i % assemblyProgressStep == 0)
{
status = "Checking cache: hashing assemblies " + (i + 1) + "/" + loadedAssemblyCount;
}
}
ModListGenerator.ModAddedByAssembly[] injectedMods = modsAddedByAssemblies.ToArray();
int injectedModCount = injectedMods.Length;
int injectedModProgressStep = Math.Max(1, injectedModCount / 10);
for (int i = 0; i < injectedModCount; i++)
{
ModListGenerator.ModAddedByAssembly mod = injectedMods[i];
byte[] modBytes = Encoding.UTF8.GetBytes(mod.modName);
sha.TransformBlock(modBytes, 0, modBytes.Length, modBytes, 0);
if (i == injectedModCount - 1 || i % injectedModProgressStep == 0)
{
status = "Checking cache: hashing injected mods " + (i + 1) + "/" + injectedModCount;
}
}
byte[] godsFinalMessageToHisCreation = Encoding.UTF8.GetBytes("We apologize for the inconvenience.");
sha.TransformFinalBlock(godsFinalMessageToHisCreation, 0, godsFinalMessageToHisCreation.Length);
configSha = BitConverter.ToString(sha.Hash);
sha.Clear();
filesha.Clear();
sw.Stop();
logger.Info("SHA generated in " + ((float)sw.ElapsedMilliseconds / 1000).ToString("F3") + "s");
logger.Info(" SHA = " + configSha);
CacheStatus cacheStatus = new CacheStatus();
cacheStatus.Summary = "metadata not found";
cacheStatus.NextAction = "full patch";
if (File.Exists(shaPath))
{
ConfigNode shaConfigNode = ConfigNode.Load(shaPath);
if (shaConfigNode != null && shaConfigNode.HasValue("SHA") && shaConfigNode.HasValue("version") && shaConfigNode.HasValue("KSPVersion"))
{
string storedSHA = shaConfigNode.GetValue("SHA");
string version = shaConfigNode.GetValue("version");
string kspVersion = shaConfigNode.GetValue("KSPVersion");
ConfigNode filesShaNode = shaConfigNode.GetNode("FilesSHA");
if (filesShaNode == null)
{
cacheStatus.IsValid = false;
cacheStatus.Summary = "metadata missing FilesSHA";
cacheStatus.NextAction = "full patch";
logger.Info("Cache rejected: FilesSHA node missing");
return cacheStatus;
}
CheckFilesChangeStatus filesStatus = CheckFilesChange(files, filesShaNode);
cacheStatus.AddedFiles = filesStatus.AddedFiles;
cacheStatus.ModifiedFiles = filesStatus.ModifiedFiles;
cacheStatus.DeletedFiles = filesStatus.DeletedFiles;
bool versionCompatible = version.Equals(Assembly.GetExecutingAssembly().GetName().Version.ToString());
bool kspCompatible = kspVersion.Equals(Versioning.version_major + "." + Versioning.version_minor + "." + Versioning.Revision + "." + Versioning.BuildID);
bool cacheFilesPresent = File.Exists(cachePath) && File.Exists(physicsPath) && File.Exists(techTreePath);
bool shaMatches = storedSHA.Equals(configSha);
// If files changed we can still use cache as a baseline for incremental patching.
cacheStatus.IsValid = versionCompatible && kspCompatible && cacheFilesPresent && (cacheStatus.HasChanges || shaMatches);
logger.Info("Cache SHA = " + storedSHA);
logger.Info("Cache valid = " + cacheStatus.IsValid);
logger.Info("Incremental patching available = " + cacheStatus.CanUseIncrementalPatching);
if (!versionCompatible)
logger.Info("Cache rejected: ModuleManager version mismatch");
if (!kspCompatible)
logger.Info("Cache rejected: KSP version mismatch");
if (!cacheFilesPresent)
{
List<string> missingFiles = new List<string>();
if (!File.Exists(cachePath)) missingFiles.Add(cachePath);
if (!File.Exists(physicsPath)) missingFiles.Add(physicsPath);
if (!File.Exists(techTreePath)) missingFiles.Add(techTreePath);
logger.Info("Cache rejected: required cache files missing: " + string.Join(", ", missingFiles));
}
if (!cacheStatus.HasChanges && !shaMatches)
logger.Info("Cache rejected: checksum changed without a file-level change set");
if (cacheStatus.HasChanges)
{
logger.Info($"Detected file changes: Added={cacheStatus.AddedFiles.Count}, Modified={cacheStatus.ModifiedFiles.Count}, Deleted={cacheStatus.DeletedFiles.Count}");
}
if (!cacheStatus.IsValid)
{
cacheStatus.Summary = "failed compatibility/integrity checks";
cacheStatus.NextAction = "full patch";
}
else if (cacheStatus.CanUseIncrementalPatching)
{
cacheStatus.Summary = $"valid; changes Added={cacheStatus.AddedFiles.Count}, Modified={cacheStatus.ModifiedFiles.Count}, Deleted={cacheStatus.DeletedFiles.Count}";
cacheStatus.NextAction = "incremental patch";
}
else
{
cacheStatus.Summary = "valid; no config file changes";
cacheStatus.NextAction = "load cache";
}
}
else
{
cacheStatus.IsValid = false;
cacheStatus.Summary = "metadata malformed/incomplete";
cacheStatus.NextAction = "full patch";
logger.Info("Cache rejected: SHA metadata file is malformed or missing required values");
}
}
return cacheStatus;
}
private class CheckFilesChangeStatus
{
public List<string> AddedFiles { get; set; } = new List<string>();
public List<string> ModifiedFiles { get; set; } = new List<string>();
public List<string> DeletedFiles { get; set; } = new List<string>();
}
private CheckFilesChangeStatus CheckFilesChange(UrlDir.UrlFile[] files, ConfigNode shaConfigNode)
{
CheckFilesChangeStatus result = new CheckFilesChangeStatus();
StringBuilder changes = new StringBuilder();
int fileCount = files.Length;
int progressStep = Math.Max(1, fileCount / 20);
changes.Append("Detected file changes:\n");
// Check for modified files
for (int i = 0; i < files.Length; i++)
{
string url = files[i].GetUrlWithExtension();
ConfigNode fileNode = GetFileNode(shaConfigNode, url);
string fileSha = fileNode?.GetValue("SHA");
if (fileNode == null)
continue;
if (fileSha == null || filesSha[url] != fileSha)
{
changes.Append("Modified: " + fileNode.GetValue("filename") + "\n");
result.ModifiedFiles.Add(url);
}
if (i == files.Length - 1 || i % progressStep == 0)
{
status = "Checking cache: scanning modified files " + (i + 1) + "/" + fileCount;
}
}
// Check for added and remove tracked files
for (int i = 0; i < files.Length; i++)
{
string url = files[i].GetUrlWithExtension();
ConfigNode fileNode = GetFileNode(shaConfigNode, url);
if (fileNode == null)
{
changes.Append("Added : " + url + "\n");
result.AddedFiles.Add(url);
}
else
{
shaConfigNode.RemoveNode(fileNode);
}
if (i == files.Length - 1 || i % progressStep == 0)
{
status = "Checking cache: scanning added files " + (i + 1) + "/" + fileCount;
}
}
// Remaining nodes are deleted files
foreach (ConfigNode fileNode in shaConfigNode.GetNodes())
{
string deletedUrl = fileNode.GetValue("filename");
changes.Append("Deleted : " + deletedUrl + "\n");
result.DeletedFiles.Add(deletedUrl);
}
status = "Checking cache: finalizing change set";
if (result.AddedFiles.Count > 0 || result.ModifiedFiles.Count > 0 || result.DeletedFiles.Count > 0)
logger.Info(changes.ToString());
return result;
}
private ConfigNode GetFileNode(ConfigNode shaConfigNode, string filename)
{
for (int i = 0; i < shaConfigNode.nodes.Count; i++)
{
ConfigNode file = shaConfigNode.nodes[i];
if (file.name == "FILE" && file.GetValue("filename") == filename)
return file;
}
return null;
}
private void SnapshotPreUpdateCacheFiles()
{
try
{
if (File.Exists(cachePath))
{
File.Copy(cachePath, preUpdateCachePath, true);
logger.Info("Saved pre-update cache snapshot to " + preUpdateCachePath);
}
if (File.Exists(shaPath))
{
File.Copy(shaPath, preUpdateShaPath, true);
logger.Info("Saved pre-update SHA snapshot to " + preUpdateShaPath);
}
}
catch (Exception e)
{
logger.Exception("Exception while creating pre-update cache snapshot", e);
}
}
private void CreateCache(IEnumerable<IProtoUrlConfig> databaseConfigs, int patchedNodeCount, int fullPatchedNodeCount = -1)
{
ConfigNode shaConfigNode = new ConfigNode();
shaConfigNode.AddValue("SHA", configSha);
shaConfigNode.AddValue("version", Assembly.GetExecutingAssembly().GetName().Version.ToString());
shaConfigNode.AddValue("KSPVersion", Versioning.version_major + "." + Versioning.version_minor + "." + Versioning.Revision + "." + Versioning.BuildID);
ConfigNode filesSHANode = shaConfigNode.AddNode("FilesSHA");
ConfigNode cache = new ConfigNode();
cache.AddValue("patchedNodeCount", patchedNodeCount.ToString());
if (fullPatchedNodeCount >= 0)
cache.AddValue("fullPatchedNodeCount", fullPatchedNodeCount.ToString());
IProtoUrlConfig[] configs = databaseConfigs as IProtoUrlConfig[] ?? databaseConfigs.ToArray();
int totalConfigCount = configs.Length;
int progressUpdateStep = Math.Max(1, totalConfigCount / 20);
for (int i = 0; i < totalConfigCount; i++)
{
IProtoUrlConfig urlConfig = configs[i];
ConfigNode node = cache.AddNode("UrlConfig");
node.AddValue("parentUrl", urlConfig.UrlFile.GetUrlWithExtension());
ConfigNode urlNode = urlConfig.Node.DeepCopy();
urlNode.EscapeValuesRecursive();
node.AddNode(urlNode);
if (i == totalConfigCount - 1 || i % progressUpdateStep == 0)
{
status = "ModuleManager: writing cache entries " + (i + 1) + "/" + totalConfigCount;
}
}
foreach (var file in GameDatabase.Instance.root.AllConfigFiles)
{
string url = file.GetUrlWithExtension();
// "/Physics" is the node we created manually to loads the PHYSIC config
if (file.url != "/Physics" && filesSha.ContainsKey(url))
{
ConfigNode shaNode = filesSHANode.AddNode("FILE");
shaNode.AddValue("filename", url);
shaNode.AddValue("SHA", filesSha[url]);
filesSha.Remove(url);
}
}
status = "ModuleManager: saving cache files";
logger.Info("Saving cache");
SnapshotPreUpdateCacheFiles();
try
{
shaConfigNode.Save(shaPath);
}
catch (Exception e)
{
logger.Exception("Exception while saving the SHA", e);
}
try
{
cache.Save(cachePath);
return;
}
catch (NullReferenceException e)
{
logger.Exception("NullReferenceException while saving the cache", e);
}
catch (Exception e)
{
logger.Exception("Exception while saving the cache", e);
}
try
{
logger.Error("An error occurred while creating the cache. Deleting cache files to avoid keeping a bad cache");
if (File.Exists(cachePath))
File.Delete(cachePath);
if (File.Exists(shaPath))
File.Delete(shaPath);
}
catch (Exception e)
{
logger.Exception("Exception while deleting the cache", e);
}
}
private void SaveModdedTechTree(IEnumerable<IProtoUrlConfig> databaseConfigs)
{
IEnumerable<IProtoUrlConfig> configs = databaseConfigs.Where(config => config.NodeType == TECH_TREE_NODE_NAME);
int count = configs.Count();
if (count == 0)
{
logger.Info($"No {TECH_TREE_NODE_NAME} node found. No custom {TECH_TREE_NODE_NAME} will be saved");
return;
}
if (count > 1)
{
logger.Info($"{count} {TECH_TREE_NODE_NAME} nodes found. A patch may be wrong. Using the first one");
}
ConfigNode techNode = new ConfigNode(TECH_TREE_NODE_NAME);
techNode.AddNode(configs.First().Node);
techNode.Save(techTreePath);
}
private IEnumerable<IProtoUrlConfig> LoadCache()
{
ConfigNode cache = ConfigNode.Load(cachePath);
int cachedPatchedNodeCount = -1;
if (cache.HasValue("patchedNodeCount"))
int.TryParse(cache.GetValue("patchedNodeCount"), out cachedPatchedNodeCount);
// fullPatchedNodeCount is written only by FullPatch and preserved by IncrementalPatch.
// Prefer it over patchedNodeCount, which IncrementalPatch may have set to a small incremental value.
int fullPatchedNodeCount = -1;
if (cache.HasValue("fullPatchedNodeCount"))
int.TryParse(cache.GetValue("fullPatchedNodeCount"), out fullPatchedNodeCount);
lastCachedPatchedNodeCount = fullPatchedNodeCount >= 0 ? fullPatchedNodeCount : cachedPatchedNodeCount;
// Create the fake file where we load the physic config cache
UrlDir gameDataDir = GameDatabase.Instance.root.AllDirectories.First(d => d.path.EndsWith("GameData") && d.name == "" && d.url == "");
// need to use a file with a cfg extension to get the right fileType or you can't AddConfig on it
UrlDir.UrlFile physicsUrlFile = new UrlDir.UrlFile(gameDataDir, new FileInfo(defaultPhysicsPath));
gameDataDir.files.Add(physicsUrlFile);
List<IProtoUrlConfig> databaseConfigs = new List<IProtoUrlConfig>(cache.nodes.Count);
int cacheNodeCount = cache.nodes.Count;
int cacheLoadProgressStep = Math.Max(1, cacheNodeCount / 20);
for (int i = 0; i < cache.nodes.Count; i++)
{
ConfigNode node = cache.nodes[i];
string parentUrl = node.GetValue("parentUrl");
UrlDir.UrlFile parent = gameDataDir.Find(parentUrl);
if (parent != null)
{
node.nodes[0].UnescapeValuesRecursive();
databaseConfigs.Add(new ProtoUrlConfig(parent, node.nodes[0]));
}
else
{
logger.Warning("Skipping cache entry with missing parent: " + parentUrl);
}
if (i == cache.nodes.Count - 1 || i % cacheLoadProgressStep == 0)
{
status = "Loading from cache: rebuilding config database " + (i + 1) + "/" + cacheNodeCount;
}
}