Skip to content

Commit

Permalink
Add list-names command
Browse files Browse the repository at this point in the history
  • Loading branch information
DaZombieKiller committed Mar 12, 2024
1 parent 18d5133 commit b25fb42
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tools/TLTool/ListNamesCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.CommandLine;
using System.CommandLine.Invocation;

namespace TLTool;

public sealed class ListNamesCommand
{
public Command Command { get; } = new("list-names");

public Argument<string> HeaderPath { get; } = new("header-path", "Path to FILEHEADER.TOFHDB");

public Argument<string> FileDictionaryPath { get; } = new("--dictionary", "Path to name dictionary file");

public Option<bool> Is32Bit { get; } = new("--bit32", "File is 32-bit (Xillia, Zestiria)");

public Option<bool> IsBigEndian { get; } = new("--big-endian", "File is big-endian");

public ListNamesCommand()
{
Command.AddArgument(HeaderPath);
Command.AddArgument(FileDictionaryPath);
Command.AddOption(Is32Bit);
Command.AddOption(IsBigEndian);
Handler.SetHandler(Command, Execute);
}

public void Execute(InvocationContext context)
{
var header = new DataHeader();
var mapper = new NameDictionary();
var is32Bit = context.ParseResult.GetValueForOption(Is32Bit);
var bigEndian = context.ParseResult.GetValueForOption(IsBigEndian);
mapper.AddNamesFromFile(context.ParseResult.GetValueForArgument(FileDictionaryPath)!);

using (var stream = File.OpenRead(context.ParseResult.GetValueForArgument(HeaderPath)))
using (var reader = bigEndian ? new BigEndianBinaryReader(stream) : new BinaryReader(stream))
header.ReadFrom(reader, new FileInfo("."), is32Bit);

// Sort the entries by data offset, which can reveal the original filesystem folder groupings.
var entries = header.Entries.Where(pair => pair.Value.DataSource is TLFileDataSource).ToArray();
Array.Sort(entries, CompareDataSourceOffsets);

foreach (var (hash, entry) in entries)
{
Console.WriteLine(mapper.GetNameOrFallback(hash, entry.Extension));
}
}

private static int CompareDataSourceOffsets(KeyValuePair<uint, DataHeaderEntry> a, KeyValuePair<uint, DataHeaderEntry> b)
{
var offset1 = ((TLFileDataSource)a.Value.DataSource).Offset;
var offset2 = ((TLFileDataSource)b.Value.DataSource).Offset;
return offset1.CompareTo(offset2);
}
}
1 change: 1 addition & 0 deletions tools/TLTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
root.AddCommand(new ScriptPackCommand().Command);
root.AddCommand(new TextureDdsConvertCommand().Command);
root.AddCommand(new InsertCommand().Command);
root.AddCommand(new ListNamesCommand().Command);
await root.InvokeAsync(args);

0 comments on commit b25fb42

Please sign in to comment.