-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathXmlInstallerWindow.xaml.cs
More file actions
1506 lines (1325 loc) · 65.2 KB
/
XmlInstallerWindow.xaml.cs
File metadata and controls
1506 lines (1325 loc) · 65.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 ModAPI.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Xml;
using ModAPI.Common.Types;
using ModAPI.Common.Update;
using ModAPI.Common.UI;
using ModAPI.Common.UI.Windows;
namespace Spore_ModAPI_Easy_Installer
{
public static class XmlInstallerCancellation
{
public static Dictionary<string, bool> Cancellation = new Dictionary<string, bool>();
}
/// <summary>
/// Interaction logic for XmlInstallerWindow.xaml
/// </summary>
public partial class XmlInstallerWindow : DecoratableWindow
{
public static string GameSporeString = "Spore";
public static string GameGaString = "GalacticAdventures";
public static List<XmlInstallerWindow> installerWindows = new List<XmlInstallerWindow>();
string ModSettingsStoragePath = string.Empty;
string ModConfigPath = string.Empty;
string ModUnique = string.Empty;
Version ModVersion = null;
List<string> ModDependencies = new List<string>();
List<Version> ModDependenciesVersions = new List<Version>();
XmlDocument Document = new XmlDocument();
List<ComponentInfo> Prerequisites = new List<ComponentInfo>();
List<ComponentInfo> CompatFiles = new List<ComponentInfo>();
List<ComponentInfo> InstalledCompatFiles = new List<ComponentInfo>();
List<RemovalInfo> Removals = new List<RemovalInfo>();
string ModDisplayName = string.Empty;
string ModName = string.Empty;
string ModDescription = string.Empty;
bool _isConfigurator = false;
bool _isUninstallingOnly = false;
int _activeComponentCount = 0;
CircleEase _ease = new CircleEase()
{
EasingMode = EasingMode.EaseOut
};
TimeSpan _time = TimeSpan.FromMilliseconds(250);
Version ModInfoVersion = null;// new Version(1, 0, 0, 0);
int _installerState = 0; //0 = waiting, 1 = installing
int _installerMode = 0; //0 = show components list, 1 = don't show components list
bool _isModMatched = false;
bool _dontUseLegacyPackagePlacement = false;
public static string GaDataPath = SporePath.MoveToData(SporePath.Game.GalacticAdventures, SporePath.GetRealParent(PathDialogs.ProcessGalacticAdventures()));
public static string SporeDataPath = SporePath.MoveToData(SporePath.Game.Spore, SporePath.GetRealParent(PathDialogs.ProcessSpore()));
private ResultType _result = ResultType.ModNotInstalled;
public XmlInstallerWindow(string modName, bool configure, bool uninstall)
{
InitializeComponent();
//LauncherSettings.Load();
installerWindows.Add(this);
if (!Directory.Exists(SporeDataPath))
PathDialogs.ProcessSpore();
if (!Directory.Exists(GaDataPath))
PathDialogs.ProcessGalacticAdventures();
ModName = modName.Trim('"');
XmlInstallerCancellation.Cancellation.Add(ModName, false);
ModConfigPath = Path.Combine(Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location).ToString(), "ModConfigs", ModName); // Settings.ProgramDataPath + @"\ModConfig\" + modName + @"\";
//if (!Directory.Exists())
_isConfigurator = configure;
_isUninstallingOnly = uninstall;
if (_isConfigurator)
{
LoadingPanel.Visibility = Visibility.Collapsed;
RevealInstaller();
if (_isUninstallingOnly)
{
Dispatcher.Invoke(new Action(() =>
{
StartUninstallationButton_Click(StartUninstallationButton, null);
}));
}
}
}
public ResultType GetResult()
{
return _result;
}
public void SignalRevealInstaller()
{
Dispatcher.Invoke(new Action(() =>
{
RevealInstaller();
}));
}
public void RevealInstaller()
{
if (!_isConfigurator)
{
Show();
Focus();
Activate();
DoubleAnimation FadeOutAnimation = new DoubleAnimation()
{
Duration = _time,
EasingFunction = _ease,
From = 1,
To = 0
};
FadeOutAnimation.Completed += (sneder, args) => LoadingPanel.Visibility = Visibility.Collapsed;
LoadingPanel.BeginAnimation(Border.OpacityProperty, FadeOutAnimation);
}
try
{
/*if (!Directory.Exists(ModConfigPath))
Directory.CreateDirectory(ModConfigPath);*/
//ModName = fixedName;
ModDisplayName = ModName;
Document.Load(Path.Combine(ModConfigPath, "ModInfo.xml"));
PrepareInstaller();
}
catch (Exception ex)
{
ErrorInfoTextBlock.Text = ex.ToString();
CyclePage(0, 3);
}
}
private void PrepareInstaller()
{
try
{
ModInfoVersion = Version.Parse(Document.SelectSingleNode("/mod").Attributes["installerSystemVersion"].Value);
if ((ModInfoVersion == new Version(1, 0, 0, 0)) ||
(ModInfoVersion == new Version(1, 0, 1, 0)) ||
(ModInfoVersion == new Version(1, 0, 1, 1)) ||
(ModInfoVersion == new Version(1, 0, 1, 2)) ||
(ModInfoVersion == new Version(1, 0, 1, 3)))
{
if (File.Exists(Path.Combine(ModConfigPath, "Branding.png")))
{
(TitleBarContent as Grid).Visibility = Visibility.Visible;
BitmapImage brandingImage = new BitmapImage();
using (MemoryStream byteStream = new MemoryStream(File.ReadAllBytes(Path.Combine(ModConfigPath, "Branding.png"))))
{
brandingImage.BeginInit();
brandingImage.CacheOption = BitmapCacheOption.OnLoad;
brandingImage.StreamSource = byteStream;
brandingImage.EndInit();
}
BorderThickness = new Thickness(BorderThickness.Left, 60, BorderThickness.Right, BorderThickness.Bottom);
BrandingCanvas.Width = brandingImage.PixelWidth;
BrandingCanvas.Source = brandingImage;
ShowTitlebarText = false;
}
else
Title = ModDisplayName + " Installer";
if ((ModInfoVersion == new Version(1, 0, 0, 0)) && (Document.SelectSingleNode("/mod").Attributes["mode"] != null))
{
if (Document.SelectSingleNode("/mod").Attributes["mode"].Value == "compatOnly")
_installerMode = 1;
}
else if ((ModInfoVersion == new Version(1, 0, 1, 0)) ||
(ModInfoVersion == new Version(1, 0, 1, 1)) ||
(ModInfoVersion == new Version(1, 0, 1, 2)) ||
(ModInfoVersion == new Version(1, 0, 1, 3)))
{
_dontUseLegacyPackagePlacement = true;
_installerMode = 1;
if (Document.SelectSingleNode("/mod").Attributes["hasCustomInstaller"] != null)
{
if (bool.TryParse(Document.SelectSingleNode("/mod").Attributes["hasCustomInstaller"].Value, out bool hasCustomInstaller) && hasCustomInstaller)
{
_installerMode = 0;
}
}
if ((Document.SelectSingleNode("/mod").Attributes["dllsBuild"] != null) && (Version.TryParse(Document.SelectSingleNode("/mod").Attributes["dllsBuild"].Value + ".0", out Version minFeaturesLevel)))
{
if (minFeaturesLevel > UpdateManager.CurrentDllsBuild)
CyclePage(0, 2);
}
else
throw new Exception("This Mod has not specified a valid minimum features level. Please inform the mod's developer of this.");
}
if (Document.SelectSingleNode("/mod").Attributes["unique"].Value != null)
ModUnique = Document.SelectSingleNode("/mod").Attributes["unique"].Value;
else
throw new Exception("This Mod's installer does not have a Unique Identifier. Please inform the mod's developer of this.");
ModConfiguration[] configs = EasyInstaller.ModList.ModConfigurations.ToArray();
foreach (ModConfiguration mod in configs)
{
if (mod.Unique == ModUnique)
{
if (mod.Name == ModName)
{
_isModMatched = true;
break;
}
}
}
if (_isConfigurator || (!_isModMatched))
{
string displayName = string.Empty;
if (Document.SelectSingleNode("/mod").Attributes["displayName"] != null)
{
displayName = Document.SelectSingleNode("/mod").Attributes["displayName"].Value;
ModDisplayName = displayName;
}
else
{
ModDisplayName = ModUnique;
}
Title = ModDisplayName + " Installer";
if (Document.SelectSingleNode("/mod").Attributes["description"] != null)
ModDescription = Document.SelectSingleNode("/mod").Attributes["description"].Value;
else
ModDescription = string.Empty;
// installerSystemVersion 1.0.1.3 introduced the version, depends and dependsVersion attributes
if (ModInfoVersion == new Version(1, 0, 1, 3))
{
if (Document.SelectSingleNode("/mod").Attributes["version"] != null)
{
if (Version.TryParse(Document.SelectSingleNode("/mod").Attributes["version"].Value, out Version parsedVersion))
ModVersion = parsedVersion;
else
throw new Exception("This mod has an invalid version. Please inform the mod's developer of this.");
}
else
ModVersion = null;
if (Document.SelectSingleNode("/mod").Attributes["depends"] != null)
{
string dependsString = Document.SelectSingleNode("/mod").Attributes["depends"].Value;
string dependsVersionString = null;
if (Document.SelectSingleNode("/mod").Attributes["dependsVersions"] != null)
dependsVersionString = Document.SelectSingleNode("/mod").Attributes["dependsVersions"].Value;
string[] dependsUniqueNames = dependsString.Split('?');
string[] dependsVersions = dependsVersionString?.Split('?');
bool[] foundDepends = new bool[dependsUniqueNames.Length];
ModDependencies = dependsUniqueNames.ToList();
if (dependsVersions != null)
{
foreach (string dependsVersion in dependsVersions)
{
if (String.IsNullOrEmpty(dependsVersion))
{ // 0.0.0.0 is interpreted as having no version attribute
// but we need to pad the list to match the amount of items
// that ModDependencies has
ModDependenciesVersions.Add(new Version(0, 0, 0, 0));
}
else
{
if (Version.TryParse(dependsVersion, out Version parsedDependsVersion))
{
ModDependenciesVersions.Add(parsedDependsVersion);
}
else
{
throw new Exception($"This mod has an invalid dependsVersions entry: \"{dependsVersion}\". Please inform the mod's developer of this.");
}
}
}
}
foreach (ModConfiguration mod in configs)
{
for (int i = 0; i < dependsUniqueNames.Length; i++)
{
if (mod.Unique == dependsUniqueNames[i])
{
if (mod.Version != null &&
(ModDependenciesVersions.Count() > i) &&
(mod.Version >= ModDependenciesVersions[i]))
{ // name + version match
foundDepends[i] = true;
break;
}
else if (ModDependenciesVersions.Count() <= i ||
ModDependenciesVersions[i] == new Version(0, 0, 0, 0))
{ // name only match
foundDepends[i] = true;
break;
}
}
}
}
bool satisfiedDepends = true;
List<string> missingDepends = new List<string>();
for (int i = 0; i < foundDepends.Length; i++)
{
if (!foundDepends[i])
{
satisfiedDepends = false;
string missingDependsText = dependsUniqueNames[i];
if (dependsVersions != null &&
dependsVersions.Length > i)
{
missingDependsText += " " + dependsVersions[i];
}
missingDepends.Add(missingDependsText);
}
}
if (!satisfiedDepends)
{
throw new Exception($"This mod has dependencies that you don't have installed: {String.Join(", ", missingDepends)}");
}
}
}
NameTextBlock.Text = displayName;
DescriptionTextBlock.Text = ModDescription;
ModSettingsStoragePath = Path.Combine(Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location).ToString(), "ModSettings", ModUnique);
string settingsStorageDir = Path.GetDirectoryName(ModSettingsStoragePath);
//Debug.WriteLine("ModSettingsStoragePath: " + ModSettingsStoragePath);
//Debug.WriteLine("settingsStorageDir: " + settingsStorageDir);
if (!Directory.Exists(settingsStorageDir))
Directory.CreateDirectory(settingsStorageDir);
GetComponents();
if (_isConfigurator)
{
StartInstallationButton.Content = "Apply";
StartUninstallationButton.Visibility = Visibility.Visible;
}
/*if (!ShowProtectiveDialogs())
return;*/
if (_installerMode == 1)
{
//if (_isConfigurator)
StartInstallationButton_Click(StartInstallationButton, null);
/*else
StartUninstallationButton_Click(StartUninstallationButton, null);*/
}
}
else
{
InstallationCompleteHeaderTextBlock.Text = "You already have this mod installed!";
InstallationCompleteDescriptionTextBlock.Text = "To change the mod's settings or uninstall it, go to the ModAPI Easy Uninstaller.";
CyclePage(0, 1);
}
}
else
{
CyclePage(0, 2);
}
}
catch (Exception ex)
{
ErrorInfoTextBlock.Text = ex.ToString();
CyclePage(0, 3);
}
}
private void GetComponents()
{
foreach (XmlNode node in Document.SelectSingleNode("/mod").ChildNodes)
{
Debug.WriteLine("/mod");
if (node.Name.ToLower() == "component")
{
Debug.WriteLine("component");
ComponentInfo info = GetComponentFromXmlNode(node, false, true);
CheckBox checkBox = new CheckBox()
{
Content = info.ComponentDisplayName,
Tag = info
};
checkBox.MouseEnter += Component_MouseEnter;
checkBox.MouseLeave += Component_MouseLeave;
checkBox.IsChecked = false;
string storedValue = GetSettingValueFromStorageString(info.ComponentUniqueName);
if (storedValue != null && bool.TryParse(storedValue, out bool valueBool))
checkBox.IsChecked = valueBool;
else
checkBox.IsChecked = info.defaultChecked;
ComponentListStackPanel.Children.Add(checkBox);
}
else if (node.Name.ToLower() == "componentgroup")
{
bool hasSelection = false;
Debug.WriteLine("componentGroup");
string groupDisplayName = node.Attributes["displayName"].Value;
string groupUnique = node.Attributes["unique"].Value;
if (!IsUniqueNameValid(groupUnique))
throw new Exception("Invalid unique name: " + groupUnique);
//if (string.IsNullOrWhiteSpace(groupUnique))
GroupBox componentRadioGroupBox = new GroupBox()
{
Header = groupDisplayName,
Tag = groupUnique,
Content = new StackPanel()
{
Orientation = Orientation.Vertical
}
};
var componentRadioStackPanel = componentRadioGroupBox.Content as StackPanel;
string storedValue = GetSettingValueFromStorageString(groupUnique);
foreach (XmlNode subNode in node.ChildNodes)
{
ComponentInfo info = GetComponentFromXmlNode(subNode, false, true);
RadioButton radioButton = new RadioButton()
{
Content = info.ComponentDisplayName,
Tag = info,
GroupName = groupDisplayName
};
radioButton.MouseEnter += Component_MouseEnter;
radioButton.MouseLeave += Component_MouseLeave;
if /*(info.defaultChecked & */(!hasSelection)//)
{
//radioButton.IsChecked = true;
radioButton.IsChecked = false;
//if (_isConfigurator && (enabledList.Contains(ComponentInfoToQuestionMarkSeparatedString(info)))) //info.ComponentFileName + "?" + info.ComponentGame)))
if (_isConfigurator)
radioButton.IsChecked = (storedValue == info.ComponentUniqueName);
else
radioButton.IsChecked = info.defaultChecked;
if (radioButton.IsChecked == true)
hasSelection = true;
}
componentRadioStackPanel.Children.Add(radioButton);
}
ComponentListStackPanel.Children.Add(componentRadioGroupBox);
}
else if (node.Name.ToLower() == "prerequisite")
{
Debug.WriteLine("prerequisite");
Prerequisites.Add(GetComponentFromXmlNode(node));
}
else if (node.Name.ToLower() == "compatfile")
{
Debug.WriteLine("compatFile");
CompatFiles.Add(GetComponentFromXmlNode(node, true));
}
else if (node.Name.ToLower() == "remove")
{
List<string> inner = node.InnerText.Split('?').ToList();
List<string> game = new List<string>();
if (node.Attributes["game"] != null)
game = node.Attributes["game"].Value.Split('?').ToList();
else
foreach (string s in inner)
game.Add(string.Empty);
Debug.WriteLine("remove");
Removals.Add(new RemovalInfo()
{
RemovalFileNames = inner,
RemovalGames = game
});
}
}
int _activeComponentCount = GetComponentCount();
}
public string ComponentInfoToQuestionMarkSeparatedString(ComponentInfo info)
{
string returnValue = string.Empty;
for (int i = 0; i < info.ComponentFileNames.Count; i++)
returnValue = returnValue + info.ComponentFileNames[i] + "?" + info.ComponentGames[i] + "?";
if (returnValue.EndsWith("?"))
returnValue = returnValue.Substring(0, returnValue.LastIndexOf("?"));
return returnValue;
}
public ComponentInfo QuestionMarkSeparatedStringToComponentInfo(string infoString)
{
ComponentInfo returnValue = new ComponentInfo();
string[] strings = infoString.Split('?');
for (int i = 0; i < strings.Count(); i += 2)
{
returnValue.ComponentFileNames.Add(strings[i]);
returnValue.ComponentGames.Add(strings[i + 1]);
}
return returnValue;
}
private void Component_MouseEnter(object sender, MouseEventArgs e)
{
var sned = (sender as Control).Tag as ComponentInfo;
NameTextBlock.Text = sned.ComponentDisplayName;
DescriptionTextBlock.Text = sned.ComponentDescription;
ComponentTopImage.Source = sned.ComponentPreviewImage;
//ComponentTopImage.Height = sned.ComponentPreviewImage.PixelHeight / (ComponentTopImage.ActualWidth / sned.ComponentPreviewImage.PixelWidth);
//ComponentBottomImage.Height = sned.ComponentPreviewImage.PixelHeight / (ComponentTopImage.ActualWidth / sned.ComponentPreviewImage.PixelWidth);
if (sned.ComponentPreviewImagePlacement == ComponentInfo.ImagePlacement.InsteadOf)
{
ComponentTopImage.Visibility = Visibility.Visible;
DescriptionTextBlock.Visibility = Visibility.Collapsed;
ComponentBottomImage.Visibility = Visibility.Collapsed;
}
else if (sned.ComponentPreviewImagePlacement == ComponentInfo.ImagePlacement.Before)
{
ComponentTopImage.Visibility = Visibility.Visible;
DescriptionTextBlock.Visibility = Visibility.Visible;
ComponentBottomImage.Visibility = Visibility.Collapsed;
}
else if (sned.ComponentPreviewImagePlacement == ComponentInfo.ImagePlacement.After)
{
ComponentTopImage.Visibility = Visibility.Collapsed;
DescriptionTextBlock.Visibility = Visibility.Visible;
ComponentBottomImage.Visibility = Visibility.Visible;
}
else
{
ComponentTopImage.Visibility = Visibility.Collapsed;
DescriptionTextBlock.Visibility = Visibility.Visible;
ComponentBottomImage.Visibility = Visibility.Collapsed;
}
if ((ComponentInfoStackPanel.ActualHeight + ComponentInfoStackPanel.Margin.Top + ComponentInfoStackPanel.Margin.Bottom) > ComponentInfoScrollViewer.ScrollableHeight)
{
ComponentInfoScrollViewer.ScrollToVerticalOffset(0);
}
}
private void Component_MouseLeave(object sender, MouseEventArgs e)
{
NameTextBlock.Text = ModDisplayName;
DescriptionTextBlock.Text = ModDescription;
ComponentTopImage.Visibility = Visibility.Collapsed;
DescriptionTextBlock.Visibility = Visibility.Visible;
ComponentBottomImage.Visibility = Visibility.Collapsed;
}
private ComponentInfo GetComponentFromXmlNode(XmlNode node)
{
return GetComponentFromXmlNode(node, false, false);
}
private ComponentInfo GetComponentFromXmlNode(XmlNode node, bool isCompatFile)
{
return GetComponentFromXmlNode(node, isCompatFile, false);
}
private ComponentInfo GetComponentFromXmlNode(XmlNode node, bool isCompatFile, bool hasUnique)
{
ComponentInfo info = new ComponentInfo()
{
ComponentFileNames = TrySplit(node.InnerText, '?')
//ComponentFileName = node.InnerText
};
//if (Uri.IsWellFormedUriString(node.Attributes["name"].Value, UriKind.RelativeOrAbsolute))
if (hasUnique)
{
if (node.Attributes["unique"] != null)
info.ComponentUniqueName = node.Attributes["unique"].Value;
else
throw new Exception("Some of this mod's components lack display names.");
if (!IsUniqueNameValid(info.ComponentUniqueName))
throw new Exception("Invalid unique name: " + info.ComponentUniqueName);
}
if (node.Attributes["displayName"] != null)
info.ComponentDisplayName = node.Attributes["displayName"].Value;
else
info.ComponentDisplayName = info.ComponentUniqueName;
//if (Uri.IsWellFormedUriString(node.Attributes["description"].Value, UriKind.RelativeOrAbsolute))
if (node.Attributes["description"] != null)
info.ComponentDescription = node.Attributes["description"].Value;
//if (Uri.IsWellFormedUriString(node.Attributes["game"].Value, UriKind.RelativeOrAbsolute))
if (node.Attributes["game"] != null)
{
info.ComponentGames = TrySplit(node.Attributes["game"].Value, '?'); //info.ComponentGame = node.Attributes["game"].Value;
}
else
{
foreach (string s in info.ComponentFileNames)
info.ComponentGames.Add(string.Empty);
}
if (isCompatFile)
{
//MessageBox.Show(node.Name.ToLower(), "node.Name.ToLower()");
if (node.Attributes["compatTargetFileName"] != null)
{
info.ComponentCompatTargetFileNames = TrySplit(node.Attributes["compatTargetFileName"].Value, '?');
//info.ComponentCompatTargetFileName = node.Attributes["compatTargetFileName"].Value;
//MessageBox.Show(node.Attributes["compatTargetFileName"].Value, "compatTargetFileName");
}
if (node.Attributes["compatTargetGame"] != null)
{
info.ComponentCompatTargetGames = TrySplit(node.Attributes["compatTargetGame"].Value, '?');
//info.ComponentCompatTargetGame = node.Attributes["compatTargetGame"].Value;
//MessageBox.Show(node.Attributes["compatTargetGame"].Value, "compatTargetGame");
}
if ((node.Attributes["removeTargets"] != null) && bool.TryParse(node.Attributes["removeTargets"].Value, out bool removeTargets))
{
info.removeTargets = removeTargets;
}
}
//info.ComponentFileNames[0].Replace(".package", ".png").Replace(".dll", ".png")
string previewImageName = Path.Combine(ModConfigPath, info.ComponentUniqueName + ".png");
if (File.Exists(previewImageName))
{
//info.ComponentPreviewImage = new BitmapImage(new Uri(previewImageName, UriKind.RelativeOrAbsolute));
BitmapImage previewImage = new BitmapImage();
using (MemoryStream byteStream = new MemoryStream(File.ReadAllBytes(previewImageName)))
{
previewImage.BeginInit();
previewImage.CacheOption = BitmapCacheOption.OnLoad;
previewImage.StreamSource = byteStream;
previewImage.EndInit();
}
info.ComponentPreviewImage = previewImage;
try
{
info.ComponentPreviewImagePlacement = (ComponentInfo.ImagePlacement)Enum.Parse(typeof(ComponentInfo.ImagePlacement), node.Attributes["imagePlacement"].Value, true);
}
catch
{
info.ComponentPreviewImagePlacement = ComponentInfo.ImagePlacement.None;
}
}
if (node.Attributes["defaultChecked"] != null)
{
info.defaultChecked = bool.Parse(node.Attributes["defaultChecked"].Value);
}
return info;
}
private List<string> TrySplit(string input, char splitMarker)
{
List<string> returnValue = new List<string>();
if (input.Contains(splitMarker))
returnValue = input.Split(splitMarker).ToList();
else
returnValue.Add(input);
/*else
info.ComponentFileNames = new List<string>()
{
node.InnerText
};*/
return returnValue;
}
private void CollapseButtons()
{
StartInstallationButton.Visibility = Visibility.Collapsed;
StartUninstallationButton.Visibility = Visibility.Collapsed;
}
private async void StartInstallationButton_Click(object sender, RoutedEventArgs e)
{
try
{
string mLibsFolder = Path.Combine(Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location).ToString(), "mLibs");
if (!Directory.Exists(mLibsFolder)) Directory.CreateDirectory(mLibsFolder);
InstallationCompleteDescriptionTextBlock.Text = "The mod \"" + ModDisplayName + "\" has been installed.";
_installerState = 1;
CollapseButtons();
var anim = new DoubleAnimation()
{
Duration = _time,
EasingFunction = _ease,
From = Height,
To = 112
};
anim.Completed += (sneder, args) =>
{
Height = 112;
BeginAnimation(HeightProperty, null);
};
BeginAnimation(HeightProperty, anim);
bool dontInstall = false;
await Dispatcher.BeginInvoke(new Action(() =>
{
if ((Document.SelectSingleNode("/mod").Attributes["isExperimental"] != null) && bool.TryParse(Document.SelectSingleNode("/mod").Attributes["isExperimental"].Value, out bool isExperimental) && isExperimental)
{
var result = MessageBox.Show("This mod \"" + ModDisplayName + "\" is still in an experimental state, and its use may have unexpected, potentially undesirable consequences. Are you sure you wish to install it?", "Experimental mod", MessageBoxButton.YesNo);
if (result != MessageBoxResult.Yes)
{
dontInstall = true;
}
}
if ((Document.SelectSingleNode("/mod").Attributes["requiresGalaxyReset"] != null) && bool.TryParse(Document.SelectSingleNode("/mod").Attributes["requiresGalaxyReset"].Value, out bool reqGalaxyReset) && reqGalaxyReset)
{
var result = MessageBox.Show("The mod \"" + ModDisplayName + "\" can be installed, but will require a Galaxy reset to take effect. Performing a Galaxy reset will erase all of your save game planets. The necessary Galaxy Reset will NOT be performed automatically, you'll have to do it by hand. Are you sure you wish to install this mod?", "Mod requires a Galaxy Reset", MessageBoxButton.YesNo);
if (result != MessageBoxResult.Yes)
{
dontInstall = true;
}
}
if ((Document.SelectSingleNode("/mod").Attributes["causesSaveDataDependency"] != null) && bool.TryParse(Document.SelectSingleNode("/mod").Attributes["requiresGalaxyReset"].Value, out bool causesSaveDataDependency) && causesSaveDataDependency)
{
var result = MessageBox.Show("The mod \"" + ModDisplayName + "\" will cause save data dependency if installed. This means that if you ever uninstall it, your save planets may become corrupted or otherwise inaccessible, or be adversely affected in some other way. Are you sure you wish to install this mod?", "Mod causes Save Data Dependency", MessageBoxButton.YesNo);
if (result != MessageBoxResult.Yes)
{
dontInstall = true;
}
}
}));
if (dontInstall)
{
XmlInstallerCancellation.Cancellation[ModName] = true;
_installerState = 2;
_installerMode = 1;
_result = ResultType.ModNotInstalled;
Close();
}
List<string> enabledComponents = new List<string>();
InstallProgressBar.Maximum = GetComponentCount(); //enabledComponents.Count + Prerequisites.Count;
// only respect <remove> during installation
if (!_isConfigurator)
{
foreach (RemovalInfo p in Removals)
p.Remove();
}
foreach (ComponentInfo p in Prerequisites)
{
await Task.Run(() => HandleModComponent(p));
if (InstallProgressBar.Value < InstallProgressBar.Maximum)
InstallProgressBar.Value += 1;
}
List<string> storedSettings = new List<string>();
foreach (Control c in ComponentListStackPanel.Children)
{
if (c is CheckBox)
{
var info = c.Tag as ComponentInfo;
storedSettings.Add(CreateSettingStorageString(info.ComponentUniqueName, (c as CheckBox).IsChecked.Value.ToString()));
if ((c as CheckBox).IsChecked.Value)
{
await Task.Run(() => HandleModComponent(info));
enabledComponents.Add(ComponentInfoToQuestionMarkSeparatedString(info)/*info.ComponentFileName + "?" + info.ComponentGame*/);
if (InstallProgressBar.Value < InstallProgressBar.Maximum)
InstallProgressBar.Value += 1;
}
else if (_isConfigurator)
{
await Task.Run(() => HandleModComponent(info, false));
}
}
else if (c is GroupBox)
{
string selected = string.Empty;
foreach (RadioButton b in ((c as GroupBox).Content as StackPanel).Children)
{
var info = b.Tag as ComponentInfo;
if ((b as RadioButton).IsChecked.Value)
{
await Task.Run(() => HandleModComponent(info));
selected = info.ComponentUniqueName;
enabledComponents.Add(ComponentInfoToQuestionMarkSeparatedString(info)/*info.ComponentFileName + "?" + info.ComponentGame*/);
if (InstallProgressBar.Value < InstallProgressBar.Maximum)
InstallProgressBar.Value += 1;
}
else if (_isConfigurator)
{
await Task.Run(() => HandleModComponent(info, false));
}
}
storedSettings.Add(CreateSettingStorageString((c as GroupBox).Tag.ToString(), selected));
}
}
foreach (ComponentInfo p in CompatFiles)
{
await Task.Run(() => HandleCompatFile(p));
if (InstallProgressBar.Value < InstallProgressBar.Maximum)
InstallProgressBar.Value += 1;
}
// only write settings if they exist
if (storedSettings.Count > 0)
{
File.WriteAllLines(ModSettingsStoragePath, storedSettings.ToArray());
}
if (_installerMode == 0)
{
ModConfiguration mod = new ModConfiguration(ModName, ModUnique)
{
DisplayName = ModDisplayName,
Version = ModVersion,
Dependencies = ModDependencies.ToArray(),
DependenciesVersions = ModDependenciesVersions.ToArray(),
ConfiguratorPath = Path.Combine(ModConfigPath, "ModInfo.xml")
};
mod.InstalledFiles.Add(new InstalledFile(ModName));
for (int i = 0; i < EasyInstaller.ModList.ModConfigurations.Count; i++)
{
ModConfiguration selMod = EasyInstaller.ModList.ModConfigurations.ElementAt(i);
if ((selMod.Name == ModName) || (selMod.Unique == ModUnique))
{
EasyInstaller.ModList.ModConfigurations.Remove(selMod);
if (i > 0)
i--;
}
}
EasyInstaller.ModList.ModConfigurations.Add(mod);
}
else if (_installerMode == 1)
{
ModConfiguration mod = new ModConfiguration(ModName, ModUnique)
{
DisplayName = ModDisplayName,
Version = ModVersion,
Dependencies = ModDependencies.ToArray(),
DependenciesVersions = ModDependenciesVersions.ToArray(),
};
foreach (ComponentInfo c in Prerequisites)
{
for (int i = 0; i < c.ComponentFileNames.Count; i++)
{
InstalledFile file = null;
if (c.ComponentGames[i] == GameSporeString)
file = new InstalledFile(c.ComponentFileNames[i], SporePath.Game.Spore);
else if (c.ComponentGames[i] == GameGaString)
file = new InstalledFile(c.ComponentFileNames[i]);
else
file = new InstalledFile(c.ComponentFileNames[i], SporePath.Game.None);
mod.InstalledFiles.Add(file);
}
}
foreach (ComponentInfo c in InstalledCompatFiles)
{
for (int i = 0; i < c.ComponentFileNames.Count; i++)
{
InstalledFile file = null;
if (c.ComponentGames[i] == GameSporeString)
file = new InstalledFile(c.ComponentFileNames[i], SporePath.Game.Spore);
else if (c.ComponentGames[i] == GameGaString)
file = new InstalledFile(c.ComponentFileNames[i]);
else
file = new InstalledFile(c.ComponentFileNames[i], SporePath.Game.None);
mod.InstalledFiles.Add(file);
}
}
for (int i = 0; i < EasyInstaller.ModList.ModConfigurations.Count; i++)
{
ModConfiguration selMod = EasyInstaller.ModList.ModConfigurations.ElementAt(i);
if ((selMod.Name == ModName) || (selMod.Unique == ModUnique))
{
EasyInstaller.ModList.ModConfigurations.Remove(selMod);
if (i > 0)
i--;
}
}
EasyInstaller.ModList.ModConfigurations.Add(mod);
}
if (_installerMode == 1)
{
_installerState = 2;
_result = ResultType.Success;
Close();
}
else
{
CyclePage(0, 1);
_result = ResultType.Success;
_installerState = 2;
}
}
catch (Exception ex)
{
_result = ResultType.ModNotInstalled;
ErrorInfoTextBlock.Text = ex.ToString();
CyclePage(0, 3);
}
}
bool IsUniqueNameValid(string value)
{
bool returnValue = true;
foreach (char c in Path.GetInvalidPathChars())
{
if (value.Contains(c))
{
returnValue = false;
break;
}
}
return returnValue;
}
string GetSettingValueFromStorageString(string setting)
{
if (File.Exists(ModSettingsStoragePath))
{
string[] storedSettings = File.ReadAllLines(ModSettingsStoragePath);
string returnValue = null;
foreach (string s in storedSettings)
{
string settingName = s.Substring(0, s.IndexOf(">")).Replace("<", "");
if (settingName == setting)
{
returnValue = s.Substring(s.IndexOf(">") + 1);
break;
}
}
return returnValue;
}
else
return null;
}
string CreateSettingStorageString(string componentName, string value)
{
return "<" + componentName + ">" + value;