15#include "absl/status/status.h"
16#include "absl/status/statusor.h"
17#include "absl/strings/str_cat.h"
18#include "absl/strings/string_view.h"
40 rom_data.erase(rom_data.begin(), rom_data.begin() +
kHeaderSize);
71 uint32_t ptr2, uint32_t ptr3) {
77 std::vector<uint8_t> sheet;
78 const uint8_t sheets[] = {0x71, 0x72, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE};
79 for (
const auto &sheet_id : sheets) {
87 for (
const auto &each_pixel : converted_sheet) {
88 sheet.push_back(each_pixel);
96 const uint32_t kLinkGfxOffset = 0x80000;
97 const uint16_t kLinkGfxLength = 0x800;
98 std::array<gfx::Bitmap, kNumLinkSheets> link_graphics;
101 auto link_sheet_data,
111 return link_graphics;
115 std::vector<uint8_t> data(0x2000);
116 for (
int i = 0; i < 0x2000; i++) {
117 data[i] = rom.
data()[0x70000 + i];
120 std::vector<uint8_t> new_data(0x4000);
121 std::vector<uint8_t> mask = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
122 int sheet_position = 0;
125 for (
int s = 0; s < 4; s++) {
126 for (
int j = 0; j < 4; j++) {
127 for (
int i = 0; i < 16; i++) {
128 for (
int y = 0; y < 8; y++) {
130 data[(y * 2) + (i * 16) + (j * 256) + sheet_position];
132 data[(y * 2) + (i * 16) + (j * 256) + 1 + sheet_position];
134 for (
int x = 0; x < 4; x++) {
136 uint8_t pixdata2 = 0;
138 if ((line_bits0 & mask[(x * 2)]) == mask[(x * 2)]) {
141 if ((line_bits1 & mask[(x * 2)]) == mask[(x * 2)]) {
145 if ((line_bits0 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
148 if ((line_bits1 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
152 new_data[(y * 64) + (x) + (i * 4) + (j * 512) + (s * 2048)] =
153 (uint8_t)((pixdata << 4) | pixdata2);
159 sheet_position += 0x400;
162 std::vector<uint8_t> fontgfx16_data(0x4000);
163 for (
int i = 0; i < 0x4000; i++) {
164 fontgfx16_data[i] = new_data[i];
168 font_gfx.
Create(128, 128, 64, fontgfx16_data);
173 Rom &rom,
bool defer_render) {
174 std::array<gfx::Bitmap, kNumGfxSheets> graphics_sheets;
175 std::vector<uint8_t> sheet;
183 if (i >= 115 && i <= 126) {
192 }
else if (i == 113 || i == 114 || i >= 218) {
217 if (palette_group.size() > 0) {
218 default_palette = palette_group[0];
220 }
else if (i < 128) {
223 if (palette_group.size() > 0) {
224 default_palette = palette_group[0];
229 if (palette_group.size() > 0) {
230 default_palette = palette_group.palette(0);
235 if (!default_palette.
empty()) {
236 graphics_sheets[i].SetPalette(default_palette);
240 for (
int j = 0; j < graphics_sheets[i].size(); ++j) {
245 for (
int j = 0; j < graphics_sheets[0].size(); ++j) {
250 return graphics_sheets;
254 Rom &rom, std::array<gfx::Bitmap, kNumGfxSheets> &gfx_sheets) {
256 if (gfx_sheets[i].is_active()) {
258 std::vector<uint8_t> final_data;
259 bool compressed =
true;
260 if (i >= 115 && i <= 126) {
262 }
else if (i == 113 || i == 114 || i >= 218) {
267 std::cout <<
"Sheet ID " << i <<
" BPP: " << to_bpp << std::endl;
268 auto sheet_data = gfx_sheets[i].vector();
269 std::cout <<
"Sheet data size: " << sheet_data.size() << std::endl;
274 final_data.data(), final_data.size(), &size, 1);
275 for (
int j = 0; j < size; j++) {
276 sheet_data[j] = compressed_data[j];
283 std::copy(final_data.begin(), final_data.end(), rom.
begin() + offset);
286 return absl::OkStatus();
298 return absl::InvalidArgumentError(
299 "Could not load ROM: parameter `filename` is empty.");
303 if (!std::filesystem::exists(
filename)) {
304 return absl::NotFoundError(
305 absl::StrCat(
"ROM file does not exist: ",
filename));
311 std::ifstream file(
filename_, std::ios::binary);
312 if (!file.is_open()) {
313 return absl::NotFoundError(
314 absl::StrCat(
"Could not open ROM file: ",
filename_));
323 return absl::InvalidArgumentError(
324 absl::StrFormat(
"ROM file too small (%zu bytes), minimum is 32KB",
size_));
326 if (
size_ > 8 * 1024 * 1024) {
327 return absl::InvalidArgumentError(
328 absl::StrFormat(
"ROM file too large (%zu bytes), maximum is 8MB",
size_));
330 }
catch (
const std::filesystem::filesystem_error &
e) {
332 file.seekg(0, std::ios::end);
334 return absl::InternalError(absl::StrCat(
335 "Could not get file size: ",
filename_,
" - ",
e.what()));
337 size_ = file.tellg();
341 return absl::InvalidArgumentError(
342 absl::StrFormat(
"Invalid ROM size: %zu bytes",
size_));
349 file.seekg(0, std::ios::beg);
353 return absl::InternalError(
354 absl::StrFormat(
"Failed to read ROM data, read %zu of %zu bytes",
355 file.gcount(),
size_));
357 }
catch (
const std::bad_alloc&
e) {
358 return absl::ResourceExhaustedError(
359 absl::StrFormat(
"Failed to allocate memory for ROM (%zu bytes)",
size_));
375 absl::StrFormat(
"%s.labels",
filename));
378 return absl::OkStatus();
390 return absl::InvalidArgumentError(
391 "Could not load ROM: parameter `data` is empty.");
405 return absl::OkStatus();
414 return absl::FailedPreconditionError(
"ROM data is empty");
431 return absl::OutOfRangeError(
432 "ROM image is too small to contain title metadata.");
460 if (
rom_data_.size() < kBaseRomSize * 2) {
467 return absl::OkStatus();
475 for (
int j = 0;
j < 8;
j++) {
481 for (
int j = 0;
j < 4;
j++) {
487 for (
int j = 0;
j < 4;
j++) {
494 for (
int j = 0;
j < 4;
j++) {
500 return absl::OkStatus();
508 for (
int j = 0;
j < 8;
j++) {
514 for (
int j = 0;
j < 4;
j++) {
520 for (
int j = 0;
j < 4;
j++) {
527 for (
int j = 0;
j < 4;
j++) {
533 return absl::OkStatus();
539 return absl::InternalError(
"ROM data is empty.");
554 auto now = std::chrono::system_clock::now();
555 auto now_c = std::chrono::system_clock::to_time_t(
now);
570 std::filesystem::copy_options::overwrite_existing);
571 }
catch (
const std::filesystem::filesystem_error &
e) {
588 auto now = std::chrono::system_clock::now();
589 auto now_c = std::chrono::system_clock::to_time_t(
now);
605 std::ofstream file(
filename.data(), std::ios::binary | std::ios::trunc);
607 return absl::InternalError(
608 absl::StrCat(
"Could not open ROM file for writing: ",
filename));
614 static_cast<const char *
>(
static_cast<const void *
>(
rom_data_.data())),
616 }
catch (
const std::ofstream::failure &
e) {
617 return absl::InternalError(absl::StrCat(
618 "Error while writing to ROM file: ",
filename,
" - ",
e.what()));
623 return absl::InternalError(
624 absl::StrCat(
"Error while writing to ROM file: ",
filename));
633 for (
size_t j = 0;
j < palette.
size(); ++
j) {
642 return absl::OkStatus();
648 for (size_t i = 0; i < group.size(); ++i) {
650 SavePalette(i, group.name(), *group.mutable_palette(i)));
652 return absl::OkStatus();
655 return absl::OkStatus();
658absl::StatusOr<uint8_t> Rom::ReadByte(
int offset) {
659 if (offset >=
static_cast<int>(rom_data_.size())) {
660 return absl::FailedPreconditionError(
"Offset out of range");
662 return rom_data_[offset];
665absl::StatusOr<uint16_t> Rom::ReadWord(
int offset) {
666 if (offset + 1 >=
static_cast<int>(rom_data_.size())) {
667 return absl::FailedPreconditionError(
"Offset out of range");
669 auto result = (uint16_t)(rom_data_[offset] | (rom_data_[offset + 1] << 8));
673absl::StatusOr<uint32_t> Rom::ReadLong(
int offset) {
674 if (offset + 2 >=
static_cast<int>(rom_data_.size())) {
675 return absl::OutOfRangeError(
"Offset out of range");
677 auto result = (uint32_t)(rom_data_[offset] | (rom_data_[offset + 1] << 8) |
678 (rom_data_[offset + 2] << 16));
682absl::StatusOr<std::vector<uint8_t>> Rom::ReadByteVector(
683 uint32_t offset, uint32_t length)
const {
684 if (offset + length >
static_cast<uint32_t
>(rom_data_.size())) {
685 return absl::OutOfRangeError(
"Offset and length out of range");
687 std::vector<uint8_t> result;
688 for (uint32_t i = offset; i < offset + length; i++) {
689 result.push_back(rom_data_[i]);
694absl::StatusOr<gfx::Tile16> Rom::ReadTile16(uint32_t tile16_id) {
699 tile16.tile0_ = gfx::WordToTileInfo(new_tile0);
702 tile16.tile1_ = gfx::WordToTileInfo(new_tile1);
705 tile16.tile2_ = gfx::WordToTileInfo(new_tile2);
708 tile16.tile3_ = gfx::WordToTileInfo(new_tile3);
712absl::Status Rom::WriteTile16(
int tile16_id,
const gfx::Tile16 &tile) {
722 return absl::OkStatus();
725absl::Status Rom::WriteByte(
int addr, uint8_t value) {
726 if (addr >=
static_cast<int>(rom_data_.size())) {
727 return absl::OutOfRangeError(absl::StrFormat(
728 "Attempt to write byte %#02x value failed, address %d out of range",
731 rom_data_[addr] = value;
732 LOG_DEBUG(
"Rom",
"WriteByte: %#06X: %s", addr, util::HexByte(value).data());
734 return absl::OkStatus();
737absl::Status Rom::WriteWord(
int addr, uint16_t value) {
738 if (addr + 1 >=
static_cast<int>(rom_data_.size())) {
739 return absl::OutOfRangeError(absl::StrFormat(
740 "Attempt to write word %#04x value failed, address %d out of range",
743 rom_data_[addr] = (uint8_t)(value & 0xFF);
744 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
745 LOG_DEBUG(
"Rom",
"WriteWord: %#06X: %s", addr, util::HexWord(value).data());
747 return absl::OkStatus();
750absl::Status Rom::WriteShort(
int addr, uint16_t value) {
751 if (addr + 1 >=
static_cast<int>(rom_data_.size())) {
752 return absl::OutOfRangeError(absl::StrFormat(
753 "Attempt to write short %#04x value failed, address %d out of range",
756 rom_data_[addr] = (uint8_t)(value & 0xFF);
757 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
758 LOG_DEBUG(
"Rom",
"WriteShort: %#06X: %s", addr, util::HexWord(value).data());
760 return absl::OkStatus();
763absl::Status Rom::WriteLong(uint32_t addr, uint32_t value) {
764 if (addr + 2 >=
static_cast<uint32_t
>(rom_data_.size())) {
765 return absl::OutOfRangeError(absl::StrFormat(
766 "Attempt to write long %#06x value failed, address %d out of range",
769 rom_data_[addr] = (uint8_t)(value & 0xFF);
770 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
771 rom_data_[addr + 2] = (uint8_t)((value >> 16) & 0xFF);
772 LOG_DEBUG(
"Rom",
"WriteLong: %#06X: %s", addr, util::HexLong(value).data());
774 return absl::OkStatus();
777absl::Status Rom::WriteVector(
int addr, std::vector<uint8_t> data) {
778 if (addr +
static_cast<int>(data.size()) >
779 static_cast<int>(rom_data_.size())) {
780 return absl::InvalidArgumentError(absl::StrFormat(
781 "Attempt to write vector value failed, address %d out of range", addr));
783 for (
int i = 0; i < static_cast<int>(data.size()); i++) {
784 rom_data_[addr + i] = data[i];
786 LOG_DEBUG(
"Rom",
"WriteVector: %#06X: %s", addr,
787 util::HexByte(data[0]).data());
789 return absl::OkStatus();
793 uint16_t bgr = ((color.
snes() >> 10) & 0x1F) | ((color.
snes() & 0x1F) << 10) |
794 (color.
snes() & 0x7C00);
797 LOG_DEBUG(
"Rom",
"WriteColor: %#06X: %s", address, util::HexWord(bgr).data());
798 auto st = WriteShort(address, bgr);
The Rom class is used to load, save, and modify Rom data.
absl::StatusOr< std::vector< uint8_t > > ReadByteVector(uint32_t offset, uint32_t length) const
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
auto mutable_graphics_buffer()
std::vector< uint8_t > rom_data_
gfx::PaletteGroupMap palette_groups_
auto palette_group() const
absl::Status WriteColor(uint32_t address, const gfx::SnesColor &color)
absl::Status LoadFromData(const std::vector< uint8_t > &data, bool z3_load=true)
absl::Status LoadZelda3()
std::array< std::array< uint8_t, 4 >, kNumSpritesets > spriteset_ids
std::array< std::array< uint8_t, 4 >, kNumPalettesets > paletteset_ids
absl::StatusOr< uint16_t > ReadWord(int offset)
absl::Status SaveToFile(const SaveSettings &settings)
std::array< std::array< uint8_t, 4 >, kNumRoomBlocksets > room_blockset_ids
zelda3_version_pointers version_constants() const
absl::Status SaveGfxGroups()
absl::Status LoadGfxGroups()
absl::Status SavePalette(int index, const std::string &group_name, gfx::SnesPalette &palette)
core::ResourceLabelManager resource_label_manager_
std::array< std::array< uint8_t, 8 >, kNumMainBlocksets > main_blockset_ids
absl::Status SaveAllPalettes()
Represents a bitmap image optimized for SNES ROM hacking.
void Create(int width, int height, int depth, std::span< uint8_t > data)
Create a bitmap with the given dimensions and data.
constexpr void set_modified(bool m)
constexpr bool is_modified() const
constexpr uint16_t snes() const
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
Tile composition of four 8x8 tiles.
#define LOG_DEBUG(category, format,...)
#define RETURN_IF_ERROR(expression)
#define ASSIGN_OR_RETURN(type_variable_name, expression)
void MaybeStripSmcHeader(std::vector< uint8_t > &rom_data, unsigned long &size)
constexpr size_t kHeaderSize
constexpr size_t kBaseRomSize
absl::StatusOr< std::vector< uint8_t > > DecompressV2(const uint8_t *data, int offset, int size, int mode)
Decompresses a buffer of data using the LC_LZ2 algorithm.
constexpr int kTilesheetHeight
constexpr int kTilesheetWidth
constexpr int kTilesheetDepth
std::vector< uint8_t > Bpp8SnesToIndexed(std::vector< uint8_t > data, uint64_t bpp)
std::vector< uint8_t > SnesTo8bppSheet(std::span< uint8_t > sheet, int bpp, int num_sheets)
uint32_t GetPaletteAddress(const std::string &group_name, size_t palette_index, size_t color_index)
std::vector< uint8_t > HyruleMagicCompress(uint8_t const *const src, int const oldsize, int *const size, int const flag)
absl::Status LoadAllPalettes(const std::vector< uint8_t > &rom_data, PaletteGroupMap &groups)
Loads all the palettes for the game.
Main namespace for the application.
constexpr uint32_t kGfxGroupsPointer
absl::StatusOr< std::vector< uint8_t > > Load2BppGraphics(const Rom &rom)
Loads 2bpp graphics from Rom data.
absl::StatusOr< std::array< gfx::Bitmap, kNumLinkSheets > > LoadLinkGraphics(const Rom &rom)
Loads the players 4bpp graphics sheet from Rom data.
constexpr int Uncompressed3BPPSize
constexpr uint32_t kNumLinkSheets
constexpr uint32_t kEntranceGfxGroup
constexpr uint32_t kNumSpritesets
int AddressFromBytes(uint8_t bank, uint8_t high, uint8_t low) noexcept
constexpr uint32_t kTile16Ptr
constexpr uint32_t kNumMainBlocksets
constexpr uint32_t kNumRoomBlocksets
absl::StatusOr< gfx::Bitmap > LoadFontGraphics(const Rom &rom)
constexpr uint32_t kNumGfxSheets
absl::StatusOr< std::array< gfx::Bitmap, kNumGfxSheets > > LoadAllGraphicsData(Rom &rom, bool defer_render)
This function iterates over all graphics sheets in the Rom and loads them into memory....
uint32_t GetGraphicsAddress(const uint8_t *data, uint8_t addr, uint32_t ptr1, uint32_t ptr2, uint32_t ptr3)
constexpr uint32_t kNumPalettesets
absl::Status SaveAllGraphicsData(Rom &rom, std::array< gfx::Bitmap, kNumGfxSheets > &gfx_sheets)
uint32_t SnesToPc(uint32_t addr) noexcept
static RomLoadOptions AppDefaults()
bool expand_to_full_image
static RomLoadOptions CliDefaults()
static RomLoadOptions RawDataOnly()
bool load_resource_labels
bool LoadLabels(const std::string &filename)
absl::Status for_each(Func &&func)
Represents a group of palettes.
uint32_t kOverworldGfxPtr1
uint32_t kOverworldGfxPtr2
uint32_t kSpriteBlocksetPointer
uint32_t kOverworldGfxPtr3
uint32_t kDungeonPalettesGroups