-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathForm1.cs
338 lines (306 loc) · 13.6 KB
/
Form1.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
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Windows.Forms;
using System.Xml;
namespace TroubleTool
{
public partial class Form1 : Form
{
private readonly Logger.Logger _log = new Logger.Logger(100u);
private AssetManager am = null;
public Form1()
{
InitializeComponent();
_log.AddToLog("Start");
FindTroubleshooter();
}
private void AddToLog(string text, Color entryColor)
{
_log.AddToLog(text, entryColor);
richTextBoxLog.Rtf = _log.GetLogAsRichText(true);
richTextBoxLog.ScrollToBottom();
}
private void FindTroubleshooter()
{
List<String> libPaths = new List<string>();
// 1. try to find Steam via regex
//opening the subkey
AddToLog($"Trying to find Steam path via registry", Color.White);
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\WOW6432Node\Valve\Steam");
if (key == null)
key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Valve\Steam");
string steamPath;
//if it does exist, retrieve the stored values
if (key != null)
{
steamPath = (String)key.GetValue("SteamPath");
key.Close();
}
else
{
AddToLog($"Couldn't find the path of Steam", Color.Red);
return;
// 1. iterate over all drives, try to find Steam
// TODO Path.Combine(drive, "Program Files (x86)", "Steam", "steamapps")
}
AddToLog($"Steam found {steamPath}", Color.Green);
libPaths.Add(Path.Combine(steamPath, "steamapps"));
// TODO add all lib paths from steamapps\libraryfolders.vdf
// 2. try to find "appmanifest_470310.acf" in the paths
AddToLog($"Searching through the Steam libraries", Color.White);
foreach (String libPath in libPaths)
{
AddToLog(libPath, Color.White);
if (File.Exists(Path.Combine(libPath, "appmanifest_470310.acf")))
{
textBoxTroubleshooterPath.Text = Path.Combine(libPath, "common", "Troubleshooter");
AddToLog($"Troubleshooter found {textBoxTroubleshooterPath.Text}", Color.Green);
return;
}
}
AddToLog($"Troubleshooter not found", Color.Red);
}
private void ButtonExtract_Click(object sender, EventArgs e)
{
am.LoadIndex();
AddToLog("Preparing Extraction", Color.White);
AddToLog($"Package Path: {am.package}", Color.White);
AddToLog($"Data Path: {am.data}", Color.White);
AddToLog("Checking which files haven't been extracted yet or have been updated", Color.White);
List<XmlElement> entries = am.FindUnExtractedAndUpdatedEntries();
int count = entries.Count;
AddToLog($"{count} assets have to be extracted or updated", Color.White);
int i = 1;
foreach (XmlElement entry in entries)
{
AddToLog(String.Format("{0}/{1}\t{2}", i++, count, entry.GetAttribute("original")), Color.White);
am.ExtractEntry(entry);
}
am.indexXml.Save(Path.Combine(am.data, "index.xml"));
AddToLog("Done", Color.White);
radioButtonData.Enabled = true;
}
private void ButtonApply_Click(object sender, EventArgs e)
{
am.LoadIndex();
AddToLog("Preparing Setup", Color.White);
AddToLog($"Package Path: {am.package}", Color.White);
if (radioButtonData.Checked)
{
AddToLog($"Data Path: {am.data}", Color.White);
AddToLog("Rewriting entries to use /Data", Color.White);
foreach (XmlElement entry in am.GetEntries())
{
entry.SetAttribute("method", "raw");
String original = entry.GetAttribute("original");
entry.SetAttribute("pack", "../Data/" + original.Replace("\\", "/"));
entry.SetAttribute("virtual", original.Substring(original.LastIndexOf('\\') + 1));
entry.SetAttribute("csize", entry.GetAttribute("size"));
}
}
if (radioButtonModsYes.Checked)
{
AddToLog($"Mods Path: {am.mods}", Color.White);
AddToLog("Adding Mods", Color.White);
Dictionary<String, XmlElement> indexDict = new Dictionary<String, XmlElement>();
foreach (XmlElement entry in am.GetEntries())
{
indexDict.Add(entry.GetAttribute("original").ToLower().Replace("\\", "/"), entry);
}
foreach (FileInfo file in new DirectoryInfo(am.mods).GetFiles())
{
if (file.Extension.ToLower() != "zip")
{
AddToLog($"Ignoring {file.Name} as it's not a zip!", Color.Yellow);
continue;
}
AddToLog(file.Name, Color.White);
using (ZipArchive z = ZipFile.OpenRead(file.FullName))
{
String pack = $"../Mods/{file.Name}";
foreach (var zentry in z.Entries)
{
if (zentry.Length == 0)
continue;
String key = zentry.FullName.ToLower();
if (indexDict.TryGetValue(key, out XmlElement entry))
{
entry.SetAttribute("virtual", zentry.FullName);
entry.SetAttribute("pack", pack);
entry.SetAttribute("method", "zip");
AddToLog(zentry.FullName, Color.White);
}
}
}
}
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("index");
root.SetAttribute("mod", "true");
doc.AppendChild(root);
foreach (var entry in indexDict)
{
XmlElement child = doc.CreateElement("entry");
foreach (XmlAttribute attr in entry.Value.Attributes)
{
child.SetAttribute(attr.Name, attr.Value);
}
root.AppendChild(child);
}
am.SaveIndex(doc);
}
else
{
am.SaveIndex();
}
AddToLog("Done", Color.White);
}
private void ButtonUninstall_Click(object sender, EventArgs e)
{
AddToLog("Restoring the data to vanilla state", Color.White);
File.Copy(am.indexBackUpPath, am.indexPath, true);
AddToLog("Done", Color.White);
}
private void ButtonLaunch_Click(object sender, EventArgs e)
{
// 1. patch exe
String exe_ori = Path.Combine(textBoxTroubleshooterPath.Text, "Release\\bin\\ProtoLion.exe");
/*String exe_mod = Path.Combine(textBoxTroubleshooterPath.Text, "Release\\bin\\ProtoLion_mod.exe");
if (!File.Exists(exe_mod))
{
byte[] index_pattern = Encoding.ASCII.GetBytes("\x00index\x00");
byte[] replacement = Encoding.ASCII.GetBytes("\x00ind_m\x00");
ExePatcher.PatchExe(exe_ori, exe_mod, index_pattern, replacement);
}
// 2. save modified index - TODO
String index_ori = Path.Combine(textBoxTroubleshooterPath.Text, "Package\\index");
String index_mod = Path.Combine(textBoxTroubleshooterPath.Text, "Package\\ind_m");
File.Copy(index_ori, index_mod, true);
*/
// 3. launch exe
// Prepare the process to run
ProcessStartInfo start = new ProcessStartInfo
{
// Enter in the command line arguments, everything you would enter after the executable name itself
Arguments = "--pack --usedic" + ((radioButtonConsoleYes.Checked) ? (" --console") : ""),
// Enter the executable to run, including the complete path
FileName = exe_ori
};
start.Environment.Add("SteamAPPID", "470310");
start.WorkingDirectory = Path.Combine(textBoxTroubleshooterPath.Text, "Release\\bin");
start.UseShellExecute = false;
// Do you want to show a console window?
//start.WindowStyle = ProcessWindowStyle.Hidden;
//start.CreateNoWindow = true;
/*
int exitCode;
// Run the external process & wait for it to finish
using (Process proc = Process.Start(start))
{
proc.WaitForExit();
// Retrieve the app's exit code
exitCode = proc.ExitCode;
}
*/
// Launch Detached
_ = Process.Start(start);
}
private void TextBoxTroubleshooterPath_TextChanged(object sender, EventArgs e)
{
if (Directory.Exists(textBoxTroubleshooterPath.Text))
{
if (am == null)
{
AddToLog("Initializing AssetManager", Color.White);
String troubleshooterPath = textBoxTroubleshooterPath.Text;
if ((troubleshooterPath.Length == 0) || !Directory.Exists(troubleshooterPath))
{
AddToLog($"Invalid path! {troubleshooterPath}", Color.Red);
return;
}
am = new AssetManager(textBoxTroubleshooterPath.Text);
AddToLog("Done", Color.White);
// enable elements
buttonApply.Enabled = true;
buttonExtract.Enabled = true;
buttonLaunch.Enabled = true;
buttonImagesets.Enabled = true;
buttonUninstall.Enabled = true;
buttonOpenGameFolder.Enabled = true;
if (Directory.Exists(am.data))
{
radioButtonData.Enabled = true;
}
}
}
else
{
// disable elements
buttonApply.Enabled = false;
buttonExtract.Enabled = false;
buttonLaunch.Enabled = false;
buttonImagesets.Enabled = false;
buttonUninstall.Enabled = false;
radioButtonData.Enabled = false;
buttonOpenGameFolder.Enabled = false;
am = null;
}
}
private void ButtonImageSets_Click(object sender, EventArgs e)
{
String path = Path.Combine(am.data, "CEGUI", "datafiles", "imagesets");
if (!Directory.Exists(path))
return;
foreach (FileInfo file in new DirectoryInfo(path).GetFiles())
{
if (!file.Name.EndsWith(".imageset"))
continue;
XmlDocument imageset = new XmlDocument();
imageset.Load(file.FullName);
XmlElement root = imageset.DocumentElement;
String imagePath = Path.Combine(file.DirectoryName, (String)root.GetAttribute("imagefile"));
if (!File.Exists(imagePath))
{
AddToLog($"Can't find imageset: {imagePath}", Color.Red);
continue;
}
Bitmap src = Image.FromFile(imagePath) as Bitmap;
String outPath = Path.Combine(file.DirectoryName, (String)root.GetAttribute("name"));
Directory.CreateDirectory(outPath);
AddToLog($"Extracting imageset: {imagePath}", Color.White);
foreach (XmlElement entry in root.ChildNodes)
{
AddToLog(entry.GetAttribute("name"), Color.White);
Rectangle cropRect = new Rectangle(
int.Parse(entry.GetAttribute("xPos")),
int.Parse(entry.GetAttribute("yPos")),
int.Parse(entry.GetAttribute("width")),
int.Parse(entry.GetAttribute("height"))
);
Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
using (Graphics g = Graphics.FromImage(target))
{
g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height),
cropRect,
GraphicsUnit.Pixel);
}
target.Save(Path.Combine(outPath, entry.GetAttribute("name").Trim('\n') + ".png"));
}
}
AddToLog("Done", Color.White);
}
private void ButtonOpenGameFolder_Click(object sender, EventArgs e)
{
Process.Start(new ProcessStartInfo()
{
FileName = textBoxTroubleshooterPath.Text,
UseShellExecute = true,
Verb = "open"
});
}
}
}