This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathModHandler.cs
1055 lines (945 loc) · 41.8 KB
/
ModHandler.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
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 AstroModIntegrator;
using Microsoft.Win32;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace AstroModLoader
{
public class ModHandler
{
internal string CustomBasePath = "";
internal string BasePath;
internal string DownloadPath;
internal string InstallPath;
internal string GamePath;
internal string LaunchCommand;
internal Dictionary<string, Mod> ModLookup;
internal string BinaryFilePath;
internal Form1 BaseForm;
public PlatformType Platform = PlatformType.Unknown;
public Dictionary<PlatformType, string> ValidPlatformTypesToPaths;
public List<PlatformType> AllPlatforms;
public List<Mod> Mods;
public Dictionary<string, ModProfile> ProfileList;
public Dictionary<string, IndexMod> GlobalIndexFile;
public Version InstalledAstroBuild = null;
public volatile bool IsReadOnly = false;
public ModHandler(Form1 baseForm)
{
BaseForm = baseForm;
OurIntegrator = new ModIntegrator();
OurIntegrator.RefuseMismatchedConnections = true;
string automaticSteamPath = null;
string automaticWin10Path = null;
if (!Program.CommandLineOptions.ServerMode)
{
try
{
automaticSteamPath = AMLUtils.FixGamePath(CheckRegistryForSteamPath(361420)); // Astroneer: 361420
}
catch { }
try
{
automaticWin10Path = AMLUtils.FixGamePath(CheckRegistryForMicrosoftStorePath());
}
catch { }
}
//automaticSteamPath = null;
//automaticWin10Path = null;
ValidPlatformTypesToPaths = new Dictionary<PlatformType, string>();
if (automaticSteamPath != null) ValidPlatformTypesToPaths[PlatformType.Steam] = automaticSteamPath;
if (automaticWin10Path != null) ValidPlatformTypesToPaths[PlatformType.Win10] = automaticWin10Path;
SyncIndependentConfigFromDisk();
if (!string.IsNullOrEmpty(CustomBasePath))
{
string customGamePath = GetGamePathFromBasePath(CustomBasePath);
if (!string.IsNullOrEmpty(customGamePath)) ValidPlatformTypesToPaths[PlatformType.Custom] = customGamePath;
}
RefreshAllPlatformsList();
if (!ValidPlatformTypesToPaths.ContainsKey(Platform) && AllPlatforms.Count > 0) Platform = AllPlatforms[0];
if (Program.CommandLineOptions.ServerMode) Platform = PlatformType.Server;
DeterminePaths();
SyncModsFromDisk();
SyncDependentConfigFromDisk();
VerifyGamePath();
if (Program.CommandLineOptions.ServerMode && Directory.Exists(Path.Combine(BasePath, "Saved")))
{
GamePath = Path.GetFullPath(Path.Combine(BasePath, ".."));
}
if (GamePath == null || !Directory.Exists(GamePath))
{
GamePath = null;
if (ValidPlatformTypesToPaths.ContainsKey(Platform))
{
GamePath = ValidPlatformTypesToPaths[Platform];
}
else
{
TextPrompt initialPathPrompt = new TextPrompt
{
StartPosition = FormStartPosition.CenterScreen,
DisplayText = "Select your game installation directory",
VerifyMode = VerifyPathMode.Game
};
if (initialPathPrompt.ShowDialog(BaseForm) == DialogResult.OK)
{
GamePath = initialPathPrompt.OutputText;
Platform = PlatformType.Custom;
ValidPlatformTypesToPaths[PlatformType.Custom] = GamePath;
RefreshAllPlatformsList();
}
else
{
Environment.Exit(0);
}
}
}
ApplyGamePathDerivatives();
VerifyIntegrity();
foreach (Mod mod in Mods)
{
mod.Dirty = true;
}
FullUpdateSynchronous();
SortMods();
RefreshAllPriorites();
SyncConfigToDisk();
}
public void VerifyGamePath()
{
bool doesValidPlatformTypesToPathsContainValue = false;
foreach (KeyValuePair<PlatformType, string> entry in ValidPlatformTypesToPaths)
{
if (entry.Value.PathEquals(GamePath))
{
doesValidPlatformTypesToPathsContainValue = true;
break;
}
}
if (GamePath != null && (!doesValidPlatformTypesToPathsContainValue || !Directory.Exists(GamePath)))
{
// Here, a game path is being provided that we can't recognize. If the set platform is Win10, that probably means there is a new update and we should just discard the one already stored; otherwise, it is custom
if (Platform == PlatformType.Win10 && ValidPlatformTypesToPaths.ContainsKey(PlatformType.Win10))
{
GamePath = ValidPlatformTypesToPaths[PlatformType.Win10];
}
else if (Directory.Exists(GamePath))
{
Platform = PlatformType.Custom;
ValidPlatformTypesToPaths[PlatformType.Custom] = GamePath;
RefreshAllPlatformsList();
}
}
}
public void RefreshAllPlatformsList()
{
AllPlatforms = ValidPlatformTypesToPaths.Keys.OrderBy(x => (int)x).ToList();
}
public string MicrosoftRuntimeID = "";
private string CheckRegistryForMicrosoftStorePath()
{
RegistryKey key1 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(@"Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\Repository\Packages"); // SystemEraSoftworks.29415440E1269_1.16.70.0_x64__ftk5pbg2rayv2
if (key1 == null) return null;
RegistryKey goalKey = null;
foreach (string k in key1.GetSubKeyNames())
{
if (k.StartsWith("SystemEraSoftworks"))
{
goalKey = key1.OpenSubKey(k);
break;
}
}
key1.Close();
if (goalKey == null) return null;
string packageID = goalKey.GetValue("PackageID") as string;
string rootFolder = goalKey.GetValue("PackageRootFolder") as string;
goalKey.Close();
if (!string.IsNullOrEmpty(packageID))
{
string[] idBits = packageID.Split('_');
if (idBits.Length >= 2) MicrosoftRuntimeID = idBits[0] + "_" + idBits[idBits.Length - 1];
}
return rootFolder;
}
private static Regex acfEntryReader = new Regex(@"\s+""(\w+)""\s+""([\w ]+)""", RegexOptions.Compiled);
private static Regex vdfEntryReader = new Regex(@"^\s*""([^""]+)""\s*""([^""]+)""", RegexOptions.Compiled);
private string CheckRegistryForSteamPath(int appID)
{
string decidedSteamPath = null;
RegistryKey key1 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(@"SOFTWARE\Wow6432Node\Valve\Steam");
RegistryKey key2 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Valve\Steam");
if (key1 != null)
{
object o = key1.GetValue("SteamPath");
if (o != null) decidedSteamPath = o as string;
}
else if (key2 != null)
{
object o = key2.GetValue("SteamPath");
if (o != null) decidedSteamPath = o as string;
}
if (key1 != null) key1.Close();
if (key2 != null) key2.Close();
if (decidedSteamPath == null) return null;
List<string> potentialInstallDirs = new List<string>();
potentialInstallDirs.Add(decidedSteamPath);
try
{
using (StreamReader f = new StreamReader(Path.Combine(decidedSteamPath, "steamapps", "libraryfolders.vdf")))
{
string vdfEntry = null;
while ((vdfEntry = f.ReadLine()) != null)
{
Match m = vdfEntryReader.Match(vdfEntry);
if (m.Groups.Count == 3)
{
potentialInstallDirs.Add(m.Groups[2].Value.Replace(@"\\", @"\").Replace("\"", ""));
}
}
}
}
catch (IOException) { }
try
{
using (StreamReader f = new StreamReader(Path.Combine(decidedSteamPath, "config", "config.vdf")))
{
string vdfEntry = null;
while ((vdfEntry = f.ReadLine()) != null)
{
Match m = vdfEntryReader.Match(vdfEntry);
if (m.Groups.Count == 3 && m.Groups[1].Value.StartsWith("BaseInstallFolder_"))
{
potentialInstallDirs.Add(m.Groups[2].Value.Replace(@"\\", @"\").Replace("\"", ""));
}
}
}
}
catch (IOException) { }
List<string> astroInstallDirs = new List<string>();
foreach (string installPath in potentialInstallDirs)
{
string tempAstroInstallDir = null;
if ((tempAstroInstallDir = CheckSteamPathForGame(appID, installPath)) != null)
{
astroInstallDirs.Add(tempAstroInstallDir);
}
}
if (astroInstallDirs.Count > 0)
{
return astroInstallDirs[0];
}
return null;
}
private string CheckSteamPathForGame(int appID, string steamPath)
{
try
{
if (!Directory.Exists(steamPath)) return null;
using (StreamReader f = new StreamReader(Path.Combine(steamPath, "steamapps", "appmanifest_" + appID + ".acf")))
{
string acfEntry = null;
while ((acfEntry = f.ReadLine()) != null)
{
Match m = acfEntryReader.Match(acfEntry);
if (m.Groups.Count == 3 && m.Groups[1].Value == "installdir")
{
string astroInstallDir = m.Groups[2].Value;
if (!string.IsNullOrEmpty(astroInstallDir))
{
string decidedAnswer = Path.Combine(steamPath, "steamapps", "common", astroInstallDir);
if (!Directory.Exists(decidedAnswer)) return null;
return decidedAnswer;
}
}
}
}
}
catch (IOException)
{
return null;
}
return null;
}
private static Regex AstroBuildRegex = new Regex(@"^(\d+\.\d+\.\d+\.\d) \w+");
public void ApplyGamePathDerivatives()
{
if (GamePath == null) return;
// BinaryFilePath
BinaryFilePath = null;
try
{
string[] allExes = Directory.GetFiles(Path.Combine(GamePath, "Astro", "Binaries"), "*.exe", SearchOption.AllDirectories);
if (allExes.Length > 0) BinaryFilePath = allExes[0];
}
catch (Exception ex)
{
if (ex is IOException)
{
BinaryFilePath = null;
}
else if (ex is UnauthorizedAccessException)
{
MessageBox.Show("Unable to access an important directory! Please make sure the current user has read access for the folder \"" + GamePath + "\".", "Uh oh!");
Environment.Exit(0);
}
else
{
throw;
}
}
// Get astroneer version
SetInstalledAstroBuild();
}
private void SetInstalledAstroBuild()
{
InstalledAstroBuild = null;
// Steam
try
{
string buildVersionPath = Path.Combine(GamePath, "build.version");
if (File.Exists(buildVersionPath))
{
using (StreamReader f = new StreamReader(buildVersionPath))
{
string fullBuildData = f.ReadToEnd();
Match m = AstroBuildRegex.Match(fullBuildData);
if (m.Groups.Count == 2) InstalledAstroBuild = new Version(m.Groups[1].Value);
}
}
}
catch
{
InstalledAstroBuild = null;
}
if (InstalledAstroBuild != null) return;
// Win10
try
{
string buildVersionPath = Path.Combine(GamePath, "AppxManifest.xml");
if (File.Exists(buildVersionPath))
{
XmlDocument doc = new XmlDocument();
doc.Load(buildVersionPath);
if (doc != null)
{
XmlNodeList identityList = doc.GetElementsByTagName("Identity");
if (identityList != null && identityList.Count > 0)
{
var verAttr = identityList[0].Attributes["Version"];
if (verAttr != null) Version.TryParse(verAttr.Value, out InstalledAstroBuild);
}
}
}
}
catch
{
InstalledAstroBuild = null;
}
}
public void DeterminePaths()
{
string normalSteamBasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Astro");
string normalMicrosoftStoreBasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Packages", "SystemEraSoftworks.29415440E1269_ftk5pbg2rayv2", "LocalState", "Astro");
BasePath = null;
if (!string.IsNullOrEmpty(Program.CommandLineOptions.LocalDataPath))
{
BasePath = AMLUtils.FixBasePath(Path.GetFullPath(Path.Combine(Program.CommandLineOptions.LocalDataPath, "Astro")));
}
else
{
if (Program.CommandLineOptions.ServerMode)
{
BasePath = Path.Combine(GamePath != null ? GamePath : Directory.GetCurrentDirectory(), "Astro");
}
else if (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) != null)
{
switch(Platform)
{
case PlatformType.Steam:
BasePath = normalSteamBasePath;
break;
case PlatformType.Win10:
BasePath = normalMicrosoftStoreBasePath;
break;
}
}
}
if (BasePath == null || !Directory.Exists(BasePath))
{
if (Platform == PlatformType.Custom || Platform == PlatformType.Unknown)
{
if (!string.IsNullOrEmpty(CustomBasePath))
{
BasePath = CustomBasePath;
}
else
{
// If the regular Steam or Microsoft Store base paths do exist, they're probably what the user actually wants, but we still want to give them the option to change it here so we just put it in as prefilled text
TextPrompt initialPathPrompt = new TextPrompt
{
StartPosition = FormStartPosition.CenterScreen,
DisplayText = "Select your local application data directory",
PrefilledText = Directory.Exists(normalSteamBasePath) ? normalSteamBasePath : (Directory.Exists(normalMicrosoftStoreBasePath) ? normalMicrosoftStoreBasePath : null),
VerifyMode = VerifyPathMode.Base
};
if (initialPathPrompt.ShowDialog(BaseForm) == DialogResult.OK)
{
CustomBasePath = initialPathPrompt.OutputText;
BasePath = CustomBasePath;
}
else
{
Environment.Exit(0);
}
}
}
else
{
MessageBox.Show("Unable to find the local application data directory. If you have never created an Astroneer save file within the game on this computer before, please do so and then re-open AstroModLoader. Otherwise, please specify a local application data directory with the --data parameter.", "Uh oh!");
Environment.Exit(0);
}
}
DetermineBasePathDerivatives();
}
public void DetermineBasePathDerivatives()
{
try
{
DownloadPath = Path.Combine(BasePath, "Saved", "Mods");
Directory.CreateDirectory(DownloadPath);
InstallPath = Path.Combine(BasePath, "Saved", "Paks");
Directory.CreateDirectory(InstallPath);
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("Unable to access an important directory! Please make sure the current user has read access for the folder \"" + BasePath + "\".", "Uh oh!");
Environment.Exit(0);
}
}
internal Metadata ExtractMetadataFromPath(string modPath)
{
if (new Mod(null, Path.GetFileName(modPath)).Priority >= 999) return null;
try
{
using (FileStream f = new FileStream(modPath, FileMode.Open, FileAccess.Read))
{
return new PakExtractor(new BinaryReader(f)).ReadMetadata();
}
}
catch
{
return null;
}
}
public ManualResetEvent IsUpdatingAvailableVersionsFromIndexFilesWaitHandler = new ManualResetEvent(true);
public void UpdateAvailableVersionsFromIndexFiles()
{
IsUpdatingAvailableVersionsFromIndexFilesWaitHandler.Reset();
Dictionary<Mod, Version> switchVersionInstructions = new Dictionary<Mod, Version>();
foreach (Mod mod in Mods)
{
Version latestVersion = null;
if (mod.AvailableVersions.Count > 0) latestVersion = mod.AvailableVersions[0];
if (GlobalIndexFile.ContainsKey(mod.CurrentModData.ModID))
{
IndexMod indexMod = GlobalIndexFile[mod.CurrentModData.ModID];
mod.AvailableVersions.AddRange(indexMod.AllVersions.Keys.Except(mod.AvailableVersions));
mod.AvailableVersions.Sort();
mod.AvailableVersions.Reverse();
latestVersion = mod.AvailableVersions[0];
//if (indexMod.LatestVersion != null) latestVersion = indexMod.LatestVersion;
}
if (mod.ForceLatest && latestVersion != null) switchVersionInstructions.Add(mod, latestVersion);
}
AMLUtils.InvokeUI(BaseForm.TableManager.Refresh);
foreach (KeyValuePair<Mod, Version> entry in switchVersionInstructions)
{
BaseForm.SwitchVersionSync(entry.Key, entry.Value);
}
IsUpdatingAvailableVersionsFromIndexFilesWaitHandler.Set();
}
private List<string> DuplicateURLs = new List<string>();
public void ResetGlobalIndexFile()
{
GlobalIndexFile = new Dictionary<string, IndexMod>();
DuplicateURLs = new List<string>();
}
public void AggregateIndexFiles()
{
if (GlobalIndexFile == null) GlobalIndexFile = new Dictionary<string, IndexMod>();
foreach (Mod mod in Mods)
{
IndexFile thisIndexFile = mod.GetIndexFile(DuplicateURLs);
if (thisIndexFile != null)
{
thisIndexFile.Mods.ToList().ForEach(x => GlobalIndexFile[x.Key] = x.Value);
DuplicateURLs.Add(thisIndexFile.OriginalURL);
}
}
UpdateAvailableVersionsFromIndexFiles();
}
public void SortVersions()
{
foreach (Mod mod in Mods)
{
mod.AvailableVersions.Sort();
mod.AvailableVersions.Reverse();
}
}
private void SafeDelete(string targetPath)
{
try
{
File.Delete(targetPath);
}
catch { }
}
// It's counterproductive to try and combat piracy, but it's a good idea to have some visible marker of it to serve as an explanation for the problems people with pirated copies may face
public bool MismatchedSteamworksDLL = false;
private static readonly byte[] steamsworksDLLHash = new byte[] { 231, 116, 51, 9, 76, 86, 67, 54, 133, 166, 138, 68, 54, 232, 27, 118, 195, 181, 225, 245 };
public void VerifyIntegrity()
{
MismatchedSteamworksDLL = false;
try
{
string potentialDllDir = Path.Combine(GamePath, "Engine", "Binaries", "ThirdParty", "Steamworks");
if (Directory.Exists(potentialDllDir))
{
string[] dllPaths = Directory.GetFiles(potentialDllDir, "steam_api*.dll", SearchOption.AllDirectories);
if (dllPaths.Length > 0 && File.Exists(dllPaths[0]))
{
var data = File.ReadAllBytes(dllPaths[0]);
var hash = SHA1.Create().ComputeHash(data);
if (!hash.SequenceEqual(steamsworksDLLHash)) MismatchedSteamworksDLL = true;
}
}
}
catch
{
MismatchedSteamworksDLL = false;
}
}
public void EviscerateMod(Mod targetMod, List<Version> targetVersions = null)
{
string[] allNormalMods = Directory.GetFiles(DownloadPath, "*.pak", SearchOption.TopDirectoryOnly);
string[] installedMods = Directory.GetFiles(InstallPath, "*.pak", SearchOption.TopDirectoryOnly);
string[] allMods = new string[allNormalMods.Length + installedMods.Length];
Array.Copy(allNormalMods, allMods, allNormalMods.Length);
Array.Copy(installedMods, 0, allMods, allNormalMods.Length, installedMods.Length);
foreach (string modPath in allMods)
{
string modNameOnDisk = Path.GetFileName(modPath);
Mod newMod = new Mod(ExtractMetadataFromPath(modPath), modNameOnDisk);
if (newMod.CurrentModData.ModID == targetMod.CurrentModData.ModID && (targetVersions == null || targetVersions.Contains(newMod.InstalledVersion)))
{
SafeDelete(modPath);
}
}
for (int i = 0; i < Mods.Count; i++)
{
if (Mods[i].CurrentModData.ModID == targetMod.CurrentModData.ModID)
{
if (targetVersions != null)
{
foreach (Version targetVersion in targetVersions)
{
Mods[i].AllModData.Remove(targetVersion);
Mods[i].AvailableVersions.Remove(targetVersion);
}
if (Mods[i].AvailableVersions.Count > 0)
{
Mods[i].AvailableVersions.Sort();
Mods[i].AvailableVersions.Reverse();
Mods[i].InstalledVersion = Mods[i].AvailableVersions[0];
continue;
}
}
ModLookup.Remove(Mods[i].CurrentModData.ModID);
Mods.RemoveAt(i);
i--;
}
}
}
public void SyncSingleMod(Mod newMod)
{
if (ModLookup.ContainsKey(newMod.CurrentModData.ModID))
{
if (!ModLookup[newMod.CurrentModData.ModID].AvailableVersions.Contains(newMod.InstalledVersion)) ModLookup[newMod.CurrentModData.ModID].AvailableVersions.Add(newMod.InstalledVersion);
ModLookup[newMod.CurrentModData.ModID].AllModData[newMod.InstalledVersion] = newMod.CurrentModData;
}
else
{
Mods.Add(newMod);
ModLookup.Add(newMod.CurrentModData.ModID, newMod);
}
}
public Mod SyncSingleModFromDisk(string modPath, out bool wasClientOnly, bool updateSort = true)
{
Mod newMod = new Mod(ExtractMetadataFromPath(modPath), Path.GetFileName(modPath));
if (Program.CommandLineOptions.ServerMode && newMod.CurrentModData.Sync == SyncMode.ClientOnly)
{
wasClientOnly = true;
return null;
}
SyncSingleMod(newMod);
if (updateSort)
{
SortVersions();
SortMods();
}
wasClientOnly = false;
return newMod;
}
public void SyncModsFromDisk(bool skipIndexing = false)
{
if (!skipIndexing)
{
Mods = new List<Mod>();
string[] allMods = Directory.GetFiles(DownloadPath, "*_P.pak", SearchOption.TopDirectoryOnly);
ModLookup = new Dictionary<string, Mod>();
foreach (string modPath in allMods)
{
SyncSingleModFromDisk(modPath, out _, false);
}
}
SortVersions();
foreach (Mod mod in Mods)
{
mod.InstalledVersion = mod.AvailableVersions[0];
}
string[] installedMods = Directory.GetFiles(InstallPath, "*_P.pak", SearchOption.TopDirectoryOnly);
foreach (string modPath in installedMods)
{
var modNameOnDisk = Path.GetFileName(modPath);
var m = new Mod(ExtractMetadataFromPath(modPath), modNameOnDisk);
if (ModLookup.ContainsKey(m.CurrentModData.ModID))
{
ModLookup[m.CurrentModData.ModID].Enabled = true;
ModLookup[m.CurrentModData.ModID].NameOnDisk = modNameOnDisk;
ModLookup[m.CurrentModData.ModID].InstalledVersion = m.InstalledVersion;
ModLookup[m.CurrentModData.ModID].Priority = m.Priority;
if (!ModLookup[m.CurrentModData.ModID].AvailableVersions.Contains(m.InstalledVersion))
{
File.Copy(modPath, Path.Combine(DownloadPath, modNameOnDisk));
ModLookup[m.CurrentModData.ModID].AvailableVersions.Add(m.InstalledVersion);
ModLookup[m.CurrentModData.ModID].AllModData.Add(m.InstalledVersion, m.CurrentModData);
}
}
else
{
//if (Program.CommandLineOptions.ServerMode && m.CurrentModData.Sync == SyncMode.ClientOnly) continue;
if (m.Priority < 999)
{
File.Copy(modPath, Path.Combine(DownloadPath, modNameOnDisk), true);
m.Enabled = true;
Mods.Add(m);
ModLookup.Add(m.CurrentModData.ModID, m);
}
}
}
SortMods();
}
public void SortMods()
{
Mods = new List<Mod>(Mods.OrderBy(o => o.Priority).ToList());
}
public string GetPathOnDisk(Mod mod, string modID = null)
{
if (string.IsNullOrEmpty(modID)) modID = mod.CurrentModData.ModID;
string[] allMods = Directory.GetFiles(DownloadPath, "*.pak", SearchOption.TopDirectoryOnly);
foreach (string modPath in allMods)
{
Mod testMod = new Mod(ExtractMetadataFromPath(modPath), Path.GetFileName(modPath));
if ((testMod.CurrentModData.ModID == modID || testMod.NameOnDisk == mod.NameOnDisk) && testMod.InstalledVersion == mod.InstalledVersion)
{
return modPath;
}
}
return null;
}
public long GetSizeOnDisk(Mod mod)
{
string copyingPath = GetPathOnDisk(mod);
if (string.IsNullOrEmpty(copyingPath)) return -1;
return new FileInfo(copyingPath).Length;
}
public void SyncModsToDisk()
{
if (IsReadOnly) return;
foreach (Mod mod in Mods)
{
if (mod.Dirty)
{
string destinedName = mod.ConstructName();
File.Delete(Path.Combine(InstallPath, mod.NameOnDisk));
if (mod.Enabled)
{
string[] allMods = Directory.GetFiles(DownloadPath, "*.pak", SearchOption.TopDirectoryOnly);
string copyingPath = null;
foreach (string modPath in allMods)
{
Mod testMod = new Mod(ExtractMetadataFromPath(modPath), Path.GetFileName(modPath));
if ((testMod.CurrentModData.ModID == mod.CurrentModData.ModID || testMod.NameOnDisk == mod.NameOnDisk) && testMod.InstalledVersion == mod.InstalledVersion)
{
copyingPath = modPath;
break;
}
}
if (!string.IsNullOrEmpty(copyingPath))
{
File.Copy(copyingPath, Path.Combine(InstallPath, destinedName), true);
mod.NameOnDisk = destinedName;
}
}
mod.Dirty = false;
}
}
}
private PlatformType FirstRecordedIndependentConfigPlatform = PlatformType.Unknown;
public void SyncIndependentConfigFromDisk()
{
IndependentConfig independentConfig = null;
try
{
independentConfig = JsonConvert.DeserializeObject<IndependentConfig>(File.ReadAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AstroModLoader", "config.json")));
}
catch
{
independentConfig = null;
}
if (independentConfig != null)
{
if (!string.IsNullOrEmpty(independentConfig.AccentColor))
{
try
{
AMLPalette.AccentColor = AMLUtils.ColorFromHTML(independentConfig.AccentColor);
}
catch { }
}
AMLPalette.CurrentTheme = independentConfig.Theme;
AMLPalette.RefreshTheme(BaseForm);
Platform = independentConfig.Platform;
if (FirstRecordedIndependentConfigPlatform == PlatformType.Unknown) FirstRecordedIndependentConfigPlatform = independentConfig.Platform;
if (!string.IsNullOrEmpty(independentConfig.CustomBasePath)) CustomBasePath = independentConfig.CustomBasePath;
if (!string.IsNullOrEmpty(independentConfig.PlayFabCustomID)) PlayFabAPI.CustomID = independentConfig.PlayFabCustomID;
if (!string.IsNullOrEmpty(independentConfig.PlayFabToken)) PlayFabAPI.Token = independentConfig.PlayFabToken;
}
}
public string GetGamePathFromBasePath(string basePath)
{
ModConfig diskConfig = null;
try
{
diskConfig = JsonConvert.DeserializeObject<ModConfig>(File.ReadAllText(Path.Combine(basePath, "Saved", "Mods", "modconfig.json")));
}
catch
{
diskConfig = null;
}
return diskConfig?.GamePath;
}
public void SyncDependentConfigFromDisk(bool includeGamePath = true)
{
ModConfig diskConfig = null;
try
{
diskConfig = JsonConvert.DeserializeObject<ModConfig>(File.ReadAllText(Path.Combine(DownloadPath, "modconfig.json")));
}
catch
{
diskConfig = null;
}
if (diskConfig != null)
{
ApplyProfile(diskConfig.ModsOnDisk, false);
ProfileList = diskConfig.Profiles;
if (ProfileList == null) ProfileList = new Dictionary<string, ModProfile>();
if (!string.IsNullOrEmpty(diskConfig.LaunchCommand)) LaunchCommand = diskConfig.LaunchCommand;
OurIntegrator.RefuseMismatchedConnections = diskConfig.RefuseMismatchedConnections;
if (includeGamePath)
{
if (!string.IsNullOrEmpty(diskConfig.GamePath)) GamePath = diskConfig.GamePath;
KeyValuePair<PlatformType, string> prospectivePlatform = ValidPlatformTypesToPaths.FirstOrDefault(x => x.Value == GamePath);
if (Platform == PlatformType.Unknown && !prospectivePlatform.Equals(default(KeyValuePair<PlatformType, string>))) Platform = prospectivePlatform.Key;
}
}
}
public void SyncConfigFromDisk()
{
SyncIndependentConfigFromDisk();
SyncDependentConfigFromDisk();
}
public void ApplyProfile(ModProfile prof, bool disableAllMods = true)
{
if (prof == null) return;
if (disableAllMods)
{
foreach (KeyValuePair<string, Mod> rawEntry in ModLookup)
{
ModLookup[rawEntry.Key].Enabled = false;
}
}
foreach (KeyValuePair<string, Mod> entry in prof.ProfileData)
{
if (ModLookup.ContainsKey(entry.Key))
{
ModLookup[entry.Key].Enabled = entry.Value.Enabled;
if (entry.Value.InstalledVersion != null) ModLookup[entry.Key].InstalledVersion = entry.Value.InstalledVersion;
ModLookup[entry.Key].Priority = entry.Value.Priority;
ModLookup[entry.Key].IsOptional = entry.Value.IsOptional;
ModLookup[entry.Key].ForceLatest = entry.Value.ForceLatest;
if (entry.Value.ForceLatest || !ModLookup[entry.Key].AllModData.ContainsKey(ModLookup[entry.Key].InstalledVersion))
{
ModLookup[entry.Key].InstalledVersion = ModLookup[entry.Key].AvailableVersions[0];
}
}
}
}
public ModProfile GenerateProfile()
{
var res = new ModProfile(new Dictionary<string, Mod>());
foreach (Mod mod in Mods)
{
res.ProfileData.Add(mod.CurrentModData.ModID, (Mod)mod.Clone());
}
return res;
}
public void SyncIndependentConfigToDisk()
{
var newIndConfig = new IndependentConfig();
if (Program.CommandLineOptions.ServerMode)
{
newIndConfig.Platform = FirstRecordedIndependentConfigPlatform;
}
else
{
newIndConfig.Platform = Platform;
}
newIndConfig.Theme = AMLPalette.CurrentTheme;
newIndConfig.AccentColor = AMLUtils.ColorToHTML(AMLPalette.AccentColor);
newIndConfig.CustomBasePath = CustomBasePath;
newIndConfig.PlayFabCustomID = PlayFabAPI.CustomID;
newIndConfig.PlayFabToken = PlayFabAPI.Token;
Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AstroModLoader"));
File.WriteAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AstroModLoader", "config.json"), Encoding.UTF8.GetBytes(AMLUtils.SerializeObject(newIndConfig)));
}
public void SyncDependentConfigToDisk()
{
var newConfig = new ModConfig();
newConfig.GamePath = GamePath;
newConfig.LaunchCommand = LaunchCommand;
newConfig.RefuseMismatchedConnections = OurIntegrator.RefuseMismatchedConnections;
newConfig.Profiles = ProfileList;
newConfig.ModsOnDisk = GenerateProfile();
File.WriteAllBytes(Path.Combine(DownloadPath, "modconfig.json"), Encoding.UTF8.GetBytes(AMLUtils.SerializeObject(newConfig)));
}
public void SyncConfigToDisk()
{
AMLUtils.InvokeUI(() =>
{
SyncDependentConfigToDisk();
SyncIndependentConfigToDisk();
});
}
public static ModIntegrator OurIntegrator;
public void IntegrateMods()
{
if (IsReadOnly || GamePath == null || InstallPath == null) return;
List<string> optionalMods = new List<string>();
foreach (Mod mod in Mods)
{
if (mod.Enabled && mod.IsOptional) optionalMods.Add(mod.CurrentModData.ModID);
}
if (TableHandler.ShouldContainOptionalColumn()) OurIntegrator.OptionalModIDs = optionalMods;
OurIntegrator.IntegrateMods(InstallPath, Path.Combine(GamePath, "Astro", "Content", "Paks"));
}
public void RefreshAllPriorites()
{
for (int i = 0; i < Mods.Count; i++) Mods[i].Priority = i + 1; // The mod loader should never save a mod's priority as 0 to disk so that external applications can use 0 to force a mod to always load first
}
public void SwapMod(Mod previouslySelectedMod, int newModIndex, bool updateAutomatically = true)
{
// First we remove the old position of the mod we're changing
for (int i = 0; i < Mods.Count; i++)
{
if (object.ReferenceEquals(Mods[i], previouslySelectedMod))
{
Mods.RemoveAt(i);
break;
}
}
Mods.Insert(newModIndex, previouslySelectedMod); // Insert at the new location
RefreshAllPriorites(); // Refresh priority list
if (updateAutomatically)
{
foreach (Mod mod in Mods) mod.Dirty = true; // Update all the priorities on disk to be safe
FullUpdate();
}
}
public bool GetReadOnly()
{
try
{
string targetPath = Path.Combine(InstallPath, "999-AstroModIntegrator_P.pak");
if (!File.Exists(targetPath)) return false;
using (FileStream f = new FileStream(targetPath, FileMode.Open, FileAccess.Write, FileShare.None)) { }