PKHeX/PKHeX.Core/PKM/Shared/GBPKML.cs
Kurt 52a67f2425 Add Stadium2 Support
Introducing a new PKM format: SK2
Split ICaughtData2 off of PK2 so it can be shared with SK2 when type-checks occur
Add conversion for PK2<->SK2
Split the split-buffer handling for GBPKM to GBPKML (what a name), so that I can reuse shared accessor logic for SK2.
2020-10-02 18:08:40 -07:00

109 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
{
public abstract class GBPKML : GBPKM
{
internal const int STRLEN_J = 6;
internal const int STRLEN_U = 11;
public sealed override int OTLength => Japanese ? 5 : 7;
public sealed override int NickLength => Japanese ? 5 : 10;
public sealed override bool Japanese => otname.Length == STRLEN_J;
internal readonly byte[] otname;
internal readonly byte[] nick;
// Trash Bytes
public sealed override byte[] Nickname_Trash { get => nick; set { if (value.Length == nick.Length) value.CopyTo(nick, 0); } }
public sealed override byte[] OT_Trash { get => otname; set { if (value.Length == otname.Length) value.CopyTo(otname, 0); } }
protected GBPKML(int size, bool jp = false) : base(size)
{
int strLen = jp ? STRLEN_J : STRLEN_U;
// initialize string buffers
otname = new byte[strLen];
nick = new byte[strLen];
for (int i = 0; i < otname.Length; i++)
otname[i] = nick[i] = 0x50;
}
protected GBPKML(byte[] data, bool jp = false) : base(data)
{
int strLen = jp ? STRLEN_J : STRLEN_U;
// initialize string buffers
otname = new byte[strLen];
nick = new byte[strLen];
for (int i = 0; i < otname.Length; i++)
otname[i] = nick[i] = 0x50;
}
public override void SetNotNicknamed(int language) => GetNonNickname(language).CopyTo(nick);
protected override IEnumerable<byte> GetNonNickname(int language)
{
var name = SpeciesName.GetSpeciesNameGeneration(Species, language, Format);
var len = Nickname_Trash.Length;
var bytes = SetString(name, len);
var data = bytes.Concat(Enumerable.Repeat((byte)0x50, len - bytes.Length));
if (!Korean)
data = data.Select(b => (byte)(b == 0xF2 ? 0xE8 : b)); // Decimal point<->period fix
return data;
}
private byte[] SetString(string value, int maxLength)
{
if (Korean)
return StringConverter2KOR.SetString2KOR(value, maxLength - 1);
return StringConverter12.SetString1(value, maxLength - 1, Japanese);
}
public sealed override string Nickname
{
get
{
if (Korean)
return StringConverter2KOR.GetString2KOR(nick, 0, nick.Length);
return StringConverter12.GetString1(nick, 0, nick.Length, Japanese);
}
set
{
if (!IsNicknamed && Nickname == value)
return;
Nickname_Trash = GetStringSpecial(value, Nickname_Trash.Length);
}
}
public sealed override string OT_Name
{
get
{
if (Korean)
return StringConverter2KOR.GetString2KOR(otname, 0, otname.Length);
return StringConverter12.GetString1(otname, 0, otname.Length, Japanese);
}
set => OT_Trash = GetStringSpecial(value, OT_Trash.Length);
}
private byte[] GetStringSpecial(string value, int length)
{
byte[] strdata = SetString(value, length);
if (nick.All(b => b != 0) || nick[length - 1] != 0x50 || Array.FindIndex(nick, b => b == 0) != strdata.Length - 1)
return strdata;
int firstInd = Array.FindIndex(nick, b => b == 0);
for (int i = firstInd; i < length - 1; i++)
{
if (nick[i] != 0)
break;
}
return strdata.Take(strdata.Length - 1).ToArray();
}
}
}