Refactor command handlers for improved code organization and readability
This commit is contained in:
33
src/cli/handlers/compress.cc
Normal file
33
src/cli/handlers/compress.cc
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "cli/command.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace cli {
|
||||
|
||||
absl::Status Compress::handle(const std::vector<std::string>& arg_vec) {
|
||||
std::cout << "Compress selected with argument: " << arg_vec[0] << std::endl;
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
absl::Status Decompress::handle(const std::vector<std::string>& arg_vec) {
|
||||
ColorModifier underline(ColorCode::FG_UNDERLINE);
|
||||
ColorModifier reset(ColorCode::FG_RESET);
|
||||
std::cout << "Please specify the tilesheets you want to export\n";
|
||||
std::cout << "You can input an individual sheet, a range X-Y, or comma "
|
||||
"separate values.\n\n";
|
||||
std::cout << underline << "Tilesheets\n" << reset;
|
||||
std::cout << "0-112 -> compressed 3bpp bgr \n";
|
||||
std::cout << "113-114 -> compressed 2bpp\n";
|
||||
std::cout << "115-126 -> uncompressed 3bpp sprites\n";
|
||||
std::cout << "127-217 -> compressed 3bpp sprites\n";
|
||||
std::cout << "218-222 -> compressed 2bpp\n";
|
||||
|
||||
std::cout << "Enter tilesheets: ";
|
||||
std::string sheet_input;
|
||||
std::cin >> sheet_input;
|
||||
|
||||
std::cout << "Decompress selected with argument: " << arg_vec[0] << std::endl;
|
||||
return absl::UnimplementedError("Decompress not implemented");
|
||||
}
|
||||
|
||||
} // namespace cli
|
||||
} // namespace yaze
|
||||
61
src/cli/handlers/patch.cc
Normal file
61
src/cli/handlers/patch.cc
Normal file
@@ -0,0 +1,61 @@
|
||||
#include "cli/command.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace cli {
|
||||
|
||||
absl::Status ApplyPatch::handle(const std::vector<std::string>& arg_vec) {
|
||||
std::string rom_filename = arg_vec[1];
|
||||
std::string patch_filename = arg_vec[2];
|
||||
RETURN_IF_ERROR(rom_.LoadFromFile(rom_filename))
|
||||
auto source = rom_.vector();
|
||||
std::ifstream patch_file(patch_filename, std::ios::binary);
|
||||
std::vector<uint8_t> patch;
|
||||
patch.resize(rom_.size());
|
||||
patch_file.read((char*)patch.data(), patch.size());
|
||||
|
||||
// Apply patch
|
||||
std::vector<uint8_t> patched;
|
||||
app::core::ApplyBpsPatch(source, patch, patched);
|
||||
|
||||
// Save patched file
|
||||
std::ofstream patched_rom("patched.sfc", std::ios::binary);
|
||||
patched_rom.write((char*)patched.data(), patched.size());
|
||||
patched_rom.close();
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
absl::Status AsarPatch::handle(const std::vector<std::string>& arg_vec) {
|
||||
std::string patch_filename = arg_vec[1];
|
||||
std::string rom_filename = arg_vec[2];
|
||||
RETURN_IF_ERROR(rom_.LoadFromFile(rom_filename))
|
||||
int buflen = rom_.vector().size();
|
||||
int romlen = rom_.vector().size();
|
||||
if (!asar_patch(patch_filename.c_str(), rom_filename.data(), buflen,
|
||||
&romlen)) {
|
||||
std::string error_message = "Failed to apply patch: ";
|
||||
int num_errors = 0;
|
||||
const errordata* errors = asar_geterrors(&num_errors);
|
||||
for (int i = 0; i < num_errors; i++) {
|
||||
error_message += absl::StrFormat("%s", errors[i].fullerrdata);
|
||||
}
|
||||
return absl::InternalError(error_message);
|
||||
}
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
absl::Status CreatePatch::handle(const std::vector<std::string>& arg_vec) {
|
||||
std::vector<uint8_t> source;
|
||||
std::vector<uint8_t> target;
|
||||
std::vector<uint8_t> patch;
|
||||
// Create patch
|
||||
app::core::CreateBpsPatch(source, target, patch);
|
||||
|
||||
// Save patch to file
|
||||
// std::ofstream patchFile("patch.bps", ios::binary);
|
||||
// patchFile.write(reinterpret_cast<const char*>(patch.data()),
|
||||
// patch.size()); patchFile.close();
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
} // namespace cli
|
||||
} // namespace yaze
|
||||
87
src/cli/handlers/tile16_transfer.cc
Normal file
87
src/cli/handlers/tile16_transfer.cc
Normal file
@@ -0,0 +1,87 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/status/status.h"
|
||||
#include "app/core/common.h"
|
||||
#include "app/core/constants.h"
|
||||
#include "app/rom.h"
|
||||
#include "cli/command.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace cli {
|
||||
|
||||
using namespace app;
|
||||
|
||||
absl::Status Tile16Transfer::handle(const std::vector<std::string>& arg_vec) {
|
||||
// Load the source rom
|
||||
RETURN_IF_ERROR(rom_.LoadFromFile(arg_vec[0]))
|
||||
|
||||
// Load the destination rom
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// Transfer if user confirms
|
||||
if (userChoice == 'y' || userChoice == 'Y') {
|
||||
RETURN_IF_ERROR(
|
||||
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, /*save_new=*/false, arg_vec[1]))
|
||||
|
||||
std::cout << "Successfully transferred tile16" << std::endl;
|
||||
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
} // namespace cli
|
||||
} // namespace yaze
|
||||
Reference in New Issue
Block a user