Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
SaveFileToolset Class Reference

Low-level read/write primitives over a SaveFile's raw 32 KB buffer. More...

#include <savefiletoolset.h>

Collaboration diagram for SaveFileToolset:

Public Member Functions

 SaveFileToolset (SaveFile *newSaveFile)
var32 bcd2int (QVector< var8 > val)
 bcd2number -> takes an array with a BCD and returns the corresponding number.
QVector< var8int2bcd (var32 number, var8 size=2)
 number2bcd -> takes a number and returns the corresponding BCD in an array input: 16 bit positive number, array size (How many bytes) output: array credit: joaomaia @ https://gist.github.com/joaomaia/3892692
QVector< var8getRange (var16 from, var16 size, bool reverse=false)
 Copies a range of bytes to a buffer of size and returns them.
void copyRange (var16 addr, var16 size, QVector< var8 > data, bool reverse=false)
 Copies data into the save at a particular place, going no further than the maximum size desired and/or the source array length.
QString getStr (var16 addr, var16 size, var8 maxLen)
 Gets a string from the sav file, converted from in-game font encoding to UTF-8 for easy reading and manipulation.
void setStr (var16 addr, var16 size, var8 maxLen, QString str)
 Sets a string to the sav file, converted from UTF-8 to in-game font encoding.
QString getHex (var16 addr, var16 size, bool reverse=false)
 Gets a value in hex from the save file.
void setHex (var16 addr, var16 size, QString hex, bool reverse=false)
 Saves a hex value to bytes in the save file.
var32 getBCD (var16 addr, var8 size)
 Get a raw BCD value as a number.
void setBCD (var16 addr, var8 size, var32 val)
 Set a raw BCD val from a given number.
bool getBit (var16 addr, var8 size, var8 bit, bool reverse=false)
 Get a bit from a value.
void setBit (var16 addr, var8 size, var8 bit, bool value, bool reverse=false)
 Set a bit into a value.
var16 getWord (var16 addr, bool reverse=false)
 Get a 16-bit value. 0x12,0x34 <=> 0x1234. If you want the reverse then reverse it.
void setWord (var16 addr, var16 val, bool reverse=false)
 Set a 16-bit value. 0x12,0x34 <=> 0x1234. If you want the reverse then reverse it.
var8 getByte (var16 addr)
 Simply gets a byte.
void setByte (var16 addr, var8 val)
 Simply sets a byte.
QVector< bool > getBitField (var16 addr, var16 size)
 Gets an entire bitfield as a vector of bools.
void setBitField (var16 addr, var16 size, QVector< bool > src)
 Sets an entire bitfield from a vector of bools.
var8 getChecksum (var16 addr, var16 size)
 Gets a single checksum from a given range.
void recalcBoxesChecksums ()
 Recalculates the checksum for all the boxes.
void recalcChecksums (bool force=false)
 Recalculates all checksums in all banks, following the strict rule of only calculating what's needed and when.

Protected Attributes

SaveFilesaveFile
 Owning save; its raw data is what every method here reads/writes.

Detailed Description

Low-level read/write primitives over a SaveFile's raw 32 KB buffer.

Every byte the editor touches goes through here. The toolset speaks the Game Boy save's actual encodings – BCD numbers, in-game font strings, packed bit-fields, big/little-endian words, and the Gen 1 checksum – so higher layers (the expanded/ objects) can ask for "the money at this address" instead of doing byte math. Addresses are absolute offsets into the raw save.

It holds a back-pointer to its owning SaveFile and reads/writes that file's data directly; it does not own the buffer. For cursor-style sequential access with auto-advancing offsets, see SaveFileIterator, which wraps this.

Note
reverse flags exist because some fields are stored in the opposite byte order; pass reverse = true to read/write them swapped.

Definition at line 41 of file savefiletoolset.h.

Constructor & Destructor Documentation

◆ SaveFileToolset()

SaveFileToolset::SaveFileToolset ( SaveFile * newSaveFile)
Parameters
newSaveFileThe save whose raw data this toolset operates on.

Definition at line 27 of file savefiletoolset.cpp.

References saveFile.

Member Function Documentation

◆ bcd2int()

var32 SaveFileToolset::bcd2int ( QVector< var8 > val)

bcd2number -> takes an array with a BCD and returns the corresponding number.

input: array output: number credit: joaomaia @ https://gist.github.com/joaomaia/3892692

