Skip to content

Commit

Permalink
fix log space, oodle lib, tests bo4, iw8 opcodes and vm opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ate47 committed Nov 23, 2024
1 parent b9c9e7f commit 5574213
Show file tree
Hide file tree
Showing 10 changed files with 432 additions and 118 deletions.
54 changes: 49 additions & 5 deletions src/acts/tools/exe_mapper_bo4.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,81 @@
#include <includes.hpp>
#include <hook/module_mapper.hpp>
#include <hook/error.hpp>
#include <deps/oodle.hpp>

namespace {
struct {

char locationDir[MAX_PATH + 1]{};
} bo4;

hook::library::Detour Com_Error_Detour;
hook::library::Detour Sys_GetAbsZoneDir_Detour;

void Com_Error_Stub(uint32_t code) {
throw std::runtime_error(utils::va("Com_Error(0x%x)", code));
}

const char* Sys_GetAbsZoneDir_Stub() {
return utils::va("%s/zone", bo4.locationDir);
}

enum PMemStack : __int32
{
PMEM_STACK_DB = 0x0,
PMEM_STACK_DB2 = 0x1,
PMEM_STACK_GAME = 0x2,
PMEM_STACK_SERVER = 0x3,
PMEM_STACK_HOT = 0x4,
PMEM_STACK_CINEMATICS = 0x5,
PMEM_STACK_DYNAMIC_IMAGES = 0x6,
PMEM_STACK_LIGHTING = 0x7,
PMEM_STACK_BINARY_PATCH = 0x8,
PMEM_STACK_DEMO_DOWNLOAD = 0x9,
PMEM_STACK_MOTION_MATCHING_KEYFRAMES = 0xA,
PMEM_STACK_COUNT = 0xB,
PMEM_STACK_INVALID = -1,
PHYS_ALLOC_LOW = 0x0,
PHYS_ALLOC_HIGH = 0x2,
PHYS_ALLOC_COUNT = 0xB,
};
typedef void DB_Interrupt(void);



int bo4_exe_mapper(int argc, const char* argv[]) {
if (tool::NotEnoughParam(argc, 1)) return tool::BAD_USAGE;
if (tool::NotEnoughParam(argc, 2)) return tool::BAD_USAGE;

hook::module_mapper::Module mod{};
hook::module_mapper::Module mod{ true };
if (!mod.Load(argv[2])) {
LOG_ERROR("Can't map module {}", argv[2]);
return tool::BASIC_ERROR;
}

strcpy_s(bo4.locationDir, argv[3]);

LOG_INFO("Module loaded");

deps::oodle::Oodle oodle{};

if (!oodle.LoadOodleFromGame(*mod)) {
LOG_ERROR("Can't load oodle");
return tool::BASIC_ERROR;
}

LOG_INFO("Deps loaded");

Com_Error_Detour.Create((*mod)[0x288B110], Com_Error_Stub);
Sys_GetAbsZoneDir_Detour.Create((*mod)[0x2895E40], Sys_GetAbsZoneDir_Stub);

LOG_INFO("Hook loaded");
LOG_INFO("Hooks loaded");

//DB_LoadXFile(char const*, int, XZoneBuffer*, char const*, XAssetList*, XBlock*, void (*)(void), uchar*, PMemStack, int).text 0000000002E0CC10 00001573 00000808 0000004C R . . ..B T .

void (*DB_LoadXFile)(char const* path, int file, void* buffer, char const* filename, void* assetlist, void* xblock, DB_Interrupt inter, byte * buf, PMemStack side, int flags)
= (decltype(DB_LoadXFile))(*mod)[0x2E0CC1];

return tool::OK;
}

ADD_TOOL(bo4_exe_mapper, "bo4", "[exe]", "test bo4 mapping", bo4_exe_mapper);
ADD_TOOL(bo4_exe_mapper, "bo4", "[exe] [gamedir]", "test bo4 mapping", bo4_exe_mapper);
}
73 changes: 64 additions & 9 deletions src/acts/tools/gsc_opcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,9 @@ class OPCodeInfoGetHash : public OPCodeInfo {

class OPCodeInfoJump : public OPCodeInfo {
public:
using OPCodeInfo::OPCodeInfo;
bool jump32;
public:
OPCodeInfoJump(OPCode id, const char* name, bool jump32 = false) : jump32(jump32), OPCodeInfo(id, name) {}

int Dump(std::ostream& out, uint16_t value, ASMContext& context, tool::gsc::T8GSCOBJContext& objctx) const override {
// get the jump opcode location
Expand Down Expand Up @@ -1330,6 +1332,7 @@ class OPCodeInfoJump : public OPCodeInfo {
name = "jumpdev";
break;
case OPCODE_Jump:
case OPCODE_IW_Jump32:
name = "goto";
break;
default:
Expand All @@ -1342,14 +1345,28 @@ class OPCodeInfoJump : public OPCodeInfo {
assert(!context.m_runDecompiler || name != nullptr);

if (objctx.m_vmInfo->HasFlag(VmFlags::VMF_ALIGN)) {
context.Aligned<int16_t>();
if (jump32) {
context.Aligned<int32_t>();
}
else {
context.Aligned<int16_t>();
}
}

auto& bytecode = context.m_bcl;

int16_t delta = *(int16_t*)bytecode;
int32_t delta;

if (jump32) {
delta = *(int32_t*)bytecode;

bytecode += 2;
bytecode += 4;
}
else {
delta = *(int16_t*)bytecode;

bytecode += 2;
}

// push a location and mark it as referenced
byte* newLoc = &context.m_bcl[delta];
Expand Down Expand Up @@ -1378,7 +1395,7 @@ class OPCodeInfoJump : public OPCodeInfo {
if (delta != 0 || m_id == OPCODE_DevblockBegin) {
if (context.m_runDecompiler) {
bool inject = true;
if (m_id == OPCODE_Jump && delta > 0) {
if ((m_id == OPCODE_Jump || m_id == OPCODE_IW_Jump32) && delta > 0) {
// might be ternary
//ASMContextNodeTernary
if (context.m_stack.size() && context.m_funcBlock.m_statements.size()) {
Expand Down Expand Up @@ -1469,13 +1486,26 @@ class OPCodeInfoJump : public OPCodeInfo {
int Skip(uint16_t value, ASMSkipContext& ctx) const override {
int32_t m_jumpLocation = ctx.FunctionRelativeLocation(ctx.m_bcl - 2);
if (ctx.m_vminfo->HasFlag(VmFlags::VMF_ALIGN | VmFlags::VMF_OPCODE_U16)) {
ctx.Aligned<int16_t>();
if (jump32) {
ctx.Aligned<int32_t>();
}
else {
ctx.Aligned<int16_t>();
}
}
auto& bytecode = ctx.m_bcl;

int16_t delta = *(int16_t*)bytecode;
int32_t delta;
if (jump32) {
delta = *(int32_t*)bytecode;

bytecode += 2;
bytecode += 4;
}
else {
delta = *(int16_t*)bytecode;

bytecode += 2;
}

ctx.PushLocation(&ctx.m_bcl[delta]);

Expand Down Expand Up @@ -2966,6 +2996,29 @@ class OPCodeInfoStatement : public OPCodeInfo {
return 0;
}
};
class OPCodeInfoGscBinStrToken : public OPCodeInfo {
public:
using OPCodeInfo::OPCodeInfo;

int Dump(std::ostream& out, uint16_t value, ASMContext& context, tool::gsc::T8GSCOBJContext& objctx) const override {
int32_t token{ *(int32_t*)context.m_bcl };
context.m_bcl += 4;

out << "0x" << std::hex << token;
if (context.m_runDecompiler) {
auto* ref = new ASMContextNodeValue<int32_t>(token, TYPE_VALUE, true);
// convert it to statement
ref->m_priority = PRIORITY_INST;
context.PushASMCNode(ref);
context.CompleteStatement();
}
out << "\n";
return 0;
}
int Skip(uint16_t value, ASMSkipContext& ctx) const override {
return 0;
}
};
class OPCodeInfoSkip : public OPCodeInfo {
const char* m_operatorName;
public:
Expand Down Expand Up @@ -5095,8 +5148,9 @@ namespace tool::gsc::opcode {
RegisterOpCodeHandler(new OPCodeInfoUnknown(OPCODE_GSCBIN_SKIP_N, "GscBinSkipN"));
RegisterOpCodeHandler(new OPCodeInfoUnknown(OPCODE_GSCBIN_SKIP_3BC_4SD, "GscBinSkip3BC4SD"));
RegisterOpCodeHandler(new OPCodeInfoUnknown(OPCODE_GSCBIN_SKIP_4BC_4SD, "GscBinSkip4BC4SD"));
RegisterOpCodeHandler(new OPCodeInfoUnknown(OPCODE_GSCBIN_SKIP_STR_TOKEN, "GscBinSkipSTRTOKEN"));
RegisterOpCodeHandler(new OPCodeInfoUnknown(OPCODE_GSCBIN_SKIP_4BC_1STR, "GscBinSkip4BC1STR"));

RegisterOpCodeHandler(new OPCodeInfoGscBinStrToken(OPCODE_GSCBIN_SKIP_STR_TOKEN, "GscBinSkipSTRTOKEN"));

// all op without params
RegisterOpCodeHandler(new OPCodeInfoStatement(OPCODE_ProfileStart, "ProfileStart", "profilestart()"));
Expand All @@ -5116,6 +5170,7 @@ namespace tool::gsc::opcode {
RegisterOpCodeHandler(new OPCodeInfoEndSwitch());
// dev/jump
RegisterOpCodeHandler(new OPCodeInfoJump(OPCODE_DevblockBegin, "DevblockBegin"));
RegisterOpCodeHandler(new OPCodeInfoJump(OPCODE_IW_Jump32, "Jump", true));
RegisterOpCodeHandler(new OPCodeInfoJump(OPCODE_Jump, "Jump"));
RegisterOpCodeHandler(new OPCodeInfoJump(OPCODE_JumpOnTrue, "JumpOnTrue"));
RegisterOpCodeHandler(new OPCodeInfoJump(OPCODE_JumpOnGreaterThan, "JumpOnGreaterThan"));
Expand Down
14 changes: 14 additions & 0 deletions src/acts/tools/gsc_opcodes_load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,20 @@ namespace tool::gsc::opcode {
RegisterOpCode(OPCODE_GSCBIN_SKIP_4BC_4SD, "GscBinSkip4BC4SD");
RegisterOpCode(OPCODE_GSCBIN_SKIP_STR_TOKEN, "GscBinSkipSTRTOKEN");
RegisterOpCode(OPCODE_GSCBIN_SKIP_4BC_1STR, "GscBinSkip4BC1STR");
RegisterOpCode(OPCODE_IW_CallBuiltinFunction0, "CallBuiltinFunction0");
RegisterOpCode(OPCODE_IW_CallBuiltinFunction1, "CallBuiltinFunction1");
RegisterOpCode(OPCODE_IW_CallBuiltinFunction2, "CallBuiltinFunction2");
RegisterOpCode(OPCODE_IW_CallBuiltinFunction3, "CallBuiltinFunction3");
RegisterOpCode(OPCODE_IW_CallBuiltinFunction4, "CallBuiltinFunction4");
RegisterOpCode(OPCODE_IW_CallBuiltinFunction5, "CallBuiltinFunction5");
RegisterOpCode(OPCODE_IW_CallBuiltinMethod0, "CallBuiltinMethod0");
RegisterOpCode(OPCODE_IW_CallBuiltinMethod1, "CallBuiltinMethod1");
RegisterOpCode(OPCODE_IW_CallBuiltinMethod2, "CallBuiltinMethod2");
RegisterOpCode(OPCODE_IW_CallBuiltinMethod3, "CallBuiltinMethod3");
RegisterOpCode(OPCODE_IW_CallBuiltinMethod4, "CallBuiltinMethod4");
RegisterOpCode(OPCODE_IW_CallBuiltinMethod5, "CallBuiltinMethod5");
RegisterOpCode(OPCODE_IW_ScriptFunctionCall2, "ScriptFunctionCall2");
RegisterOpCode(OPCODE_IW_Jump32, "Jump32");
RegisterOpCode(OPCODE_InvalidOpCode, "InvalidOpCode");
}
};
Expand Down
1 change: 1 addition & 0 deletions src/acts/tools/gsc_opcodes_load.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ namespace tool::gsc::opcode {
OPCODE_IW_CallBuiltinMethod5,

OPCODE_IW_ScriptFunctionCall2,
OPCODE_IW_Jump32,

OPCODE_COUNT,
};
Expand Down
Loading

0 comments on commit 5574213

Please sign in to comment.