Revise enc1/2 -> pk1/2 nickname set

Skip the IsNicknamed evaluation, saving at least 1 string allocation on ctor. Remember the IsNicknamed state when we set false.
GUI: provide the selected language rather than recalculate
This commit is contained in:
Kurt 2024-11-10 09:32:33 -06:00
parent 51464fb4af
commit 3c574a45f1
10 changed files with 44 additions and 39 deletions

View file

@ -109,7 +109,6 @@ public sealed record EncounterGift1 : IEncounterable, IEncounterMatch, IEncounte
{
Species = Species,
CurrentLevel = LevelMin,
Nickname = SpeciesName.GetSpeciesNameGeneration(Species, (int)lang, Generation),
Type1 = pi.Type1,
Type2 = pi.Type2,
DV16 = IVs.IsSpecified ? EncounterUtil.GetDV16(IVs) : EncounterUtil.GetRandomDVs(rand),
@ -144,6 +143,7 @@ public sealed record EncounterGift1 : IEncounterable, IEncounterMatch, IEncounte
_ => EncounterUtil.GetTrainerName(tr, (int)lang),
},
};
pk.SetNotNicknamed((int)lang);
pk.SetMoves(Moves);
pk.ResetPartyStats();

View file

@ -43,10 +43,10 @@ public sealed record EncounterSlot1(EncounterArea1 Parent, ushort Species, byte
OriginalTrainerName = EncounterUtil.GetTrainerName(tr, lang),
TID16 = tr.TID16,
Nickname = SpeciesName.GetSpeciesNameGeneration(Species, lang, Generation),
Type1 = pi.Type1,
Type2 = pi.Type2,
};
pk.SetNotNicknamed(lang);
EncounterUtil.SetEncounterMoves(pk, Version, LevelMin);

View file

@ -46,10 +46,10 @@ public sealed record EncounterStatic1(ushort Species, byte Level, GameVersion Ve
OriginalTrainerName = EncounterUtil.GetTrainerName(tr, lang),
TID16 = tr.TID16,
Nickname = SpeciesName.GetSpeciesNameGeneration(Species, lang, Generation),
Type1 = pi.Type1,
Type2 = pi.Type2,
};
pk.SetNotNicknamed(lang);
EncounterUtil.SetEncounterMoves(pk, Version, LevelMin);

View file

