Skip to content

Commit

Permalink
Implement fully command line support
Browse files Browse the repository at this point in the history
  • Loading branch information
R-YaTian committed Aug 1, 2024
1 parent ed336a2 commit b5f1a84
Show file tree
Hide file tree
Showing 17 changed files with 13,739 additions and 84 deletions.
153 changes: 153 additions & 0 deletions Tinke/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,58 @@
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using CommandLine;
using System.Collections.Generic;
using System.Linq;

[Verb("extract", HelpText = "Extract all files from nds rom.")]
internal class ExtractOptions
{
[Option('f', "file", Required = true, HelpText = "The path of nds rom file to be extract.")]
public string FilePath { get; set; }

[Option('o', "out", HelpText = "The output path. If not provided, will use the same path of rom file.")]
public string OutputPath { get; set; }
}

[Verb("replace", HelpText = "Replace all nitrofs files by dir.")]
internal class ReplaceOptions
{
[Option('f', "file", Required = true, HelpText = "The path of the input nds rom file.")]
public string FilePath { get; set; }

[Option('d', "dir", Required = true, HelpText = "The folder path which contains all nitrofs files. It should be named 'Root'.")]
public string ResPath { get; set; }

[Option('o', "out", Required = true, HelpText = "Set the output rom path.")]
public string OutputFile { get; set; }

[Option('t', "trim", HelpText = "Safe trim the output rom.")]
public bool SafeTrim { get; set; }

[Option('k', "keep-sig", HelpText = "Keep Original RSA SHA1 Signature for output rom.")]
public bool KeepSig { get; set; }

[Option('r', "re-comp", HelpText = "Recompress ARM9 binary for output rom.")]
public bool Recompress { get; set; }

[Option('b', "blz-cue", HelpText = "Use better compress method to compress ARM9 binary (BLZ-Cue). Will be ignore if -r/--re-comp not passed.")]
public bool BlzCue { get; set; }
}

[Verb("open", HelpText = "Open file(s) or a folder via TinkeDSi GUI")]
internal class OpenOptions
{
[Option('f', "folder", HelpText = "Call a folder select dialog then open the selected folder.")]
public bool IsFolder { get; set; }

[Value(0, MetaName = "RomPath", HelpText = "Path of the file(s). Can be provided multiple. Will be ignore if -f/--folder passed.")]
public IEnumerable<string> Props
{
get;
set;
}
}

namespace Tinke
{
Expand All @@ -32,6 +84,23 @@ static class Program
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool FreeConsole();

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetConsoleOutputCP(uint wCodePageID);

public static string extractFilePath;
public static string extractOutputPath;
public static string replaceResPath;
public static string replaceInputFile;
public static string replaceOutputFile;
public static bool safeTrim = false;
public static bool keepSig = false;
public static bool recompressA9 = false;
public static bool blzcueA9 = false;
public static int curCommand = -1;
public static List<string> tblRoms;
public static bool bIsFolder = false;
public static bool bOpenDefault = false;

/// <summary>
/// Punto de entrada principal para la aplicación.
/// </summary>
Expand All @@ -54,9 +123,93 @@ static void Main(string[] args)
}
#endregion

if (Environment.GetCommandLineArgs().Length >= 2)
{
if (Type.GetType("Mono.Runtime") == null)
{
Version osVersion = Environment.OSVersion.Version;
Version win7Version = new Version(6, 1);
AttachConsole(-1);
if (osVersion >= win7Version)
SetConsoleOutputCP(65001);
Console.WriteLine();
}
Parser.Default.ParseArguments<ExtractOptions, ReplaceOptions, OpenOptions>(args)
.WithParsed(HandleArgs)
.WithNotParsed(HandleErrors);
if (Type.GetType("Mono.Runtime") == null && curCommand == 0)
{
FreeConsole();
SendKeys.SendWait("{ENTER}");
}
}

if (curCommand == 0)
{
Application.Exit();
return;
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Sistema());
}

private static void HandleArgs(object obj)
{
switch (obj)
{
case ExtractOptions e:
RunExtract(e);
break;
case ReplaceOptions p:
RunReplace(p);
break;
case OpenOptions o:
RunOpen(o);
break;
}

Check failure on line 171 in Tinke/Program.cs

View workflow job for this annotation

GitHub Actions / build

} expected

Check failure on line 171 in Tinke/Program.cs

View workflow job for this annotation

GitHub Actions / build

} expected

//process ExtractOptions
void RunExtract(ExtractOptions opts)
{
extractFilePath = opts.FilePath;
extractOutputPath = opts.OutputPath;
curCommand = 1;
}

//process ReplaceOptions
void RunReplace(ReplaceOptions opts)
{
replaceResPath = opts.ResPath;
replaceInputFile = opts.FilePath;
replaceOutputFile = opts.OutputFile;
safeTrim = opts.SafeTrim;
keepSig = opts.KeepSig;
recompressA9 = opts.Recompress;
blzcueA9 = opts.BlzCue;
curCommand = 2;
}

//process OpenOptions
void RunOpen(OpenOptions opts)
{
if (Environment.GetCommandLineArgs().Length <= 2)
{
bOpenDefault = true;
curCommand = -1;
return;
}
else
curCommand = 3;
tblRoms = opts.Props.ToList();
bIsFolder = opts.IsFolder;
}
}

private static void HandleErrors(IEnumerable<Error> obj)

Check failure on line 210 in Tinke/Program.cs

View workflow job for this annotation

GitHub Actions / build

Expected class, delegate, enum, interface, or struct

Check failure on line 210 in Tinke/Program.cs

View workflow job for this annotation

GitHub Actions / build

Expected class, delegate, enum, interface, or struct
{
curCommand = 0;
}
}
}

Check failure on line 215 in Tinke/Program.cs

View workflow job for this annotation

GitHub Actions / build

Type or namespace definition, or end-of-file expected

Check failure on line 215 in Tinke/Program.cs

View workflow job for this annotation

GitHub Actions / build

Type or namespace definition, or end-of-file expected
Loading

0 comments on commit b5f1a84

Please sign in to comment.