yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
patch.cc
Go to the documentation of this file.
1#include "cli/command.h"
2
3namespace yaze {
4namespace cli {
5
6absl::Status ApplyPatch::handle(const std::vector<std::string>& arg_vec) {
7 std::string rom_filename = arg_vec[1];
8 std::string patch_filename = arg_vec[2];
10 auto source = rom_.vector();
11 std::ifstream patch_file(patch_filename, std::ios::binary);
12 std::vector<uint8_t> patch;
13 patch.resize(rom_.size());
14 patch_file.read((char*)patch.data(), patch.size());
15
16 // Apply patch
17 std::vector<uint8_t> patched;
18 app::core::ApplyBpsPatch(source, patch, patched);
19
20 // Save patched file
21 std::ofstream patched_rom("patched.sfc", std::ios::binary);
22 patched_rom.write((char*)patched.data(), patched.size());
23 patched_rom.close();
24 return absl::OkStatus();
25}
26
27absl::Status AsarPatch::handle(const std::vector<std::string>& arg_vec) {
28 std::string patch_filename = arg_vec[1];
29 std::string rom_filename = arg_vec[2];
30 RETURN_IF_ERROR(rom_.LoadFromFile(rom_filename))
31 int buflen = rom_.vector().size();
32 int romlen = rom_.vector().size();
33 if (!asar_patch(patch_filename.c_str(), rom_filename.data(), buflen,
34 &romlen)) {
35 std::string error_message = "Failed to apply patch: ";
36 int num_errors = 0;
37 const errordata* errors = asar_geterrors(&num_errors);
38 for (int i = 0; i < num_errors; i++) {
39 error_message += absl::StrFormat("%s", errors[i].fullerrdata);
40 }
41 return absl::InternalError(error_message);
42 }
43 return absl::OkStatus();
44}
45
46absl::Status CreatePatch::handle(const std::vector<std::string>& arg_vec) {
47 std::vector<uint8_t> source;
48 std::vector<uint8_t> target;
49 std::vector<uint8_t> patch;
50 // Create patch
51 app::core::CreateBpsPatch(source, target, patch);
52
53 // Save patch to file
54 // std::ofstream patchFile("patch.bps", ios::binary);
55 // patchFile.write(reinterpret_cast<const char*>(patch.data()),
56 // patch.size()); patchFile.close();
57 return absl::OkStatus();
58}
59
60} // namespace cli
61} // namespace yaze
auto vector() const
Definition rom.h:464
auto size() const
Definition rom.h:460
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
Definition rom.cc:168
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition patch.cc:6
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition patch.cc:27
absl::Status handle(const std::vector< std::string > &arg_vec) override
Definition patch.cc:46
#define RETURN_IF_ERROR(expression)
Definition constants.h:62
void ApplyBpsPatch(const std::vector< uint8_t > &source, const std::vector< uint8_t > &patch, std::vector< uint8_t > &target)
Definition common.cc:257
void CreateBpsPatch(const std::vector< uint8_t > &source, const std::vector< uint8_t > &target, std::vector< uint8_t > &patch)
Definition common.cc:168
Definition common.cc:22