PKHeX/PKHeX.Core/PKM/Strings/StringConverter345.cs
Kurt 47071b41f3
Refactoring: Span-based value writes and method signatures (#3361)
Existing `get`/`set` logic is flawed in that it doesn't work on Big Endian operating systems, and it allocates heap objects when it doesn't need to.

`System.Buffers.Binary.BinaryPrimitives` in the `System.Memory` NuGet package provides both Little Endian and Big Endian methods to read and write data; all the `get`/`set` operations have been reworked to use this new API. This removes the need for PKHeX's manual `BigEndian` class, as all functions are already covered by the BinaryPrimitives API.

The `StringConverter` has now been rewritten to accept a Span to read from & write to, no longer requiring a temporary StringBuilder.

Other Fixes included:
- The Super Training UI for Gen6 has been reworked according to the latest block structure additions.
- Cloning a Stadium2 Save File now works correctly (opening from the Folder browser list).
- Checksum & Sanity properties removed from parent PKM class, and is now implemented via interface.
2022-01-02 21:35:59 -08:00

66 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core;
/// <summary>
/// Logic for converting a <see cref="string"/> for to future Generations.
/// </summary>
public static class StringConverter345
{
/// <summary>
/// Trash Bytes for Generation 3->4
/// </summary>
/// <remarks>String buffers are reused, data is not cleared which yields the trash bytes.</remarks>
public static readonly byte[][] G4TransferTrashBytes = {
Array.Empty<byte>(), // Unused
new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
new byte[] { 0x18, 0x20, 0x0D, 0x02, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0xA1, 0x0C, 0x02, 0xE0, 0xFF },
new byte[] { 0x74, 0x20, 0x0D, 0x02, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0xA1, 0x0C, 0x02, 0xE0, 0xFF },
new byte[] { 0x54, 0x20, 0x0D, 0x02, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x84, 0xA1, 0x0C, 0x02, 0xE0, 0xFF },
new byte[] { 0x74, 0x20, 0x0D, 0x02, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0xA1, 0x0C, 0x02, 0xE0, 0xFF },
Array.Empty<byte>(), // Unused
new byte[] { 0x74, 0x20, 0x0D, 0x02, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0xA1, 0x0C, 0x02, 0xE0, 0xFF },
};
/// <summary>
/// Remaps Gen5 Glyphs to unicode codepoint.
/// </summary>
/// <param name="buffer">Input characters to transfer in place</param>
/// <returns>Remapped string</returns>
public static void TransferGlyphs56(Span<byte> buffer)
{
var table = Glyph56;
for (int i = 0; i < buffer.Length; i += 2)
{
var c = (char)ReadUInt16LittleEndian(buffer[i..]);
if (table.TryGetValue(c, out c))
WriteUInt16LittleEndian(buffer[i..], c);
}
}
private static readonly Dictionary<char, char> Glyph56 = new()
{
{'\u2467', '\u00d7'}, // ×
{'\u2468', '\u00f7'}, // ÷
{'\u246c', '\uE08D'}, // …
{'\u246d', '\uE08E'}, // ♂
{'\u246e', '\uE08F'}, // ♀
{'\u246f', '\uE090'}, // ♠
{'\u2470', '\uE091'}, // ♣
{'\u2471', '\uE092'}, // ♥
{'\u2472', '\uE093'}, // ♦
{'\u2473', '\uE094'}, // ★
{'\u2474', '\uE095'}, // ◎
{'\u2475', '\uE096'}, // ○
{'\u2476', '\uE097'}, // □
{'\u2477', '\uE098'}, // △
{'\u2478', '\uE099'}, // ◇
{'\u2479', '\uE09A'}, // ♪
{'\u247a', '\uE09B'}, // ☀
{'\u247b', '\uE09C'}, // ☁
{'\u247d', '\uE09D'}, // ☂
};
}