-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.cs
202 lines (156 loc) · 5.97 KB
/
MainWindow.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
using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace UniversalUnityPatcher {
public partial class MainWindow : Form {
private string assemblyPath;
private string patchPath;
private string backupPath;
private int toolTipIndex = -1;
public static MainWindow Instance;
public MainWindow() {
Instance = this;
InitializeComponent();
Patcher.BackupFiles = CreateBackupCheckbox.Checked;
WriteToConsole($"{Properties.Resources.ApplicationName} v{typeof(MainWindow).Assembly.GetName().Version}\nAwaiting input...\n");
if (!string.IsNullOrWhiteSpace(Patcher.AssemblyPath)) {
assemblyPath = Patcher.AssemblyPath;
AssemblyPathUpdated();
}
if (!string.IsNullOrWhiteSpace(Patcher.PatchPath)) {
patchPath = Path.GetFullPath(Patcher.PatchPath);
PatchPathUpdated();
}
if (!string.IsNullOrWhiteSpace(Program.CLIOptions.BackupDirectory)) {
backupPath = Path.GetFullPath(Program.CLIOptions.BackupDirectory);
}
BackupPathUpdated();
}
public void ClearConsole() {
ConsoleWindow.Clear();
}
public void WriteToConsole(string text, bool newLine = true) {
if (newLine) {
ConsoleWindow.Text += text + "\n";
} else {
ConsoleWindow.Text += text;
}
ConsoleWindow.SelectionStart = ConsoleWindow.Text.Length;
ConsoleWindow.ScrollToCaret();
}
private void LoadAssemblyButton_Click(object sender, EventArgs e) {
using (var openFile = new CommonOpenFileDialog { IsFolderPicker = true, Title = "Select an Assembly Directory" }) {
if (openFile.ShowDialog() == CommonFileDialogResult.Ok) {
assemblyPath = openFile.FileName;
Patcher.AssemblyPath = assemblyPath;
AssemblyPathUpdated();
BackupPathUpdated();
}
}
}
private void LoadPatchesButton_Click(object sender, EventArgs e) {
OpenFileDialog openFile = new OpenFileDialog {
Title = "Select a Patch file",
Filter = "Patch XML|*.xml"
};
if (openFile.ShowDialog() == DialogResult.OK) {
patchPath = Path.GetFullPath(openFile.FileName);
PatchPathUpdated();
}
}
private void SetBackupPathButton_Click(object sender, EventArgs e) {
using (var openFile = new CommonOpenFileDialog { IsFolderPicker = true, Title = "Select a Backup Directory" }) {
if (openFile.ShowDialog() == CommonFileDialogResult.Ok) {
backupPath = openFile.FileName;
Patcher.BackupPath = backupPath;
BackupPathUpdated();
}
}
}
private void ResetBackupPathButton_Click(object sender, EventArgs e) {
backupPath = null;
Patcher.BackupPath = null;
BackupPathUpdated();
}
private void PatchButton_Click(object sender, EventArgs e) {
try {
Patcher.ApplyPatches();
} catch (Exception ex) {
MessageBox.Show($"Failed to apply patches:\n{ex.Message}\n\n{ex.StackTrace}", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
//WriteToConsole($"Failed to apply patches:\n{ex.Message}\n\n{ex.StackTrace}");
}
}
private void AvailablePatches_ItemCheck(object sender, ItemCheckEventArgs e) {
Patcher.LoadedPatches.Patches.ElementAt(e.Index).IsEnabled = (e.NewValue == CheckState.Checked);
UpdatePatchButtonEnabled();
}
private void AvailablePatches_MouseMove(object sender, MouseEventArgs e) {
if (toolTipIndex != AvailablePatches.IndexFromPoint(e.Location)) {
toolTipIndex = AvailablePatches.IndexFromPoint(e.Location);
if (toolTipIndex >= 0) {
patchToolTip.Active = true;
patchToolTip.SetToolTip(AvailablePatches, Patcher.LoadedPatches.Patches.ElementAt(toolTipIndex).Description.Trim());
} else {
patchToolTip.Hide(AvailablePatches);
patchToolTip.Active = false;
}
}
}
private void CreateBackupCheckbox_CheckedChanged(object sender, EventArgs e) {
Patcher.BackupFiles = CreateBackupCheckbox.Checked;
}
#region GUI Updates
private void AssemblyPathUpdated() {
AssemblyPathLabel.Text = assemblyPath;
WriteToConsole($"[INFO] Selected Assembly directory: {assemblyPath}");
UpdatePatchButtonEnabled();
}
private void PatchPathUpdated() {
PatchesPathLabel.Text = Path.GetFileName(patchPath);
try {
int loadedPatchesCount = Patcher.LoadPatches(patchPath);
WriteToConsole($"[INFO] Loaded {loadedPatchesCount} patches from {patchPath}");
} catch (Exception ex) {
WriteToConsole($"[ERROR] Failed to load patches from \"{Path.GetFullPath(patchPath)}\":\n{ex.Message}");
}
if (Patcher.HasLoadedPatches) {
AvailablePatches.Items.Clear();
foreach (var patch in Patcher.LoadedPatches.Patches) {
if (!patch.IsEnabled && Program.CLIOptions.EnabledPatches.Contains(Patcher.LoadedPatches.Patches.IndexOf(patch) + 1)) {
patch.IsEnabled = true;
} else if (patch.IsEnabled && Program.CLIOptions.DisabledPatches.Contains(Patcher.LoadedPatches.Patches.IndexOf(patch) + 1)) {
patch.IsEnabled = false;
}
if (patch.ShortDescription != null && patch.ShortDescription.Length > 0) {
AvailablePatches.Items.Add(patch.ShortDescription);
} else {
AvailablePatches.Items.Add(patch.Name);
}
AvailablePatches.SetItemChecked(AvailablePatches.Items.Count - 1, patch.IsEnabled);
}
AvailablePatches.Enabled = true;
} else {
AvailablePatches.Enabled = false;
AvailablePatches.Items.Clear();
AvailablePatches.Items.Add("No patches available");
}
UpdatePatchButtonEnabled();
}
private void BackupPathUpdated() {
var path = string.IsNullOrWhiteSpace(backupPath) ? Patcher.BackupPath : backupPath;
WriteToConsole($"[INFO] Selected backup directory: {path}");
BackupPathLabel.Text = path;
UpdateResetBackupPathButtonEnabled();
}
private void UpdatePatchButtonEnabled() {
PatchButton.Enabled = (!string.IsNullOrWhiteSpace(assemblyPath) && !string.IsNullOrWhiteSpace(patchPath) && Patcher.EnabledPatches.Count > 0);
}
private void UpdateResetBackupPathButtonEnabled() {
ResetBackupPathButton.Enabled = !string.IsNullOrWhiteSpace(backupPath);
}
#endregion
}
}