Refactor ROM loading and saving methods for clarity and consistency; streamline filename handling, remove unused flags, and rename SaveGroupsToRom to SaveGfxGroups for better readability.

This commit is contained in:
scawful
2025-04-29 10:27:35 -04:00
parent d77aa2ab61
commit 114084ef95
2 changed files with 10 additions and 63 deletions

View File

@@ -174,9 +174,7 @@ absl::Status Rom::LoadFromFile(const std::string &filename, bool z3_load) {
return absl::InvalidArgumentError(
"Could not load ROM: parameter `filename` is empty.");
}
std::string full_filename = std::filesystem::absolute(filename).string();
filename_ = full_filename;
// Get the short name of the ROM
filename_ = std::filesystem::absolute(filename).string();
short_name_ = filename_.substr(filename_.find_last_of("/\\") + 1);
std::ifstream file(filename_, std::ios::binary);
@@ -203,16 +201,12 @@ absl::Status Rom::LoadFromFile(const std::string &filename, bool z3_load) {
file.read(reinterpret_cast<char *>(rom_data_.data()), size_);
file.close();
// Set is_loaded_ flag and return success
is_loaded_ = true;
if (z3_load) {
RETURN_IF_ERROR(LoadZelda3());
}
// Set up the resource labels
std::string resource_label_filename = absl::StrFormat("%s.labels", filename);
resource_label_manager_.LoadLabels(resource_label_filename);
resource_label_manager_.LoadLabels(absl::StrFormat("%s.labels", filename));
return absl::OkStatus();
}
@@ -221,12 +215,8 @@ absl::Status Rom::LoadFromData(const std::vector<uint8_t> &data, bool z3_load) {
return absl::InvalidArgumentError(
"Could not load ROM: parameter `data` is empty.");
}
if (!palette_groups_.empty()) palette_groups_.clear();
rom_data_ = data;
size_ = data.size();
is_loaded_ = true;
if (z3_load) {
RETURN_IF_ERROR(LoadZelda3());
}
@@ -447,7 +437,7 @@ absl::Status Rom::SaveToFile(bool backup, bool save_new, std::string filename) {
if (core::FeatureFlags::get().kSaveAllPalettes)
RETURN_IF_ERROR(SaveAllPalettes());
if (core::FeatureFlags::get().kSaveGfxGroups)
RETURN_IF_ERROR(SaveGroupsToRom());
RETURN_IF_ERROR(SaveGfxGroups());
if (save_new) {
// Create a file of the same name and append the date between the filename
@@ -562,7 +552,7 @@ absl::Status Rom::LoadGfxGroups() {
return absl::OkStatus();
}
absl::Status Rom::SaveGroupsToRom() {
absl::Status Rom::SaveGfxGroups() {
ASSIGN_OR_RETURN(auto main_blockset_ptr, ReadWord(kGfxGroupsPointer));
main_blockset_ptr = SnesToPc(main_blockset_ptr);

View File

@@ -16,7 +16,6 @@
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/match.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "app/core/project.h"
@@ -58,45 +57,17 @@ static const std::map<zelda3_version, zelda3_version_pointers>
*/
class Rom {
public:
/**
* Load Rom data from a file.
*
* @param filename The name of the file to load.
* @param z3_load Whether to load data specific to Zelda 3.
*
*/
absl::Status LoadFromFile(const std::string& filename, bool z3_load = true);
absl::Status LoadFromData(const std::vector<uint8_t>& data,
bool z3_load = true);
absl::Status LoadZelda3();
absl::Status LoadGfxGroups();
/**
* @brief Saves the Rom data to a file
*
* @param backup If true, creates a backup file with timestamp in its name
* @param filename The name of the file to save the Rom data to
*
* @return absl::Status Returns an OK status if the save was successful,
* otherwise returns an error status
*/
absl::Status SaveGfxGroups();
absl::Status SaveToFile(bool backup, bool save_new = false,
std::string filename = "");
/**
* Saves the given palette to the Rom if any of its colors have been modified.
*
* @param index The index of the palette to save.
* @param group_name The name of the group containing the palette.
* @param palette The palette to save.
*/
absl::Status SavePalette(int index, const std::string& group_name,
gfx::SnesPalette& palette);
/**
* @brief Saves all palettes in the Rom.
*
* This function iterates through all palette groups and all palettes in each
* group, and saves each palette using the SavePalette() function.
*/
absl::Status SaveAllPalettes();
void Expand(int size) {
@@ -106,8 +77,8 @@ class Rom {
void Close() {
rom_data_.clear();
palette_groups_.clear();
size_ = 0;
is_loaded_ = false;
}
absl::StatusOr<uint8_t> ReadByte(int offset);
@@ -150,14 +121,7 @@ class Rom {
return rom_data_[i];
}
bool is_loaded() const {
if (!absl::StrContains(filename_, ".sfc") &&
!absl::StrContains(filename_, ".smc")) {
return false;
}
return is_loaded_;
}
bool is_loaded() const { return !rom_data_.empty(); }
auto title() const { return title_; }
auto size() const { return size_; }
auto data() const { return rom_data_.data(); }
@@ -169,7 +133,7 @@ class Rom {
auto filename() const { return filename_; }
auto set_filename(std::string name) { filename_ = name; }
auto short_name() const { return short_name_; }
std::vector<uint8_t> graphics_buffer() const { return graphics_buffer_; }
auto graphics_buffer() const { return graphics_buffer_; }
auto mutable_graphics_buffer() { return &graphics_buffer_; }
auto palette_group() const { return palette_groups_; }
auto mutable_palette_group() { return &palette_groups_; }
@@ -232,13 +196,6 @@ class Rom {
return absl::OkStatus();
}
absl::Status LoadZelda3();
absl::Status LoadGfxGroups();
absl::Status SaveGroupsToRom();
// ROM file loaded flag
bool is_loaded_ = false;
// Size of the ROM data.
unsigned long size_ = 0;