Skip to content

Commit 2d5aa95

Browse files
committed
0.1.2
- Automatic detection of JSON file for DDS texture format. Fallback to filename detection if not - Refactor GenerateXMLs() to handle JJSON and some minor efficiencies. - Cleanup code by moving some unnecessary repeated items to either subroutine or outside loops. - Check for KTX2 conversion failure. Unspecified error for now. - Add logging.
1 parent f666772 commit 2d5aa95

File tree

4 files changed

+111
-91
lines changed

4 files changed

+111
-91
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# 0.1.2
2-
- Automatic detection of JSON file for DDS texture format. Fallback to filename detection if not (NF)
3-
-
2+
- Automatic detection of JSON file for DDS texture format. Fallback to filename detection if not
3+
- Refactor GenerateXMLs() to handle JJSON and some minor efficiencies.
4+
- Cleanup code by moving some unnecessary repeated items to either subroutine or outside loops.
5+
- Check for KTX2 conversion failure. Unspecified error for now.
6+
- Add logging.
47

58
# 0.1.1
69
- Cleanup Code.

LiveryConverter2024.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
2223
<PackageReference Include="WPF-UI" Version="4.0.2" />
2324
</ItemGroup>
2425

MainWindow.xaml.cs

Lines changed: 104 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using Microsoft.Win32;
22
using Newtonsoft.Json;
33
using System.IO;
4+
using System.IO.Packaging;
45
using System.Reflection;
56
using System.Windows;
67
using System.Windows.Controls;
78
using System.Xml.Linq;
9+
using static System.Net.Mime.MediaTypeNames;
810

911
namespace LiveryConverter2024
1012
{
@@ -37,6 +39,14 @@ public MainWindow()
3739
};
3840
}
3941

