src/exdeath.cc: truly a successful patcher

main
Kiyoshi Aman 2020-06-17 23:46:54 -06:00
parent 711c601d6a
commit 0bce0d12bc
1 changed files with 16 additions and 13 deletions

View File

@ -103,8 +103,7 @@ void Exdeath::btnApply_clicked(bool trigger) {
}
target = new QFile(output);
target->open(QIODevice::WriteOnly | QIODevice::Append);
target->seek(0);
target->open(QIODevice::ReadWrite);
for (int i = 0; i < patches.size(); i++) {
applyPatch(target, patches[i]);
@ -116,22 +115,23 @@ void Exdeath::btnApply_clicked(bool trigger) {
void Exdeath::applyPatch(QFile *file, QString patch) {
QFile *data = new QFile(patch);
data->open(QIODevice::ReadOnly);
char *temp = reinterpret_cast<char *>(malloc(65536));
data->read(temp, 5);
if (strncmp(temp, "PATCH", 5)) {
// not a patch
data->close();
return;
}
// skipping header
data->seek(5);
while (!data->atEnd()) {
unsigned int seek = 0, length = 0;
unsigned int seek = 0;
unsigned short length = 0;
char *temp = reinterpret_cast<char *>(malloc(65536));
data->read(temp, 3);
if (!strncmp(temp, "EOF", 3)) {
break;
}
seek = ((temp[0] & 0xff) << 16) + ((temp[1] & 0xff) << 8) + (temp[2] & 0xff);
printf("Seeking to 0x%06x", seek);
file->seek(seek);
data->read(temp, 2);
length = ((temp[0] & 0xff) << 8) + (temp[1] & 0xff);
@ -140,15 +140,18 @@ void Exdeath::applyPatch(QFile *file, QString patch) {
data->read(temp, 2);
length = ((temp[0] & 0xff) << 8) + (temp[1] & 0xff);
data->read(temp, 1);
printf(", now at 0x%08llx", file->pos());
printf(" and writing 0x%04hx bytes of 0x%02hhx\n", length, temp[0]);
file->seek(seek);
for (unsigned int i = 0; i < length; i++) {
file->write(temp, 1);
}
printf("position after write: 0x%08llx\n", file->pos());
} else {
printf(" and writing 0x%04hx bytes\n", length);
data->read(temp, length);
file->seek(seek);
file->write(temp, length);
printf("position after write: 0x%08llx\n", file->pos());
}
}