Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Batch export using command line arguments #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions Ohana3DS Rebirth/BatchMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.IO;
using Ohana3DS_Rebirth.Ohana;
using Ohana3DS_Rebirth.Ohana.Models.GenericFormats;

namespace Ohana3DS_Rebirth
{
class BatchMode
{
// private List<FileIO.file> files = new List<FileIO.file>();
private string[] filenames;

public void openFolder(string path)
{
filenames = Directory.GetFiles(path);

//foreach(string filename in filenames)
//{
// files.Add(FileIO.load(filename));
//}
}

public void exportModels(string destination, int format)
{
if (!Directory.Exists(destination))
{
System.Console.Error.WriteLine("Invalid output directory!");
Environment.Exit(-1);
}

foreach (string filename in filenames)
{
var file = FileIO.load(filename);
var data = (RenderBase.OModelGroup)file.data;
for (int i = 0; i < data.model.Count; i++)
{
string fileName = Path.Combine(destination, Path.GetFileNameWithoutExtension(filename) + data.model[i].name);
switch (format)
{
case 0: DAE.export(data, fileName + ".dae", i); break;
case 1: SMD.export(data, fileName + ".smd", i); break;
case 2: OBJ.export(data, fileName + ".obj", i); break;
case 3: CMDL.export(data, fileName + ".cmdl", i); break;
}
}
}
Console.WriteLine("All " + filenames.Length + " models exported.");
}

public void exportTextures(string destination)
{
if (!Directory.Exists(destination))
{
System.Console.Error.WriteLine("Invalid output directory!");
Environment.Exit(-1);
}

foreach (string filename in filenames)
{
var file = FileIO.load(filename);
var data = (RenderBase.OModelGroup)file.data;
foreach (RenderBase.OTexture tex in data.texture)
{
string fileName = Path.Combine(destination, Path.GetFileNameWithoutExtension(filename) + tex.name) + ".png";
tex.texture.Save(fileName);
}
}
Console.WriteLine("Textures for all " + filenames.Length + " models exported.");
}
}
}
108 changes: 108 additions & 0 deletions Ohana3DS Rebirth/CommandLineArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.IO;

namespace Ohana3DS_Rebirth
{
class CommandLineArgs
{
private const string helpText =
@"Ohana3DS-Rebirth Command line Syntax:

Ohana3DSRebirth
Start the GUI of Ohana 3DS Rebirth
Ohana3DSRebirth [filename]
Start the GUI of Ohana 3DS Rebirth with [filename] loaded.
Ohana3DSRebirth --batch [inputfolder] [OPTIONS]
Start Ohana 3DS Rebirth in BATCH mode. It will export all files contained in [inputfolder].
Ohana3DSRebirth --help
Show this help

Options for --batch mode:
--batch / -b: Must be first argument in batch mode
--output [output] / -o [output]: specify output directory
--models: export models
--textures: export textures
--format {dae|smd|obj|cmdl} / -f {dae|smd|obj|cmdl}: Specify export format for the model
";


public bool batchMode { get; private set; } = false;

public bool hasFile { get; private set; } = false;
public string filename { get; private set; } = "";

public string outputFolder { get; private set; } = ".";
public string inputFolder { get; private set; }
public bool exportModels { get; private set; } = false;
public bool exportTextures { get; private set; } = false;
public int modelFormat { get; private set; } = 0;

public CommandLineArgs(String[] args)
{
if (args.Length < 1)
return;


if (args[0].Equals("--help") || args[0].Equals("-h"))
{
Console.Write(helpText);
Environment.Exit(0);
}

if(args[0].Equals("--batch") || args[0].Equals("-b"))
{
batchMode = true;

if(args.Length < 2)
{
Console.Error.WriteLine("Syntax Error.\n" + helpText);
Environment.Exit(-1);
}

inputFolder = args[1];

for(int i = 2; i < args.Length; i++)
{
if (args[i].Equals("--models") || args[i].Equals("-m")) exportModels = true;
else if (args[i].Equals("--textures") || args[i].Equals("-t")) exportTextures = true;
else if (args[i].Equals("--output") || args[i].Equals("-o")) outputFolder = args[++i];
else if (args[i].Equals("--format") || args[i].Equals("-f"))
{
switch(args[++i])
{
case "dae":
case "DAE":
modelFormat = 0;
break;
case "smd":
case "SMD":
modelFormat = 1;
break;
case "obj":
case "OBJ":
modelFormat = 2;
break;
case "cmdl":
case "CMDL":
modelFormat = 3;
break;
default:
Console.Error.WriteLine("format " + args[i] + "is not known. Possible options: dae, smd, obj, cmdl");
Environment.Exit(-1);
break;
}
}
else
{
Console.Error.WriteLine("Syntax error: unknown flag " + args[i] + " entered.");
Environment.Exit(-1);
}
}
} else
{
hasFile = args.Length > 0 && File.Exists(args[0]);
if (hasFile) filename = args[0];
}
}
}
}
2 changes: 2 additions & 0 deletions Ohana3DS Rebirth/Ohana3DS Rebirth.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BatchMode.cs" />
<Compile Include="CommandLineArgs.cs" />
<Compile Include="FrmMain.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
47 changes: 41 additions & 6 deletions Ohana3DS Rebirth/Program.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Ohana3DS_Rebirth
{
static class Program
{
// we want command line output for batch mode.
// https://stackoverflow.com/a/7199024
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;


/// <summary>
/// Ponto de entrada principal para o aplicativo.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FrmMain form = new FrmMain();
if (args.Length > 0 && File.Exists(args[0])) form.setFileToOpen(args[0]);
Application.Run(form);
// redirect console output to parent process;
// must be before any calls to Console.WriteLine()
AttachConsole(ATTACH_PARENT_PROCESS);

var cmdArgs = new CommandLineArgs(args);
if (cmdArgs.batchMode)
{
var batch = new BatchMode();

Console.WriteLine("input Folder: " + cmdArgs.inputFolder);
Console.WriteLine("output Folder: " + cmdArgs.outputFolder);
Console.WriteLine("export models? " + cmdArgs.exportModels);
Console.WriteLine("export textures? " + cmdArgs.exportTextures);
Console.WriteLine("model format: " + cmdArgs.modelFormat);

batch.openFolder(cmdArgs.inputFolder);

if (cmdArgs.exportModels)
batch.exportModels(cmdArgs.outputFolder, cmdArgs.modelFormat);
if (cmdArgs.exportTextures)
batch.exportTextures(cmdArgs.outputFolder);
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

FrmMain form = new FrmMain();

if (cmdArgs.hasFile) form.setFileToOpen(cmdArgs.filename);

Application.Run(form);
}
}
}
}