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