PKHeX.Core provides powerful utilities for detecting and loading save files from all Pokémon generations (Gen 1-9). The library automatically detects save file types, handles encryption, and supports various formats including emulator saves and special handlers.
The simplest way to load a save file is using SaveUtil.GetSaveFile():
using PKHeX.Core;// Load a save file from diskstring path = "path/to/save.sav";SaveFile? sav = SaveUtil.GetSaveFile(path);if (sav != null){ Console.WriteLine($"Loaded {sav.Version} save file"); Console.WriteLine($"Trainer: {sav.OT}"); Console.WriteLine($"Play Time: {sav.PlayTimeString}");}else{ Console.WriteLine("Failed to load save file");}
You can also load save data from a byte array or Memory<byte>:
// Read save data into memorybyte[] data = File.ReadAllBytes("path/to/save.sav");Memory<byte> memory = new Memory<byte>(data);// Load the save fileSaveFile? sav = SaveUtil.GetSaveFile(memory);if (sav != null){ Console.WriteLine($"Loaded Generation {sav.Generation} save");}
Before attempting to load a file, you can check if the size is valid:
string path = "save.sav";long fileSize = new FileInfo(path).Length;if (SaveUtil.IsSizeValid(fileSize)){ var sav = SaveUtil.GetSaveFile(path); // Process save file}else{ Console.WriteLine("Invalid save file size");}
For Colosseum and XD saves stored in GameCube Memory Cards:
byte[] memoryCardData = File.ReadAllBytes("Card.raw");var memCard = new SAV3GCMemoryCard(memoryCardData);if (SaveUtil.TryGetSaveFile(memCard, out SaveFile? gcSave)){ if (gcSave is SAV3Colosseum colo) { Console.WriteLine("Colosseum save loaded"); } else if (gcSave is SAV3XD xd) { Console.WriteLine("XD: Gale of Darkness save loaded"); }}
PKHeX automatically handles various emulator formats:
// Handles DeSmuME, Action Replay, and other formatsvar sav = SaveUtil.GetSaveFile("save.dsv");// Check if special handlers were usedif (sav?.Metadata.Handler != null){ Console.WriteLine($"Loaded with handler: {sav.Metadata.Handler.GetType().Name}");}