-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
654 lines (576 loc) · 21.4 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
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.WindowsAPICodePack.Dialogs;
using Microsoft.Win32;
using System.IO;
using System.ComponentModel;
using System.Diagnostics;
namespace _3DDBBuilderGUI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
// load previously used Object folder locations
// to do still
// where should that get saved? appinfo?
// load default Object folder location from registry
using (RegistryKey view32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
{
using (RegistryKey bmskey = view32.OpenSubKey(@"Software\Benchmark Sims\Falcon BMS 4.33 U1", false))
{
if (bmskey != null)
{
Object dir = bmskey.GetValue("baseDir");
if (dir != null)
{
BMSInstall = dir.ToString();
}
else
{
MessageBox.Show("Found a BMS key in the registry, but no baseDir subkey. Your BMS install is probably borked. Search for Dunc's registry cleaner and use that before reinstalling.", "Registry Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else
{
MessageBox.Show("Could not find a BMS install in the registry. You will have to point to the DB you want to use manually.", "Could not find a BMS Install", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
DBsList = new ObservableCollection<ObjDB>();
// Now knowing the BMSInstall, find all object folders in that install
// can do this by reading theater.lst and then reading all .tdf files
if (BMSInstall != null)
{
string theaterlst = BMSInstall + @"\Data\Terrdata\theaterdefinition\theater.lst";
if (File.Exists(theaterlst))
{
string[] theaters = File.ReadAllLines(theaterlst);
Regex objmatch = new Regex(@"3ddatadir (.*)");
foreach (string theater in theaters)
{
string tdfstring = BMSInstall + @"\Data\" + theater;
if (File.Exists(tdfstring))
{
string[] tdf = File.ReadAllLines(tdfstring);
foreach (string line in tdf)
{
// can any of this be tidied up?
if (line.StartsWith("3ddatadir"))
{
Match match = objmatch.Match(line);
string lobjdir = match.Result("$1");
string objdir = BMSInstall + @"\Data\" + lobjdir;
DBsList.Add(new ObjDB(objdir));
}
}
}
}
SelectedDB = DBsList[0];
MessageBox.Show("Found " + DBsList.Count + " object databases in your BMS install. You can select them from the dropdown menu.", "Search Results", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
MessageBox.Show("Could not find theater.lst in your BMS install. Either I done goofed, or your install is probably borked.", "BMS Install Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
DataContext = this;
}
private string BMSInstall;
private string extractionPath;
private string updatePath;
private string buildSource;
private string buildOutput;
private ObjDB selectedDB;
private int texturenumber;
private string texturefolder;
public ObservableCollection<ObjDB> DBsList { get; set; }
public string CurExtractionPath
{
get => extractionPath;
set
{
if (value != extractionPath)
{
extractionPath = value;
NotifyPropertyChanged("CurExtractionPath");
}
}
}
public string CurUpdatePath
{
get => updatePath;
set
{
if (value != updatePath)
{
updatePath = value;
NotifyPropertyChanged("CurUpdatePath");
}
}
}
public string BuildSource
{
get => buildSource;
set
{
if (value != buildSource)
{
buildSource = value;
NotifyPropertyChanged("BuildSource");
}
}
}
public string BuildOutput
{
get => buildOutput;
set
{
if (value != buildOutput)
{
buildOutput = value;
NotifyPropertyChanged("BuildOutput");
}
}
}
public ObjDB SelectedDB
{
get => selectedDB;
set
{
if (value != selectedDB)
{
selectedDB = value;
NotifyPropertyChanged("SelectedDB");
}
}
}
public string GUIWorDir // does this actually get used anywhere? Can just remove I think
{
get => Directory.GetCurrentDirectory();
}
public int TextureNumber
{
get => texturenumber;
set
{
if (value != texturenumber)
{
texturenumber = value;
NotifyPropertyChanged("TextureNumber");
// Need to update this whenever a new DB is selected to be built
}
}
}
public string TextureFolder
{
get => texturefolder;
set
{
if (value != texturefolder)
{
texturefolder = value;
// not sure anything relies on that property changing anyway? Oh well
NotifyPropertyChanged("TextureFolder");
}
TextureNumber = ObjDB.GetHighestTexture(value);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyname)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
private ObjDB GetExistingDB(string dir)
{
// do we already have this DB?
foreach (ObjDB objDB in DBsList)
{
if (dir == objDB.DirPath)
{
return objDB;
}
}
return null;
}
private void AddDB(string dir)
{
SelectedDB = GetExistingDB(dir);
if (SelectedDB == null)
{
ObjDB objDB = new ObjDB(dir);
DBsList.Add(objDB);
SelectedDB = objDB;
}
}
public string GetFolder(bool returnType, bool isFolder, string windowTitle, string initDir)
{
// returnType true: save folder returned in Settings
// isFolder: for folderpicker instead of file picker
CommonOpenFileDialog dialog = new CommonOpenFileDialog()
{
IsFolderPicker = isFolder,
EnsureReadOnly = true,
Multiselect = false,
Title = windowTitle,
InitialDirectory = initDir
};
if (!Directory.Exists(initDir))
{
dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
}
if (dialog.ShowDialog() == CommonFileDialogResult.Ok && Directory.Exists(dialog.FileName))
{
if (returnType)
Properties.Settings.Default.ObjFolderName = dialog.FileName;
Properties.Settings.Default.Save();
return dialog.FileName;
}
else
{
return null;
}
}
private void OpenFolder(string folder)
{
if (Directory.Exists(folder))
{
Process.Start(folder);
}
else
{
if (folder == null)
{
MessageBox.Show("Please select a directory to open first.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
}
else
{
MessageBox.Show("Could not find the directory specified:" + folder, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private void ExCommand(string args)
{
// add a function to check to see if the exe is co-located here?
using (var process = new Process())
{
try
{
ProcessStartInfo startinfo = new ProcessStartInfo("3ddb_builder.exe", args);
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
startinfo.CreateNoWindow = true;
process.StartInfo = startinfo;
process.Start();
string result = process.StandardOutput.ReadToEnd();
}
catch (Exception)
{
throw;
}
}
statuslabel.Content = "Success!";
}
private void ExCommand(bool IsTest, ObjDB DB)
{
if (!DB.IsValid())
{
MessageBox.Show("The DB could not be found at " + DB.DirPath, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
string args;
if (IsTest)
{
args = @"/objectdir " + "\"" + DB.DirPath + "\"" + @" /test";
ExCommand(args);
// then figure out what to do with the test results which chill in the output file
Process.Start("HeaderChecker.txt");
}
else
{
args = @"/objectdir " + "\"" + DB.DirPath + "\"" + @" /parents";
ExCommand(args);
// then figure out what to do with the newly generated UnusedParents.txt file which currently just chills in the debug dir
Process.Start("UnusedParents.txt");
}
}
private void BuildCommand(bool IsNew, string source, string dest)
{
// how best to check whether the source is a valid extracted DB?
bool InputValid = Directory.Exists(source) && Directory.Exists(dest) && !ObjDB.Exists(dest);
string command;
if (IsNew)
{
command = "/build";
}
else
{
command = "/build_old";
}
if (InputValid)
{
string args = @"/objectdir " + "\"" + dest + "\" " + command + " \"" + source + "\"";
ExCommand(args);
}
}
private void ExCommand(bool IsExtract, ObjDB DB, string ExtractedFolders)
{
if (!DB.IsValid())
{
MessageBox.Show("The selected database could not be found at " + DB.DirPath, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (!(Directory.Exists(ExtractedFolders) && ExtractedFolders != null))
{
MessageBox.Show("Could not find the specified path: " + ExtractedFolders, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (ExtractedFolders == null)
{
MessageBox.Show("Please select an extraction path first.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (IsExtract)
{
// extract DB
string args = @"/objectdir " + "\"" + DB.DirPath + "\"" + @" /extract " + "\"" + ExtractedFolders + "\"";
ExCommand(args);
// write 1x1 texture to dest/Textures directory
string texpath = ExtractedFolders + @"\Textures\" + DB.TextureNo.ToString() + ".BMP";
WriteTexture(texpath);
}
else
{
string args = @"/objectdir " + "\"" + DB.DirPath + "\"" + @" /update " + "\"" + ExtractedFolders + "\"";
ExCommand(args);
}
}
private void WriteTexture(string path)
{
Properties.Resources.B.Save(path);
}
private void SourceSelectButton_Click(object sender, RoutedEventArgs e)
{
string dir = GetFolder(true, true, "Select Object Folder...", BMSInstall + @"\Data\Terrdata");
if (dir != null && ObjDB.Exists(dir))
{
AddDB(dir);
}
if (dir != null && !ObjDB.Exists(dir))
{
MessageBox.Show("No database was selected as one could not be located in the selected directory: \n\n" + dir, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private void DestSelectButton_Click(object sender, RoutedEventArgs e)
{
string dir = GetFolder(false, true, "Select folder to extract Database to...", CurExtractionPath);
if (dir != null)
{
CurExtractionPath = dir;
}
}
private void ExtrButton_Click(object sender, RoutedEventArgs e)
{
ExCommand(true, SelectedDB, CurExtractionPath);
BuildSource = CurExtractionPath;
}
private void ListParents(object sender, RoutedEventArgs e)
{
ExCommand(false, SelectedDB);
}
private void BuildNewButton_Click(object sender, RoutedEventArgs e)
{
string Message = "The output of this function is a database which is incompatible with all legacy tools, including LOD Editor. Do you wish to continue?";
if (MessageBox.Show(Message, "Warning", MessageBoxButton.OKCancel, MessageBoxImage.Warning) == MessageBoxResult.OK)
{
BuildCommand(true, BuildSource, BuildOutput);
}
}
private void BuildOldButton_Click(object sender, RoutedEventArgs e)
{
BuildCommand(false, BuildSource, BuildOutput);
}
private void UpdateButton_Click(object sender, RoutedEventArgs e)
{
ExCommand(false, SelectedDB, CurUpdatePath);
}
private void TestButton_Click(object sender, RoutedEventArgs e)
{
ExCommand(true, SelectedDB);
}
private void ResetExtractionDirButton_Click(object sender, RoutedEventArgs e)
{
try
{
CurExtractionPath = Directory.GetCurrentDirectory();
}
catch (Exception)
{
throw;
}
}
private void OpenExtractionDirButton_Click(object sender, RoutedEventArgs e)
{
OpenFolder(CurExtractionPath);
}
private void BuildSourceSelectButton_Click(object sender, RoutedEventArgs e)
{
string dir = GetFolder(false, true, "Select folder to Build Database from...", BuildSource);
if (dir != null)
{
BuildSource = dir;
}
}
private void BuildDestSelectButton_Click(object sender, RoutedEventArgs e)
{
string dir = GetFolder(false, true, "Select folder to save Database to...", BuildOutput);
if (dir != null)
{
BuildOutput = dir;
}
}
private void OpenBuildSourceDirectoryButton_Click(object sender, RoutedEventArgs e)
{
OpenFolder(BuildSource);
}
private void OpenBuildOutputDirectoryButton_Click(object sender, RoutedEventArgs e)
{
OpenFolder(BuildOutput);
}
private void SelCurUpdatePathButton_Click(object sender, RoutedEventArgs e)
{
string dir = GetFolder(false, true, "Select Updates folder", CurUpdatePath);
if (dir != null)
{
CurUpdatePath = dir;
}
}
private void OpenCurUpdatePathButton_Click(object sender, RoutedEventArgs e)
{
OpenFolder(CurUpdatePath);
}
private void ResetCurUpdatePathButton_Click(object sender, RoutedEventArgs e)
{
try
{
CurUpdatePath = Directory.GetCurrentDirectory();
}
catch (Exception)
{
throw;
}
}
private void SelTextureFolderButton_Click(object sender, RoutedEventArgs e)
{
string message = "Select the folder the database was originally extracted from: ";
MessageBox.Show(message, "Notification", MessageBoxButton.OK, MessageBoxImage.Information);
string KoreaObjDir = GetFolder(false, true, "Select Object Folder...", "");
if (KoreaObjDir != null)
{
TextureFolder = KoreaObjDir;
}
}
private void SetTextureNumberButton_Click(object sender, RoutedEventArgs e)
{
if (BuildSource == null)
{
string message = "Select a source folder to build, first.";
MessageBox.Show(message, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (TextureNumber == 0)
{
string message = "Specify an existing database, or a texture number, first.";
MessageBox.Show(message, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
//first, check to see if a texture file exists already
string dir = BuildSource + @"\Textures";
string[] files = Directory.GetFiles(dir);
if (files.Length == 0)
{
string path = dir + @"\" + TextureNumber.ToString() + ".BMP";
WriteTexture(path);
}
else // a texture file already exists - likely case is that a single texture file already exists
{
// rename it to a new name I guess
string message = "A texture file already exists in the source /Textures folder. Please delete it and try again.";
MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
public class ObjDB
{
public ObjDB(string dir)
{
DirPath = dir;
TextureNo = GetHighestTexture(dir);
}
public static bool Exists(String FolderName)
{
if (Directory.Exists(FolderName) && File.Exists(FolderName + @"\KoreaOBJ.LOD"))
{
return true;
}
else
{
return false;
}
}
public static int GetHighestTexture(string dir)
{
// dir is the object directory, so get the texture directory
string texpath = dir + @"\KoreaObj";
string[] filepaths = Directory.GetFiles(texpath, "*.dds");
int texno = 0;
foreach (string path in filepaths)
{
int number;
bool result = Int32.TryParse(System.IO.Path.GetFileNameWithoutExtension(path), out number);
if (result)
{
if (number > texno)
{
texno = number;
}
}
else
{
string message = "Could not successfully parse the desired string as a number: " + System.IO.Path.GetFileNameWithoutExtension(path);
}
}
return texno;
}
public bool IsValid()
{
return Exists(DirPath);
}
public string DirPath { get; set; }
public int TextureNo { get; set; }
}
public class ExtrDB
{
public ExtrDB()
{
}
}
}