2020-10-03 01:08:40 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Mainline format for Generation 1 & 2 <see cref="PKM"/> objects.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>This format stores <see cref="PKM.Nickname"/> and <see cref="PKM.OT_Name"/> in buffers separate from the rest of the details.</remarks>
|
|
|
|
|
public abstract class GBPKML : GBPKM
|
2020-10-03 01:08:40 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
internal const int StringLengthJapanese = 6;
|
|
|
|
|
internal const int StringLengthNotJapan = 11;
|
2022-11-25 01:42:17 +00:00
|
|
|
|
public sealed override int MaxStringLengthOT => Japanese ? 5 : 7;
|
|
|
|
|
public sealed override int MaxStringLengthNickname => Japanese ? 5 : 10;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public sealed override bool Japanese => RawOT.Length == StringLengthJapanese;
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
internal readonly byte[] RawOT;
|
|
|
|
|
internal readonly byte[] RawNickname;
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
// Trash Bytes
|
|
|
|
|
public sealed override Span<byte> Nickname_Trash => RawNickname;
|
|
|
|
|
public sealed override Span<byte> OT_Trash => RawOT;
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
protected GBPKML(int size, bool jp = false) : base(size)
|
|
|
|
|
{
|
|
|
|
|
int strLen = jp ? StringLengthJapanese : StringLengthNotJapan;
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
// initialize string buffers
|
|
|
|
|
RawOT = new byte[strLen];
|
|
|
|
|
RawNickname = new byte[strLen];
|
|
|
|
|
RawOT.AsSpan().Fill(StringConverter12.G1TerminatorCode);
|
|
|
|
|
RawNickname.AsSpan().Fill(StringConverter12.G1TerminatorCode);
|
|
|
|
|
}
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
protected GBPKML(byte[] data, bool jp = false) : base(data)
|
|
|
|
|
{
|
|
|
|
|
int strLen = jp ? StringLengthJapanese : StringLengthNotJapan;
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
// initialize string buffers
|
|
|
|
|
RawOT = new byte[strLen];
|
|
|
|
|
RawNickname = new byte[strLen];
|
|
|
|
|
RawOT.AsSpan().Fill(StringConverter12.G1TerminatorCode);
|
|
|
|
|
RawNickname.AsSpan().Fill(StringConverter12.G1TerminatorCode);
|
|
|
|
|
}
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public override void SetNotNicknamed(int language) => GetNonNickname(language).CopyTo(RawNickname);
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
protected override byte[] GetNonNickname(int language)
|
|
|
|
|
{
|
|
|
|
|
var name = SpeciesName.GetSpeciesNameGeneration(Species, language, Format);
|
|
|
|
|
var len = Nickname_Trash.Length;
|
|
|
|
|
byte[] data = new byte[len];
|
|
|
|
|
SetString(name.AsSpan(), data, len, StringConverterOption.Clear50);
|
|
|
|
|
if (!Korean)
|
2020-10-03 01:08:40 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
// Decimal point<->period fix
|
|
|
|
|
for (int i = 0; i < data.Length; i++)
|
2021-05-29 22:31:47 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (data[i] == 0xF2)
|
|
|
|
|
data[i] = 0xE8;
|
2021-05-29 22:31:47 +00:00
|
|
|
|
}
|
2020-10-03 01:08:40 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int SetString(ReadOnlySpan<char> value, Span<byte> destBuffer, int maxLength, StringConverterOption option = StringConverterOption.None)
|
|
|
|
|
{
|
|
|
|
|
if (Korean)
|
|
|
|
|
return StringConverter2KOR.SetString(destBuffer, value, maxLength, option);
|
|
|
|
|
return StringConverter12.SetString(destBuffer, value, maxLength, Japanese, option);
|
|
|
|
|
}
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public sealed override string Nickname
|
|
|
|
|
{
|
|
|
|
|
get
|
2020-10-03 01:08:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (Korean)
|
2022-06-18 18:04:24 +00:00
|
|
|
|
return StringConverter2KOR.GetString(RawNickname);
|
|
|
|
|
return StringConverter12.GetString(RawNickname, Japanese);
|
2020-10-03 01:08:40 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
set
|
2020-10-03 01:08:40 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (!IsNicknamed && Nickname == value)
|
|
|
|
|
return;
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
SetStringKeepTerminatorStyle(value.AsSpan(), RawNickname);
|
2020-10-03 01:08:40 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
}
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public sealed override string OT_Name
|
|
|
|
|
{
|
|
|
|
|
get
|
2020-10-03 01:08:40 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (Korean)
|
|
|
|
|
return StringConverter2KOR.GetString(RawOT.AsSpan());
|
|
|
|
|
return StringConverter12.GetString(RawOT.AsSpan(), Japanese);
|
2020-10-03 01:08:40 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
set
|
2020-10-03 01:08:40 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (value == OT_Name)
|
|
|
|
|
return;
|
|
|
|
|
SetStringKeepTerminatorStyle(value.AsSpan(), RawOT);
|
2020-10-03 01:08:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
|
|
private void SetStringKeepTerminatorStyle(ReadOnlySpan<char> value, Span<byte> exist)
|
|
|
|
|
{
|
|
|
|
|
// Reset the destination buffer based on the termination style of the existing string.
|
|
|
|
|
bool zeroed = exist.IndexOf((byte)0) != -1;
|
|
|
|
|
byte fill = zeroed ? (byte)0 : StringConverter12.G1TerminatorCode;
|
|
|
|
|
exist.Fill(fill);
|
|
|
|
|
|
|
|
|
|
int finalLength = Math.Min(value.Length + 1, exist.Length);
|
|
|
|
|
SetString(value, exist, finalLength);
|
|
|
|
|
}
|
2020-10-03 01:08:40 +00:00
|
|
|
|
}
|