-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEasyUninstaller.cs
More file actions
257 lines (219 loc) · 8.55 KB
/
EasyUninstaller.cs
File metadata and controls
257 lines (219 loc) · 8.55 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Text;
using System.IO;
using ModAPI.Common;
using ModAPI.Common.Types;
using ModAPI.Common.Update;
namespace Spore_ModAPI_Easy_Uninstaller
{
public static class EasyUninstaller
{
private static string SporeDataPath;
private static string SporeDataEP1Path;
private static string DllPath;
private static InstalledMods Mods;
private static UninstallerForm Form;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (!Permissions.IsAdministrator())
{
UpdateManager.CheckForUpdates();
Permissions.RerunAsAdministrator();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LauncherSettings.Load();
// ensure we find Spore & GA as early as possible
if (PathDialogs.ProcessSpore() == null ||
PathDialogs.ProcessGalacticAdventures() == null)
{
return;
}
Form = new UninstallerForm();
Mods = new InstalledMods();
Mods.Load();
ReloadMods();
Application.Run(Form);
}
}
public static void ReloadMods()
{
Mods.Load();
Form.AddMods(Mods.ModConfigurations);
}
public static void UninstallMods(Dictionary<ModConfiguration, bool> mods)
{
List<ModConfiguration> successfulMods = new List<ModConfiguration>();
try
{
// ensure we don't remove a mod which
// is a dependency of another mod
List<string> modDependencies = new List<string>();
List<string> reliantMods = new List<string>();
foreach (var mod in Mods.ModConfigurations)
{
foreach (var uninstallMod in mods)
{
if (mod.Dependencies != null &&
mod.Dependencies.Contains(uninstallMod.Key.Unique) &&
!mods.Select(x => x.Key.Unique).Contains(mod.Unique))
{
modDependencies.Add(uninstallMod.Key.DisplayName);
if (!reliantMods.Contains(mod.DisplayName))
reliantMods.Add(mod.DisplayName);
}
}
}
if (modDependencies.Count() > 0)
{
MessageBox.Show($"Cannot uninstall {String.Join(", ", modDependencies.ToArray())} because {String.Join(", ", reliantMods.ToArray())} relies on them being installed!", CommonStrings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
foreach (var mod in mods)
{
ResultType result = ResultType.Success;
if (mod.Value)
{
result = ExecuteConfigurator(mod.Key, true);
}
else
{
RemoveModFiles(mod.Key);
}
if (result == ResultType.Success)
{
Mods.RemoveMod(mod.Key);
successfulMods.Add(mod.Key);
}
}
}
catch (UnauthorizedAccessException)
{
MessageBox.Show(CommonStrings.UnauthorizedAccess, Strings.CouldNotUninstall, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Strings.CouldNotUninstall, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (successfulMods.Count > 0)
{
Mods.Save();
// reload before we show the message box
EasyUninstaller.ReloadMods();
var sb = new StringBuilder();
foreach (ModConfiguration mod in successfulMods)
{
sb.Append(mod);
sb.Append("\n");
}
MessageBox.Show(Strings.ModsWereUninstalled + "\n" + sb.ToString(), Strings.UninstallationSuccessful);
}
}
private static void CheckSporeDataEP1Path(string modName)
{
if (SporeDataEP1Path == null)
{
string path = PathDialogs.ProcessGalacticAdventures();
if (path == null && modName != null)
{
throw new Exception(Strings.CouldNotUninstall + " \"" + modName + "\"\n" + CommonStrings.GalacticAdventuresNotFound);
}
else
{
SporeDataEP1Path = SporePath.MoveToData(SporePath.Game.GalacticAdventures, SporePath.GetRealParent(path));
}
}
}
private static void CheckSporeDataPath(string modName)
{
if (SporeDataPath == null)
{
string path = PathDialogs.ProcessSpore();
if (path == null && modName != null)
{
throw new Exception(Strings.CouldNotUninstall + " \"" + modName + "\"\n" + CommonStrings.SporeNotFound);
}
else
{
SporeDataPath = SporePath.MoveToData(SporePath.Game.Spore, SporePath.GetRealParent(path));
}
}
}
private static void CheckDllPath()
{
if (DllPath == null)
{
DllPath = Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location).ToString();
}
}
private static string GetOutputPath(string pathType, string modName)
{
switch (pathType)
{
case "GalacticAdventures":
CheckSporeDataEP1Path(modName);
return SporeDataEP1Path;
case "Spore":
CheckSporeDataPath(modName);
return SporeDataPath;
case "None":
CheckDllPath();
return DllPath;
default:
return null;
}
}
// returns number of files with errors
private static void RemoveModFiles(ModConfiguration mod)
{
foreach (InstalledFile file in mod.InstalledFiles)
{
string outputPath = GetOutputPath(file.PathType, mod.Name);
if (outputPath != null)
{
string outputFile = Path.Combine(outputPath, file.Name);
string outputFile2 = Path.Combine(outputPath, "mLibs", file.Name);
if (File.Exists(outputFile))
File.Delete(outputFile);
if (File.Exists(outputFile2)) //if ((file.Name.Contains("-disk") || file.Name.Contains("-steam") || file.Name.Contains("-steam_patched")))
File.Delete(outputFile2);
}
}
string modConfigPath = Path.Combine(Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location).ToString(), "ModConfigs", mod.Name);
if (Directory.Exists(modConfigPath))
Directory.Delete(modConfigPath, true);
}
private static string ConvertToArgument(string path)
{
if (path == null)
{
return "null";
}
else
{
return "\"" + path + "\"";
}
}
public static ResultType ExecuteConfigurator(ModConfiguration mod, bool uninstall)
{
string path = Path.Combine(
Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location).ToString()
, "Spore ModAPI Easy Installer.exe");
string args = "\"" + mod.Name + "\"" + " true " + uninstall.ToString();
var process = Process.Start(path, args);
process.WaitForExit();
return (ResultType)process.ExitCode;
}
}
}