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"
34 uint32_t ptr2, uint32_t ptr3) {
40 std::vector<uint8_t> sheet;
41 const uint8_t sheets[] = {113, 114, 218, 219, 220, 221};
43 for (
const auto &sheet_id : sheets) {
51 for (
const auto &each_pixel : converted_sheet) {
52 sheet.push_back(each_pixel);
60 const uint32_t kLinkGfxOffset = 0x80000;
61 const uint16_t kLinkGfxLength = 0x800;
62 std::array<gfx::Bitmap, kNumLinkSheets> link_graphics;
72 link_graphics[i].ApplyPalette(rom.
palette_group().armors[0]);)
79 Rom &rom,
bool defer_render) {
80 std::array<gfx::Bitmap, kNumGfxSheets> graphics_sheets;
81 std::vector<uint8_t> sheet;
85 if (i >= 115 && i <= 126) {
94 }
else if (i == 113 || i == 114 || i >= 218) {
109 if (graphics_sheets[i].is_active()) {
124 for (
int j = 0; j < graphics_sheets[i].size(); ++j) {
129 for (
int j = 0; j < graphics_sheets[0].size(); ++j) {
134 return graphics_sheets;
138 Rom &rom, std::array<gfx::Bitmap, kNumGfxSheets> &gfx_sheets) {
140 if (gfx_sheets[i].is_active()) {
142 std::vector<uint8_t> final_data;
143 bool compressed =
true;
144 if (i >= 115 && i <= 126) {
146 }
else if (i == 113 || i == 114 || i >= 218) {
151 std::cout <<
"Sheet ID " << i <<
" BPP: " << to_bpp << std::endl;
152 auto sheet_data = gfx_sheets[i].vector();
153 std::cout <<
"Sheet data size: " << sheet_data.size() << std::endl;
158 final_data.data(), final_data.size(), &size, 1);
159 for (
int j = 0; j < size; j++) {
160 sheet_data[j] = compressed_data[j];
167 std::copy(final_data.begin(), final_data.end(), rom.
begin() + offset);
170 return absl::OkStatus();
175 return absl::InvalidArgumentError(
176 "Could not load ROM: parameter `filename` is empty.");
178 std::string full_filename = std::filesystem::absolute(
filename).string();
181 std::ifstream file(
filename_, std::ios::binary);
182 if (!file.is_open()) {
183 return absl::NotFoundError(
184 absl::StrCat(
"Could not open ROM file: ",
filename_));
190 }
catch (
const std::filesystem::filesystem_error &e) {
192 file.seekg(0, std::ios::end);
194 return absl::InternalError(absl::StrCat(
195 "Could not get file size: ",
filename_,
" - ", e.what()));
197 size_ = file.tellg();
213 std::string resource_label_filename = absl::StrFormat(
"%s.labels",
filename);
215 return absl::OkStatus();
220 return absl::InvalidArgumentError(
221 "Could not load ROM: parameter `data` is empty.");
232 return absl::OkStatus();
236 if (offset >=
static_cast<int>(
rom_data_.size())) {
237 return absl::FailedPreconditionError(
"Offset out of range");
243 if (offset + 1 >=
static_cast<int>(
rom_data_.size())) {
244 return absl::FailedPreconditionError(
"Offset out of range");
251 if (offset + 2 >=
static_cast<int>(
rom_data_.size())) {
252 return absl::OutOfRangeError(
"Offset out of range");
260 uint32_t offset, uint32_t length)
const {
261 if (offset + length >
static_cast<uint32_t
>(
rom_data_.size())) {
262 return absl::OutOfRangeError(
"Offset and length out of range");
264 std::vector<uint8_t> result;
265 for (uint32_t i = offset; i < offset + length; i++) {
299 return absl::OkStatus();
303 if (addr >=
static_cast<int>(
rom_data_.size())) {
304 return absl::OutOfRangeError(absl::StrFormat(
305 "Attempt to write byte %#02x value failed, address %d out of range",
310 return absl::OkStatus();
314 if (addr + 1 >=
static_cast<int>(
rom_data_.size())) {
315 return absl::OutOfRangeError(absl::StrFormat(
316 "Attempt to write word %#04x value failed, address %d out of range",
319 rom_data_[addr] = (uint8_t)(value & 0xFF);
320 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
322 return absl::OkStatus();
326 if (addr + 1 >=
static_cast<int>(
rom_data_.size())) {
327 return absl::OutOfRangeError(absl::StrFormat(
328 "Attempt to write short %#04x value failed, address %d out of range",
331 rom_data_[addr] = (uint8_t)(value & 0xFF);
332 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
334 return absl::OkStatus();
338 if (addr + 2 >=
static_cast<uint32_t
>(
rom_data_.size())) {
339 return absl::OutOfRangeError(absl::StrFormat(
340 "Attempt to write long %#06x value failed, address %d out of range",
343 rom_data_[addr] = (uint8_t)(value & 0xFF);
344 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
345 rom_data_[addr + 2] = (uint8_t)((value >> 16) & 0xFF);
347 return absl::OkStatus();
351 if (addr +
static_cast<int>(
data.size()) >
353 return absl::InvalidArgumentError(absl::StrFormat(
354 "Attempt to write vector value failed, address %d out of range", addr));
356 for (
int i = 0; i < static_cast<int>(
data.size()); i++) {
360 return absl::OkStatus();
364 uint16_t bgr = ((color.
snes() >> 10) & 0x1F) | ((color.
snes() & 0x1F) << 10) |
365 (color.
snes() & 0x7C00);
374 constexpr size_t kBaseRomSize = 1048576;
375 constexpr size_t kHeaderSize = 0x200;
376 if (
size_ % kBaseRomSize == kHeaderSize) {
377 auto header = std::vector<uint8_t>(
rom_data_.begin(),
384 constexpr uint32_t kTitleStringOffset = 0x7FC0;
385 constexpr uint32_t kTitleStringLength = 20;
386 title_.resize(kTitleStringLength);
387 std::copy(
rom_data_.begin() + kTitleStringOffset,
388 rom_data_.begin() + kTitleStringOffset + kTitleStringLength,
390 if (
rom_data_[kTitleStringOffset + 0x19] == 0) {
403 size_ = kBaseRomSize * 2;
405 return absl::OkStatus();
409 absl::Status non_firing_status;
411 return absl::InternalError(
"ROM data is empty.");
422 auto now = std::chrono::system_clock::now();
423 auto now_c = std::chrono::system_clock::to_time_t(now);
424 std::string backup_filename =
425 absl::StrCat(
filename,
"_backup_", std::ctime(&now_c));
428 backup_filename.erase(
429 std::remove(backup_filename.begin(), backup_filename.end(),
'\n'),
430 backup_filename.end());
433 std::replace(backup_filename.begin(), backup_filename.end(),
' ',
'_');
437 std::filesystem::copy(
filename_, backup_filename,
438 std::filesystem::copy_options::overwrite_existing);
439 }
catch (
const std::filesystem::filesystem_error &e) {
440 non_firing_status = absl::InternalError(absl::StrCat(
441 "Could not create backup file: ", backup_filename,
" - ", e.what()));
454 auto now = std::chrono::system_clock::now();
455 auto now_c = std::chrono::system_clock::to_time_t(now);
457 std::cout << filename_no_ext << std::endl;
458 filename = absl::StrCat(filename_no_ext,
"_", std::ctime(&now_c));
471 std::ofstream file(
filename.data(), std::ios::binary | std::ios::app);
474 file.open(
filename.data(), std::ios::binary);
476 return absl::InternalError(
477 absl::StrCat(
"Could not open or create ROM file: ",
filename));
484 static_cast<const char *
>(
static_cast<const void *
>(
rom_data_.data())),
486 }
catch (
const std::ofstream::failure &e) {
487 return absl::InternalError(absl::StrCat(
488 "Error while writing to ROM file: ",
filename,
" - ", e.what()));
493 return absl::InternalError(
494 absl::StrCat(
"Error while writing to ROM file: ",
filename));
497 if (!non_firing_status.ok()) {
498 return non_firing_status;
501 return absl::OkStatus();
506 for (
size_t j = 0; j < palette.
size(); ++j) {
515 return absl::OkStatus();
521 for (size_t i = 0; i < group.size(); ++i) {
523 SavePalette(i, group.name(), *group.mutable_palette(i)));
525 return absl::OkStatus();
528 return absl::OkStatus();
533 main_blockset_ptr =
SnesToPc(main_blockset_ptr);
536 for (
int j = 0; j < 8; j++) {
542 for (
int j = 0; j < 4; j++) {
548 for (
int j = 0; j < 4; j++) {
555 for (
int j = 0; j < 4; j++) {
561 return absl::OkStatus();
566 main_blockset_ptr =
SnesToPc(main_blockset_ptr);
569 for (
int j = 0; j < 8; j++) {
575 for (
int j = 0; j < 4; j++) {
581 for (
int j = 0; j < 4; j++) {
588 for (
int j = 0; j < 4; j++) {
594 return absl::OkStatus();
static Renderer & GetInstance()
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()
absl::Status SaveGroupsToRom()
std::vector< uint8_t > rom_data_
gfx::PaletteGroupMap palette_groups_
auto palette_group() const
absl::StatusOr< gfx::Tile16 > ReadTile16(uint32_t tile16_id)
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 WriteByte(int addr, uint8_t value)
absl::Status SaveToFile(bool backup, bool save_new=false, std::string filename="")
Saves the Rom data to a file.
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 WriteVector(int addr, std::vector< uint8_t > data)
absl::Status WriteTile16(int tile16_id, const gfx::Tile16 &tile)
std::array< std::array< uint8_t, 4 >, kNumRoomBlocksets > room_blockset_ids
absl::StatusOr< uint32_t > ReadLong(int offset)
zelda3_version_pointers version_constants() const
ResourceLabelManager resource_label_manager_
absl::Status LoadGfxGroups()
absl::Status SavePalette(int index, const std::string &group_name, gfx::SnesPalette &palette)
absl::StatusOr< uint8_t > ReadByte(int offset)
absl::Status WriteShort(int addr, uint16_t value)
absl::Status WriteWord(int addr, uint16_t value)
absl::Status WriteLong(uint32_t addr, uint32_t value)
std::array< std::array< uint8_t, 8 >, kNumMainBlocksets > main_blockset_ids
absl::Status SaveAllPalettes()
Saves all palettes in the Rom.
static std::shared_ptr< Rom > shared_rom_
The Renderer class represents the renderer for the Yaze application.
void RenderBitmap(gfx::Bitmap *bitmap)
Used to render a bitmap to the screen.
void set_modified(bool m)
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
Tile composition of four 8x8 tiles.
#define RETURN_IF_ERROR(expression)
#define ASSIGN_OR_RETURN(type_variable_name, expression)
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
uint16_t TileInfoToWord(TileInfo tile_info)
constexpr int kTilesheetDepth
std::vector< uint8_t > Bpp8SnesToIndexed(std::vector< uint8_t > data, uint64_t bpp)
uint32_t GetPaletteAddress(const std::string &group_name, size_t palette_index, size_t color_index)
TileInfo WordToTileInfo(uint16_t word)
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.
std::vector< uint8_t > SnesTo8bppSheet(const std::vector< uint8_t > &sheet, int bpp, int num_sheets)
std::string HexWord(uint16_t word, HexStringParams params)
std::string HexByte(uint8_t byte, HexStringParams params)
std::string HexLong(uint32_t dword, HexStringParams params)
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
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
Represents a group of palettes.
uint32_t kOverworldGfxPtr1
uint32_t kOverworldGfxPtr2
uint32_t kOverworldGfxPtr3