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;
78 Rom &rom,
bool defer_render) {
79 std::array<gfx::Bitmap, kNumGfxSheets> graphics_sheets;
80 std::vector<uint8_t> sheet;
84 if (i >= 115 && i <= 126) {
93 }
else if (i == 113 || i == 114 || i >= 218) {
108 if (graphics_sheets[i].is_active()) {
111 graphics_sheets[i].SetPaletteWithTransparent(
114 graphics_sheets[i].SetPaletteWithTransparent(
123 for (
int j = 0; j < graphics_sheets[i].size(); ++j) {
128 for (
int j = 0; j < graphics_sheets[0].size(); ++j) {
133 return graphics_sheets;
137 Rom &rom, std::array<gfx::Bitmap, kNumGfxSheets> &gfx_sheets) {
139 if (gfx_sheets[i].is_active()) {
141 std::vector<uint8_t> final_data;
142 bool compressed =
true;
143 if (i >= 115 && i <= 126) {
145 }
else if (i == 113 || i == 114 || i >= 218) {
150 std::cout <<
"Sheet ID " << i <<
" BPP: " << to_bpp << std::endl;
151 auto sheet_data = gfx_sheets[i].vector();
152 std::cout <<
"Sheet data size: " << sheet_data.size() << std::endl;
157 final_data.data(), final_data.size(), &size, 1);
158 for (
int j = 0; j < size; j++) {
159 sheet_data[j] = compressed_data[j];
166 std::copy(final_data.begin(), final_data.end(), rom.
begin() + offset);
169 return absl::OkStatus();
174 return absl::InvalidArgumentError(
175 "Could not load ROM: parameter `filename` is empty.");
177 std::string full_filename = std::filesystem::absolute(
filename).string();
182 std::ifstream file(
filename_, std::ios::binary);
183 if (!file.is_open()) {
184 return absl::NotFoundError(
185 absl::StrCat(
"Could not open ROM file: ",
filename_));
191 }
catch (
const std::filesystem::filesystem_error &e) {
193 file.seekg(0, std::ios::end);
195 return absl::InternalError(absl::StrCat(
196 "Could not get file size: ",
filename_,
" - ", e.what()));
198 size_ = file.tellg();
214 std::string resource_label_filename = absl::StrFormat(
"%s.labels",
filename);
216 return absl::OkStatus();
221 return absl::InvalidArgumentError(
222 "Could not load ROM: parameter `data` is empty.");
233 return absl::OkStatus();
237 if (offset >=
static_cast<int>(
rom_data_.size())) {
238 return absl::FailedPreconditionError(
"Offset out of range");
244 if (offset + 1 >=
static_cast<int>(
rom_data_.size())) {
245 return absl::FailedPreconditionError(
"Offset out of range");
252 if (offset + 2 >=
static_cast<int>(
rom_data_.size())) {
253 return absl::OutOfRangeError(
"Offset out of range");
261 uint32_t offset, uint32_t length)
const {
262 if (offset + length >
static_cast<uint32_t
>(
rom_data_.size())) {
263 return absl::OutOfRangeError(
"Offset and length out of range");
265 std::vector<uint8_t> result;
266 for (uint32_t i = offset; i < offset + length; i++) {
300 return absl::OkStatus();
304 if (addr >=
static_cast<int>(
rom_data_.size())) {
305 return absl::OutOfRangeError(absl::StrFormat(
306 "Attempt to write byte %#02x value failed, address %d out of range",
311 return absl::OkStatus();
315 if (addr + 1 >=
static_cast<int>(
rom_data_.size())) {
316 return absl::OutOfRangeError(absl::StrFormat(
317 "Attempt to write word %#04x value failed, address %d out of range",
320 rom_data_[addr] = (uint8_t)(value & 0xFF);
321 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
323 return absl::OkStatus();
327 if (addr + 1 >=
static_cast<int>(
rom_data_.size())) {
328 return absl::OutOfRangeError(absl::StrFormat(
329 "Attempt to write short %#04x value failed, address %d out of range",
332 rom_data_[addr] = (uint8_t)(value & 0xFF);
333 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
335 return absl::OkStatus();
339 if (addr + 2 >=
static_cast<uint32_t
>(
rom_data_.size())) {
340 return absl::OutOfRangeError(absl::StrFormat(
341 "Attempt to write long %#06x value failed, address %d out of range",
344 rom_data_[addr] = (uint8_t)(value & 0xFF);
345 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
346 rom_data_[addr + 2] = (uint8_t)((value >> 16) & 0xFF);
348 return absl::OkStatus();
352 if (addr +
static_cast<int>(
data.size()) >
354 return absl::InvalidArgumentError(absl::StrFormat(
355 "Attempt to write vector value failed, address %d out of range", addr));
357 for (
int i = 0; i < static_cast<int>(
data.size()); i++) {
361 return absl::OkStatus();
365 uint16_t bgr = ((color.
snes() >> 10) & 0x1F) | ((color.
snes() & 0x1F) << 10) |
366 (color.
snes() & 0x7C00);
375 constexpr size_t kBaseRomSize = 1048576;
376 constexpr size_t kHeaderSize = 0x200;
377 if (
size_ % kBaseRomSize == kHeaderSize) {
378 auto header = std::vector<uint8_t>(
rom_data_.begin(),
385 constexpr uint32_t kTitleStringOffset = 0x7FC0;
386 constexpr uint32_t kTitleStringLength = 20;
387 title_.resize(kTitleStringLength);
388 std::copy(
rom_data_.begin() + kTitleStringOffset,
389 rom_data_.begin() + kTitleStringOffset + kTitleStringLength,
391 if (
rom_data_[kTitleStringOffset + 0x19] == 0) {
404 size_ = kBaseRomSize * 2;
406 return absl::OkStatus();
410 absl::Status non_firing_status;
412 return absl::InternalError(
"ROM data is empty.");
423 auto now = std::chrono::system_clock::now();
424 auto now_c = std::chrono::system_clock::to_time_t(now);
425 std::string backup_filename =
426 absl::StrCat(
filename,
"_backup_", std::ctime(&now_c));
429 backup_filename.erase(
430 std::remove(backup_filename.begin(), backup_filename.end(),
'\n'),
431 backup_filename.end());
434 std::replace(backup_filename.begin(), backup_filename.end(),
' ',
'_');
438 std::filesystem::copy(
filename_, backup_filename,
439 std::filesystem::copy_options::overwrite_existing);
440 }
catch (
const std::filesystem::filesystem_error &e) {
441 non_firing_status = absl::InternalError(absl::StrCat(
442 "Could not create backup file: ", backup_filename,
" - ", e.what()));
455 auto now = std::chrono::system_clock::now();
456 auto now_c = std::chrono::system_clock::to_time_t(now);
458 std::cout << filename_no_ext << std::endl;
459 filename = absl::StrCat(filename_no_ext,
"_", std::ctime(&now_c));
472 std::ofstream file(
filename.data(), std::ios::binary | std::ios::app);
475 file.open(
filename.data(), std::ios::binary);
477 return absl::InternalError(
478 absl::StrCat(
"Could not open or create ROM file: ",
filename));
485 static_cast<const char *
>(
static_cast<const void *
>(
rom_data_.data())),
487 }
catch (
const std::ofstream::failure &e) {
488 return absl::InternalError(absl::StrCat(
489 "Error while writing to ROM file: ",
filename,
" - ", e.what()));
494 return absl::InternalError(
495 absl::StrCat(
"Error while writing to ROM file: ",
filename));
498 if (!non_firing_status.ok()) {
499 return non_firing_status;
502 return absl::OkStatus();
507 for (
size_t j = 0; j < palette.
size(); ++j) {
516 return absl::OkStatus();
522 for (size_t i = 0; i < group.size(); ++i) {
524 SavePalette(i, group.name(), *group.mutable_palette(i)));
526 return absl::OkStatus();
529 return absl::OkStatus();
534 main_blockset_ptr =
SnesToPc(main_blockset_ptr);
537 for (
int j = 0; j < 8; j++) {
543 for (
int j = 0; j < 4; j++) {
549 for (
int j = 0; j < 4; j++) {
556 for (
int j = 0; j < 4; j++) {
562 return absl::OkStatus();
567 main_blockset_ptr =
SnesToPc(main_blockset_ptr);
570 for (
int j = 0; j < 8; j++) {
576 for (
int j = 0; j < 4; j++) {
582 for (
int j = 0; j < 4; j++) {
589 for (
int j = 0; j < 4; j++) {
595 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.
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 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