@ -100,7 +100,6 @@ public sealed record EncounterGift2
Species = Species,
CurrentLevel = CurrentLevel == 0 ? LevelMin : CurrentLevel,
OriginalTrainerFriendship = pi.BaseFriendship,
Nickname = SpeciesName.GetSpeciesNameGeneration(Species, (int)lang, Generation),
TID16 = Trainer switch
{
@ -127,6 +126,7 @@ public sealed record EncounterGift2
_ => EncounterUtil.GetTrainerName(tr, 1),
},
};
pk.SetNotNicknamed((int)lang);
if (IsEgg)
{

View file

@ -94,8 +94,8 @@ public sealed record EncounterSlot2(EncounterArea2 Parent, ushort Species, byte
Language = lang,
OriginalTrainerName = tr.OT,
TID16 = tr.TID16,
Nickname = SpeciesName.GetSpeciesNameGeneration(Species, lang, Generation),
};
pk.SetNotNicknamed(lang);
if (Version == GameVersion.C)
{

View file

@ -51,9 +51,8 @@ public sealed record EncounterStatic2(ushort Species, byte Level, GameVersion Ve
OriginalTrainerName = tr.OT,
OriginalTrainerFriendship = pi.BaseFriendship,
Nickname = SpeciesName.GetSpeciesNameGeneration(Species, lang, Generation),
};
pk.SetNotNicknamed(lang);
if (IsEgg)
{

View file

@ -134,10 +134,10 @@ public sealed class SK2 : GBPKM, ICaughtData2
public override bool HasOriginalMetLocation => CaughtData != 0;
public override GameVersion Version { get => GameVersion.GSC; set { } }
protected override void GetNonNickname(int language, Span<byte> data)
protected override int GetNonNickname(int language, Span<byte> data)
{
var name = SpeciesName.GetSpeciesNameGeneration(Species, language, 2);
StringConverter2.SetString(data, name, data.Length, language, StringConverterOption.Clear50);
return StringConverter2.SetString(data, name, data.Length, language, StringConverterOption.Clear50);
}
public override void SetNotNicknamed(int language) => GetNonNickname(language, NicknameTrash);

View file

@ -31,8 +31,8 @@ public abstract class GBPKM : PKM
public override bool Valid { get => true; set { } }
public sealed override void RefreshChecksum() { }
private bool? _isnicknamed;
protected abstract void GetNonNickname(int language, Span<byte> data);
private protected bool? _isnicknamed;
protected abstract int GetNonNickname(int language, Span<byte> data);
public sealed override bool IsNicknamed
{
@ -41,11 +41,8 @@ public abstract class GBPKM : PKM
if (_isnicknamed is {} actual)
return actual;
var current = NicknameTrash;
Span<byte> expect = stackalloc byte[current.Length];
var language = GuessedLanguage();
GetNonNickname(language, expect);
var result = !current.SequenceEqual(expect);
bool result = GetIsNicknamedTrash(language);
_isnicknamed = result;
return result;
}
@ -57,18 +54,26 @@ public abstract class GBPKM : PKM
}
}
protected bool IsNicknamedBank
private bool GetIsNicknamedTrash(int language)
{
get
{
var spName = SpeciesName.GetSpeciesNameGeneration(Species, GuessedLanguage(), Format);
Span<char> nickname = stackalloc char[TrashCharCountNickname];
int len = LoadString(NicknameTrash, nickname);
return !nickname[..len].SequenceEqual(spName);
}
// Verify that all trash bytes match the expected nickname.
var current = NicknameTrash;
Span<byte> expect = stackalloc byte[current.Length];
GetNonNickname(language, expect);
return !current.SequenceEqual(expect);
}
private bool GetIsNicknamedLength(int language)
{
// Verify that only the displayed nickname bytes match the expected nickname.
var current = NicknameTrash;
Span<byte> expect = stackalloc byte[current.Length];
int length = GetNonNickname(language, expect);
return !current[..length].SequenceEqual(expect[..length]);
}
protected bool IsNicknamedBank => GetIsNicknamedLength(GuessedLanguage());
public sealed override int Language
{
get

View file

@ -46,17 +46,19 @@ public abstract class GBPKML : GBPKM
NicknameTrash.Fill(StringConverter1.TerminatorCode);
}
public override void SetNotNicknamed(int language) => GetNonNickname(language, RawNickname);
public override void SetNotNicknamed(int language)
{
GetNonNickname(language, RawNickname);
_isnicknamed = false;
}
protected override void GetNonNickname(int language, Span<byte> data)
protected override int GetNonNickname(int language, Span<byte> data)
{
var name = SpeciesName.GetSpeciesNameGeneration(Species, language, Format);
SetString(data, name, data.Length, StringConverterOption.Clear50);
if (Korean)
return;
// Decimal point<->period fix
data.Replace<byte>(0xF2, 0xE8);
int length = SetString(data, name, data.Length, StringConverterOption.Clear50);
if (!Korean) // Decimal point<->period fix
data.Replace<byte>(0xF2, 0xE8);
return length;
}
public sealed override string Nickname

View file

@ -1354,25 +1354,24 @@ public sealed partial class PKMEditor : UserControl, IMainEditor
return;
}
string nick;
string nickname;
int language = WinFormsUtil.GetIndex(CB_Language);
if (CHK_IsEgg.Checked)
{
// Get the egg name.
int language = WinFormsUtil.GetIndex(CB_Language);
nick = SpeciesName.GetEggName(language, Entity.Format);
nickname = SpeciesName.GetEggName(language, Entity.Format);
}
else
{
// If name is that of another language, don't replace the nickname
if (sender != CB_Language && !SpeciesName.IsNicknamedAnyLanguage(species, TB_Nickname.Text, Entity.Format))
return;
int lang = WinFormsUtil.GetIndex(CB_Language);
nick = SpeciesName.GetSpeciesNameGeneration(species, lang, Entity.Format);
nickname = SpeciesName.GetSpeciesNameGeneration(species, language, Entity.Format);
}
TB_Nickname.Text = nick;
TB_Nickname.Text = nickname;
if (Entity is GBPKM pk)
pk.SetNotNicknamed();
pk.SetNotNicknamed(language);
}
private void UpdateNicknameClick(object sender, MouseEventArgs e)