Cloned TableStreams should be the same length as the original

This matters because having the table stream be a different length breaks the pointer-update logic when the final new run is added.
This commit is contained in:
Haven1433 2022-06-18 13:55:03 -05:00
parent aef73703fd
commit eb3554c5bc
2 changed files with 30 additions and 3 deletions

View File

@ -149,7 +149,7 @@ namespace HavenSoft.HexManiac.Core.Models.Runs {
}
protected override BaseRun Clone(SortedSpan<int> newPointerSources) =>
new TableStreamRun(model, Start, newPointerSources, FormatString, ElementContent, endStream);
new TableStreamRun(model, Start, newPointerSources, FormatString, ElementContent, endStream, ElementCount);
#endregion
@ -666,8 +666,9 @@ namespace HavenSoft.HexManiac.Core.Models.Runs {
foreach (var source in pointerSources) {
var offsets = parent.ConvertByteOffsetToArrayOffset(source);
if (offsets.ElementIndex < 0 || offsets.ElementIndex > parent.ElementCount) continue;
if (model.ReadMultiByteValue(parent.Start + offsets.ElementIndex * parent.ElementLength + segmentOffset, segmentLength) != newValue) {
model.WriteMultiByteValue(parent.Start + offsets.ElementIndex * parent.ElementLength + segmentOffset, segmentLength, token, newValue);
var lengthAddress = parent.Start + offsets.ElementIndex * parent.ElementLength + segmentOffset;
if (model.ReadMultiByteValue(lengthAddress, segmentLength) != newValue) {
model.WriteMultiByteValue(lengthAddress, segmentLength, token, newValue);
}
}
}

View File

@ -1035,6 +1035,32 @@ namespace HavenSoft.HexManiac.Tests {
Assert.Equal(2, Model[0]);
}
[Fact]
public void TableWithTableStreamChild_IncreaseLengthCausesRepoint_NoAssert() {
SetFullModel(0xFF);
ViewPort.Edit("@100 <180> <1C0> DEAD @00 <100> 02 @00 ^table[child<[text<\"\">]/count> count.]1 ");
ViewPort.Edit("@04 3 ");
Model.ResolveConflicts();
}
/// <summary>
/// Cloning a TableStreamRun should keep the same length as the parent, even in cases where that length is 'wrong'
/// due to the underlying rom data being different from when the TableStreamRun was created.
/// </summary>
[Fact]
public void TableStreamRun_Clone_SameLength() {
SetFullModel(0xFF);
ViewPort.Edit("<100> 01 @00 ^parent[child<[a.]/count> count.]1 ");
Model.RawData[4] = 2;
var childTable = (ITableRun)Model.GetNextRun(0x100);
var clone = (ITableRun)childTable.Duplicate(0x100, childTable.PointerSources);
Assert.Equal(childTable.ElementCount, clone.ElementCount);
}
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());