Parameters
valBytes holding a binary-coded-decimal value.
Returns
The decoded base-10 number.

Definition at line 32 of file savefiletoolset.cpp.

Referenced by getBCD().

◆ copyRange()

void SaveFileToolset::copyRange ( var16 addr,
var16 size,
QVector< var8 > data,
bool reverse = false )

Copies data into the save at a particular place, going no further than the maximum size desired and/or the source array length.

Parameters
addrindex to start copying at, inclusive
sizemaximum length to copy
dataarray of data to copy into, will stop at size or data length
reversereverse copies the data into the specified location

Definition at line 74 of file savefiletoolset.cpp.

References saveFile.

Referenced by SaveFileIterator::copyRange(), recalcBoxesChecksums(), AreaLoadedSprites::save(), setBCD(), setHex(), setStr(), and setWord().

◆ getBCD()

var32 SaveFileToolset::getBCD ( var16 addr,
var8 size )

Get a raw BCD value as a number.

Casino coins are 16-bit BCD meaning the maximum is 9,999. Player money is 24-bit BCD meaning the maximum is 999,999.

Definition at line 154 of file savefiletoolset.cpp.

References bcd2int(), and getRange().

Referenced by SaveFileIterator::getBCD().

◆ getBit()

bool SaveFileToolset::getBit ( var16 addr,
var8 size,
var8 bit,
bool reverse = false )

Get a bit from a value.

Here for conv. but it's actually simpler just to test it yourself given this is so complicated and expensive for bit testing. This method can only bit test a bit up to 4 bytes.

Definition at line 164 of file savefiletoolset.cpp.

References getRange().

Referenced by getBitField().

◆ getBitField()

QVector< bool > SaveFileToolset::getBitField ( var16 addr,
var16 size )

Gets an entire bitfield as a vector of bools.

Definition at line 242 of file savefiletoolset.cpp.

References getBit().

Referenced by SaveFileIterator::getBitField(), WorldHidden::load(), WorldMissables::load(), WorldTowns::load(), WorldTrades::load(), and PlayerPokedex::loadPokedex().

◆ getByte()

var8 SaveFileToolset::getByte ( var16 addr)

Simply gets a byte.

Definition at line 232 of file savefiletoolset.cpp.

References saveFile.

Referenced by SaveFileIterator::getByte().

◆ getChecksum()

var8 SaveFileToolset::getChecksum ( var16 addr,
var16 size )

Gets a single checksum from a given range.

This properly calculates the checksum that Gen 1 games expect.

Definition at line 293 of file savefiletoolset.cpp.

References getRange().

Referenced by recalcBoxesChecksums(), and recalcChecksums().

◆ getHex()

QString SaveFileToolset::getHex ( var16 addr,
var16 size,
bool reverse = false )

Gets a value in hex from the save file.

Definition at line 106 of file savefiletoolset.cpp.

References getRange().

Referenced by SaveFileIterator::getHex().

◆ getRange()

QVector< var8 > SaveFileToolset::getRange ( var16 from,
var16 size,
bool reverse = false )

Copies a range of bytes to a buffer of size and returns them.

Parameters
fromStart offset (inclusive).
sizeByte count.
reverseRead the range in reversed byte order.

Definition at line 59 of file savefiletoolset.cpp.

References saveFile.

Referenced by getBCD(), getBit(), getChecksum(), getHex(), SaveFileIterator::getRange(), getStr(), getWord(), AreaLoadedSprites::load(), and setBit().

◆ getStr()

QString SaveFileToolset::getStr ( var16 addr,
var16 size,
var8 maxLen )

Gets a string from the sav file, converted from in-game font encoding to UTF-8 for easy reading and manipulation.

Parameters
addrStart offset.
sizeField byte size.
maxLenMax characters.

Definition at line 88 of file savefiletoolset.cpp.

References FontsDB::convertFromCode(), getRange(), and FontsDB::inst().

Referenced by SaveFileIterator::getStr().

◆ getWord()

var16 SaveFileToolset::getWord ( var16 addr,
bool reverse = false )

Get a 16-bit value. 0x12,0x34 <=> 0x1234. If you want the reverse then reverse it.

Definition at line 209 of file savefiletoolset.cpp.

References getRange().

Referenced by SaveFileIterator::getWord().

◆ int2bcd()

QVector< var8 > SaveFileToolset::int2bcd ( var32 number,
var8 size = 2 )

