-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPackBrowserControl.cs
209 lines (181 loc) · 8.17 KB
/
PackBrowserControl.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
using Autofac;
using BrightIdeasSoftware;
using HgmViewer.Classes;
using HgmViewer.Service;
using Krypton.Navigator;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HgmViewer
{
/// <summary>
/// Browser control for Pack-files (.hpk)
/// </summary>
public partial class PackBrowserControl : UserControl
{
private readonly ApplicationService _applicationService;
private readonly ConfigService _configService;
private readonly PackService _packService;
private readonly ExportService _exportService;
private readonly ViewerService _viewerService;
public PackBrowserControl(
ApplicationService applicationService,
ConfigService configService,
PackService packService,
ExportService exportService,
ViewerService viewerService
)
{
InitializeComponent();
_applicationService = applicationService;
_configService = configService;
_packService = packService;
_exportService = exportService;
_viewerService = viewerService;
Load += PackBrowserControl_Load;
}
private void PackBrowserControl_Load(object sender, EventArgs e)
{
txtFilter.KeyDown += TxtFilter_KeyDown;
tlvPackBrowser.CanExpandGetter = (object x) => x is TreeEntry te && te.Children.Count > 0;
tlvPackBrowser.ChildrenGetter = (object x) =>
{
var te = x as TreeEntry;
return te.Children;
};
LoadDir(_configService.Config.GameDirPath);
}
private void TxtFilter_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (txtFilter.Text.Length > 0)
{
var filter = txtFilter.Text;
tlvPackBrowser.ExpandAll();
tlvPackBrowser.ModelFilter = new ModelFilter((object x) =>
{
var te = x as TreeEntry;
return te.Name.Contains(filter, StringComparison.OrdinalIgnoreCase);
});
}
else
{
tlvPackBrowser.ModelFilter = null;
tlvPackBrowser.CollapseAll();
}
}
}
private void tlvPackBrowser_ItemActivate(object sender, EventArgs e)
{
if (tlvPackBrowser.SelectedObject is TreeEntry item && item.IsFile && Path.GetExtension(item.FilePath).Equals(".hgm", StringComparison.OrdinalIgnoreCase))
{
var unpacked = _packService.UnpackHgm(item, unpackMaterialAndTextures: true);
if (unpacked == null) return;
var exported = _exportService.ExportGlb(unpacked);
_viewerService.LoadGlb(exported);
}
}
private void tlvPackBrowser_CellRightClick(object sender, CellRightClickEventArgs e)
{
var selectedFiles = tlvPackBrowser.SelectedObjects.OfType<TreeEntry>().Where(x => x.IsFile).ToArray();
var menu = new ContextMenuStrip();
if (selectedFiles.Length > 0)
{
menu.Items.Add("Extract selected file(s)", null, async (e, s) =>
{
var extracted = await Task.Run(() =>
{
var packs = selectedFiles.GroupBy(x => x.PackFilePath).ToArray();
var extracted = new List<string>();
foreach (var pack in packs)
{
_applicationService.SetStatusBarText($"Extracting {pack.Count()} file(s) from {Path.GetFileName(pack.Key)}");
var unpackedFilePaths = _packService.UnpackFiles(pack.Key, pack.Select(x => x.FilePath).ToArray());
extracted.AddRange(unpackedFilePaths);
}
_applicationService.SetStatusBarText($"Completed ({extracted.Count}/{selectedFiles.Length})");
return extracted;
});
});
}
var hgms = tlvPackBrowser.SelectedObjects.OfType<TreeEntry>().Where(x => x.IsFile && Path.GetExtension(x.FilePath).Equals(".hgm", StringComparison.OrdinalIgnoreCase)).ToArray();
if (hgms.Length > 0)
{
menu.Items.Add("Export selected Hgm(s)", null, async (e, s) =>
{
var exported = await Task.Run(() => _packService.UnpackAndExportMultiple(hgms, new Progress<ExportAndUnpackProgress>((p) =>
{
_applicationService.SetStatusBarText(
$"{p.Action} ({p.Current}/{p.Total})"
+ (p.FileName != null ? $": {p.FileName}" : "")
+ (p.ErrorMessage != null ? $" [{p.ErrorMessage}]" : "")
);
})));
});
menu.Items.Add("Export selected Hgm(s) + material/textures", null, async (e, s) =>
{
var exported = await Task.Run(() => _packService.UnpackAndExportMultiple(hgms, new Progress<ExportAndUnpackProgress>((p) =>
{
_applicationService.SetStatusBarText(
$"{p.Action} ({p.Current}/{p.Total})"
+ (p.FileName != null ? $": {p.FileName}" : "")
+ (p.ErrorMessage != null ? $" [{p.ErrorMessage}]" : "")
);
}), unpackMaterialAndTextures: true));
});
}
var dds = tlvPackBrowser.SelectedObjects.OfType<TreeEntry>().Where(x => x.IsFile && Path.GetExtension(x.FilePath).Equals(".dds", StringComparison.OrdinalIgnoreCase)).ToArray();
if (dds.Length > 0)
{
menu.Items.Add("Extract selected dds(s) + convert to .tga", null, async (e, s) =>
{
var extracted = await Task.Run(() =>
{
var packs = dds.GroupBy(x => x.PackFilePath).ToArray();
var extracted = new List<string>();
foreach (var pack in packs)
{
_applicationService.SetStatusBarText($"Extracting {pack.Count()} file(s) from {Path.GetFileName(pack.Key)}");
var unpackedFilePaths = _packService.UnpackFiles(pack.Key, pack.Select(x => x.FilePath).ToArray());
foreach (var unpackedFilePath in unpackedFilePaths)
{
Helpers.ConvertDDS(unpackedFilePath, Path.ChangeExtension(unpackedFilePath, ".tga"));
}
extracted.AddRange(unpackedFilePaths);
}
_applicationService.SetStatusBarText($"Completed ({extracted.Count}/{dds.Length})");
return extracted;
});
});
}
if (menu.Items.Count == 0) return;
e.MenuStrip = menu;
}
internal void LoadDir(string dirPath)
{
_packService.LoadDir(dirPath);
tlvPackBrowser.SetObjects(_packService.Entries);
}
/// <summary>
/// Krypton Docking page for <see cref="PackBrowserControl"/>
/// </summary>
public class PackBrowserPage : KryptonPage
{
private readonly ILifetimeScope _scope;
private readonly PackBrowserControl _ctrl;
public PackBrowserControl Ctrl => _ctrl;
public PackBrowserPage(ILifetimeScope scope)
{
_scope = scope;
_ctrl = scope.Resolve<PackBrowserControl>();
_ctrl.Dock = DockStyle.Fill;
Controls.Add(_ctrl);
}
}
}
}