42+
class TextureType
43+
{
44+
public static string ALBD = "MTL_BITMAP_DECAL0";
45+
public static string COMP = "MTL_BITMAP_METAL_ROUGH_AO";
46+
public static string NORM = "MTL_BITMAP_NORMAL";
47+
// DECAL skipped, it follows ALBD
48+
};
49+
4050
public string DebugConsole
4151
{
4252
get { return debug.Text; }
@@ -60,7 +70,7 @@ private void ClearPath(System.IO.DirectoryInfo directory)
6070
{
6171
foreach (System.IO.FileInfo file in directory.GetFiles())
6272
{
63-
if (!file.Name.ToString().Contains("texconv.exe"))
73+
if (!file.Name.ToString().Contains("texconv.exe") && !file.Name.ToString().Contains(".log"))
6474
{
6575
file.Delete();
6676
}
@@ -197,61 +207,53 @@ private void GenerateXMLs(bool isJsonAvail = false)
197207

198208
string pkgSourceDir = path + "PackageSources\\SimObjects\\Airplanes\\livery-converter-2024\\common\\texture\\";
199209

200-
201-
/////// TODO: Add json check to each if isJsonAvail = true
202-
/////// TODO: replace generation of XML with GenerateSingleXML(path, file, type)
203210
foreach (FileInfo f in i)
204211
{
205212
string shortFileName = f.Name;
206213
string shortNewName = shortFileName.Substring(0, shortFileName.Length - 4);
207214
string file = f.FullName;
208-
if (file.Contains("ALBD"))
209-
{
210-
XDocument xmldoc = new XDocument(
211-
new XElement("BitmapConfiguration",
212-
new XElement("BitmapSlot", "MTL_BITMAP_DECAL0"),
213-
new XElement("UserFlags", new XAttribute("type", "_DEFAULT"), "QUALITYHIGH")
214-
)
215-
);
216-
xmldoc.Save(pkgSourceDir + shortNewName + ".xml");
217-
File.Move(file, pkgSourceDir + shortNewName);
218-
}
219-
else
220-
if (file.Contains("COMP"))
221-
{
222-
XDocument xmldoc = new XDocument(
223-
new XElement("BitmapConfiguration",
224-
new XElement("BitmapSlot", "MTL_BITMAP_METAL_ROUGH_AO"),
225-
new XElement("UserFlags", new XAttribute("type", "_DEFAULT"), "QUALITYHIGH"),
226-
new XElement("ForceNoAlpha", true)
227-
)
228-
);
229-
xmldoc.Save(pkgSourceDir + shortNewName + ".xml");
230-
File.Move(file, pkgSourceDir + shortNewName);
231-
}
232-
else
233-
if (file.Contains("DECAL"))
215+
bool jsonFailure = false;
216+
217+
File.Move(file, pkgSourceDir + shortNewName);
218+
219+
if (isJsonAvail)
234220
{
235-
XDocument xmldoc = new XDocument(
236-
new XElement("BitmapConfiguration",
237-
new XElement("BitmapSlot", "MTL_BITMAP_DECAL0"),
238-
new XElement("UserFlags", new XAttribute("type", "_DEFAULT"), "QUALITYHIGH")
239-
)
240-
);
241-
xmldoc.Save(pkgSourceDir + shortNewName + ".xml");
242-
File.Move(file, pkgSourceDir + shortNewName);
221+
string type = UnpackJson(path + "DDSINPUT\\" + shortNewName + ".DDS.json"); // Infer original name due to timing happening after conversion to PNG.
222+
switch (type)
223+
{
224+
case "NORM":
225+
GenerateSingleXML(pkgSourceDir, shortNewName + ".xml", TextureType.NORM);
226+
break;
227+
case "COMP":
228+
GenerateSingleXML(pkgSourceDir, shortNewName + ".xml", TextureType.COMP);
229+
break;
230+
case "ALBD":
231+
case "DECAL":
232+
GenerateSingleXML(pkgSourceDir, shortNewName + ".xml", TextureType.ALBD);// Use ALBD settings for DECAL
233+
break;
234+
case "FAIL":
235+
default:
236+
jsonFailure = true;
237+
break;
238+
}
243239
}
244-
else
245-
if (file.Contains("NORM"))
240+
241+
if (!isJsonAvail || jsonFailure) // fallback to filename check if json fails...
246242
{
247-
XDocument xmldoc = new XDocument(
248-
new XElement("BitmapConfiguration",
249-
new XElement("BitmapSlot", "MTL_BITMAP_NORMAL"),
250-
new XElement("UserFlags", new XAttribute("type", "_DEFAULT"), "QUALITYHIGH")
251-
)
252-
);
253-
xmldoc.Save(pkgSourceDir + shortNewName + ".xml");
254-
File.Move(file, pkgSourceDir + shortNewName);
243+
if (file.Contains("ALBD") || file.Contains("DECAL"))
244+
{
245+
GenerateSingleXML(pkgSourceDir, shortNewName + ".xml", TextureType.ALBD);// Use ALBD settings for DECAL
246+
}
247+
else
248+
if (file.Contains("COMP"))
249+
{
250+
GenerateSingleXML(pkgSourceDir, shortNewName + ".xml", TextureType.COMP);
251+
}
252+
else
253+
if (file.Contains("NORM"))
254+
{
255+
GenerateSingleXML(pkgSourceDir, shortNewName + ".xml", TextureType.NORM);
256+
}
255257
}
256258
}
257259

@@ -302,14 +304,6 @@ private void GenerateXMLs(bool isJsonAvail = false)
302304
xProject.Save(path + "livery-converter-2024.xml");
303305
}
304306

305-
class TextureType
306-
{
307-
public static string ALBD = "MTL_BITMAP_DECAL0";
308-
public static string COMP = "MTL_BITMAP_METAL_ROUGH_AO";
309-
public static string NORM = "MTL_BITMAP_NORMAL";
310-
// DECAL skipped, it follows ALBD
311-
};
312-
313307
private void GenerateSingleXML(string dir, string file, string tex)
314308
{
315309
XDocument xmldoc = new XDocument(
@@ -320,30 +314,33 @@ private void GenerateSingleXML(string dir, string file, string tex)
320314
);
321315
if (tex == TextureType.COMP)
322316
{
323-
#pragma warning disable CS8602
324-
xmldoc.Root.Element("BitmapConfiguration").Add(new XElement("ForceNoAlpha", true));
325-
#pragma warning restore CS8602
317+
xmldoc.Descendants("BitmapConfiguration").First().Add(new XElement("ForceNoAlpha", true));
326318
}
327-
xmldoc.Save(dir + file + ".xml");
319+
xmldoc.Save(dir + file);
328320
}
329321

330322
private string UnpackJson(string filename)
331323
{
332324
try
333325
{
326+
ConsoleWriteLine("Scanning included DDS.json file...");
334327
string fileContents = File.ReadAllText(filename);
335328
dynamic stuff = JsonConvert.DeserializeObject(fileContents);
336-
string[] flags = stuff.Flags;
329+
string[] flags = stuff.Flags.ToObject<string[]>();
337330
foreach (string f in flags)
338331
{
332+
ConsoleWriteLine("Found flag: " + f + "!");
339333
switch (f)
340334
{
341335
case "FL_BITMAP_METAL_ROUGH_AO_DATA":
336+
ConsoleWriteLine("Inferring Type COMP!");
342337
return TextureType.COMP;
343338
case "FL_BITMAP_TANGENT_DXT5N":
339+
ConsoleWriteLine("Inferring Type NORM!");
344340
return TextureType.NORM;
345341
}
346342
}
343+
ConsoleWriteLine("Inferring Type ALBD!");
347344
return TextureType.ALBD;
348345
}
349346
catch (Exception err)
@@ -367,7 +364,7 @@ private async void ProcessHandler(bool isJsonAvail = false)
367364
if (isJsonAvail)
368365
{
369366
// TODO special handling
370-
GenerateXMLs();
367+
GenerateXMLs(true);
371368
}
372369
else
373370
{
@@ -395,44 +392,61 @@ private async void ProcessHandler(bool isJsonAvail = false)
395392
}
396393
if (!error)
397394
{
398-
DirectoryInfo d = new DirectoryInfo(path + "Packages\\livery-converter-2024\\SimObjects\\Airplanes\\livery-converter-2024\\common\\texture");
399-
FileInfo[] i = d.GetFiles();
395+
if (File.Exists(path + "Packages\\livery-converter-2024\\manifest.json"))
396+
{
397+
DirectoryInfo d = new DirectoryInfo(path + "Packages\\livery-converter-2024\\SimObjects\\Airplanes\\livery-converter-2024\\common\\texture");
398+
FileInfo[] i = d.GetFiles();
400399

401-
ConsoleWriteLine("Moving textures to project path...");
400+
ConsoleWriteLine("Moving textures to project path...");
402401

403-
string pkgSourceDir = path + "PackageSources\\SimObjects\\Airplanes\\livery-converter-2024\\common\\texture\\";
402+
string pkgSourceDir = path + "PackageSources\\SimObjects\\Airplanes\\livery-converter-2024\\common\\texture\\";
404403

405-
foreach (FileInfo f in i)
406-
{
407-
string target = Properties.Settings.Default.texturePath + "\\" + f.Name;
408-
if (File.Exists(target))
409-
{
410-
ConsoleWriteLine(target + " Already exists! skipping...");
411-
}
412-
else
404+
foreach (FileInfo f in i)
413405
{
414-
File.Copy(f.FullName, target);
406+
string target = Properties.Settings.Default.texturePath + "\\" + f.Name;
407+
if (File.Exists(target))
408+
{
409+
ConsoleWriteLine(target + " Already exists! skipping...");
410+
}
411+
else
412+
{
413+
File.Copy(f.FullName, target);
414+
}
415415
}
416-
}
417-
if (File.Exists(Properties.Settings.Default.projectPath + "\\layout.json"))
418-
{
419-
ConsoleWriteLine("layout.json found! running MSFSLayoutGenerator.exe...");
420-
if (File.Exists(Properties.Settings.Default.layoutGenPath + "\\MSFSLayoutGenerator.exe"))
416+
if (File.Exists(Properties.Settings.Default.projectPath + "\\layout.json"))
421417
{
422-
await exeClass.SpawnProc(Properties.Settings.Default.layoutGenPath + "\\MSFSLayoutGenerator.exe", Properties.Settings.Default.projectPath + "\\layout.json");
418+
ConsoleWriteLine("layout.json found! running MSFSLayoutGenerator.exe...");
419+
if (File.Exists(Properties.Settings.Default.layoutGenPath + "\\MSFSLayoutGenerator.exe"))
420+
{
421+
await exeClass.SpawnProc(Properties.Settings.Default.layoutGenPath + "\\MSFSLayoutGenerator.exe", Properties.Settings.Default.projectPath + "\\layout.json");
422+
}
423+
else
424+
{
425+
ConsoleWriteLine("Error: MSFSLayoutGenerator.exe not found! Layout not updated...");
426+
ConsoleWriteLine("Converted textures can be found here: " + Properties.Settings.Default.texturePath);
427+
}
423428
}
424429
else
425430
{
426-
ConsoleWriteLine("Error: MSFSLayoutGenerator.exe not found! Layout not updated...");
431+
ConsoleWriteLine("Unable to locate layout.json in Project Path");
427432
ConsoleWriteLine("Converted textures can be found here: " + Properties.Settings.Default.texturePath);
428433
}
434+
ConsoleWriteLine("Conversion Complete!");
429435
}
430436
else
431437
{
432-
ConsoleWriteLine("Unable to locate layout.json in Project Path");
433-
ConsoleWriteLine("Converted textures can be found here: " + Properties.Settings.Default.texturePath);
438+
ConsoleWriteLine("Unspecified Error: fspackagetool.exe Failed...");
439+
ConsoleWriteLine("Logs from fspackagetool can be found in the same directory as FlightSimulator2024.exe under the name: BuilderLogError.txt");
440+
//string logpath = "";
441+
//if (Properties.Settings.Default.store == "Steam")
442+
//{
443+
444+
//}
445+
//else
446+
//{
447+
// logpath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "Packages\\Microsoft.Limitless_8wekyb3d8bbwe\\LocalCache\\BuilderLogError.txt";
448+
//}
434449
}
435-
ConsoleWriteLine("Conversion Complete!");
436450
}
437451
else
438452
{
@@ -443,7 +457,9 @@ private async void ProcessHandler(bool isJsonAvail = false)
443457
Progress.Visibility = Visibility.Hidden;
444458
Progress.IsIndeterminate = false;
445459
});
446-
460+
string t = DateTime.Now.ToString(@"MM-dd-yyyy-h-mm-tt");
461+
File.WriteAllText(path + "LC24-" + t + ".log", DebugConsole);
462+
447463
}
448464

449465

@@ -463,10 +479,10 @@ private void button4_Click(object sender, RoutedEventArgs e)
463479
{
464480
ConsoleWriteLine("Uploading textures to project...");
465481
File.Copy(f, path + "DDSINPUT\\" + System.IO.Path.GetFileName(f));
466-
// Grab json files for typing if not exists
482+
// Grab json files for typing if exists
467483
if (File.Exists(f + ".json"))
468484
{
469-
File.Copy(f+".json", path + "DDSINPUT\\" + System.IO.Path.GetFileName(f));
485+
File.Copy(f+".json", path + "DDSINPUT\\" + System.IO.Path.GetFileName(f) + ".json");
470486
isJsonAvail = true;
471487
}
472488
}

VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.1
1+
0.1.2

0 commit comments

Comments
 (0)