diff --git a/tools/TLTool/ListNamesCommand.cs b/tools/TLTool/ListNamesCommand.cs new file mode 100644 index 0000000..a6def67 --- /dev/null +++ b/tools/TLTool/ListNamesCommand.cs @@ -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 HeaderPath { get; } = new("header-path", "Path to FILEHEADER.TOFHDB"); + + public Argument FileDictionaryPath { get; } = new("--dictionary", "Path to name dictionary file"); + + public Option Is32Bit { get; } = new("--bit32", "File is 32-bit (Xillia, Zestiria)"); + + public Option 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 a, KeyValuePair b) + { + var offset1 = ((TLFileDataSource)a.Value.DataSource).Offset; + var offset2 = ((TLFileDataSource)b.Value.DataSource).Offset; + return offset1.CompareTo(offset2); + } +} diff --git a/tools/TLTool/Program.cs b/tools/TLTool/Program.cs index b769d56..afefbfa 100644 --- a/tools/TLTool/Program.cs +++ b/tools/TLTool/Program.cs @@ -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);