Start to add batch export option (only works on g1m and cmb atm)

This commit is contained in:
KillzXGaming 2020-03-08 18:48:18 -04:00
parent a87a14d093
commit 574590e869
6 changed files with 132 additions and 88 deletions

View file

@ -16,7 +16,7 @@ using SPICA.PICA.Commands;
namespace FirstPlugin
{
public class CMB : TreeNodeFile, IFileFormat, IContextMenuNode
public class CMB : TreeNodeFile, IFileFormat, IContextMenuNode, IExportableModel
{
public FileType FileType { get; set; } = FileType.Layout;
@ -44,11 +44,15 @@ namespace FirstPlugin
}
}
public IEnumerable<STGenericObject> ExportableMeshes => Renderer.Meshes;
public IEnumerable<STGenericMaterial> ExportableMaterials => Materials;
public IEnumerable<STGenericTexture> ExportableTextures => Renderer.TextureList;
public STSkeleton ExportableSkeleton => Skeleton;
public ToolStripItem[] GetContextMenuItems()
{
List<ToolStripItem> Items = new List<ToolStripItem>();
Items.Add(new ToolStripMenuItem("Save", null, SaveAction, Keys.Control | Keys.S));
Items.Add(new ToolStripMenuItem("Export", null, ExportAction, Keys.Control | Keys.E));
return Items.ToArray();
}
@ -64,30 +68,6 @@ namespace FirstPlugin
}
}
private void ExportAction(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Supported Formats|*.dae;";
if (sfd.ShowDialog() == DialogResult.OK)
{
ExportModelSettings exportDlg = new ExportModelSettings();
if (exportDlg.ShowDialog() == DialogResult.OK)
ExportModel(sfd.FileName, exportDlg.Settings);
}
}
public void ExportModel(string fileName, DAE.ExportSettings settings)
{
var model = new STGenericModel();
model.Materials = Materials;
model.Objects = Renderer.Meshes;
var textures = new List<STGenericTexture>();
foreach (var tex in Renderer.TextureList)
textures.Add(tex);
DAE.Export(fileName, settings, model, textures, Skeleton);
}
bool DrawablesLoaded = false;
public override void OnClick(TreeView treeView)
{

View file

@ -85,35 +85,10 @@ namespace HyruleWarriors.G1M
}
}
public List<STGenericObject> Meshes
{
get {
List<STGenericObject> meshes = new List<STGenericObject>();
foreach (var mesh in Model.GenericMeshes)
meshes.Add(mesh);
return meshes;
}
}
public List<STGenericMaterial> Materials
{
get {
List<STGenericMaterial> materials = new List<STGenericMaterial>();
foreach (var mat in Model.Materials) {
materials.Add(mat);
foreach (G1MTextureMap texMap in mat.TextureMaps)
{
var texList = TextureList;
if (texList.Count > texMap.TextureIndex) {
texMap.Name = texList[texMap.TextureIndex].Text;
}
}
}
return materials;
}
}
public STSkeleton Skeleton => G1MSkeleton.GenericSkeleton;
public IEnumerable<STGenericObject> ExportableMeshes => Model.GenericMeshes;
public IEnumerable<STGenericMaterial> ExportableMaterials => Model.Materials;
public IEnumerable<STGenericTexture> ExportableTextures => TextureList;
public STSkeleton ExportableSkeleton => G1MSkeleton.GenericSkeleton;
public List<STGenericTexture> TextureList
{
@ -230,7 +205,7 @@ namespace HyruleWarriors.G1M
foreach (var mesh in Model.GenericMeshes)
meshNode.Nodes.Add(mesh);
if (Skeleton != null)
if (G1MSkeleton != null)
{
foreach (var mesh in Model.GenericMeshes)
{

View file

@ -505,13 +505,13 @@ namespace Toolbox.Library.Forms
public void ExportModel(IExportableModel exportableModel, string fileName, DAE.ExportSettings settings)
{
var model = new STGenericModel();
model.Materials = exportableModel.Materials;
model.Objects = exportableModel.Meshes;
model.Materials = exportableModel.ExportableMaterials;
model.Objects = exportableModel.ExportableMeshes;
var textures = new List<STGenericTexture>();
foreach (var tex in exportableModel.TextureList)
foreach (var tex in exportableModel.ExportableTextures)
textures.Add(tex);
DAE.Export(fileName, settings, model, textures, exportableModel.Skeleton);
DAE.Export(fileName, settings, model, textures, exportableModel.ExportableSkeleton);
}
private void SelectFileInExplorer(object sender, EventArgs args)

View file

@ -8,9 +8,9 @@ namespace Toolbox.Library
{
public interface IExportableModel
{
List<STGenericObject> Meshes { get; }
List<STGenericMaterial> Materials { get; }
STSkeleton Skeleton { get; }
List<STGenericTexture> TextureList { get; }
IEnumerable<STGenericObject> ExportableMeshes { get; }
IEnumerable<STGenericMaterial> ExportableMaterials { get; }
IEnumerable<STGenericTexture> ExportableTextures { get; }
STSkeleton ExportableSkeleton { get; }
}
}

View file

@ -45,6 +45,7 @@
this.compressionToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.batchToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.hashCalculatorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.batchExportTexturesAllSupportedFormatsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.experimentalToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.windowsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.cascadeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
@ -73,7 +74,7 @@
this.stToolStrip1 = new Toolbox.Library.Forms.STToolStrip();
this.saveToolStripButton = new System.Windows.Forms.ToolStripButton();
this.updateToolstrip = new System.Windows.Forms.ToolStripButton();
this.batchExportTexturesAllSupportedFormatsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.batchExportModelsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.stPanel1.SuspendLayout();
this.tabControlContextMenuStrip.SuspendLayout();
@ -187,35 +188,43 @@
this.compressionToolStripMenuItem,
this.batchToolStripMenuItem,
this.hashCalculatorToolStripMenuItem,
this.batchExportTexturesAllSupportedFormatsToolStripMenuItem});
this.batchExportTexturesAllSupportedFormatsToolStripMenuItem,
this.batchExportModelsToolStripMenuItem});
this.toolsToolStripMenuItem.Name = "toolsToolStripMenuItem";
this.toolsToolStripMenuItem.Size = new System.Drawing.Size(47, 21);
this.toolsToolStripMenuItem.Size = new System.Drawing.Size(46, 21);
this.toolsToolStripMenuItem.Text = "Tools";
//
// compressionToolStripMenuItem
//
this.compressionToolStripMenuItem.Name = "compressionToolStripMenuItem";
this.compressionToolStripMenuItem.Size = new System.Drawing.Size(315, 22);
this.compressionToolStripMenuItem.Size = new System.Drawing.Size(316, 22);
this.compressionToolStripMenuItem.Text = "Compression";
//
// batchToolStripMenuItem
//
this.batchToolStripMenuItem.Name = "batchToolStripMenuItem";
this.batchToolStripMenuItem.Size = new System.Drawing.Size(315, 22);
this.batchToolStripMenuItem.Size = new System.Drawing.Size(316, 22);
this.batchToolStripMenuItem.Text = "Batch Set File Table";
this.batchToolStripMenuItem.Click += new System.EventHandler(this.batchToolStripMenuItem_Click);
//
// hashCalculatorToolStripMenuItem
//
this.hashCalculatorToolStripMenuItem.Name = "hashCalculatorToolStripMenuItem";
this.hashCalculatorToolStripMenuItem.Size = new System.Drawing.Size(315, 22);
this.hashCalculatorToolStripMenuItem.Size = new System.Drawing.Size(316, 22);
this.hashCalculatorToolStripMenuItem.Text = "Hash Calculator";
this.hashCalculatorToolStripMenuItem.Click += new System.EventHandler(this.hashCalculatorToolStripMenuItem_Click);
//
// batchExportTexturesAllSupportedFormatsToolStripMenuItem
//
this.batchExportTexturesAllSupportedFormatsToolStripMenuItem.Name = "batchExportTexturesAllSupportedFormatsToolStripMenuItem";
this.batchExportTexturesAllSupportedFormatsToolStripMenuItem.Size = new System.Drawing.Size(316, 22);
this.batchExportTexturesAllSupportedFormatsToolStripMenuItem.Text = "Batch Export Textures (All Supported Formats)";
this.batchExportTexturesAllSupportedFormatsToolStripMenuItem.Click += new System.EventHandler(this.batchExportTexturesAllSupportedFormatsToolStripMenuItem_Click);
//
// experimentalToolStripMenuItem
//
this.experimentalToolStripMenuItem.Name = "experimentalToolStripMenuItem";
this.experimentalToolStripMenuItem.Size = new System.Drawing.Size(87, 21);
this.experimentalToolStripMenuItem.Size = new System.Drawing.Size(88, 21);
this.experimentalToolStripMenuItem.Text = "Experimental";
//
// windowsToolStripMenuItem
@ -233,35 +242,35 @@
// cascadeToolStripMenuItem
//
this.cascadeToolStripMenuItem.Name = "cascadeToolStripMenuItem";
this.cascadeToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.cascadeToolStripMenuItem.Size = new System.Drawing.Size(125, 22);
this.cascadeToolStripMenuItem.Text = "Cascade";
this.cascadeToolStripMenuItem.Click += new System.EventHandler(this.cascadeToolStripMenuItem_Click);
//
// minimizeToolStripMenuItem
//
this.minimizeToolStripMenuItem.Name = "minimizeToolStripMenuItem";
this.minimizeToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.minimizeToolStripMenuItem.Size = new System.Drawing.Size(125, 22);
this.minimizeToolStripMenuItem.Text = "Minimize";
this.minimizeToolStripMenuItem.Click += new System.EventHandler(this.minimizeToolStripMenuItem_Click);
//
// maximizeToolStripMenuItem
//
this.maximizeToolStripMenuItem.Name = "maximizeToolStripMenuItem";
this.maximizeToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.maximizeToolStripMenuItem.Size = new System.Drawing.Size(125, 22);
this.maximizeToolStripMenuItem.Text = "Maximize";
this.maximizeToolStripMenuItem.Click += new System.EventHandler(this.maximizeToolStripMenuItem_Click);
//
// closeToolStripMenuItem
//
this.closeToolStripMenuItem.Name = "closeToolStripMenuItem";
this.closeToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.closeToolStripMenuItem.Size = new System.Drawing.Size(125, 22);
this.closeToolStripMenuItem.Text = "Close";
this.closeToolStripMenuItem.Click += new System.EventHandler(this.closeToolStripMenuItem_Click);
//
// closeAllToolStripMenuItem
//
this.closeAllToolStripMenuItem.Name = "closeAllToolStripMenuItem";
this.closeAllToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.closeAllToolStripMenuItem.Size = new System.Drawing.Size(125, 22);
this.closeAllToolStripMenuItem.Text = "Close All";
this.closeAllToolStripMenuItem.Click += new System.EventHandler(this.closeAllToolStripMenuItem_Click);
//
@ -474,12 +483,12 @@
this.updateToolstrip.ToolTipText = "Update Tool";
this.updateToolstrip.Click += new System.EventHandler(this.updateToolstrip_Click);
//
// batchExportTexturesAllSupportedFormatsToolStripMenuItem
// batchExportModelsToolStripMenuItem
//
this.batchExportTexturesAllSupportedFormatsToolStripMenuItem.Name = "batchExportTexturesAllSupportedFormatsToolStripMenuItem";
this.batchExportTexturesAllSupportedFormatsToolStripMenuItem.Size = new System.Drawing.Size(315, 22);
this.batchExportTexturesAllSupportedFormatsToolStripMenuItem.Text = "Batch Export Textures (All Supported Formats)";
this.batchExportTexturesAllSupportedFormatsToolStripMenuItem.Click += new System.EventHandler(this.batchExportTexturesAllSupportedFormatsToolStripMenuItem_Click);
this.batchExportModelsToolStripMenuItem.Name = "batchExportModelsToolStripMenuItem";
this.batchExportModelsToolStripMenuItem.Size = new System.Drawing.Size(316, 22);
this.batchExportModelsToolStripMenuItem.Text = "Batch Export Models";
this.batchExportModelsToolStripMenuItem.Click += new System.EventHandler(this.batchExportModelsToolStripMenuItem_Click);
//
// MainForm
//
@ -562,5 +571,6 @@
private System.Windows.Forms.ToolStripMenuItem batchToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem hashCalculatorToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem batchExportTexturesAllSupportedFormatsToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem batchExportModelsToolStripMenuItem;
}
}