number2bcd -> takes a number and returns the corresponding BCD in an array input: 16 bit positive number, array size (How many bytes) output: array credit: joaomaia @ https://gist.github.com/joaomaia/3892692

Parameters
numberThe value to encode.
sizeOutput width in bytes.
Returns
number encoded as binary-coded decimal.

Definition at line 44 of file savefiletoolset.cpp.

Referenced by setBCD().

◆ recalcBoxesChecksums()

void SaveFileToolset::recalcBoxesChecksums ( )

Recalculates the checksum for all the boxes.

This only needs to be called when the boxes checksums are used. The game doesn't always use the box checksums or calculate them. Use recalcChecksums below to have them calculated as appropriate.

Definition at line 315 of file savefiletoolset.cpp.

References copyRange(), getChecksum(), and saveFile.

◆ recalcChecksums()

void SaveFileToolset::recalcChecksums ( bool force = false)

Recalculates all checksums in all banks, following the strict rule of only calculating what's needed and when.

If the game will never calculate the box checksums then neither will we – there are cases when it won't. This also calculates bank 1, thus covering all checksums in the file. Part of honouring byte-exact fidelity: we don't write checksums the real game wouldn't.

Parameters
forceRecalculate even the conditionally-skipped checksums.

Definition at line 351 of file savefiletoolset.cpp.

References getChecksum(), and saveFile.

◆ setBCD()

void SaveFileToolset::setBCD ( var16 addr,
var8 size,
var32 val )

Set a raw BCD val from a given number.

Casino coins are 16-bit BCD meaning the maximum is 9,999. Player money is 24-bit BCD meaning the maximum is 999,999.

Definition at line 159 of file savefiletoolset.cpp.

References copyRange(), and int2bcd().

Referenced by SaveFileIterator::setBCD().

◆ setBit()

void SaveFileToolset::setBit ( var16 addr,
var8 size,
var8 bit,
bool value,
bool reverse = false )

Set a bit into a value.

Here for conv. but it's actually simpler just to set it yourself given this is so complicated and expensive for bit setting. This method can only bit set a bit up to 4 bytes.

Definition at line 183 of file savefiletoolset.cpp.

References getRange(), and setHex().

Referenced by AreaNPC::save(), WorldCompleted::save(), and setBitField().

◆ setBitField()

void SaveFileToolset::setBitField ( var16 addr,
var16 size,
QVector< bool > src )

Sets an entire bitfield from a vector of bools.

Definition at line 265 of file savefiletoolset.cpp.

References setBit().

Referenced by WorldHidden::save(), WorldMissables::save(), WorldTowns::save(), WorldTrades::save(), PlayerPokedex::savePokedex(), and SaveFileIterator::setBitField().

◆ setByte()

◆ setHex()

void SaveFileToolset::setHex ( var16 addr,
var16 size,
QString hex,
bool reverse = false )

Saves a hex value to bytes in the save file.

Definition at line 126 of file savefiletoolset.cpp.

References copyRange().

Referenced by setBit(), and SaveFileIterator::setHex().

◆ setStr()

void SaveFileToolset::setStr ( var16 addr,
var16 size,
var8 maxLen,
QString str )

Sets a string to the sav file, converted from UTF-8 to in-game font encoding.

Parameters
addrStart offset.
sizeField byte size.
maxLenMax characters.
strThe UTF-8 text to store.

Definition at line 97 of file savefiletoolset.cpp.

References FontsDB::convertToCode(), copyRange(), and FontsDB::inst().

Referenced by PlayerBasics::save(), Rival::save(), and SaveFileIterator::setStr().

◆ setWord()

void SaveFileToolset::setWord ( var16 addr,
var16 val,
bool reverse = false )

Set a 16-bit value. 0x12,0x34 <=> 0x1234. If you want the reverse then reverse it.

Definition at line 220 of file savefiletoolset.cpp.

References copyRange().

Referenced by SaveFileIterator::setWord().

Member Data Documentation

◆ saveFile

SaveFile* SaveFileToolset::saveFile
protected

Owning save; its raw data is what every method here reads/writes.

Definition at line 161 of file savefiletoolset.h.

Referenced by copyRange(), getByte(), getRange(), recalcBoxesChecksums(), recalcChecksums(), SaveFileToolset(), and setByte().


The documentation for this class was generated from the following files: