From d79e0b94973c50067d305a96a7fa0fb2b4e295cd Mon Sep 17 00:00:00 2001 From: Benjamin Popp Date: Tue, 2 Jul 2019 21:12:59 -0500 Subject: [PATCH] Use constants instead of magic strings --- src/HexManiac.Core/Models/AutoSearchModel.cs | 20 +++++++++++-------- .../ViewModels/IQuickEditItem.cs | 20 ++++++++++--------- src/HexManiac.Tests/AutoSearchTests.cs | 10 +++++----- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/HexManiac.Core/Models/AutoSearchModel.cs b/src/HexManiac.Core/Models/AutoSearchModel.cs index 82739a51..120c112d 100644 --- a/src/HexManiac.Core/Models/AutoSearchModel.cs +++ b/src/HexManiac.Core/Models/AutoSearchModel.cs @@ -14,6 +14,10 @@ namespace HavenSoft.HexManiac.Core.Models { FireRed = "BPRE", LeafGreen = "BPGE"; + public const string + MoveTutors = "tutormoves", + TutorCompatibility = "tutorcompatibility"; + private readonly string gameCode; private readonly ModelDelta noChangeDelta = new NoDataChangeDeltaModel(); @@ -158,10 +162,10 @@ namespace HavenSoft.HexManiac.Core.Models { tutorCompatibility = ReadPointer(list[0] + originalCode.Length); if (tutorMoves < 0 || tutorMoves > Count || tutorCompatibility < 0 || tutorCompatibility > Count) return; if (list.Count != 1) return; - if (!TryParse(this, "[move:movenames]15", tutorMoves, null, out var tutorMovesRun).HasError) { - ObserveAnchorWritten(noChangeDelta, "tutormoves", tutorMovesRun); - if (!TryParse(this, "[pokemon|b[]tutormoves]pokenames", tutorCompatibility, null, out var tutorCompatibilityRun).HasError) { - ObserveAnchorWritten(noChangeDelta, "tutorcompatibility", tutorCompatibilityRun); + if (!TryParse(this, $"[move:{EggMoveRun.MoveNamesTable}]15", tutorMoves, null, out var tutorMovesRun).HasError) { + ObserveAnchorWritten(noChangeDelta, MoveTutors, tutorMovesRun); + if (!TryParse(this, $"[pokemon|b[]{MoveTutors}]{EggMoveRun.PokemonNameTable}", tutorCompatibility, null, out var tutorCompatibilityRun).HasError) { + ObserveAnchorWritten(noChangeDelta, TutorCompatibility, tutorCompatibilityRun); } } } else if (gameCode == Emerald) { @@ -169,10 +173,10 @@ namespace HavenSoft.HexManiac.Core.Models { tutorMoves = ReadPointer(0x1B236C); tutorCompatibility = ReadPointer(0x1B2390); if (tutorMoves < 0 || tutorMoves > Count || tutorCompatibility < 0 || tutorCompatibility > Count) return; - if (!TryParse(this, "[move:movenames]30", tutorMoves, null, out var tutorMovesRun).HasError) { - ObserveAnchorWritten(noChangeDelta, "tutormoves", tutorMovesRun); - if (!TryParse(this, "[pokemon|b[]tutormoves]pokenames", tutorCompatibility, null, out var tutorCompatibilityRun).HasError) { - ObserveAnchorWritten(noChangeDelta, "tutorcompatibility", tutorCompatibilityRun); + if (!TryParse(this, $"[move:{EggMoveRun.MoveNamesTable}]30", tutorMoves, null, out var tutorMovesRun).HasError) { + ObserveAnchorWritten(noChangeDelta, MoveTutors, tutorMovesRun); + if (!TryParse(this, $"[pokemon|b[]{MoveTutors}]{EggMoveRun.PokemonNameTable}", tutorCompatibility, null, out var tutorCompatibilityRun).HasError) { + ObserveAnchorWritten(noChangeDelta, TutorCompatibility, tutorCompatibilityRun); } } } diff --git a/src/HexManiac.Core/ViewModels/IQuickEditItem.cs b/src/HexManiac.Core/ViewModels/IQuickEditItem.cs index 641919ff..74ae1f78 100644 --- a/src/HexManiac.Core/ViewModels/IQuickEditItem.cs +++ b/src/HexManiac.Core/ViewModels/IQuickEditItem.cs @@ -3,6 +3,8 @@ using HavenSoft.HexManiac.Core.ViewModels.DataFormats; using System; using System.Linq; +using static HavenSoft.HexManiac.Core.Models.AutoSearchModel; + namespace HavenSoft.HexManiac.Core.ViewModels { public interface IQuickEditItem { string Name { get; } @@ -27,8 +29,8 @@ namespace HavenSoft.HexManiac.Core.ViewModels { if (getTutorMove < 0 || canPokemonLearnTutorMove < 0) return false; // require that this data has a tutormoves and tutorcompatibility table, since we're messing with those - var tutormoves = model.GetAddressFromAnchor(viewPort.CurrentChange, -1, "tutormoves"); - var tutorcompatibility = model.GetAddressFromAnchor(viewPort.CurrentChange, -1, "tutorcompatibility"); + var tutormoves = model.GetAddressFromAnchor(viewPort.CurrentChange, -1, MoveTutors); + var tutorcompatibility = model.GetAddressFromAnchor(viewPort.CurrentChange, -1, TutorCompatibility); if (tutormoves == Pointer.NULL || tutorcompatibility == Pointer.NULL) { return false; } @@ -42,8 +44,8 @@ namespace HavenSoft.HexManiac.Core.ViewModels { var viewPort = (ViewPort)viewPortInterface; var model = viewPort.Model; var (getTutorMove, canPokemonLearnTutorMove, getTutorMove_Length, canPokemonLearnTutorMove_Length) = GetOffsets(viewPort); - var tutormoves = model.GetAddressFromAnchor(viewPort.CurrentChange, -1, "tutormoves"); - var tutorcompatibility = model.GetAddressFromAnchor(viewPort.CurrentChange, -1, "tutorcompatibility"); + var tutormoves = model.GetAddressFromAnchor(viewPort.CurrentChange, -1, MoveTutors); + var tutorcompatibility = model.GetAddressFromAnchor(viewPort.CurrentChange, -1, TutorCompatibility); InsertRoutine_GetTutorMove(viewPort, getTutorMove, getTutorMove_Length); InsertRoutine_CanPokemonLearnTutorMove(viewPort, canPokemonLearnTutorMove, canPokemonLearnTutorMove_Length); @@ -54,11 +56,11 @@ namespace HavenSoft.HexManiac.Core.ViewModels { public static (int getTutorMove, int canPokemonLearnTutorMove, int getTutorMove_Length, int canPokemonLearnTutorMove_Length) GetOffsets(ViewPort viewPort) { var model = viewPort.Model; var gameCode = new string(Enumerable.Range(0xAC, 4).Select(i => ((char)model[i])).ToArray()); - if (gameCode == "BPRE") { + if (gameCode == FireRed) { return (0x120BA8, 0x120BE8, 0x40, 0x54); - } else if (gameCode == "BPGE") { + } else if (gameCode == LeafGreen) { return (0x120B80, 0x120BC0, 0x40, 0x54); - } else if (gameCode == "BPEE") { + } else if (gameCode == Emerald) { return (0x1B2360, 0x1B2370, 0x10, 0x2C); } else { return (-1, -1, 0, 0); @@ -76,7 +78,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { .word */ - viewPort.Edit($"@{address:X6} 40 00 01 49 40 5A 70 47 "); // new data only 0xC long + viewPort.Edit($"@{address:X6} 40 00 01 49 40 5A 70 47 <{MoveTutors}> "); // new data only 0xC long for (int i = 0x0C; i < originalLength; i++) viewPort.Edit("00 "); } @@ -105,7 +107,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { viewPort.Edit($"@{address:X6} "); viewPort.Edit("07 4A 07 32 D2 08 50 43 CA 08 80 18 07 22 11 40 "); viewPort.Edit("02 4A 10 5C C8 40 01 22 10 40 70 47 "); - viewPort.Edit(" ::tutormoves "); // new data only 0x24 long + viewPort.Edit($"<{TutorCompatibility}> ::{MoveTutors} "); // new data only 0x24 long for (int i = 0x24; i < originalLength; i++) viewPort.Edit("00 "); } } diff --git a/src/HexManiac.Tests/AutoSearchTests.cs b/src/HexManiac.Tests/AutoSearchTests.cs index c4b6c5aa..bf9e2a5d 100644 --- a/src/HexManiac.Tests/AutoSearchTests.cs +++ b/src/HexManiac.Tests/AutoSearchTests.cs @@ -206,8 +206,8 @@ namespace HavenSoft.HexManiac.Tests { var model = LoadModel(game); var noChange = new NoDataChangeDeltaModel(); - var movesLocation = model.GetAddressFromAnchor(noChange, -1, "tutormoves"); - var compatibilityLocation = model.GetAddressFromAnchor(noChange, -1, "tutorcompatibility"); + var movesLocation = model.GetAddressFromAnchor(noChange, -1, AutoSearchModel.MoveTutors); + var compatibilityLocation = model.GetAddressFromAnchor(noChange, -1, AutoSearchModel.TutorCompatibility); // ruby and sapphire have no tutors // Gaia has move tutors, but it does a bunch of custom stuff (multiple tables) so I don't feel bad about not supporting it by default. @@ -251,13 +251,13 @@ namespace HavenSoft.HexManiac.Tests { expandTutors.Run(viewPort); // extend the table - var table = (ArrayRun)model.GetNextRun(model.GetAddressFromAnchor(new ModelDelta(), -1, "tutormoves")); + var table = (ArrayRun)model.GetNextRun(model.GetAddressFromAnchor(new ModelDelta(), -1, AutoSearchModel.MoveTutors)); viewPort.Goto.Execute((table.Start + table.Length).ToString("X6")); viewPort.Edit("+"); // the 4 bytes after the last pointer to tutor-compatibility should store the length of tutormoves - table = (ArrayRun)model.GetNextRun(model.GetAddressFromAnchor(new ModelDelta(), -1, "tutormoves")); - var tutorCompatibilityPointerSources = model.GetNextRun(model.GetAddressFromAnchor(new ModelDelta(), -1, "tutorcompatibility")).PointerSources; + table = (ArrayRun)model.GetNextRun(model.GetAddressFromAnchor(new ModelDelta(), -1, AutoSearchModel.MoveTutors)); + var tutorCompatibilityPointerSources = model.GetNextRun(model.GetAddressFromAnchor(new ModelDelta(), -1, AutoSearchModel.TutorCompatibility)).PointerSources; var word = (WordRun)model.GetNextRun(tutorCompatibilityPointerSources.Last() + 4); Assert.Equal(table.ElementCount, model.ReadValue(word.Start)); }