View file

@ -391,6 +391,12 @@ namespace Toolbox
if (!format.CanSave)
return;
if (ActiveMdiChild is IFIleEditor)
{
if (((IFIleEditor)ActiveMdiChild).GetFileFormats().Count > 0)
((IFIleEditor)ActiveMdiChild).BeforeFileSaved();
}
string FileName = format.FilePath;
if (!File.Exists(FileName))
UseSaveDialog = true;
@ -1355,6 +1361,58 @@ namespace Toolbox
}
}
private void batchExportModelsToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
FolderSelectDialog folderDlg = new FolderSelectDialog();
if (folderDlg.ShowDialog() == DialogResult.OK) {
BatchExportModels(ofd.FileNames, folderDlg.SelectedPath);
}
}
}
private void BatchExportModels(string[] files, string outputFolder)
{
List<string> Formats = new List<string>();
Formats.Add("DAE (.dae)");
List<string> failedFiles = new List<string>();
BatchFormatExport form = new BatchFormatExport(Formats);
if (form.ShowDialog() == DialogResult.OK)
{
string extension = form.GetSelectedExtension();
foreach (var file in files)
{
try
{
var fileFormat = STFileLoader.OpenFileFormat(file);
SearchFileFormat(form.BatchSettings, fileFormat, extension, outputFolder, ExportMode.Models);
}
catch (Exception ex)
{
failedFiles.Add($"{file} \n Error:\n {ex} \n");
}
}
}
if (failedFiles.Count > 0)
{
string detailList = "";
foreach (var file in failedFiles)
detailList += $"{file}\n";
STErrorDialog.Show("Some files failed to export! See detail list of failed files.", "Switch Toolbox", detailList);
}
else
MessageBox.Show("Files batched successfully!");
}
private void BatchExportTextures(string[] files, string outputFolder)
{
List<string> Formats = new List<string>();
@ -1376,7 +1434,7 @@ namespace Toolbox
try
{
var fileFormat = STFileLoader.OpenFileFormat(file);
SearchFileFormat(form.BatchSettings, fileFormat, extension, outputFolder);
SearchFileFormat(form.BatchSettings, fileFormat, extension, outputFolder, ExportMode.Textures);
}
catch (Exception ex)
{
@ -1397,17 +1455,18 @@ namespace Toolbox
MessageBox.Show("Files batched successfully!");
}
private void SearchFileFormat(BatchFormatExport.Settings settings, IFileFormat fileFormat, string extension, string outputFolder)
private void SearchFileFormat(BatchFormatExport.Settings settings, IFileFormat fileFormat,
string extension, string outputFolder, ExportMode exportMode)
{
if (fileFormat == null) return;
if (fileFormat is STGenericTexture) {
if (fileFormat is STGenericTexture && exportMode == ExportMode.Textures) {
string name = ((STGenericTexture)fileFormat).Text;
ExportTexture(((STGenericTexture)fileFormat), $"{outputFolder}/{name}.{extension}");
}
else if (fileFormat is IArchiveFile)
SearchArchive(settings, (IArchiveFile)fileFormat, extension, outputFolder);
else if (fileFormat is ITextureContainer)
SearchArchive(settings, (IArchiveFile)fileFormat, extension, outputFolder, exportMode);
else if (fileFormat is ITextureContainer && exportMode == ExportMode.Textures)
{
if (settings.SeperateTextureContainers)
{
@ -1421,15 +1480,35 @@ namespace Toolbox
ExportTexture(tex, $"{outputFolder}/{tex.Text}.{extension}");
}
}
else if (fileFormat is IExportableModel && exportMode == ExportMode.Models)
{
DAE.ExportSettings daesettings = new DAE.ExportSettings();
daesettings.SuppressConfirmDialog = true;
var model = new STGenericModel();
model.Materials = ((IExportableModel)fileFormat).ExportableMaterials;
model.Objects = ((IExportableModel)fileFormat).ExportableMeshes;
var textures = ((IExportableModel)fileFormat).ExportableTextures.ToList();
var skeleton = ((IExportableModel)fileFormat).ExportableSkeleton;
string name = Path.GetFileNameWithoutExtension(fileFormat.FileName);
DAE.Export($"{outputFolder}/{name}.{extension}", daesettings, model, textures, skeleton);
}
fileFormat.Unload();
}
public enum ExportMode
{
Models,
Textures,
}
private void ExportTexture(STGenericTexture tex, string filePath) {
tex.Export(filePath);
}
private void SearchArchive(BatchFormatExport.Settings settings, IArchiveFile archiveFile, string extension, string outputFolder)
private void SearchArchive(BatchFormatExport.Settings settings, IArchiveFile archiveFile,
string extension, string outputFolder, ExportMode exportMode)
{
string ArchiveFilePath = Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(((IFileFormat)archiveFile).FileName));
if (!Directory.Exists(ArchiveFilePath))
@ -1438,7 +1517,7 @@ namespace Toolbox
ArchiveFilePath = outputFolder;
foreach (var file in archiveFile.Files)
SearchFileFormat(settings, file.OpenFile(), extension, ArchiveFilePath);
SearchFileFormat(settings, file.OpenFile(), extension, ArchiveFilePath, exportMode);
}
}
}