diff --git a/DS_Map/BuildingEditor.cs b/DS_Map/BuildingEditor.cs index 24453ad..862bc0d 100644 --- a/DS_Map/BuildingEditor.cs +++ b/DS_Map/BuildingEditor.cs @@ -275,7 +275,7 @@ namespace DSPRE { } private void bldExportDAEbutton_Click(object sender, EventArgs e) { - DSUtils.ModelToDAE( + ModelUtils.ModelToDAE( modelName: buildingEditorBldListBox.SelectedItem.ToString().TrimEnd('\0'), modelData: currentModelData, textureData: textureComboBox.SelectedIndex < 1 ? null : File.ReadAllBytes(RomInfo.gameDirs[DirNames.buildingTextures].unpackedDir + "\\" + (textureComboBox.SelectedIndex - 1).ToString("D4")) diff --git a/DS_Map/DSPRE.csproj b/DS_Map/DSPRE.csproj index 3b6fd62..afbe987 100644 --- a/DS_Map/DSPRE.csproj +++ b/DS_Map/DSPRE.csproj @@ -113,6 +113,7 @@ + Form diff --git a/DS_Map/DSUtils/DSUtils.cs b/DS_Map/DSUtils/DSUtils.cs index 003f189..a0fc2b5 100644 --- a/DS_Map/DSUtils/DSUtils.cs +++ b/DS_Map/DSUtils/DSUtils.cs @@ -37,136 +37,6 @@ namespace DSPRE { } } - public static void ModelToDAE(string modelName, byte[] modelData, byte[] textureData) { - MessageBox.Show("Choose output folder.\nDSPRE will automatically create a sub-folder in it.", "Awaiting user input", MessageBoxButtons.OK, MessageBoxIcon.Information); - - CommonOpenFileDialog cofd = new CommonOpenFileDialog { - IsFolderPicker = true, - Multiselect = false - }; - if (cofd.ShowDialog() != CommonFileDialogResult.Ok) { - return; - } - - string outDir = Path.Combine(cofd.FileName, modelName); - - if (Directory.Exists(outDir)) { - if (Directory.GetFiles(outDir).Length > 0) { - DialogResult d = MessageBox.Show($"Directory \"{outDir}\" already exists and is not empty.\nIts contents will be lost.\n\nDo you want to proceed?", "Directory not empty", MessageBoxButtons.YesNo, MessageBoxIcon.Question); - - if (d.Equals(DialogResult.No)) { - return; - } else { - Directory.Delete(outDir, recursive: true); - } - } else { - Directory.Delete(outDir, recursive: true); - } - } - string tempNSBMDPath = outDir + "_temp.nsbmd"; - - if (textureData != null && textureData.Length > 0) { - modelData = NSBUtils.BuildNSBMDwithTextures(modelData, textureData); - } - - File.WriteAllBytes(tempNSBMDPath, modelData); - - /* Check correct creation of temp NSBMD file*/ - if (!File.Exists(tempNSBMDPath)) { - MessageBox.Show("Expected NSBMD file could not be found.\nAborting", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); - return; - } - - Process apicula = new Process(); - apicula.StartInfo.FileName = @"Tools\apicula.exe"; - apicula.StartInfo.Arguments = $" convert \"{tempNSBMDPath}\" --output \"{outDir}\""; - apicula.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - apicula.StartInfo.CreateNoWindow = true; - apicula.Start(); - apicula.WaitForExit(); - - if (File.Exists(tempNSBMDPath)) { - File.Delete(tempNSBMDPath); - - if (File.Exists(tempNSBMDPath)) { - MessageBox.Show("Temporary NSBMD file deletion failed.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); - } - } else { - MessageBox.Show("Temporary NSBMD file corresponding to this map disappeared.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); - } - - if (apicula.ExitCode == 0) { - MessageBox.Show("NSBMD was exported and converted successfully!", "Operation successful", MessageBoxButtons.OK, MessageBoxIcon.Information); - } else { - MessageBox.Show("NSBMD to DAE conversion failed.", "Apicula error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - - public static void ModelToGLB(string modelName, byte[] modelData, byte[] textureData) { - MessageBox.Show("Choose output folder.\nDSPRE will automatically create a sub-folder in it.", "Awaiting user input", MessageBoxButtons.OK, MessageBoxIcon.Information); - - CommonOpenFileDialog cofd = new CommonOpenFileDialog { - IsFolderPicker = true, - Multiselect = false - }; - if (cofd.ShowDialog() != CommonFileDialogResult.Ok) { - return; - } - - string outDir = Path.Combine(cofd.FileName, modelName); - - if (Directory.Exists(outDir)) { - if (Directory.GetFiles(outDir).Length > 0) { - DialogResult d = MessageBox.Show($"Directory \"{outDir}\" already exists and is not empty.\nIts contents will be lost.\n\nDo you want to proceed?", "Directory not empty", MessageBoxButtons.YesNo, MessageBoxIcon.Question); - - if (d.Equals(DialogResult.No)) { - return; - } else { - Directory.Delete(outDir, recursive: true); - } - } else { - Directory.Delete(outDir, recursive: true); - } - } - string tempNSBMDPath = outDir + "_temp.nsbmd"; - - if (textureData != null && textureData.Length > 0) { - modelData = NSBUtils.BuildNSBMDwithTextures(modelData, textureData); - } - - File.WriteAllBytes(tempNSBMDPath, modelData); - - /* Check correct creation of temp NSBMD file*/ - if (!File.Exists(tempNSBMDPath)) { - MessageBox.Show("NSBMD file corresponding to this map could not be found.\nAborting", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); - return; - } - - Process apicula = new Process(); - apicula.StartInfo.FileName = @"Tools\apicula.exe"; - apicula.StartInfo.Arguments = $" convert \"{tempNSBMDPath}\" -f glb --output \"{outDir}\""; - apicula.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - apicula.StartInfo.CreateNoWindow = true; - apicula.Start(); - apicula.WaitForExit(); - - if (File.Exists(tempNSBMDPath)) { - File.Delete(tempNSBMDPath); - - if (File.Exists(tempNSBMDPath)) { - MessageBox.Show("Temporary NSBMD file deletion failed.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); - } - } else { - MessageBox.Show("Temporary NSBMD file corresponding to this map disappeared.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); - } - - if (apicula.ExitCode == 0) { - MessageBox.Show("NSBMD was exported and converted successfully!", "Operation successful", MessageBoxButtons.OK, MessageBoxIcon.Information); - } else { - MessageBox.Show("NSBMD to GLB conversion failed.", "Apicula error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - public static void WriteToFile(string filepath, byte[] toOutput, uint writeAt = 0, int indexFirstByteToWrite = 0, int? indexLastByteToWrite = null, FileMode fmode = FileMode.OpenOrCreate) { using (EasyWriter writer = new EasyWriter(filepath, writeAt, fmode)) { writer.Write(toOutput, indexFirstByteToWrite, indexLastByteToWrite is null ? toOutput.Length - indexFirstByteToWrite : (int)indexLastByteToWrite); diff --git a/DS_Map/DSUtils/ModelUtils.cs b/DS_Map/DSUtils/ModelUtils.cs new file mode 100644 index 0000000..b9232bc --- /dev/null +++ b/DS_Map/DSUtils/ModelUtils.cs @@ -0,0 +1,139 @@ +using Microsoft.WindowsAPICodePack.Dialogs; +using System.Diagnostics; +using System.IO; +using System.Windows.Forms; + +namespace DSPRE { + public static class ModelUtils { + + public static void ModelToDAE(string modelName, byte[] modelData, byte[] textureData) { + MessageBox.Show("Choose output folder.\nDSPRE will automatically create a sub-folder in it.", "Awaiting user input", MessageBoxButtons.OK, MessageBoxIcon.Information); + + CommonOpenFileDialog cofd = new CommonOpenFileDialog { + IsFolderPicker = true, + Multiselect = false + }; + if (cofd.ShowDialog() != CommonFileDialogResult.Ok) { + return; + } + + string outDir = Path.Combine(cofd.FileName, modelName); + + if (Directory.Exists(outDir)) { + if (Directory.GetFiles(outDir).Length > 0) { + DialogResult d = MessageBox.Show($"Directory \"{outDir}\" already exists and is not empty.\nIts contents will be lost.\n\nDo you want to proceed?", "Directory not empty", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + + if (d.Equals(DialogResult.No)) { + return; + } else { + Directory.Delete(outDir, recursive: true); + } + } else { + Directory.Delete(outDir, recursive: true); + } + } + string tempNSBMDPath = outDir + "_temp.nsbmd"; + + if (textureData != null && textureData.Length > 0) { + modelData = NSBUtils.BuildNSBMDwithTextures(modelData, textureData); + } + + File.WriteAllBytes(tempNSBMDPath, modelData); + + /* Check correct creation of temp NSBMD file*/ + if (!File.Exists(tempNSBMDPath)) { + MessageBox.Show("Expected NSBMD file could not be found.\nAborting", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); + return; + } + + Process apicula = new Process(); + apicula.StartInfo.FileName = @"Tools\apicula.exe"; + apicula.StartInfo.Arguments = $" convert \"{tempNSBMDPath}\" --output \"{outDir}\""; + apicula.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; + apicula.StartInfo.CreateNoWindow = true; + apicula.Start(); + apicula.WaitForExit(); + + if (File.Exists(tempNSBMDPath)) { + File.Delete(tempNSBMDPath); + + if (File.Exists(tempNSBMDPath)) { + MessageBox.Show("Temporary NSBMD file deletion failed.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + } else { + MessageBox.Show("Temporary NSBMD file corresponding to this map disappeared.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + + if (apicula.ExitCode == 0) { + MessageBox.Show("NSBMD was exported and converted successfully!", "Operation successful", MessageBoxButtons.OK, MessageBoxIcon.Information); + } else { + MessageBox.Show("NSBMD to DAE conversion failed.", "Apicula error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + public static void ModelToGLB(string modelName, byte[] modelData, byte[] textureData) { + MessageBox.Show("Choose output folder.\nDSPRE will automatically create a sub-folder in it.", "Awaiting user input", MessageBoxButtons.OK, MessageBoxIcon.Information); + + CommonOpenFileDialog cofd = new CommonOpenFileDialog { + IsFolderPicker = true, + Multiselect = false + }; + if (cofd.ShowDialog() != CommonFileDialogResult.Ok) { + return; + } + + string outDir = Path.Combine(cofd.FileName, modelName); + + if (Directory.Exists(outDir)) { + if (Directory.GetFiles(outDir).Length > 0) { + DialogResult d = MessageBox.Show($"Directory \"{outDir}\" already exists and is not empty.\nIts contents will be lost.\n\nDo you want to proceed?", "Directory not empty", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + + if (d.Equals(DialogResult.No)) { + return; + } else { + Directory.Delete(outDir, recursive: true); + } + } else { + Directory.Delete(outDir, recursive: true); + } + } + string tempNSBMDPath = outDir + "_temp.nsbmd"; + + if (textureData != null && textureData.Length > 0) { + modelData = NSBUtils.BuildNSBMDwithTextures(modelData, textureData); + } + + File.WriteAllBytes(tempNSBMDPath, modelData); + + /* Check correct creation of temp NSBMD file*/ + if (!File.Exists(tempNSBMDPath)) { + MessageBox.Show("NSBMD file corresponding to this map could not be found.\nAborting", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); + return; + } + + Process apicula = new Process(); + apicula.StartInfo.FileName = @"Tools\apicula.exe"; + apicula.StartInfo.Arguments = $" convert \"{tempNSBMDPath}\" -f glb --output \"{outDir}\""; + apicula.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; + apicula.StartInfo.CreateNoWindow = true; + apicula.Start(); + apicula.WaitForExit(); + + if (File.Exists(tempNSBMDPath)) { + File.Delete(tempNSBMDPath); + + if (File.Exists(tempNSBMDPath)) { + MessageBox.Show("Temporary NSBMD file deletion failed.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + } else { + MessageBox.Show("Temporary NSBMD file corresponding to this map disappeared.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + + if (apicula.ExitCode == 0) { + MessageBox.Show("NSBMD was exported and converted successfully!", "Operation successful", MessageBoxButtons.OK, MessageBoxIcon.Information); + } else { + MessageBox.Show("NSBMD to GLB conversion failed.", "Apicula error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} \ No newline at end of file diff --git a/DS_Map/Main Window.cs b/DS_Map/Main Window.cs index f88a6d3..abce981 100644 --- a/DS_Map/Main Window.cs +++ b/DS_Map/Main Window.cs @@ -4696,7 +4696,7 @@ namespace DSPRE { } private void daeExportButton_Click(object sender, EventArgs e) { - DSUtils.ModelToDAE( + ModelUtils.ModelToDAE( modelName: selectMapComboBox.SelectedItem.ToString().TrimEnd('\0'), modelData: currentMapFile.mapModelData, textureData: mapTextureComboBox.SelectedIndex < 0 ? null : File.ReadAllBytes(RomInfo.gameDirs[DirNames.mapTextures].unpackedDir + "\\" + (mapTextureComboBox.SelectedIndex - 1).ToString("D4")) @@ -4704,7 +4704,7 @@ namespace DSPRE { } private void glbExportButton_Click(object sender, EventArgs e) { - DSUtils.ModelToGLB( + ModelUtils.ModelToGLB( modelName: selectMapComboBox.SelectedItem.ToString().TrimEnd('\0'), modelData: currentMapFile.mapModelData, textureData: mapTextureComboBox.SelectedIndex < 0 ? null : File.ReadAllBytes(RomInfo.gameDirs[DirNames.mapTextures].unpackedDir + "\\" + (mapTextureComboBox.SelectedIndex - 1).ToString("D4"))