-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
470 lines (443 loc) · 17.1 KB
/
MainWindow.xaml.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;
namespace BLHXChgPainting
{
public partial class MainWindow : Window
{
private bool loadList = false;
public MainWindow()
{
InitializeComponent();
}
private void SelectHashes(object sender, RoutedEventArgs e)
{
var op = new OpenFileDialog();
op.Filter = "Hashes.csv|hashes.csv";
op.Title = "打开Hashes.csv文件...";
if (op.ShowDialog() == true)
{
Vm.HashesPath = op.FileName;
}
}
private void SelectBundle(object sender, RoutedEventArgs e)
{
var op = new OpenFileDialog();
op.Filter = "资源文件|*.*";
op.Title = "打开painting目录下的资源文件...";
if (op.ShowDialog() == true)
{
if (LoadImageFromBundle(op.FileName))
{
MessageBox.Show("加载成功。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
Vm.BundleFile = op.FileName;
}
}
}
private void SelectPngFile(object sender, RoutedEventArgs e)
{
var op = new OpenFileDialog
{
Filter = "png文件|*.png",
Title = "打开要替换的png文件..."
};
if (op.ShowDialog() != true) return;
Vm.PngFile = op.FileName;
var img = new BitmapImage(new Uri(Vm.PngFile));
Vm.PngImg = img;
}
private readonly Random _random = new Random((int)DateTime.Now.ToBinary());
private class HashesItem
{
public string AssetName { get; set; }
public int AssetSize { get; set; }
public string AssetMd5 { get; set; }
}
private List<HashesItem> ReadHashesCsv()
{
var lst = new List<HashesItem>();
using (var stream = new StreamReader(Vm.HashesPath))
{
string line;
while ((line = stream.ReadLine()) != null)
{
var row = line.Split(',');
lst.Add(new HashesItem
{
AssetName = row[0],
AssetSize = int.Parse(row[1]),
AssetMd5 = row[2]
});
}
}
return lst;
}
private bool WriteHashesCsv(List<HashesItem> lst)
{
try
{
using (var stream = new StreamWriter(Vm.HashesPath))
{
lst.ForEach(item =>
{
stream.WriteLine("{0},{1},{2}", item.AssetName, item.AssetSize, item.AssetMd5);
});
}
return true;
}
catch(Exception e)
{
MessageBox.Show(e.Message);
return false;
}
}
private static string Md5Value(string fileName)
{
MD5 md5 = new MD5CryptoServiceProvider();
if (!File.Exists(fileName))
return string.Empty;
var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
var md5Ch = md5.ComputeHash(fs);
fs.Close();
md5.Clear();
var strMd5 = md5Ch.Aggregate("", (current, b) => current + b.ToString("x").PadLeft(2, '0'));
return strMd5.ToLower();
}
private static async Task<string> Md5ValueAsync(string fileName)
{
MD5 md5 = new MD5CryptoServiceProvider();
if (!File.Exists(fileName))
return string.Empty;
var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
var md5Ch = await Task.Run(() => md5.ComputeHash(fs));
fs.Close();
md5.Clear();
var strMd5 = md5Ch.Aggregate("", (current, b) => current + b.ToString("x").PadLeft(2, '0'));
return strMd5.ToLower();
}
private void Process(object sender, RoutedEventArgs e)
{
#if (DEBUG)
var debugOutput = "d:\\temp\\debug_output";
File.Copy(Vm.BundleFile, debugOutput, true);
if (!AssetTool.ReplaceImageFile(debugOutput, Vm.PngFile, Vm.TextureList[Vm.CurSel]))
{
MessageBox.Show("修改文件出错。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
MessageBox.Show("修改文件成功。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
#else
int idx;
if (Vm.BundleFile == "" || Vm.HashesPath == "" || Vm.PngFile == "")
{
var errMsg = new List<string>
{
"你在逗我?",
"搞笑吧?",
"作死?"
};
idx = _random.Next(0, errMsg.Count);
MessageBox.Show(errMsg[idx], errMsg[idx], MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (Vm.TextureList.Count == 0 || Vm.CurSel >= Vm.TextureList.Count)
{
return;
}
if (MessageBox.Show(Vm.ProcessTips, "继续吗?", MessageBoxButton.YesNo, MessageBoxImage.Warning) !=
MessageBoxResult.Yes) return;
var assetList = ReadHashesCsv();
idx = assetList.FindIndex(item => Vm.BundleFile.EndsWith(item.AssetName.Replace("/", "\\")));
if (idx < 0)
{
MessageBox.Show("请保持游戏原始目录结构。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
var backupError = false;
try
{
File.Copy(Vm.BundleFile, Vm.BundleFile + ".bak", true);
File.Copy(Vm.HashesPath, Vm.HashesPath + ".bak", true);
}
catch
{
backupError = true;
if (MessageBox.Show("备份文件出错,是否继续?", "继续吗?", MessageBoxButton.YesNo, MessageBoxImage.Warning) !=
MessageBoxResult.Yes) return;
}
if (!AssetTool.ReplaceImageFile(Vm.BundleFile, Vm.PngFile, Vm.TextureList[Vm.CurSel]))
{
MessageBox.Show("修改文件出错。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
try
{
File.Copy(Vm.BundleFile + ".bak", Vm.BundleFile, true);
File.Copy(Vm.HashesPath + ".bak", Vm.HashesPath, true);
}
catch
{
MessageBox.Show("恢复备份文件出错,祝福你。", ">_<", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
else
{
assetList[idx].AssetMd5 = Md5Value(Vm.BundleFile);
try
{
using (var f = File.Open(Vm.BundleFile, FileMode.Open))
{
assetList[idx].AssetSize = (int) f.Length;
}
}
catch
{
// ignored
}
if (WriteHashesCsv(assetList))
{
MessageBox.Show("替换成功。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
if (backupError)
{
MessageBox.Show("修改Hashes.csv出错,请手工修改Hashes.csv或重试。", "错误", MessageBoxButton.OK,
MessageBoxImage.Error);
}
else
{
if (MessageBox.Show("修改Hashes.csv出错,是否保留修改后的资源文件?(若保留,则需手工修改Hashes.csv)", "提示",
MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) return;
//不保留
try
{
File.Copy(Vm.BundleFile + ".bak", Vm.BundleFile, true);
File.Copy(Vm.HashesPath + ".bak", Vm.HashesPath, true);
}
catch
{
MessageBox.Show("恢复备份文件出错,祝福你。", ">_<", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
}
#endif
}
private static byte[] GetImageFromBundle(string bundleFile, string textureName)
{
var bufSize = 0;
if (!AssetTool.GetImageInfo(bundleFile, ref bufSize, textureName)) return null;
var buf = Marshal.AllocHGlobal(bufSize);
var image = new byte[bufSize];
if (!AssetTool.LoadImageFromBundle(bundleFile, buf, textureName))
{
Marshal.FreeHGlobal(buf);
return null;
}
Marshal.Copy(buf, image, 0, bufSize);
return image;
}
private bool LoadImageFromBundle(string bundleFile, string textureFile)
{
var image = GetImageFromBundle(bundleFile, Vm.TextureList[Vm.CurSel]);
if (image == null)
{
MessageBox.Show("加载图片失败。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
try
{
var img = new BitmapImage();
img.BeginInit();
img.StreamSource = new MemoryStream(image);
img.EndInit();
Vm.Img = img;
}
catch (Exception e)
{
MessageBox.Show(e.Message, "发生错误", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
return true;
}
private bool LoadImageFromBundle(string bundleFile)
{
loadList = true;
Vm.TextureList.Clear();
var lst = AssetTool.GetTextureList(bundleFile);
if (lst == IntPtr.Zero)
{
MessageBox.Show("加载图片失败。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
var texLst = Marshal.PtrToStringAnsi(lst);
if (texLst == null)
{
MessageBox.Show("加载图片失败。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
foreach (var s in texLst.Split(','))
{
Vm.TextureList.Add(s);
}
Vm.CurSel = 0;
loadList = false;
return LoadImageFromBundle(bundleFile, Vm.TextureList[Vm.CurSel]);
}
private void SaveOri(object sender, RoutedEventArgs re)
{
var image = GetImageFromBundle(Vm.BundleFile, Vm.TextureList[Vm.CurSel]);
if (image == null)
{
MessageBox.Show("加载图片失败。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
var op = new SaveFileDialog
{
Filter = "png文件|*.png",
Title = "保存图片..."
};
if (op.ShowDialog() != true) return;
try
{
using (var f = new FileStream(op.FileName, FileMode.Create))
{
f.Write(image, 0, image.Length);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "发生错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void SelctTexture(object sender, SelectionChangedEventArgs e)
{
if (Vm.CurSel < 0 || Vm.CurSel >= Vm.TextureList.Count || loadList) return;
LoadImageFromBundle(Vm.BundleFile, Vm.TextureList[Vm.CurSel]);
}
private void SelectFolders(string filename, IList<Node> childrens)
{
var dirInfo = new DirectoryInfo(filename);
var subDirectories = dirInfo.GetDirectories();
foreach (var directory in subDirectories)
{
var dir = new DirectoryNode
{
Name = directory.Name,
IsDir = true
};
childrens.Add(dir);
SelectFolders(directory.FullName, dir.Childrens);
}
var info = dirInfo.GetFiles("*.*");
foreach (var f in info)
{
var dof = new FileNode
{
Name = f.Name,
FullName = f.FullName,
IsDir = false
};
childrens.Add(dof);
}
}
private void OpenForBroswer(object sender, RoutedEventArgs e)
{
var dlg = new CommonOpenFileDialog(@"打开游戏资源目录(AssetBundles)...") {IsFolderPicker = true};
if (dlg.ShowDialog() != CommonFileDialogResult.Ok) return;
Browser.DirPath = dlg.FileName;
Browser.Root.Clear();
SelectFolders(dlg.FileName, Browser.Root);
}
private void BrowserLoadImage()
{
var node = (Node)BrowserTree.SelectedItem;
if (node.IsDir) return;
var fnode = (FileNode)node;
var image = GetImageFromBundle(fnode.FullName, Browser.TextureList[Browser.CurSel]);
if (image == null)
{
MessageBox.Show("加载图片失败。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
try
{
var img = new BitmapImage();
img.BeginInit();
img.StreamSource = new MemoryStream(image);
img.EndInit();
Browser.Img = img;
}
catch (Exception e)
{
MessageBox.Show(e.Message, "发生错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void OnFileSelect(object sender, RoutedEventArgs ev)
{
var node = (Node)BrowserTree.SelectedItem;
if (node == null || node.IsDir) return;
var fnode = (FileNode) node;
Browser.ListLoading = true;
Browser.TextureList.Clear();
var lst = AssetTool.GetTextureList(fnode.FullName);
if (lst == IntPtr.Zero)
{
MessageBox.Show("加载图片失败。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
var texLst = Marshal.PtrToStringAnsi(lst);
if (texLst == null)
{
MessageBox.Show("加载图片失败。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
foreach (var s in texLst.Split(','))
{
Browser.TextureList.Add(s);
}
Browser.CurSel = 0;
Browser.ListLoading = false;
BrowserLoadImage();
}
private void EditSelectedItem(object sender, RoutedEventArgs e)
{
var node = (Node)BrowserTree.SelectedItem;
if (node.IsDir) return;
var fnode = (FileNode)node;
if (!LoadImageFromBundle(fnode.FullName)) return;
MessageBox.Show("加载成功。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
Vm.BundleFile = fnode.FullName;
MainTabControl.SelectedIndex = 0;
}
private void BrowserSelctTexture(object sender, SelectionChangedEventArgs e)
{
if (Browser.ListLoading) return;
BrowserLoadImage();
}
private void ExportAll(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("全部导出时间较长,确定要继续吗?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.No) return;
var dlg = new CommonOpenFileDialog(@"选择输出路径...") { IsFolderPicker = true };
if (dlg.ShowDialog() != CommonFileDialogResult.Ok) return;
var inputBase = Browser.DirPath;
var outputBase = dlg.FileName;
if (inputBase == outputBase)
{
MessageBox.Show("输出文件夹不能与输入文件夹相同。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
var progress = new Progress {Owner = this};
progress.BeginExport(inputBase, outputBase);
progress.ShowDialog();
}
}
}