PKHeX/PKHeX.Core/Saves/Substructures/Gen6/PSS6.cs

89 lines
2.9 KiB
C#
Raw Normal View History

Refactoring: Move Source (Legality) (#3560) Rewrites a good amount of legality APIs pertaining to: * Legal moves that can be learned * Evolution chains & cross-generation paths * Memory validation with forgotten moves In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data. The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space. The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation. * `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game. * `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`). * Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
using System;
using System.Collections.Generic;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core;
public static class PSS6
{
private const string Header = "PSS List";
private static readonly string[] headers = { "PSS Data - Friends", "PSS Data - Acquaintances", "PSS Data - Passerby" };
public static List<string> GetPSSParse(SAV6 SAV)
{
var result = new List<string> {Header};
int offset = SAV.PSS;
var data = SAV.Data;
for (int g = 0; g < 3; g++)
{
result.Add("----");
result.Add(headers[g]);
result.Add("----");
// uint count = ReadUInt32LittleEndian(data.AsSpan(offset + 0x4E20));
ReadTrainers(result, data, offset, 100);
offset += 0x5000; // Advance to next block
}
return result;
}
Refactoring: Move Source (Legality) (#3560) Rewrites a good amount of legality APIs pertaining to: * Legal moves that can be learned * Evolution chains & cross-generation paths * Memory validation with forgotten moves In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data. The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space. The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation. * `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game. * `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`). * Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
private static void ReadTrainers(ICollection<string> result, Span<byte> data, int offset, int count)
{
int r_offset = offset;
const int size = 0xC8;
for (int i = 0; i < count; i++)
{
Refactoring: Move Source (Legality) (#3560) Rewrites a good amount of legality APIs pertaining to: * Legal moves that can be learned * Evolution chains & cross-generation paths * Memory validation with forgotten moves In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data. The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space. The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation. * `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game. * `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`). * Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
if (!ReadTrainer(result, data.Slice(r_offset, size)))
break; // No data present here
if (i > 0)
result.Add(string.Empty);
r_offset += size; // Advance to next entry
}
}
private static bool ReadTrainer(ICollection<string> result, ReadOnlySpan<byte> data)
{
ulong pssID = ReadUInt64LittleEndian(data);
if (pssID == 0)
return false; // no data
string otname = StringConverter6.GetString(data.Slice(0x08, 0x1A));
string message = StringConverter6.GetString(data.Slice(0x22, 0x22));
// Trim terminated
// uint unk1 = ReadUInt32LittleEndian(data[0x44..]);
// ulong unk2 = ReadUInt64LittleEndian(data[0x48..]);
// uint unk3 = ReadUInt32LittleEndian(data[0x50..]);
// uint unk4 = ReadUInt16LittleEndian(data[0x54..]);
byte regionID = data[0x56];
byte countryID = data[0x57];
byte game = data[0x5A];
// ulong outfit = ReadUInt64LittleEndian(data.AsSpan(ofs + 0x5C));
int favpkm = ReadUInt16LittleEndian(data[0x9C..]) & 0x7FF;
string gamename = GetGameName(game);
var (country, region) = GeoLocation.GetCountryRegionText(countryID, regionID, GameInfo.CurrentLanguage);
result.Add($"OT: {otname}");
result.Add($"Message: {message}");
result.Add($"Game: {gamename}");
result.Add($"Country: {country}");
result.Add($"Region: {region}");
result.Add($"Favorite: {GameInfo.Strings.specieslist[favpkm]}");
return false;
}
private static string GetGameName(int game)
{
const string unk = "UNKNOWN GAME";
if (game < 0)
return unk;
var list = GameInfo.Strings.gamelist;
if (game >= list.Length)
return unk;
return list[game];
}
}