Skip to content

Commit

Permalink
Add list command to enumerate IDX content
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed May 12, 2020
1 parent fc2484d commit c338da8
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion OpenKh.Command.IdxImg/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
using System.IO;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace OpenKh.Command.IdxImg
{
[Command("OpenKh.Command.IdxImg")]
[VersionOptionFromMember("--version", MemberName = nameof(GetVersion))]
[Subcommand(typeof(ExtractCommand))]
[Subcommand(typeof(ExtractCommand), typeof(ListCommand))]
class Program
{
static int Main(string[] args)
Expand Down Expand Up @@ -119,6 +120,61 @@ public static List<string> ExtractIdx(Img img, Idx idx, string basePath)
}
}

private class ListCommand
{
private Program Parent { get; set; }

[Required]
[FileExists]
[Option(CommandOptionType.SingleValue, Description = "Kingdom Hearts II IDX file, paired with a IMG", ShortName = "i", LongName = "idx")]
public string InputIdx { get; set; }

[Option(CommandOptionType.NoValue, Description = "Sort file list by their position in the IMG", ShortName = "s", LongName = "sort")]
public bool Sort { get; set; }

protected int OnExecute(CommandLineApplication app)
{
var entries = OpenIdx(InputIdx).GetNameEntries();
if (Sort)
entries = entries.OrderBy(x => x.Entry.Offset);

foreach (var entry in entries)
Console.WriteLine(entry.Name);

return 0;
}

public static List<string> ExtractIdx(Img img, Idx idx, string basePath)
{
var idxs = new List<string>();

foreach (var entry in idx.GetNameEntries())
{
var fileName = entry.Name;
if (fileName == null)
fileName = $"@noname/{entry.Entry.Hash32:X08}-{entry.Entry.Hash16:X04}";

Console.WriteLine(fileName);

var outputFile = Path.Combine(basePath, fileName);
var outputDir = Path.GetDirectoryName(outputFile);
if (Directory.Exists(outputDir) == false)
Directory.CreateDirectory(outputDir);

using (var file = File.Create(outputFile))
{
// TODO handle decompression
img.FileOpen(entry.Entry).CopyTo(file);
}

if (Path.GetExtension(fileName) == ".idx")
idxs.Add(outputFile);
}

return idxs;
}
}

private static Idx OpenIdx(string fileName)
{
using (var idxStream = File.OpenRead(fileName))
Expand Down

0 comments on commit c338da8

Please sign in to comment.