Skip to content

Commit

Permalink
model pointer improvements
Browse files Browse the repository at this point in the history
When generating a default anchor name, make sure that name isn't already in use.
When generating a default anchor name, put it in `misc.temp.`. Don't save anchors in from namespace to the toml file. This should make cut/paste operations less prone to adding noise to the toml.
  • Loading branch information
haven1433 committed May 30, 2022
1 parent 7a8ab97 commit 3d98a79
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/HexManiac.Core/Models/PokemonModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1918,14 +1918,21 @@ public class PokemonModel : BaseModel {
var textSample = GetSampleText(run);
var initialAddress = run.Start.ToString("X6");

return $"misc.{gameCodeText}_{initialAddress}{textSample}";
var defaultName = $"misc.temp.{gameCodeText}_{initialAddress}{textSample}";
if (!addressForAnchor.ContainsKey(defaultName)) return defaultName;

int counter = 0;
while (true) {
counter++;
if (!addressForAnchor.ContainsKey(defaultName + "_" + counter)) return defaultName + "_" + counter;
}
}

/// <summary>
/// If this model recognizes a GameCode AsciiRun, return that code formatted as a name.
/// </summary>
public static string ReadGameCode(IDataModel model) {
var address = model.GetAddressFromAnchor(new NoDataChangeDeltaModel(), -1, "GameCode");
var address = model.GetAddressFromAnchor(new NoDataChangeDeltaModel(), -1, "data.header.gamecode");
if (address == Pointer.NULL) return string.Empty;
if (!(model.GetNextRun(address) is AsciiRun gameCode) || gameCode.Start != address) return string.Empty;
return new string(gameCode.Length.Range().Select(i => (char)model[gameCode.Start + i]).ToArray());
Expand Down Expand Up @@ -2066,6 +2073,7 @@ public class PokemonModel : BaseModel {
var anchors = new List<StoredAnchor>();
foreach (var kvp in anchorForAddress) {
var (address, name) = (kvp.Key, kvp.Value);
if (name.StartsWith("misc.temp.")) continue; // don't persist miscilaneous temp anchors
var index = BinarySearch(address);
if (index < 0) continue;
var format = runs[index].FormatString;
Expand Down
34 changes: 34 additions & 0 deletions src/HexManiac.Tests/Before_Baseclass/PointerModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,40 @@ public class PointerModelTests : BaseViewModelTestClass {
Assert.True(ViewPort.IsMetadataOnlyChange);
}

[Fact]
public void DefaultAnchorNameInPuse_CreateDefaultAnchor_UsesDifferentName() {
SetFullModel(0xFF);
ViewPort.Edit("@20 ^misc.temp._000100 @00!00(8) ^table[ptr<>]2 <100> ");

ViewPort.Goto.Execute("100");
ViewPort.SelectionEnd = new(3, 0);
ViewPort.Cut(FileSystem);

Assert.Equal(0x20, Model.GetAddressFromAnchor(new(), -1, "misc.temp._000100")); // old anchor is still there
Assert.Equal("^misc.temp._000100_1 FF FF FF FF", FileSystem.CopyText.value); // copy includes new anchor
Assert.Equal(new[] { 0x00 }, Model.GetUnmappedSourcesToAnchor("misc.temp._000100_1")); // address 0 should point to the new anchor
}

[Fact]
public void TempAnchor_ExportMetadata_DoNotIncludeTempAnchor() {
ViewPort.Edit("^misc.temp.stuff ");

var metadata = Model.ExportMetadata(Singletons.MetadataInfo);

var anchors = metadata.NamedAnchors.Select(anchor => anchor.Name).ToList();
Assert.DoesNotContain("misc.temp.stuff", anchors);
}

[Fact]
public void PointerToTempAnchor_ExportMetadata_DoIncludeUnmappedPointer() {
ViewPort.Edit("<misc.temp.stuff>");

var metadata = Model.ExportMetadata(Singletons.MetadataInfo);

var unmappedPointers = metadata.UnmappedPointers.Select(up => up.Name).ToList();
Assert.Contains("misc.temp.stuff", unmappedPointers);
}

private void StandardSetup(out byte[] data, out PokemonModel model, out ViewPort viewPort) {
data = new byte[0x200];
model = new PokemonModel(data);
Expand Down

0 comments on commit 3d98a79

Please sign in to comment.