forked from chenxiaolong/DualBootPatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmbpatcher_test.cpp
149 lines (121 loc) · 3.85 KB
/
libmbpatcher_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (C) 2015 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
* DualBootPatcher is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DualBootPatcher is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DualBootPatcher. If not, see <http://www.gnu.org/licenses/>.
*/
#include <memory>
#include <cstring>
#include <mbdevice/json.h>
#include <mblog/base_logger.h>
#include <mblog/logging.h>
#include <mbpatcher/patcherconfig.h>
#include <mbpatcher/patcherinterface.h>
class BasicLogger : public mb::log::BaseLogger
{
public:
virtual void log(const mb::log::LogRecord &rec) override
{
printf("%s\n", rec.fmt_msg.c_str());
}
virtual bool formatted() override
{
return true;
}
};
static bool file_read_all(const std::string &path, std::string &data_out)
{
FILE *fp = fopen(path.c_str(), "rbe");
if (!fp) {
return false;
}
if (fseeko64(fp, 0, SEEK_END) < 0) {
return false;
}
auto size = ftello64(fp);
if (size < 0 || std::make_unsigned_t<decltype(size)>(size) > SIZE_MAX) {
return false;
}
if (fseeko64(fp, 0, SEEK_SET) < 0) {
return false;
}
std::string data(static_cast<size_t>(size), '\0');
if (fread(data.data(), data.size(), 1, fp) != 1) {
fclose(fp);
return false;
}
data_out.swap(data);
fclose(fp);
return true;
}
static bool get_device(const char *path, mb::device::Device &device)
{
std::string contents;
if (!file_read_all(path, contents)) {
fprintf(stderr, "%s: Failed to read file: %s\n", path, strerror(errno));
return false;
}
contents.push_back('\0');
mb::device::JsonError error;
if (!mb::device::device_from_json(contents, device, error)) {
fprintf(stderr, "%s: Failed to load devices\n", path);
return false;
}
if (device.validate() != 0) {
fprintf(stderr, "%s: Validation failed\n", path);
return false;
}
return true;
}
static void mbp_progress_cb(uint64_t bytes, uint64_t maxBytes)
{
printf("Current bytes percentage: %.1f\n",
100.0 * static_cast<double>(bytes) / static_cast<double>(maxBytes));
}
int main(int argc, char *argv[]) {
if (argc != 6) {
fprintf(stderr, "Usage: %s <patcher id> <device file> <rom id> "
"<input path> <output path>\n", argv[0]);
return EXIT_FAILURE;
}
const char *patcher_id = argv[1];
const char *device_file = argv[2];
const char *rom_id = argv[3];
const char *input_path = argv[4];
const char *output_path = argv[5];
mb::log::set_logger(std::make_shared<BasicLogger>());
mb::device::Device device;
if (!get_device(device_file, device)) {
return EXIT_FAILURE;
}
mb::patcher::PatcherConfig pc;
pc.set_data_directory("data");
mb::patcher::FileInfo fi;
fi.set_device(std::move(device));
fi.set_input_path(input_path);
fi.set_output_path(output_path);
fi.set_rom_id(rom_id);
auto *patcher = pc.create_patcher(patcher_id);
if (!patcher) {
fprintf(stderr, "Invalid patcher ID: %s\n", patcher_id);
return EXIT_FAILURE;
}
patcher->set_file_info(&fi);
bool ret = patcher->patch_file(&mbp_progress_cb, nullptr, nullptr);
if (!ret) {
fprintf(stderr, "Error: %d\n", static_cast<int>(patcher->error()));
}
return ret ? EXIT_SUCCESS : EXIT_FAILURE;
}