-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathProgram.cs
334 lines (314 loc) · 10.4 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibGGPK;
using System.IO;
using System.Linq.Expressions;
using LibDat;
using Ionic.Zip;
using LibGGPK.Records;
namespace PatchGGPK
{
class Program
{
static void Output(string msg)
{
Console.Write(msg);
}
private static void OutputLine(string msg)
{
Output(msg + Environment.NewLine);
}
private static string contentGGPK = @"\Content.ggpk";
private static string ggpkPath = Directory.GetCurrentDirectory() + contentGGPK;
private static Dictionary<string, FileRecord> RecordsByPath;
private static GrindingGearsPackageContainer content = null;
private static List<string> ggpkPaths = new List<string>();
public static void Main(string[] args)
{
string[] archiveFiles;
if (args.Length > 0)
{
archiveFiles = args;
}
else
{
archiveFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.zip");
}
if (archiveFiles.Length > 0)
{
InitGGPK();
foreach (string archivePath in archiveFiles)
{
InitPatchArchive(archivePath);
}
}
// Addons
if (Directory.Exists(Directory.GetCurrentDirectory() + @"\Addons\"))
{
archiveFiles = Directory.GetFiles(Directory.GetCurrentDirectory()+@"\Addons\", "*.zip");
if (archiveFiles.Length > 0)
{
InitGGPK();
foreach (string archivePath in archiveFiles)
{
string msg = string.Format("Apply {0} [y/N]? ", Path.GetFileName(archivePath));
Output(msg);
ConsoleKeyInfo cki = Console.ReadKey();
OutputLine("");
if (cki.Key == ConsoleKey.Y)
{
InitPatchArchive(archivePath);
}
}
}
}
OutputLine("Press any key to continue...");
Console.ReadKey();
}
private static void searchContentGGPK()
{
string contentGGPK = @"\Content.ggpk";
string ggpkPath = Directory.GetCurrentDirectory() + contentGGPK;
Microsoft.Win32.RegistryKey start = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey programName = start.OpenSubKey(@"SOFTWARE\Garena\POETW");
if (File.Exists(ggpkPath))
{
if (!ggpkPaths.Exists(e => e.Equals(ggpkPath)))
ggpkPaths.Add(ggpkPath);
}
// GarenaTW
start = Microsoft.Win32.Registry.LocalMachine;
programName = start.OpenSubKey(@"SOFTWARE\Wow6432Node\Garena\POETW");
if (programName != null)
{
string pathString = (string)programName.GetValue("Path");
if (pathString != string.Empty && File.Exists(pathString + contentGGPK))
{
ggpkPath = pathString + contentGGPK;
if (!ggpkPaths.Exists(e => e.Equals(ggpkPath)))
ggpkPaths.Add(ggpkPath);
}
}
// GarenaTW reg
start = Microsoft.Win32.Registry.LocalMachine;
programName = start.OpenSubKey(@"SOFTWARE\Garena\POETW");
if (programName != null)
{
string pathString = (string)programName.GetValue("Path");
if (pathString != string.Empty && File.Exists(pathString + contentGGPK))
{
ggpkPath = pathString + contentGGPK;
if (!ggpkPaths.Exists(e => e.Equals(ggpkPath)))
ggpkPaths.Add(ggpkPath);
}
}
// GarenaTW x86 path
if (File.Exists(@"C:\Program Files (x86)\GarenaPoETW\GameData\Apps\POETW" + contentGGPK))
{
ggpkPath = @"C:\Program Files (x86)\GarenaPoETW\GameData\Apps\POETW" + contentGGPK;
if (!ggpkPaths.Exists(e => e.Equals(ggpkPath)))
ggpkPaths.Add(ggpkPath);
}
// GarenaTW 32b path
if (File.Exists(@"C:\Program Files\GarenaPoETW\GameData\Apps\POETW" + contentGGPK))
{
ggpkPath = @"C:\Program Files\GarenaPoETW\GameData\Apps\POETW" + contentGGPK;
if (!ggpkPaths.Exists(e => e.Equals(ggpkPath)))
ggpkPaths.Add(ggpkPath);
}
// GGG path
start = Microsoft.Win32.Registry.CurrentUser;
programName = start.OpenSubKey(@"Software\GrindingGearGames\Path of Exile");
if (programName != null)
{
string pathString = (string)programName.GetValue("InstallLocation");
if (pathString != string.Empty && File.Exists(pathString + contentGGPK))
{
ggpkPath = pathString + contentGGPK;
if (!ggpkPaths.Exists(e => e.Equals(ggpkPath)))
ggpkPaths.Add(ggpkPath);
}
}
// GGG x86
if (File.Exists(@"C:\Program Files (x86)\Grinding Gear Games\Path of Exile" + contentGGPK))
{
ggpkPath = @"C:\Program Files (x86)\Grinding Gear Games\Path of Exile" + contentGGPK;
if (!ggpkPaths.Exists(e => e.Equals(ggpkPath)))
ggpkPaths.Add(ggpkPath);
}
// GGG 32b
if (File.Exists(@"C:\Program Files\Grinding Gear Games\Path of Exile" + contentGGPK))
{
ggpkPath = @"C:\Program Files\Grinding Gear Games\Path of Exile" + contentGGPK;
if (!ggpkPaths.Exists(e => e.Equals(ggpkPath)))
ggpkPaths.Add(ggpkPath);
}
// GGG reg
start = Microsoft.Win32.Registry.LocalMachine;
programName = start.OpenSubKey(@"SOFTWARE\Wow6432Node\Garena\PoE");
if (programName != null)
{
string pathString = (string)programName.GetValue("Path");
if (pathString != string.Empty && File.Exists(pathString + contentGGPK))
{
ggpkPath = pathString + contentGGPK;
if (!ggpkPaths.Exists(e => e.Equals(ggpkPath)))
ggpkPaths.Add(ggpkPath);
}
}
// GareneHK reg
start = Microsoft.Win32.Registry.LocalMachine;
programName = start.OpenSubKey(@"SOFTWARE\Garena\PoE");
if (programName != null)
{
string pathString = (string)programName.GetValue("Path");
if (pathString != string.Empty && File.Exists(pathString + contentGGPK))
{
ggpkPath = pathString + contentGGPK;
if (!ggpkPaths.Exists(e => e.Equals(ggpkPath)))
ggpkPaths.Add(ggpkPath);
}
}
// GarenaHK x64
if (File.Exists(@"C:\Program Files (x86)\GarenaPoE\GameData\Apps\PoE" + contentGGPK))
{
ggpkPath = @"C:\Program Files (x86)\GarenaPoE\GameData\Apps\PoE" + contentGGPK;
if (!ggpkPaths.Exists(e => e.Equals(ggpkPath)))
ggpkPaths.Add(ggpkPath);
}
// GarenaHK 32b
if (File.Exists(@"C:\Program Files\GarenaPoE\GameData\Apps\PoE" + contentGGPK))
{
ggpkPath = @"C:\Program Files\GarenaPoE\GameData\Apps\PoE" + contentGGPK;
if (!ggpkPaths.Exists(e => e.Equals(ggpkPath)))
ggpkPaths.Add(ggpkPath);
}
}
private static void InitGGPK()
{
if (content != null)
return;
searchContentGGPK();
if (ggpkPaths.Count == 1)
{
ggpkPath = ggpkPaths[0];
}
else if (ggpkPaths.Count > 1)
{
int pos = 0;
foreach (string ggpkPath2 in ggpkPaths)
{
OutputLine(string.Format("[{0}] {1}", pos++, ggpkPath2));
}
OutputLine(string.Format("Choose [0-{0}]: ", pos-1));
ConsoleKeyInfo cki = Console.ReadKey();
OutputLine("");
Int32 number;
if (Int32.TryParse(cki.KeyChar.ToString(), out number))
{
if (number >= 0 && number <= pos-1)
ggpkPath = ggpkPaths[number];
}
}
if (!File.Exists(ggpkPath))
{
OutputLine(string.Format("GGPK {0} not exists.", ggpkPath));
return;
}
OutputLine(string.Format("Parsing {0}", ggpkPath));
content = new GrindingGearsPackageContainer();
content.Read(ggpkPath, Output);
RecordsByPath = new Dictionary<string, FileRecord>(content.RecordOffsets.Count);
DirectoryTreeNode.TraverseTreePostorder(content.DirectoryRoot, null, n => RecordsByPath.Add(n.GetDirectoryPath() + n.Name, n as FileRecord));
}
private static void InitPatchArchive(string archivePath)
{
if (content != null)
{
if (content.IsReadOnly)
{
OutputLine("Content.ggpk is Read Only.");
}
else if (File.Exists(archivePath) && Path.GetExtension(archivePath).ToLower() == ".zip")
{
HandlePatchArchive(archivePath);
}
}
}
private static void HandlePatchArchive(string archivePath)
{
using (ZipFile zipFile = new ZipFile(archivePath))
{
OutputLine(string.Format("Archive {0}", archivePath));
/*
foreach (var item in zipFile.Entries)
{
if (item.FileName.Equals("version.txt"))
{
using (var reader = item.OpenReader())
{
byte[] versionData = new byte[item.UncompressedSize];
reader.Read(versionData, 0, versionData.Length);
string versionStr = Encoding.UTF8.GetString(versionData, 0, versionData.Length);
if (RecordsByPath.ContainsKey("patch_notes.rtf"))
{
string Hash = BitConverter.ToString(RecordsByPath["patch_notes.rtf"].Hash);
if (versionStr.Substring(0, Hash.Length).Equals(Hash))
{
VersionCheck = true;
}
}
}
break;
}
else if (Path.GetDirectoryName(item.FileName) == "Data" && Path.GetExtension(item.FileName).ToLower() == ".dat")
{
NeedVersionCheck = true;
}
else if (Path.GetDirectoryName(item.FileName) == "Metadata" && Path.GetExtension(item.FileName).ToLower() == ".txt")
{
NeedVersionCheck = true;
}
}
if (NeedVersionCheck && !VersionCheck)
{
OutputLine("Version Check Failed");
return;
}
*/
foreach (var item in zipFile.Entries)
{
if (item.IsDirectory)
{
continue;
}
if (item.FileName.Equals("version.txt"))
{
continue;
}
string fixedFileName = "ROOT" + Path.DirectorySeparatorChar + item.FileName;
if (Path.DirectorySeparatorChar != '/')
{
fixedFileName = fixedFileName.Replace('/', Path.DirectorySeparatorChar);
}
if (!RecordsByPath.ContainsKey(fixedFileName))
{
OutputLine(string.Format("Failed {0}", fixedFileName));
continue;
}
OutputLine(string.Format("Replace {0}", fixedFileName));
using (var reader = item.OpenReader())
{
byte[] replacementData = new byte[item.UncompressedSize];
reader.Read(replacementData, 0, replacementData.Length);
RecordsByPath[fixedFileName].ReplaceContents(ggpkPath, replacementData, content.FreeRoot);
}
}
OutputLine("Content.ggpk is Fine.");
}
}
}
}