Skip to content

Commit

Permalink
bo3 decompiler strings enhancement, start working f1 vm
Browse files Browse the repository at this point in the history
  • Loading branch information
ate47 committed Oct 13, 2024
1 parent 11c26e6 commit f33a9ed
Show file tree
Hide file tree
Showing 17 changed files with 846 additions and 38 deletions.
29 changes: 29 additions & 0 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,32 @@ project "AtianCodToolsBOCWDLL"
dependson "asmjit"
dependson "imgui"

project "ACTSVM"
kind "StaticLib"
language "C++"
cppdialect "C++20"
targetdir "%{wks.location}/bin/"
objdir "%{wks.location}/obj/"

targetname "acts-vm"

files {
"./src/vm/**.hpp",
"./src/vm/**.h",
"./src/vm/**.cpp",
}

includedirs {
"src/shared"
}

vpaths {
["*"] = "*"
}

links { "ACTSSharedLibrary" }
dependson "ACTSSharedLibrary"

project "AtianCodTools"
kind "SharedLib"
language "C++"
Expand Down Expand Up @@ -282,6 +308,7 @@ project "AtianCodTools"
"src/acts",
"src/shared",
"src/lib",
"src/vm",
-- link antlr4
"deps/antlr4/runtime/Cpp/runtime/src/",
"deps/zlib/",
Expand Down Expand Up @@ -316,6 +343,7 @@ project "AtianCodTools"
links { "antlr4-runtime" }
links { "ACTSSharedLibrary" }
links { "ACTSLibrary" }
links { "ACTSVM" }
links { "zlib" }
links { "libps4debug" }
links { "asmjit" }
Expand All @@ -331,6 +359,7 @@ project "AtianCodTools"
dependson "antlr4-runtime"
dependson "ACTSSharedLibrary"
dependson "ACTSLibrary"
dependson "ACTSVM"
dependson "zlib"
dependson "libps4debug"
dependson "asmjit"
Expand Down
36 changes: 32 additions & 4 deletions src/acts/tools/gsc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ int GSCOBJHandler::PatchCode(T8GSCOBJContext& ctx) {
else {
for (size_t j = 0; j < unk2c->num_address; j++) {
Ref(vars[j]) = (byte)ref;
ctx.AddStringRef(vars[j], ref);
}
}
unk2c_location += sizeof(*unk2c) + sizeof(*vars) * unk2c->num_address;
Expand All @@ -439,6 +440,7 @@ int GSCOBJHandler::PatchCode(T8GSCOBJContext& ctx) {
const auto* strings = reinterpret_cast<const uint32_t*>(&str[1]);
for (size_t j = 0; j < str->num_address; j++) {
Ref<uint32_t>(strings[j]) = ref;
ctx.AddStringRef(strings[j], ref);
}
str_location += sizeof(*str) + sizeof(*strings) * str->num_address;
}
Expand All @@ -458,12 +460,16 @@ int GSCOBJHandler::PatchCode(T8GSCOBJContext& ctx) {
if (it != ctx.gdbctx->strings.end()) {
std::string& str = it->second;

Ref<uint32_t>(loc[j]) = ctx.AddStringValue(str.c_str());
uint32_t strref = ctx.AddStringValue(str.c_str());
Ref<uint32_t>(loc[j]) = strref;
ctx.AddStringRef(loc[j], strref);
continue;
}
}
ctx.m_unkstrings[str].insert(loc[j]);
Ref<uint32_t>(loc[j]) = ctx.AddStringValue(str);
uint32_t strref = ctx.AddStringValue(str);
Ref<uint32_t>(loc[j]) = strref;
ctx.AddStringRef(loc[j], strref);
}
val = reinterpret_cast<T8GSCString*>(loc + val->num_address);
}
Expand Down Expand Up @@ -528,6 +534,9 @@ int GSCOBJHandler::PatchCode(T8GSCOBJContext& ctx) {
// use strings to link them
loc[0] = ref1;
loc[1] = ref2;

ctx.AddStringRef(vars[j], ref1);
ctx.AddStringRef(vars[j] + sizeof(*loc), ref2);
}
animt_location += sizeof(*animt) + sizeof(*vars) * animt->num_address;
}
Expand Down Expand Up @@ -640,6 +649,7 @@ int GSCOBJHandler::PatchCode(T8GSCOBJContext& ctx) {
for (size_t j = 0; j < str->num_address; j++) {
// no align too....
Ref<uint32_t>(strings[j]) = ref;
ctx.AddStringRef(strings[j], ref);
}
str_location += sizeof(*str) + sizeof(*strings) * str->num_address;
}
Expand All @@ -659,12 +669,16 @@ int GSCOBJHandler::PatchCode(T8GSCOBJContext& ctx) {
if (it != ctx.gdbctx->strings.end()) {
std::string& str = it->second;

Ref<uint32_t>(loc[j]) = ctx.AddStringValue(str.c_str());
uint32_t strref = ctx.AddStringValue(str.c_str());
Ref<uint32_t>(loc[j]) = strref;
ctx.AddStringRef(loc[j], strref);
continue;
}
}
ctx.m_unkstrings[str].insert(loc[j]);
Ref<uint32_t>(loc[j]) = ctx.AddStringValue(str);
uint32_t strref = ctx.AddStringValue(str);
Ref<uint32_t>(loc[j]) = strref;
ctx.AddStringRef(loc[j], strref);
}
val = reinterpret_cast<T8GSCString*>(loc + val->num_address);
}
Expand Down Expand Up @@ -698,6 +712,8 @@ int GSCOBJHandler::PatchCode(T8GSCOBJContext& ctx) {
uint32_t* loc = Ptr<uint32_t>(rloc);
loc[0] = ref1;
loc[1] = ref2;
ctx.AddStringRef(rloc, ref1);
ctx.AddStringRef(rloc + sizeof(*loc), ref2);

}
vars2 += 2;
Expand Down Expand Up @@ -2341,6 +2357,18 @@ const char* tool::gsc::T8GSCOBJContext::GetStringValue(uint32_t stringRef) {
}
return f->second;
}
void tool::gsc::T8GSCOBJContext::AddStringRef(uint32_t floc, uint32_t str) {
m_stringRefsLoc[floc] = str;
}

