From 5c76f35bb793cd58fa7cf4729db840f217c3521f Mon Sep 17 00:00:00 2001 From: Benjamin Popp Date: Wed, 14 Apr 2021 20:36:24 -0500 Subject: [PATCH] Provide custom headers for anchors that are pointed to by named tables --- src/HexManiac.Core/Models/PokemonModel.cs | 24 +++++++++++++++++------ src/HexManiac.Tests/TableTests.cs | 11 ++++++++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/HexManiac.Core/Models/PokemonModel.cs b/src/HexManiac.Core/Models/PokemonModel.cs index d7dc0a97..31856313 100644 --- a/src/HexManiac.Core/Models/PokemonModel.cs +++ b/src/HexManiac.Core/Models/PokemonModel.cs @@ -767,14 +767,26 @@ namespace HavenSoft.HexManiac.Core.Models { // only produce headers for arrays with length based on other arrays that start with a text member. var run = GetNextRun(address); if (run.Start > address) return false; - if (!(run is ArrayRun array)) return false; + if (!(run is ArrayRun array)) { + if (run.PointerSources != null && run.PointerSources.Count > 0 && run.Start == address) { + var parentRun = GetNextRun(run.PointerSources[0]); + if (parentRun is ArrayRun parentArray) { + array = parentArray; + var arrayIndex = parentArray.ConvertByteOffsetToArrayOffset(run.PointerSources[0]).ElementIndex; + address = parentArray.Start + arrayIndex * parentArray.ElementLength; + } else { + return false; + } + } else { + return false; + } + } + if ((address - array.Start) % array.ElementLength != 0) return false; - using (ModelCacheScope.CreateScope(this)) { - var index = (address - array.Start) / array.ElementLength; - if (array.ElementNames.Count <= index) return false; - header = array.ElementNames[index]; - } + var index = (address - array.Start) / array.ElementLength; + if (array.ElementNames.Count <= index) return false; + header = array.ElementNames[index]; return true; } diff --git a/src/HexManiac.Tests/TableTests.cs b/src/HexManiac.Tests/TableTests.cs index 247d2098..4cec8d29 100644 --- a/src/HexManiac.Tests/TableTests.cs +++ b/src/HexManiac.Tests/TableTests.cs @@ -632,7 +632,6 @@ namespace HavenSoft.HexManiac.Tests { Assert.Equal(1, table.ElementCount); } - [Fact] public void TableWithNegativeOffset_ShrinkAndGrowParent_ChildLengthRestored() { CreateTextTable("names", 0, "adam", "bob", "carl", "dave"); @@ -661,6 +660,16 @@ namespace HavenSoft.HexManiac.Tests { Assert.Equal(12, Model.ReadMultiByteValue(4, 2)); } + [Fact] + public void PointerTable_RequestHeaderForDestination_GetTableIndexName() { + Model.SetList("options", new[] { "Adam", "Bob", "Carl", "Dave" }); + ViewPort.Edit("^table[entry<>]options <100> <120> <140> <160>"); + + Model.TryGetUsefulHeader(0x120, out var header); + + Assert.Equal("Bob", header); + } + private void ArrangeTrainerPokemonTeamData(byte structType, byte pokemonCount, int trainerCount) { CreateTextTable(HardcodeTablesModel.PokemonNameTable, 0x180, "ABCDEFGHIJKLMNOP".Select(c => c.ToString()).ToArray()); CreateTextTable(HardcodeTablesModel.MoveNamesTable, 0x1B0, "qrstuvwxyz".Select(c => c.ToString()).ToArray());