forked from MontagueM/Phonon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
640 lines (572 loc) · 24 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
640 lines (572 loc) · 24 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public enum PhononType
{
[Description("Destiny1")]
Destiny1 = 1,
[Description("Destiny2PREBL")]
Destiny2PREBL = 2,
[Description("Destiny2BL")]
Destiny2BL = 3,
}
public partial class MainWindow : Window
{
ConcurrentDictionary<string, Package> Packages = new ConcurrentDictionary<string, Package>();
List<Dynamic> currentdynamics = new List<Dynamic>(); //probably a better way of doing this
string currentpkg = "";
Exporter ExportSettings = new Exporter();
PhononType ePhononType;
string PkgPathKey = "";
string PkgCacheName = "";
public MainWindow()
{
InitializeComponent();
InitialiseConfig();
}
private void InitialiseConfig()
{
// Check if we need to get the package path first
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath);
// Check what version to load (BL or pre-BL)
if (config.AppSettings.Settings["Version"] != null)
{
if (config.AppSettings.Settings["Version"].Value == PhononType.Destiny2BL.ToString())
{
ePhononType = PhononType.Destiny2BL;
Wind.Title = "Phonon BL";
PkgPathKey = "PackagesPathBL";
PkgCacheName = "packagesBL.dat";
Destiny2BL.IsChecked = true;
}
else if (config.AppSettings.Settings["Version"].Value == PhononType.Destiny2PREBL.ToString())
{
ePhononType = PhononType.Destiny2PREBL;
Wind.Title = "Phonon PRE-BL";
PkgPathKey = "PackagesPathPREBL";
PkgCacheName = "packagesPREBL.dat";
Destiny2PreBL.IsChecked = true;
}
else if (config.AppSettings.Settings["Version"].Value == PhononType.Destiny1.ToString())
{
ePhononType = PhononType.Destiny1;
Wind.Title = "Phonon D1";
PkgPathKey = "PackagesPathD1";
PkgCacheName = "packagesD1.dat";
Destiny1.IsChecked = true;
}
else
{
System.Windows.MessageBox.Show("Incorrect value set for 'Version', defaulting to Beyond Light settings");
ePhononType = PhononType.Destiny2BL;
Wind.Title = "Phonon BL";
PkgPathKey = "PackagesPathBL";
PkgCacheName = "packagesBL.dat";
}
}
else
{
System.Windows.MessageBox.Show("Defaulting to Beyond Light settings");
ePhononType = PhononType.Destiny2BL;
Wind.Title = "Phonon BL";
PkgPathKey = "PackagesPathBL";
PkgCacheName = "packagesBL.dat";
config.AppSettings.Settings.Add("Version", PhononType.Destiny2BL.ToString());
config.Save(ConfigurationSaveMode.Minimal);
}
// Check for package path and load the list
if (config.AppSettings.Settings[PkgPathKey] != null)
{
SelectPkgsDirectoryButton.Visibility = Visibility.Hidden;
LoadPackageList();
ShowPackageList();
}
else
{
System.Windows.MessageBox.Show($"No package path found for {ePhononType.ToString()}");
}
}
private void SelectPkgsDirectoryButton_Click(object sender, RoutedEventArgs e)
{
using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
{
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)
{
currentdynamics.Clear();
string ClickedPackageName = (((sender as ToggleButton).Content) as TextBlock).Text;
currentpkg = ClickedPackageName;
Package pkg = Packages[ClickedPackageName];
currentdynamics = pkg.Dynamics;
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);
int btnNum = 0;
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;
btn.KeyDown += PrimaryList_KeyDown;
btn.PreviewGotKeyboardFocus += Dynamic_Click;
if (btnNum == 0)
{
btn.Focus();
}
if(btn.IsFocused == false)
{
btn.IsChecked = false;
}
PrimaryList.Children.Add(btn);
btnNum++;
}
ScrollView.ScrollToTop();
}
private void PrimaryList_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Down)
{
//this.Dynamic_Click(sender, e);
this.PredictFocus(FocusNavigationDirection.Down);
this.MoveFocus(FocusNavigationDirection.Down);
}
if (e.Key == Key.Up)
{
//this.Dynamic_Click(sender, e);
this.PredictFocus(FocusNavigationDirection.Up);
this.MoveFocus(FocusNavigationDirection.Up);
}
}
private void MoveFocus(FocusNavigationDirection direction)
{
var request = new TraversalRequest(direction);
this.MoveFocus(request);
}
private void Dynamic_Click(object sender, RoutedEventArgs e)
{
(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);
ExportSettings.Hash = ClickedDynamicHash;
Dynamic dynamic = new Dynamic(h);
dynamic.GetDynamicMesh(GetPackagesPath(), ePhononType);
MainViewModel MVM = (MainViewModel)Wind.Resources["MVM"];
MVM.UpdateModel(dynamic.Vertices, dynamic.Faces);
}
private void GoBack_Click(object sender, RoutedEventArgs e)
{
ShowPackageList();
}
private void LoadPackageList()
{
if (File.Exists(PkgCacheName))
{
bool success = ParsePackageList();
if (!success) // TODO properly implement this, currently always returns true
{
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(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, 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(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, 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;
}
}
}
foreach (Package pkg in Packages.Values.ToList())
{
//ThreadPool.QueueUserWorkItem(ThreadProc, new object[] { pkg });
ThreadProc(pkg);
}
int workerThreads = 0; int completionPortThreads = 0; int maxWorkerThreads = 0; int maxCompletionPortThreads = 0;
ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads);
//while (workerThreads != maxWorkerThreads)
//{
// ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
// continue;
//}
System.Diagnostics.Debug.WriteLine("Finished generation");
}
private void SavePackageList()
{
BinaryWriter Handle = new BinaryWriter(File.Open(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(object state)
private void ThreadProc(Package pkg)
{
//object[] array = state as object[];
//Package pkg = (Package)array[0];
//if (pkg.Header.PkgID != 0x2f0)
//{
// Packages.TryRemove(pkg.Name, out pkg);
// return;
//}
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);
}
}
private 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.Focus();
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.MouseEnter += ButtonEnter; // this doesnt work for some reason
//btn.MouseLeave += ButtonLeave;
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)
{
// 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;
}
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath);
config.AppSettings.Settings.Remove(PkgPathKey);
config.AppSettings.Settings.Add(PkgPathKey, Path);
config.Save(ConfigurationSaveMode.Minimal);
return true;
}
public string GetPackagesPath()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath);
return config.AppSettings.Settings[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 ExportTextures_Checked(object sender, RoutedEventArgs e)
{
ExportSettings.bTextures = true;
}
private void ExportTextures_Unchecked(object sender, RoutedEventArgs e)
{
ExportSettings.bTextures = false;
}
private void SetExportPath_Clicked(object sender, RoutedEventArgs e)
{
using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
{
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
bool success = SetExportPath(dialog.SelectedPath);
if (success)
{
System.Windows.MessageBox.Show("Export path successfully set");
}
}
}
private bool SetExportPath(string Path)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath);
config.AppSettings.Settings.Remove("ExportPath");
config.AppSettings.Settings.Add("ExportPath", Path);
config.Save(ConfigurationSaveMode.Minimal);
return true;
}
private void HandleVersionCheck(object sender, RoutedEventArgs e) //Changes Version based on selected radio button
{
RadioButton rb = sender as RadioButton;
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath);
switch (rb.Name)
{
case "Destiny1":
config.AppSettings.Settings["Version"].Value = PhononType.Destiny1.ToString();
ePhononType = PhononType.Destiny1;
Wind.Title = "Phonon D1";
PkgPathKey = "PackagesPathD1";
PkgCacheName = "packagesD1.dat";
break;
case "Destiny2BL":
config.AppSettings.Settings["Version"].Value = PhononType.Destiny2BL.ToString();
ePhononType = PhononType.Destiny2BL;
Wind.Title = "Phonon BL";
PkgPathKey = "PackagesPathBL";
PkgCacheName = "packagesBL.dat";
break;
case "Destiny2PreBL":
config.AppSettings.Settings["Version"].Value = PhononType.Destiny2PREBL.ToString();
ePhononType = PhononType.Destiny2PREBL;
Wind.Title = "Phonon PRE-BL";
PkgPathKey = "PackagesPathPREBL";
PkgCacheName = "packagesPREBL.dat";
break;
default:
break;
}
config.Save(ConfigurationSaveMode.Minimal);
Packages.Clear();
if (config.AppSettings.Settings[PkgPathKey] != null)
{
LoadPackageList();
ShowPackageList();
}
else
{
System.Windows.MessageBox.Show($"No package path found for {ePhononType.ToString()}");
SelectPkgsDirectoryButton_Click(sender, e);
}
}
private void Export_Clicked(object sender, RoutedEventArgs e)
{
if (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();
ExportSettings.Path = dialog.FileName;
}
bool status = ExportSettings.Export(GetPackagesPath(), ePhononType);
if (status)
{
System.Windows.MessageBox.Show("Export success");
}
else
{
System.Windows.MessageBox.Show("Export failed");
}
}
private void ExportAll_Clicked(object sender, RoutedEventArgs e)
{
if (currentdynamics.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;
}
foreach (Dynamic dynamic in currentdynamics)
{
ExportSettings.Hash = dynamic.HashString;
ExportSettings.Path = $"{outputpath}\\{currentpkg}\\{dynamic.Hash.ToString()}.fbx";
bool status = ExportSettings.Export(GetPackagesPath(), ePhononType);
}
System.Windows.MessageBox.Show("Export success");
}
}
}