const char* tool::gsc::T8GSCOBJContext::GetStringValueByLoc(uint32_t floc) {
auto f = m_stringRefsLoc.find(floc);
if (f == m_stringRefsLoc.end()) {
return nullptr;
}
return GetStringValue(f->second);
}

const char* tool::gsc::T8GSCOBJContext::GetStringValueOrError(uint32_t stringRef, uint32_t floc, const char* errorValue) {
const char* v = GetStringValue(stringRef);

Expand Down
13 changes: 13 additions & 0 deletions src/acts/tools/gsc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ namespace tool::gsc {
public:
std::unordered_map<uint16_t, uint64_t> m_gvars{};
std::unordered_map<uint32_t, const char*> m_stringRefs{};
std::unordered_map<uint32_t, uint32_t> m_stringRefsLoc{};
std::vector<IW23GSCImport> m_linkedImports{};
// getnumber hack
std::unordered_map<uint32_t, uint32_t> m_animTreeLocations{};
Expand All @@ -773,6 +774,12 @@ namespace tool::gsc {
* @return string or null
*/
const char* GetStringValue(uint32_t stringRef);
/*
* Get a string for a string floc
* @param floc loc
* @return string or null
*/
const char* GetStringValueByLoc(uint32_t floc);
/*
* Get a string for a string ref, return errorValue in case of error
* @param stringRef string ref
Expand All @@ -792,6 +799,12 @@ namespace tool::gsc {
* @return new string ref
*/
uint32_t AddStringValue(const char* value);
/*
* Add a string
* @param floc location
* @param str string ref
*/
void AddStringRef(uint32_t floc, uint32_t str);
/*
* Clone a string inside this context
* @param str the string
Expand Down
62 changes: 29 additions & 33 deletions src/acts/tools/gsc_opcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,17 @@ namespace {

// VM->vminfo
std::unordered_map<byte, VmInfo> g_opcodeMap{};
std::unordered_map<uint64_t, byte> g_vmMap{};
// opcode->opcode handler
std::unordered_map<OPCode, const OPCodeInfo*> g_opcodeHandlerMap{};
}
namespace tool::gsc::opcode {
VM VMOf(const char* name) {
if (!_strcmpi("t8_31", name) || !_strcmpi("bo4_31", name) || !_strcmpi("blackops4_31", name) || !_strcmpi("31", name)) {
return VM_T831;
}
if (!_strcmpi("t8", name) || !_strcmpi("bo4", name) || !_strcmpi("blackops4", name) || !_strcmpi("36", name)) {
return VM_T8;
}
if (!_strcmpi("t937", name) || !_strcmpi("cw37", name) || !_strcmpi("coldwar37", name) || !_strcmpi("37", name)) {
return VM_T937;
}
if (!_strcmpi("t9", name) || !_strcmpi("cw", name) || !_strcmpi("coldwar", name) || !_strcmpi("38", name)) {
return VM_T9;
}
if (!_strcmpi("t7", name) || !_strcmpi("bo3", name) || !_strcmpi("blackops3", name) || !_strcmpi("1c", name)) {
return VM_T7;
}
if (!_strcmpi("t7_1b", name) || !_strcmpi("bo3_1b", name) || !_strcmpi("blackops3_1b", name) || !_strcmpi("1b", name)) {
return VM_T71B;
}
if (!_strcmpi("jupa", name) || !_strcmpi("s5a", name) || !_strcmpi("mwiiia", name) || !_strcmpi("modernwarfareiiia", name) || !_strcmpi("mw23a", name) || !_strcmpi("8a", name)) {
return VM_MW23;
}
if (!_strcmpi("jupb", name) || !_strcmpi("s5b", name) || !_strcmpi("mwiii", name) || !_strcmpi("modernwarfareiii", name) || !_strcmpi("mw23", name) || !_strcmpi("8b", name)) {
return VM_MW23B;
}
if (!_strcmpi("cer6", name) || !_strcmpi("t10_6", name) || !_strcmpi("bo6_6", name) || !_strcmpi("blackops6_6", name) || !_strcmpi("6", name)) {
return VM_BO6_06;
}
if (!_strcmpi("cer", name) || !_strcmpi("t10", name) || !_strcmpi("bo6", name) || !_strcmpi("blackops6", name) || !_strcmpi("7", name)) {
return VM_BO6_07;
RegisterOpCodes();
auto it = g_vmMap.find(hash::Hash64(name));

if (it != g_vmMap.end()) {
return (VM)it->second;
}
return VM_UNKNOWN;
}
Expand Down Expand Up @@ -1654,7 +1631,7 @@ class OPCodeInfoVectorConstant : public OPCodeInfo {
context.PushASMCNode(new ASMContextNodeVector(x, y, z));
}

out << "(" << x << ", " << y<< ", " << z << ")\n";
out << "(" << x << ", " << y << ", " << z << ")\n";

return 0;
}
Expand Down Expand Up @@ -4001,6 +3978,7 @@ class OPCodeInfoSwitch : public OPCodeInfo {

for (size_t c = 1; c <= cases; c++) {
uint64_t caseValue;
byte* caseLoc;
int64_t caseDelta;
byte* endBase;
if (context.m_vm >= VM_T8) {
Expand All @@ -4009,6 +3987,7 @@ class OPCodeInfoSwitch : public OPCodeInfo {
context.WritePadding(out);

caseValue = *(uint64_t*)baseCaseValue;
caseLoc = baseCaseValue;
baseCaseValue += 8;
auto& baseCaseDelta = context.Aligned<int64_t>();
caseDelta = *(int64_t*)baseCaseDelta;
Expand All @@ -4021,6 +4000,7 @@ class OPCodeInfoSwitch : public OPCodeInfo {
context.WritePadding(out);

caseValue = *(uint32_t*)baseCaseValue;
caseLoc = baseCaseValue;
baseCaseValue += 4;
auto& baseCaseDelta = context.Aligned<int32_t>();
caseDelta = *(int32_t*)baseCaseDelta;
Expand All @@ -4046,9 +4026,19 @@ class OPCodeInfoSwitch : public OPCodeInfo {
}
}
else {
out << std::dec << caseValue;
if (node) {
node->m_cases.push_back({ new ASMContextNodeValue<int64_t>(caseValue, TYPE_VALUE), caseRLoc });
// bo3 string decomp
const char* cv = objctx.GetStringValueByLoc((uint32_t)(caseLoc - context.m_gscReader.file));
if (cv) {
PrintFormattedString(out << "\"", cv) << "\"";
if (node) {
node->m_cases.push_back({ new ASMContextNodeValue<const char*>(cv, TYPE_VALUE), caseRLoc });
}
}
else {
out << std::dec << caseValue;
if (node) {
node->m_cases.push_back({ new ASMContextNodeValue<int64_t>(caseValue, TYPE_VALUE), caseRLoc });
}
}
}
}
Expand Down Expand Up @@ -4796,6 +4786,12 @@ namespace tool::gsc::opcode {
return; // assuming good name
}
g_opcodeMap[vm] = { vm, name, codeName, internalName, flags, {} };
g_vmMap[hash::Hash64(utils::va("%x", (int)vm))] = vm;
g_vmMap[hash::Hash64(internalName)] = vm;
}

void RegisterVmName(byte vm, uint64_t hash) {
g_vmMap[hash] = vm;
}

void RegisterVMOperatorFunction(byte vm, const char* name, const char* usage, OPCode opcode, int flags, int minArgs, int maxArgs) {
Expand Down
11 changes: 11 additions & 0 deletions src/acts/tools/gsc_opcodes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ namespace tool::gsc::opcode {
void RegisterDevCall(byte vm, const char* devCall);
void RegisterDatatypeRenamed(byte vm, const char* datatype, const char* trueName);
void RegisterDatatype(byte vm, const char* datatype);
void RegisterVmName(byte vm, uint64_t hash);
void RegisterOpCodes();

inline void RegisterOpCode(byte vm, Platform platform, OPCode enumValue) {}
Expand All @@ -160,6 +161,16 @@ namespace tool::gsc::opcode {
RegisterDevCall(vm, calls...);
}

inline void RegisterVmName(byte vm, const char* name) {
RegisterVmName(vm, hash::Hash64(name));
}
inline void RegisterVmName(byte vm) {}
template<typename... Names>
inline void RegisterVmName(byte vm, const char* name, Names... names) {
RegisterVmName(vm, name);
RegisterVmName(vm, names...);
}


inline void RegisterDatatype(byte vm) {}
template<typename... Datatypes>
Expand Down
Loading

0 comments on commit f33a9ed

Please sign in to comment.