forked from MontagueM/Phonon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelView.xaml.cs
More file actions
494 lines (448 loc) · 18.8 KB
/
ModelView.xaml.cs
File metadata and controls
494 lines (448 loc) · 18.8 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Phonon
{
public partial class ModelView : UserControl
{
public ConcurrentDictionary<string, Package> Packages = new ConcurrentDictionary<string, Package>();
Package CurrentPkg;
private MainWindow mainWindow = null; // Reference to the MainWindow
int SelectedDynamicIndex = 0;
public ModelView()
{
InitializeComponent();
}
private void OnControlLoaded(object sender, RoutedEventArgs e)
{
mainWindow = Window.GetWindow(this) as MainWindow;
if (mainWindow.PkgCacheName != "")
{
InitialiseConfig();
}
}
private void InitialiseConfig()
{
// Check for package path and load the list
if (mainWindow.config.AppSettings.Settings[mainWindow.PkgPathKey] != null)
{
SelectPkgsDirectoryButton.Visibility = Visibility.Hidden;
LoadPackageList();
ShowPackageList();
}
else
{
System.Windows.MessageBox.Show($"No package path found for {mainWindow.ePhononType.ToString()}");
}
}
public void SelectPkgsDirectoryButton_Click(object sender, RoutedEventArgs e)
{
using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
{
dialog.Description = $"Select the packages folder for {mainWindow.ePhononType.ToString()}";
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
bool success = SetPackagePath(dialog.SelectedPath);
if (success)
{
SelectPkgsDirectoryButton.Visibility = Visibility.Hidden;
LoadPackageList();
ShowPackageList();
}
}
}
private void PkgButton_Click(object sender, RoutedEventArgs e)
{
string ClickedPackageName = (((sender as ToggleButton).Content) as TextBlock).Text;
Package pkg = Packages[ClickedPackageName];
CurrentPkg = pkg;
ShowDynamicList(pkg);
}
private void ShowDynamicList(Package pkg)
{
if (pkg.Dynamics.Count == 0)
{
System.Windows.MessageBox.Show("Package " + pkg.Name + " has no valid dynamics");
return;
}
PrimaryList.Children.Clear();
// Go back
ToggleButton btn = new ToggleButton();
Style style = Application.Current.Resources["Button_Command"] as Style;
btn.Style = style;
btn.HorizontalAlignment = HorizontalAlignment.Stretch;
btn.VerticalAlignment = VerticalAlignment.Center;
btn.Height = 40;
btn.Focusable = false;
btn.Content = "Go back to package list";
btn.Padding = new Thickness(10, 5, 0, 5);
btn.Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(61, 61, 61));
btn.Foreground = new SolidColorBrush(System.Windows.Media.Color.FromRgb(230, 230, 230));
btn.HorizontalContentAlignment = HorizontalAlignment.Left;
btn.Click += GoBack_Click;
PrimaryList.Children.Add(btn);
foreach (Dynamic dynamic in pkg.Dynamics)
{
btn = new ToggleButton();
btn.Focusable = true;
btn.Content = new TextBlock
{
Text = dynamic.GetHashString() + "\nHas Skeleton: " + dynamic.bHasSkeleton.ToString() + "\nMesh Count: " + dynamic.MeshCount.ToString(),
TextWrapping = TextWrapping.Wrap,
FontSize = 13
};
btn.Style = style;
btn.Height = 70;
btn.Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(61, 61, 61));
btn.Foreground = new SolidColorBrush(System.Windows.Media.Color.FromRgb(230, 230, 230));
btn.HorizontalContentAlignment = HorizontalAlignment.Left;
btn.Click += Dynamic_Click;
PrimaryList.Children.Add(btn);
}
ScrollView.ScrollToTop();
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if ((e.Key == Key.Down || e.Key == Key.Right) && SelectedDynamicIndex < PrimaryList.Children.Count - 1)
{
(PrimaryList.Children[SelectedDynamicIndex] as ToggleButton).IsChecked = false;
SelectedDynamicIndex++;
PrimaryList.Children[SelectedDynamicIndex].Focus();
(PrimaryList.Children[SelectedDynamicIndex] as ToggleButton).IsChecked = true;
Dynamic_Click(PrimaryList.Children[SelectedDynamicIndex], new RoutedEventArgs());
}
else if ((e.Key == Key.Up || e.Key == Key.Left) && SelectedDynamicIndex > 1)
{
(PrimaryList.Children[SelectedDynamicIndex] as ToggleButton).IsChecked = false;
SelectedDynamicIndex--;
PrimaryList.Children[SelectedDynamicIndex].Focus();
(PrimaryList.Children[SelectedDynamicIndex] as ToggleButton).IsChecked = true;
Dynamic_Click(PrimaryList.Children[SelectedDynamicIndex], new RoutedEventArgs());
}
}
private void Dynamic_Click(object sender, RoutedEventArgs e)
{
foreach (ToggleButton button in PrimaryList.Children)
{
button.IsChecked = false;
}
SelectedDynamicIndex = PrimaryList.Children.IndexOf((sender as ToggleButton));
(sender as ToggleButton).IsChecked = true;
string ClickedDynamicHash = (((sender as ToggleButton).Content) as TextBlock).Text.Split("\n")[0];
System.Diagnostics.Debug.WriteLine($"Clicked {ClickedDynamicHash}");
File.AppendAllText("debug_phonon.log", $"Clicked { ClickedDynamicHash}" + Environment.NewLine);
uint h = Convert.ToUInt32(ClickedDynamicHash, 16);
mainWindow.ExportSettings.Hash = ClickedDynamicHash;
mainWindow.ExportSettings.SaveName = "";
Dynamic dynamic = new Dynamic(h);
dynamic.GetDynamicMesh(GetPackagesPath(), mainWindow.ePhononType);
MainViewModel MVM = (MainViewModel)UCModelView.Resources["MVM"];
MVM.UpdateModel(dynamic.Vertices, dynamic.Faces);
}
private void GoBack_Click(object sender, RoutedEventArgs e)
{
ShowPackageList();
}
public void LoadPackageList()
{
if (File.Exists(mainWindow.PkgCacheName))
{
bool success = ParsePackageList();
if (!success)
{
System.Windows.MessageBox.Show("Manifest change detected, rebuilding data file.");
GeneratePackageList();
SavePackageList();
}
}
else
{
GeneratePackageList();
SavePackageList();
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath);
config.Save(ConfigurationSaveMode.Minimal);
}
}
private bool ParsePackageList()
{
BinaryReader Handle = new BinaryReader(File.Open(mainWindow.PkgCacheName, FileMode.Open));
// PkgID : Path dict
IDictionary<int, Package> PackagePaths = new Dictionary<int, Package>();
string[] files = Directory.GetFiles(GetPackagesPath(), "*.pkg", SearchOption.TopDirectoryOnly);
// First get the dictionary of name : highest patch pkg
foreach (string file in files)
{
Package pkg = new Package(file, mainWindow.ePhononType);
if (!PackagePaths.ContainsKey(pkg.Header.PkgID))
{
PackagePaths.Add(pkg.Header.PkgID, pkg);
}
else
{
if (pkg.Header.PatchID > PackagePaths[pkg.Header.PkgID].Header.PatchID)
{
PackagePaths[pkg.Header.PkgID] = pkg;
}
}
}
while (Handle.BaseStream.Position != Handle.BaseStream.Length)
{
Package pkg = new Package(mainWindow.ePhononType);
pkg.Header.PkgID = Handle.ReadUInt16();
pkg.Header.PatchID = Handle.ReadUInt16();
ushort DynamicCount = Handle.ReadUInt16();
if (DynamicCount == 0)
{
continue;
}
pkg.Dynamics = new List<Dynamic>();
for (int i = 0; i < DynamicCount; i++)
{
uint DynamicHash = Handle.ReadUInt32();
Dynamic dynamic = new Dynamic(DynamicHash);
dynamic.MeshCount = Handle.ReadUInt16();
dynamic.bHasSkeleton = Handle.ReadBoolean();
pkg.Dynamics.Add(dynamic);
}
// Get package path
pkg.Path = PackagePaths[pkg.Header.PkgID].Path;
Packages.AddOrUpdate(pkg.MakeName(), pkg, (Key, OldValue) => OldValue);
}
Handle.Close();
return true;
}
private void GeneratePackageList()
{
string[] files = Directory.GetFiles(GetPackagesPath(), "*.pkg", SearchOption.TopDirectoryOnly);
// First get the dictionary of name : highest patch pkg
foreach (string file in files)
{
if (!file.EndsWith(".pkg") || file.Contains("audio") || file.Contains("investment")) continue;
Package pkg = new Package(file, mainWindow.ePhononType);
if (!Packages.ContainsKey(pkg.Name))
{
Packages.AddOrUpdate(pkg.Name, pkg, (Key, OldValue) => OldValue);
}
else
{
if (pkg.Header.PatchID > Packages[pkg.Name].Header.PatchID)
{
Packages[pkg.Name] = pkg;
}
}
}
Parallel.ForEach(Packages.Values.ToList(), pkg =>
{
ThreadProc(pkg);
});
System.Diagnostics.Debug.WriteLine("Finished generation");
}
private void SavePackageList()
{
BinaryWriter Handle = new BinaryWriter(File.Open(mainWindow.PkgCacheName, FileMode.Create));
foreach (Package pkg in Packages.Values)
{
Handle.Write(pkg.Header.PkgID);
Handle.Write(pkg.Header.PatchID);
if (pkg.Dynamics == null)
{
Handle.Write((ushort)0);
}
else
{
Handle.Write((ushort)pkg.Dynamics.Count);
foreach (Dynamic dyn in pkg.Dynamics)
{
Handle.Write((uint)dyn.Hash);
Handle.Write((ushort)dyn.MeshCount);
Handle.Write((bool)dyn.bHasSkeleton);
}
}
}
Handle.Close();
}
private void ThreadProc(Package pkg)
{
if (!pkg.GetDynamicIndices())
{
Packages.TryRemove(pkg.Name, out pkg);
return;
}
pkg.GetDynamics(GetPackagesPath());
System.Diagnostics.Debug.WriteLine("Package " + pkg.Name);
if (pkg.Dynamics.Count == 0)
{
Packages.TryRemove(pkg.Name, out pkg);
}
}
public void ShowPackageList()
{
PrimaryList.Children.Clear();
List<string> PackageNames = Packages.Keys.ToList<string>();
PackageNames.Sort();
foreach (string PkgName in PackageNames)
{
// We want to verify that this pkg has at least 1 dynamic model in it.
ToggleButton btn = new ToggleButton();
Style style = Application.Current.Resources["Button_Command"] as Style;
btn.Style = style;
btn.HorizontalAlignment = HorizontalAlignment.Stretch;
btn.VerticalAlignment = VerticalAlignment.Center;
btn.Focusable = true;
btn.Content = new TextBlock
{
Text = PkgName,
TextWrapping = TextWrapping.Wrap,
FontSize = 13
}; ;
btn.Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(61, 61, 61));
btn.Foreground = new SolidColorBrush(System.Windows.Media.Color.FromRgb(230, 230, 230));
btn.Height = 50;
btn.Click += PkgButton_Click;
PrimaryList.Children.Add(btn);
}
ScrollView.ScrollToTop();
}
private void ButtonEnter(object sender, MouseEventArgs e)
{
(sender as ToggleButton).Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 0, 0));
}
private void ButtonLeave(object sender, MouseEventArgs e)
{
(sender as ToggleButton).Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(61, 61, 61));
}
private bool SetPackagePath(string Path)
{
if (Path == "")
{
System.Windows.MessageBox.Show("Directory selected is invalid, please select the correct packages directory.");
return false;
}
// Verify this is a valid path by checking to see if a .pkg file is inside
string[] files = Directory.GetFiles(Path, "*.pkg", SearchOption.TopDirectoryOnly);
if (files.Length == 0)
{
System.Windows.MessageBox.Show("Directory selected is invalid, please select the correct packages directory.");
return false;
}
// Checking the path fits the version
if (mainWindow.ePhononType == PhononType.Destiny1)
{
if (!files[0].Contains("ps4_"))
{
System.Windows.MessageBox.Show("Directory selected is invalid (not PS4 packages), please select the correct packages directory.");
return false;
}
}
else if (mainWindow.ePhononType == PhononType.Destiny2PREBL || mainWindow.ePhononType == PhononType.Destiny2BL)
{
if (!files[0].Contains("w64_"))
{
System.Windows.MessageBox.Show("Directory selected is invalid (not PC packages), please select the correct packages directory.");
return false;
}
}
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath);
config.AppSettings.Settings.Remove(mainWindow.PkgPathKey);
config.AppSettings.Settings.Add(mainWindow.PkgPathKey, Path);
config.Save(ConfigurationSaveMode.Minimal);
return true;
}
public string GetPackagesPath()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath);
return config.AppSettings.Settings[mainWindow.PkgPathKey].Value.ToString();
}
// Menu buttons
private void Grid_Checked(object sender, RoutedEventArgs e)
{
if (HelixGrid != null)
{
HelixGrid.Visible = true;
}
}
private void Grid_Unchecked(object sender, RoutedEventArgs e)
{
if (HelixGrid != null)
{
HelixGrid.Visible = false;
}
}
private void Export_Clicked(object sender, RoutedEventArgs e)
{
if (mainWindow.ExportSettings.Hash == "")
{
System.Windows.MessageBox.Show("No dynamic model selected");
return;
}
using (var dialog = new System.Windows.Forms.SaveFileDialog())
{
dialog.Filter = "Model files | *.fbx";
dialog.DefaultExt = "fbx";
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
mainWindow.ExportSettings.Path = dialog.FileName;
}
mainWindow.ExportSettings.SaveName = "";
bool status = mainWindow.ExportSettings.Export(GetPackagesPath());
if (status)
{
System.Windows.MessageBox.Show("Export success");
}
else
{
System.Windows.MessageBox.Show("Export failed");
}
}
private void ExportAll_Clicked(object sender, RoutedEventArgs e)
{
if (CurrentPkg.Dynamics.Count == 0)
{
System.Windows.MessageBox.Show("No PKG selected");
return;
}
string OutputPath;
using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
{
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
OutputPath = dialog.SelectedPath;
}
if (OutputPath == "")
{
System.Windows.MessageBox.Show("No output path selected");
return;
}
bool status = true;
foreach (Dynamic dynamic in CurrentPkg.Dynamics)
{
mainWindow.ExportSettings.Hash = dynamic.HashString;
mainWindow.ExportSettings.Path = $"{OutputPath}/{CurrentPkg}/{dynamic.Hash.ToString()}.fbx";
status &= mainWindow.ExportSettings.Export(GetPackagesPath());
}
if (status)
{
System.Windows.MessageBox.Show("Export success");
}
else
{
System.Windows.MessageBox.Show("Export failed");
}
}
}
}