162 lines
5.0 KiB
C++
162 lines
5.0 KiB
C++
// Differential harness for gamekdm.dll wave-1 pure-leaf reconstructions. Reads a
|
|
// hex payload on stdin, runs the reconstruction named by argv[1] on the same
|
|
// inputs the Unicorn side feeds the original, and writes the hex result the T8
|
|
// differ compares. One mode per reconstructed rva.
|
|
#include <cctype>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "game/leaf_ops.h"
|
|
|
|
using namespace gk::game;
|
|
|
|
namespace {
|
|
|
|
constexpr std::size_t kObj = 0xB0;
|
|
constexpr std::size_t kDst = 0x90;
|
|
|
|
std::vector<std::uint8_t> ReadHex() {
|
|
std::vector<std::uint8_t> out;
|
|
std::string digits;
|
|
int c = 0;
|
|
while ((c = std::getchar()) != EOF) {
|
|
if (std::isxdigit(c) != 0) {
|
|
digits.push_back(static_cast<char>(c));
|
|
}
|
|
}
|
|
for (std::size_t i = 0; i + 1 < digits.size(); i += 2) {
|
|
out.push_back(static_cast<std::uint8_t>(
|
|
std::strtol(digits.substr(i, 2).c_str(), nullptr, 16)));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
void PutHex(const void* data, std::size_t size) {
|
|
const auto* bytes = static_cast<const std::uint8_t*>(data);
|
|
for (std::size_t i = 0; i < size; ++i) {
|
|
std::printf("%02x", bytes[i]);
|
|
}
|
|
std::printf("\n");
|
|
}
|
|
|
|
std::uint32_t U32(const std::uint8_t* p) {
|
|
std::uint32_t v = 0;
|
|
std::memcpy(&v, p, 4);
|
|
return v;
|
|
}
|
|
|
|
void PutU32(std::uint32_t value) { PutHex(&value, 4); }
|
|
|
|
int Dispatch(const std::string& mode, const std::vector<std::uint8_t>& in) {
|
|
if (mode == "ret2") { PutU32(ReturnTwo()); return 0; }
|
|
if (mode == "ret0") { PutU32(ReturnZero()); return 0; }
|
|
if (mode == "ret3") { PutU32(ReturnThree()); return 0; }
|
|
if (mode == "ret8") { PutU32(ReturnEight()); return 0; }
|
|
if (mode == "ret1") { PutU32(ReturnOne()); return 0; }
|
|
if (mode == "ret7") { PutU32(ReturnSeven()); return 0; }
|
|
if (mode == "ptrplusb") { PutU32(PointerPlusB(U32(in.data()))); return 0; }
|
|
if (mode == "computeoffset") {
|
|
auto sel = static_cast<std::int32_t>(U32(in.data() + 0));
|
|
auto base = static_cast<std::int32_t>(U32(in.data() + 4));
|
|
auto m1 = static_cast<std::int32_t>(U32(in.data() + 8));
|
|
auto m2 = static_cast<std::int32_t>(U32(in.data() + 12));
|
|
auto result = ComputeTableOffset(sel, base, m1, m2);
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
if (mode == "getfield94" || mode == "getfield10" || mode == "getfield14p4") {
|
|
std::vector<std::uint8_t> obj(kObj, 0);
|
|
std::memcpy(obj.data(), in.data(), kObj);
|
|
std::uint32_t result = mode == "getfield94" ? GetField94(obj.data())
|
|
: mode == "getfield10" ? GetField10(obj.data())
|
|
: GetField14Plus4(obj.data());
|
|
PutU32(result);
|
|
return 0;
|
|
}
|
|
if (mode == "getbyte9c") {
|
|
std::vector<std::uint8_t> obj(kObj, 0);
|
|
std::memcpy(obj.data(), in.data(), kObj);
|
|
std::uint8_t result = GetByte9c(obj.data());
|
|
PutHex(&result, 1);
|
|
return 0;
|
|
}
|
|
if (mode == "setbyte9c") {
|
|
std::vector<std::uint8_t> obj(kObj, 0);
|
|
std::memcpy(obj.data(), in.data(), kObj);
|
|
SetByte9c(obj.data(), in.at(kObj));
|
|
PutHex(obj.data(), kObj);
|
|
return 0;
|
|
}
|
|
if (mode == "setpaira0") {
|
|
std::vector<std::uint8_t> obj(kObj, 0);
|
|
std::memcpy(obj.data(), in.data(), kObj);
|
|
SetPairA0(obj.data(), U32(in.data() + kObj), U32(in.data() + kObj + 4));
|
|
PutHex(obj.data(), kObj);
|
|
return 0;
|
|
}
|
|
if (mode == "clearfield40") {
|
|
std::vector<std::uint8_t> obj(kObj, 0);
|
|
std::memcpy(obj.data(), in.data(), kObj);
|
|
ClearField40(obj.data());
|
|
PutHex(obj.data(), kObj);
|
|
return 0;
|
|
}
|
|
if (mode == "initrecord") {
|
|
std::vector<std::uint8_t> obj(kObj, 0);
|
|
std::memcpy(obj.data(), in.data(), kObj);
|
|
InitRecord(obj.data());
|
|
PutHex(obj.data(), kObj);
|
|
return 0;
|
|
}
|
|
if (mode == "packcolorbits") {
|
|
std::uint32_t out = U32(in.data());
|
|
std::uint32_t p1 = U32(in.data() + 4);
|
|
std::uint32_t p2 = U32(in.data() + 8);
|
|
std::uint32_t low_a = U32(in.data() + 12);
|
|
std::uint8_t byte1 = in.at(16);
|
|
std::uint32_t low_b = U32(in.data() + 17);
|
|
std::uint8_t byte3 = in.at(21);
|
|
PackColorBits(p1, p2, &out, low_a, byte1, low_b, byte3);
|
|
PutHex(&out, 4);
|
|
return 0;
|
|
}
|
|
if (mode == "comparebytes") {
|
|
std::uint32_t len = U32(in.data());
|
|
const std::uint8_t* a = in.data() + 4;
|
|
const std::uint8_t* b = in.data() + 4 + len;
|
|
std::int32_t result = CompareBytes(a, b, len);
|
|
PutHex(&result, 4);
|
|
return 0;
|
|
}
|
|
if (mode == "copy7f" || mode == "copy3f") {
|
|
std::uint32_t srclen = U32(in.data());
|
|
std::vector<char> src(srclen + 1, 0);
|
|
std::memcpy(src.data(), in.data() + 4, srclen);
|
|
std::vector<char> dst(kDst, 0);
|
|
if (mode == "copy7f") {
|
|
CopyCapped7f(src.data(), dst.data());
|
|
} else {
|
|
CopyCapped3f(src.data(), dst.data());
|
|
}
|
|
PutHex(dst.data(), kDst);
|
|
return 0;
|
|
}
|
|
std::fprintf(stderr, "unknown mode %s\n", mode.c_str());
|
|
return 2;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2) {
|
|
std::fprintf(stderr, "usage: recon_game <mode>\n");
|
|
return 2;
|
|
}
|
|
const std::vector<std::uint8_t> in = ReadHex();
|
|
return Dispatch(argv[1], in);
|
|
}
|