libpokemegb/include/RomReader.h
Philippe Symons aedb58832c
V0.3 (#7)
* Feature/add support for localized versions (#3)

* Add support for gen I localizations

There currently is no way to automatically detect the localization yet though.

* Add gen1_determineGameLanguage() and use it automatically in Gen1GameReader if not specified in the constructor

* Add support for the gen II localizations

Note: for Korean games, specifically, I did not implement the Korean character set
(nor do I plan to)

The reason is that it is entirely different and more complex to implement than the other
character sets. I'm not personally invested nor interested enough to go through the hassle.

This affects the pokémon names that will be displayed when using libpokemegb. Instead as
a fallback, it will return "poke-<number>" for these roms.

Feel free to contribute proper Korean character set (and pokémon names) support!

* Streamline the Gen I localization code to be structured similarly to the gen II's

* Fix bug

When Gen1GameReader is triggering the language detection automatically, the
Gen1IconDecoder instance is not updated with the detected language

* Update README.md

* Fix markdown special char escape problem in README.md

* Fix name decoding for localizations

* Fix detection of German Pokémon Yellow

* Fix bug: Japanese saves were not detected correctly for gen I

* Convert every character map entry into UTF-8.

libdragon requires every string to be in UTF-8 encoding

* Fix bug with Japanese saves. Wrong offset was used for the party storage in several functions

* Fix OT names and nicknames for Japanese cartridges

* Fix another issue with Gen I nicknames. Gen II is also still entirely broken in this regard

* Also fix Japanese pokemon name retrieval in gen I

* Fix OT and nicknames for Japanese gen I games

* Add support for Japanese and Korean Gen II save offsets

Japenese and Korean Gen II saves have a different save structure (data at different offsets)

* Disable save corruption check for Japanese/Korean games.

I don't have the right save offsets nor can find them. (I'm not adept at decompilation myself and pokegold/pokecrystal don't have the japanese offsets. Nor does PkHex)

* Refactor the gen1 related code to store the different SRAM offsets for gen 1 saves similarly as we do for gen 2

* Remove unnecessary casts

* Avoid trying to decode Korean trainername and rival name.

We don't support the character set, neither in libpokemegb and PokeMe64.

The Korean character set is a lot more complex than the other ones. But it would also require the inclusion of a lot of additional character glyphs in PokeMe64
in order to show the characters. Right now I don't feel like doing that for a localization that won't be used by many in combination with PokeMe64.

So instead of showing the trainer name and rival name, I will just replace it with "player" and "rival". It's not an amazing solution, but it's the best I'm willing to do for now.

* Add support for the Korean character sets.

This was needed after all, because we don't want injected pokémon on a Korean cartridge to end up having a nickname like "poke-<indexnumber>".
So that implies the need for being able to decode and encode pokémon names in Korean.

So yeah, I bit the bullet and added support for it. This was really annoying to do, but it has been done!

* Feature/add japanese distribution pokemon (#4)

* Add Gen I Japanese Distribution Event Pokémon

* Remove external definitions of each individual distribution pokémon from the headers and make them static in the source file

* Add isJapanese field to Gen2DistributionPokemon struct and adapt every existing definition

* Fix mistake in last commit

* Make sure the OT name is replaced when dealing with Japanese distribution pokémon on a different language cart and vice versa

* Add Japanese Mystery Egg #3 gen II  event distribution Pokémon

* Add remaining Japanese Event Distribution Pokémon

* Fix another issue with Gen I nicknames. Gen II is also still entirely broken in this regard

* Fix OT and nicknames for Japanese gen I games

* Refactor the gen1 related code to store the different SRAM offsets for gen 1 saves similarly as we do for gen 2

* Bugfix/fix distribution event pokemon inconsistencies (#5)

* Add Gen I Japanese Distribution Event Pokémon

* Remove external definitions of each individual distribution pokémon from the headers and make them static in the source file

* Add isJapanese field to Gen2DistributionPokemon struct and adapt every existing definition

* Fix mistake in last commit

* Make sure the OT name is replaced when dealing with Japanese distribution pokémon on a different language cart and vice versa

* Add Japanese Mystery Egg #3 gen II  event distribution Pokémon

* Add remaining Japanese Event Distribution Pokémon

* Fix another issue with Gen I nicknames. Gen II is also still entirely broken in this regard

* Fix OT and nicknames for Japanese gen I games

* Refactor the gen1 related code to store the different SRAM offsets for gen 1 saves similarly as we do for gen 2

* Fix some inconsistencies with Gen II distribution event pokémon.

* Make use of the Move enum instead of magic numbers for the pokémon moves (#6)

* Add Gen1GameReader::getCurrentMap() function to retrieve the current map the player is on.

I may be able to use this when adding a move deleter for gen1 games later, to make sure the player is in the pokemon center.
(to make sure the player doesn't get trapped because he/she would delete an HM move)

* Fix broken Japanese PC Box support + add support for Pokémon Green (JPN) + fix currentMap retrieval on japanese saves

So, turns out that PC Box support was entirely broken for the Japanese games with PokeMe64.

Apparently, the japanese games only have 8 boxes instead of 12, but each of them has a capacity of 30 pokémon instead
of 20!

So that changes things for the save offsets.

Also: the currentMap offset was wrong for Japanese games. So I had to fix that too!

* Updated README.md

* Fix the same issue with PC Boxes for Japanese Gen II games.

The international releases have 14 pc boxes with 20 pokemon each. But the japanese gen II games have 9 pc boxes with 30 entries each.

* Rework rom offsets for pokémon blue to avoid duplicate rom offset definitions.

The only one different from the red/green offsets is the japanese one anyway.

* Replace "PokeMe64" OT with "PM64" when injecting japanese mons into international games.

Turns out the official character limit was 7

* Fill the unused bytes of a nickname with the terminator byte.

This is needed because according to Bulbapedia, a nickname is only considered not a nickname if it matches with the
uppercase species name with all unused bytes set to the 0x50 terminator.

* Add gen1_isAPokeCenter() function
2025-02-17 12:38:21 +01:00

205 lines
6.4 KiB
C++

#ifndef ROMREADER_H
#define ROMREADER_H
#include "common.h"
#include <cstdint>
class IRomReader
{
public:
virtual ~IRomReader();
/**
* @brief This function reads a byte, returns it and advances the internal pointer by 1 byte
*
* @return uint8_t
*/
virtual bool readByte(uint8_t& outByte) = 0;
/**
* @brief This function reads multiple bytes into the specified output buffer
*
* @return whether the operation was successful
*/
virtual bool read(uint8_t* outBuffer, uint32_t bytesToRead) = 0;
/**
* @brief This function keeps reading until either maxRead number of bytes have been read or the terminator has been encountered.
*/
virtual uint32_t readUntil(uint8_t* outBuffer, uint8_t terminator, uint32_t maxRead) = 0;
/**
* @brief Same as above but with multiple possible terminator characters
*/
virtual uint32_t readUntil(uint8_t* outBuffer, const uint8_t* terminatorList, uint8_t numTerminators, uint32_t maxRead) = 0;
/**
* @brief Reads a 16 bit unsigned integer from the rom at the current position.
* The Endianness enum indicates whether the field we're trying to read is actually stored in little endian or big endian form.
* Turns out there's a mix of them in the roms even though most fields are in little endian.
*
* We need to use this value to determine whether we should do a byteswap for the CPU we're running this program on.
*/
virtual bool readUint16(uint16_t& outByte, Endianness fieldEndianness = Endianness::LITTLE) = 0;
/**
* @brief See @readUint16. This is the same but then for uint32_t
*/
virtual bool readUint32(uint32_t& outByte, Endianness fieldEndianness = Endianness::LITTLE) = 0;
/**
* @brief This function reads the current byte without advancing the internal pointer by 1 byte
*
* @return uint8_t
*/
virtual uint8_t peek() = 0;
/**
* @brief This function advances the internal pointer by the specified numBytes.
* This can be considered a relative seek
*
*/
virtual bool advance(uint32_t numBytes = 1) = 0;
/**
* @brief This function seeks to the specified absolute rom offset
*
* @param absoluteOffset
*/
virtual bool seek(uint32_t absoluteOffset) = 0;
/**
* @brief Returns the index of the current bank
*/
virtual uint8_t getCurrentBankIndex() const = 0;
/**
* @brief This function seeks to the start of the specified bank index
*
* @param bankIndex
* @return true
* @return false
*/
virtual bool seekToBank(uint8_t bankIndex) = 0;
/**
* @brief This function seeks to the specified rom pointer. Note this should be an unprocessed pointer as stored in the rom (except for endianness conversions done by readUint16)
* The reason is that the pointers stored in the rom assume the memory address of the rom location AFTER it has been mounted/mapped in the gameboy memory.
* Therefore this function will subtract 0x4000 from it (because 0x0000 - 0x4000 in gameboy address space is ALWAYS occupied by bank 0)
*
* If bankIndex is not specified (= set to 0xFF), we will seek to the pointer in the current bank
*/
virtual bool seekToRomPointer(uint16_t pointer, uint8_t bankIndex = 0xFF) = 0;
/**
* @brief This function searches for a sequence of bytes (the needle) in the buffer starting from
* the current internal position
* @return true we found the sequence of bytes and we've seeked toward this point
* @return false we didn't find the sequence of bytes anywhere
*/
virtual bool searchFor(const uint8_t* needle, uint32_t needleLength) = 0;
protected:
private:
};
class BaseRomReader : public IRomReader
{
public:
BaseRomReader();
virtual ~BaseRomReader();
bool readUint16(uint16_t& outByte, Endianness fieldEndianness = Endianness::LITTLE) override;
bool readUint32(uint32_t& outByte, Endianness fieldEndianness = Endianness::LITTLE) override;
bool seekToBank(uint8_t bankIndex) override;
bool seekToRomPointer(uint16_t pointer, uint8_t bankIndex = 0xFF) override;
uint32_t readUntil(uint8_t* outBuffer, uint8_t terminator, uint32_t maxRead) override;
uint32_t readUntil(uint8_t* outBuffer, const uint8_t* terminatorList, uint8_t numTerminators, uint32_t maxRead) override;
protected:
private:
const bool cpuLittleEndian_;
};
class BufferBasedRomReader : public BaseRomReader
{
public:
BufferBasedRomReader(const uint8_t* buffer, uint32_t bufferSize);
virtual ~BufferBasedRomReader();
/**
* @brief This function reads a byte, returns it and advances the internal pointer by 1 byte
*
* @return uint8_t
*/
bool readByte(uint8_t& outByte) override;
/**
* @brief This function reads multiple bytes into the specified output buffer
*
* @return whether the operation was successful
*/
bool read(uint8_t* outBuffer, uint32_t bytesToRead) override;
/**
* @brief This function reads the current byte without advancing the internal pointer by 1 byte
*
* @return uint8_t
*/
uint8_t peek() override;
/**
* @brief This function advances the internal pointer by 1 byte
*
*/
bool advance(uint32_t numBytes = 1) override;
/**
* @brief This function seeks to the specified absolute rom offset
*
* @param absoluteOffset
*/
bool seek(uint32_t absoluteOffset) override;
/**
* @brief This function searches for a sequence of bytes (the needle) in the buffer starting from
* the current internal position
* @return true we found the sequence of bytes and we've seeked toward this point
* @return false we didn't find the sequence of bytes anywhere
*/
bool searchFor(const uint8_t* needle, uint32_t needleLength) override;
uint8_t getCurrentBankIndex() const override;
protected:
private:
const uint8_t* const buffer_;
const uint8_t* const end_;
const uint8_t* cur_;
};
class BitReader
{
public:
BitReader(IRomReader& romReader);
/**
* @brief Read the specified number of bits
*
* @param numBits
* @return uint8_t
*/
uint8_t read(uint8_t numBits);
bool seek(uint32_t byteOffset, uint8_t bitOffset = 0);
protected:
private:
IRomReader& romReader_;
uint8_t bitIndex_;
};
bool readGameboyCartridgeHeader(IRomReader& romReader, GameboyCartridgeHeader& cartridgeHeader);
#endif