Refactor ROM class, add RunTransaction

This commit is contained in:
scawful
2023-10-22 03:23:26 -04:00
parent 279940b1d2
commit 6a0dc078c1
9 changed files with 940 additions and 327 deletions

View File

@@ -21,20 +21,58 @@ absl::Status Tile16Transfer::handle(const std::vector<std::string>& arg_vec) {
ROM dest_rom;
RETURN_IF_ERROR(dest_rom.LoadFromFile(arg_vec[1]))
std::vector<uint32_t> tileIDs;
// Parse the CSV list of tile16 IDs.
std::stringstream ss(arg_vec[2].data());
for (std::string tile16_id; std::getline(ss, tile16_id, ',');) {
std::cout << "Writing tile16 ID " << tile16_id << " to dest rom."
<< std::endl;
for (std::string tileID; std::getline(ss, tileID, ',');) {
if (tileID == "*") {
// for (uint32_t i = 0; i <= rom_.GetMaxTileID(); ++i) {
// tileIDs.push_back(i);
// }
break; // No need to continue parsing if * is used
} else if (tileID.find('-') != std::string::npos) {
// Handle range: split by hyphen and add all tile IDs in the range.
std::stringstream rangeSS(tileID);
std::string start;
std::string end;
std::getline(rangeSS, start, '-');
std::getline(rangeSS, end);
uint32_t startID = std::stoi(start, nullptr, 16);
uint32_t endID = std::stoi(end, nullptr, 16);
for (uint32_t i = startID; i <= endID; ++i) {
tileIDs.push_back(i);
}
} else {
// Handle single tile ID
uint32_t tileID_int = std::stoi(tileID, nullptr, 16);
tileIDs.push_back(tileID_int);
}
}
// Convert the string to a base16 integer.
uint32_t tile16_id_int = std::stoi(tile16_id, nullptr, /*base=*/16);
for (const auto& tile16_id_int : tileIDs) {
// Compare the tile16 data between source and destination ROMs.
// auto source_tile16_data = rom_.ReadTile16(tile16_id_int);
// auto dest_tile16_data = dest_rom.ReadTile16(tile16_id_int);
ASSIGN_OR_RETURN(auto source_tile16_data, rom_.ReadTile16(tile16_id_int))
ASSIGN_OR_RETURN(auto dest_tile16_data, dest_rom.ReadTile16(tile16_id_int))
if (source_tile16_data != dest_tile16_data) {
// Notify user of difference
std::cout << "Difference detected in tile16 ID " << tile16_id_int
<< ". Do you want to transfer it to dest rom? (y/n): ";
char userChoice;
std::cin >> userChoice;
// Read the tile16 definition from the source ROM.
auto tile16_data = rom_.ReadTile16(tile16_id_int);
// Write the tile16 definition to the destination ROM.
dest_rom.WriteTile16(tile16_id_int, tile16_data);
// Transfer if user confirms
if (userChoice == 'y' || userChoice == 'Y') {
dest_rom.WriteTile16(tile16_id_int, source_tile16_data);
std::cout << "Transferred tile16 ID " << tile16_id_int
<< " to dest rom." << std::endl;
} else {
std::cout << "Skipped transferring tile16 ID " << tile16_id_int << "."
<< std::endl;
}
}
}
RETURN_IF_ERROR(dest_rom.SaveToFile(/*backup=*/true, arg_vec[1]))

View File

@@ -230,14 +230,18 @@ class ReadFromRom : public CommandHandler {
}
if (length > 1) {
auto returned_bytes = rom_.ReadByteVector(offset, length);
auto returned_bytes_status = rom_.ReadByteVector(offset, length);
if (!returned_bytes_status.ok()) {
return returned_bytes_status.status();
}
auto returned_bytes = returned_bytes_status.value();
for (const auto& each : returned_bytes) {
std::cout << each;
}
std::cout << std::endl;
} else {
auto byte = rom_.ReadByte(offset);
std::cout << std::hex << byte << std::endl;
std::cout << std::hex << byte.value() << std::endl;
}
return absl::OkStatus();