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()) {
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();
180 std::ifstream file(
filename_, std::ios::binary);
181 if (!file.is_open()) {
182 return absl::NotFoundError(
183 absl::StrCat(
"Could not open ROM file: ",
filename_));
189 }
catch (
const std::filesystem::filesystem_error &e) {
191 file.seekg(0, std::ios::end);
193 return absl::InternalError(absl::StrCat(
194 "Could not get file size: ",
filename_,
" - ", e.what()));
196 size_ = file.tellg();
212 std::string resource_label_filename = absl::StrFormat(
"%s.labels",
filename);
214 return absl::OkStatus();
219 return absl::InvalidArgumentError(
220 "Could not load ROM: parameter `data` is empty.");
231 return absl::OkStatus();
235 if (offset >=
static_cast<int>(
rom_data_.size())) {
236 return absl::FailedPreconditionError(
"Offset out of range");
242 if (offset + 1 >=
static_cast<int>(
rom_data_.size())) {
243 return absl::FailedPreconditionError(
"Offset out of range");
250 if (offset + 2 >=
static_cast<int>(
rom_data_.size())) {
251 return absl::OutOfRangeError(
"Offset out of range");
259 uint32_t offset, uint32_t length)
const {
260 if (offset + length >
static_cast<uint32_t
>(
rom_data_.size())) {
261 return absl::OutOfRangeError(
"Offset and length out of range");
263 std::vector<uint8_t> result;
264 for (uint32_t i = offset; i < offset + length; i++) {
298 return absl::OkStatus();
302 if (addr >=
static_cast<int>(
rom_data_.size())) {
303 return absl::OutOfRangeError(absl::StrFormat(
304 "Attempt to write byte %#02x value failed, address %d out of range",
309 return absl::OkStatus();
313 if (addr + 1 >=
static_cast<int>(
rom_data_.size())) {
314 return absl::OutOfRangeError(absl::StrFormat(
315 "Attempt to write word %#04x value failed, address %d out of range",
318 rom_data_[addr] = (uint8_t)(value & 0xFF);
319 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
321 return absl::OkStatus();
325 if (addr + 1 >=
static_cast<int>(
rom_data_.size())) {
326 return absl::OutOfRangeError(absl::StrFormat(
327 "Attempt to write short %#04x value failed, address %d out of range",
330 rom_data_[addr] = (uint8_t)(value & 0xFF);
331 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
333 return absl::OkStatus();
337 if (addr + 2 >=
static_cast<uint32_t
>(
rom_data_.size())) {
338 return absl::OutOfRangeError(absl::StrFormat(
339 "Attempt to write long %#06x value failed, address %d out of range",
342 rom_data_[addr] = (uint8_t)(value & 0xFF);
343 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
344 rom_data_[addr + 2] = (uint8_t)((value >> 16) & 0xFF);
346 return absl::OkStatus();
350 if (addr +
static_cast<int>(
data.size()) >
352 return absl::InvalidArgumentError(absl::StrFormat(
353 "Attempt to write vector value failed, address %d out of range", addr));
355 for (
int i = 0; i < static_cast<int>(
data.size()); i++) {
359 return absl::OkStatus();
363 uint16_t bgr = ((color.
snes() >> 10) & 0x1F) | ((color.
snes() & 0x1F) << 10) |
364 (color.
snes() & 0x7C00);
373 constexpr size_t kBaseRomSize = 1048576;
374 constexpr size_t kHeaderSize = 0x200;
375 if (
size_ % kBaseRomSize == kHeaderSize) {
376 auto header = std::vector<uint8_t>(
rom_data_.begin(),
383 constexpr uint32_t kTitleStringOffset = 0x7FC0;
384 constexpr uint32_t kTitleStringLength = 20;
385 title_.resize(kTitleStringLength);
386 std::copy(
rom_data_.begin() + kTitleStringOffset,
387 rom_data_.begin() + kTitleStringOffset + kTitleStringLength,
389 if (
rom_data_[kTitleStringOffset + 0x19] == 0) {
402 size_ = kBaseRomSize * 2;
404 return absl::OkStatus();
408 absl::Status non_firing_status;
410 return absl::InternalError(
"ROM data is empty.");
421 auto now = std::chrono::system_clock::now();
422 auto now_c = std::chrono::system_clock::to_time_t(now);
423 std::string backup_filename =
424 absl::StrCat(
filename,
"_backup_", std::ctime(&now_c));
427 backup_filename.erase(
428 std::remove(backup_filename.begin(), backup_filename.end(),
'\n'),
429 backup_filename.end());
432 std::replace(backup_filename.begin(), backup_filename.end(),
' ',
'_');
436 std::filesystem::copy(
filename_, backup_filename,
437 std::filesystem::copy_options::overwrite_existing);
438 }
catch (
const std::filesystem::filesystem_error &e) {
439 non_firing_status = absl::InternalError(absl::StrCat(
440 "Could not create backup file: ", backup_filename,
" - ", e.what()));
453 auto now = std::chrono::system_clock::now();
454 auto now_c = std::chrono::system_clock::to_time_t(now);
456 std::cout << filename_no_ext << std::endl;
457 filename = absl::StrCat(filename_no_ext,
"_", std::ctime(&now_c));
470 std::ofstream file(
filename.data(), std::ios::binary | std::ios::app);
473 file.open(
filename.data(), std::ios::binary);
475 return absl::InternalError(
476 absl::StrCat(
"Could not open or create ROM file: ",
filename));
483 static_cast<const char *
>(
static_cast<const void *
>(
rom_data_.data())),
485 }
catch (
const std::ofstream::failure &e) {
486 return absl::InternalError(absl::StrCat(
487 "Error while writing to ROM file: ",
filename,
" - ", e.what()));
492 return absl::InternalError(
493 absl::StrCat(
"Error while writing to ROM file: ",
filename));
496 if (!non_firing_status.ok()) {
497 return non_firing_status;
500 return absl::OkStatus();
505 for (
size_t j = 0; j < palette.
size(); ++j) {
514 return absl::OkStatus();
520 for (size_t i = 0; i < group.size(); ++i) {
522 SavePalette(i, group.name(), *group.mutable_palette(i)));
524 return absl::OkStatus();
527 return absl::OkStatus();
532 main_blockset_ptr =
SnesToPc(main_blockset_ptr);
535 for (
int j = 0; j < 8; j++) {
541 for (
int j = 0; j < 4; j++) {
547 for (
int j = 0; j < 4; j++) {
554 for (
int j = 0; j < 4; j++) {
560 return absl::OkStatus();
565 main_blockset_ptr =
SnesToPc(main_blockset_ptr);
568 for (
int j = 0; j < 8; j++) {
574 for (
int j = 0; j < 4; j++) {
580 for (
int j = 0; j < 4; j++) {
587 for (
int j = 0; j < 4; j++) {
593 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