-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUninstallerForm.cs
More file actions
241 lines (210 loc) · 8.98 KB
/
UninstallerForm.cs
File metadata and controls
241 lines (210 loc) · 8.98 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ModAPI.Common;
using ModAPI.Common.Update;
namespace Spore_ModAPI_Easy_Uninstaller
{
public partial class UninstallerForm : Form
{
private bool eventBeingHandled = false;
public UninstallerForm()
{
InitializeComponent();
// create header row
this.dataGridView1.Rows.Add(new object[] { false, "Installed Mods" });
this.dataGridView1.Rows[0].DefaultCellStyle.ApplyStyle(dataGridView1.ColumnHeadersDefaultCellStyle);
this.dataGridView1.Rows[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dataGridView1.Rows[0].Cells[2].Value = Strings.InstalledMods;
this.dataGridView1.Columns[1].Visible = false;
this.dataGridView1.CellDoubleClick += dataGridView_CellDoubleClick;
this.dataGridView1.CellContentClick += dataGridView_CellContentClick;
this.dataGridView1.CellValueChanged += dataGridView_CellValueChanged;
this.dataGridView1.CellPainting += dataGridView_CellPainting;
this.label1.Text = Strings.ChooseTheMods;
this.btnCancel.Text = Strings.Cancel;
this.btnUninstall.Text = Strings.UninstallSelected + " (0)";
this.btnUninstall.Enabled = false;
this.label2.Text = "Spore ModAPI Launcher Kit Version " + UpdateManager.CurrentVersion.ToString() + "\nDLLs Build " + UpdateManager.CurrentDllsBuild;
this.BringToFront();
}
public void AddMods(HashSet<ModConfiguration> mods)
{
// clear rows
while (this.dataGridView1.RowCount > 1)
{
this.dataGridView1.Rows.RemoveAt(1);
}
// add mods
foreach (var mod in mods)
{
string versionString = "";
if (mod.Version != null)
{
versionString = mod.Version.ToString();
}
int index = this.dataGridView1.Rows.Add(new object[] { false, mod, mod.DisplayName, versionString });
if (GetConfiguratorPath(index) != null)
this.dataGridView1.Rows[index].Cells[4].ReadOnly = true;
}
// sort mods
this.dataGridView1.Sort(new ModRowComparer());
// reset header state
dataGridView_CellValueChanged(this, new DataGridViewCellEventArgs(0, 1));
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void dataGridView_CellDoubleClick(Object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != 0 && e.ColumnIndex == 4 && GetConfiguratorPath(e.RowIndex) != null)
{
// execute configurator and close uninstaller
ExecuteConfigurator(GetModConfiguration(e.RowIndex));
}
else
{
this.dataGridView1.Rows[e.RowIndex].Cells[0].Value = !(bool)this.dataGridView1.Rows[e.RowIndex].Cells[0].Value;
}
}
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != 0 && e.ColumnIndex == 4 && GetConfiguratorPath(e.RowIndex) != null)
{
// execute configurator and close uninstaller
ExecuteConfigurator(GetModConfiguration(e.RowIndex));
}
else
{
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (!eventBeingHandled)
{
eventBeingHandled = true;
if (e.RowIndex == 0)
{
bool value = (bool)this.dataGridView1.Rows[0].Cells[0].Value;
for (int i = 1; i < this.dataGridView1.RowCount; i++)
{
this.dataGridView1.Rows[i].Cells[0].Value = value;
}
}
else
{
// unselect header row
this.dataGridView1.Rows[0].Cells[0].Value = false;
}
int selectedCount = 0;
for (int i = 1; i < this.dataGridView1.RowCount; i++)
{
if ((bool) this.dataGridView1.Rows[i].Cells[0].Value)
{
selectedCount++;
}
}
this.btnUninstall.Text = Strings.UninstallSelected + " (" + selectedCount + ")";
this.btnUninstall.Enabled = selectedCount > 0;
eventBeingHandled = false;
}
}
private void btnUninstall_Click(object sender, EventArgs e)
{
var list = new Dictionary<ModConfiguration, bool>();
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
if (((bool)row.Cells[0].Value) && (row.Cells[1].Value is ModConfiguration conf))
{
list.Add(conf, (GetConfiguratorPath(row.Index) != null));
}
}
if (list.Count > 0)
{
var result = MessageBox.Show(this, Strings.AreYouSure, Strings.ConfirmationNeeded, MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
EasyUninstaller.UninstallMods(list);
}
}
}
private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 4 && this.dataGridView1.SelectedRows.Count > 0)
{
if (e.RowIndex > 0)
{
if (GetConfiguratorPath(e.RowIndex) != null)
{
var w = Properties.Resources.ConfigIcon.Width;
var h = Properties.Resources.ConfigIcon.Height;
var x = e.CellBounds.Left + (e.CellBounds.Width - w) / 2;
var y = e.CellBounds.Top + (e.CellBounds.Height - h) / 2;
Bitmap image = Properties.Resources.ConfigIcon;
e.Paint(e.CellBounds, DataGridViewPaintParts.All);
e.Graphics.DrawImage(image, new Rectangle(x, y, w, h));
e.Handled = true;
}
else
{
SolidBrush brush = new SolidBrush(e.CellStyle.BackColor);
if (this.dataGridView1.SelectedRows[0].Index == e.RowIndex)
{
brush = new SolidBrush(e.CellStyle.SelectionBackColor);
}
// ensure we make the cell lines visible
// to make it look consistent
var bounds = e.CellBounds;
bounds.Height -= 5;
bounds.Width -= 5;
e.Paint(bounds, DataGridViewPaintParts.All);
e.Graphics.FillRectangle(brush, bounds);
e.Handled = true;
}
}
else
{ // header row
SolidBrush brush = new SolidBrush(e.CellStyle.BackColor);
if (this.dataGridView1.SelectedRows[0].Index == 0)
{
brush = new SolidBrush(e.CellStyle.SelectionBackColor);
}
// ensure we make the cell lines visible
// to make it look consistent
var bounds = e.CellBounds;
bounds.Height -= 4;
bounds.Width -= 2;
bounds.Y += 2;
e.Paint(bounds, DataGridViewPaintParts.All);
e.Graphics.FillRectangle(brush, bounds);
e.Handled = true;
}
}
}
private ModConfiguration GetModConfiguration(int rowIndex)
{
return (ModConfiguration)this.dataGridView1.Rows[rowIndex].Cells[1].Value;
}
private string GetConfiguratorPath(int rowIndex)
{
return ((ModConfiguration)this.dataGridView1.Rows[rowIndex].Cells[1].Value).ConfiguratorPath;
}
private void ExecuteConfigurator(ModConfiguration mod)
{
try
{
this.Hide();
EasyUninstaller.ExecuteConfigurator(mod, false);
EasyUninstaller.ReloadMods();
this.Show();
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, CommonStrings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}