Skip to content

Commit

Permalink
change vm registry pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ate47 committed Oct 30, 2024
1 parent 5821b32 commit 9921383
Show file tree
Hide file tree
Showing 17 changed files with 5,853 additions and 5,494 deletions.
10 changes: 4 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@
.antlr
scripts/antlr4.jar
.antlr4
*.aps

# Private stuff
src/mw23-dll/main.cpp
src/mw23-dll/main.hpp
src/mw23-dll/Main.cpp
src/mw23-dll/Main.hpp
src/acts/tools/ps4/*
src/acts/tools/sp23/gsc23_opcodes_load.cpp
src/acts/tools/sp23/gsc23_opcodes_load.hpp

*.aps

src/acts/tools/gsc_vm/vm_private_*.cpp
# VS
/.vs/
/.vscode/
Expand Down
38 changes: 7 additions & 31 deletions src/acts/tools/gsc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <decrypt.hpp>
#include <BS_thread_pool.hpp>
#include "tools/gsc.hpp"
#include "tools/gsc_vm.hpp"
#include "tools/gsc_opcode_nodes.hpp"
#include "tools/cw/cw.hpp"
#include "tools/gsc_acts_debug.hpp"
Expand Down Expand Up @@ -294,7 +295,9 @@ bool GscInfoOption::Compute(const char** args, INT startIndex, INT endIndex) {
}
}
if (!m_inputFiles.size()) {
m_inputFiles.push_back("scriptparsetree");
//m_inputFiles.push_back("scriptparsetree");
LOG_ERROR("No input param");
return false; // missing param
}
if (!m_dbgOutputDir) {
m_dbgOutputDir = m_outputDir;
Expand All @@ -303,6 +306,7 @@ bool GscInfoOption::Compute(const char** args, INT startIndex, INT endIndex) {
}

void GscInfoOption::PrintHelp() {
LOG_INFO("> gscd [options] (input)");
LOG_INFO("-h --help : Print help");
LOG_INFO("-g --gsc : Produce GSC");
LOG_INFO("-a --asm : Produce ASM");
Expand Down Expand Up @@ -739,32 +743,8 @@ int GSCOBJHandler::PatchCode(T8GSCOBJContext& ctx) {
void tool::gsc::GSCOBJHandler::DumpExperimental(std::ostream& asmout, const GscInfoOption& opt, T8GSCOBJContext& ctx) {
}

namespace {
#include "gsc_vm.hpp"
std::unordered_map<byte, std::function<std::shared_ptr<GSCOBJHandler>(byte*,size_t)>> gscReaders = {
{ VM_T831,[](byte* file, size_t fileSize) { return std::make_shared<T831GSCOBJHandler>(file, fileSize); }},
{ VM_T8,[](byte* file, size_t fileSize) { return std::make_shared<T8GSCOBJHandler>(file, fileSize); }},
{ VM_T937,[](byte* file, size_t fileSize) { return std::make_shared<T937GSCOBJHandler>(file, fileSize); }},
{ VM_T9,[](byte* file, size_t fileSize) { return std::make_shared<T9GSCOBJHandler>(file, fileSize); }},
{ VM_MW23,[](byte* file, size_t fileSize) { return std::make_shared<MW23GSCOBJHandler>(file, fileSize); }},
{ VM_MW23B,[](byte* file, size_t fileSize) { return std::make_shared<MW23BGSCOBJHandler>(file, fileSize); }},
{ VM_T7,[](byte* file, size_t fileSize) { return std::make_shared<T7GSCOBJHandler>(file, fileSize); }},
{ VM_T71B,[](byte* file, size_t fileSize) { return std::make_shared<T71BGSCOBJHandler>(file, fileSize); }},
{ VM_BO6_06,[](byte* file, size_t fileSize) { return std::make_shared<T10GSCOBJHandler>(file, fileSize); }},
{ VM_BO6_07,[](byte* file, size_t fileSize) { return std::make_shared<T1007GSCOBJHandler>(file, fileSize); }},
{ VM_BO6_0B,[](byte* file, size_t fileSize) { return std::make_shared<T100BGSCOBJHandler>(file, fileSize); }},
{ VM_BO6_0C,[](byte* file, size_t fileSize) { return std::make_shared<T100CGSCOBJHandler>(file, fileSize); }},
};
}

std::function<std::shared_ptr<GSCOBJHandler>(byte*, size_t)>* tool::gsc::GetGscReader(byte vm) {
auto it = gscReaders.find(vm);

if (it == gscReaders.end()) {
return nullptr;
}

return &it->second;
return tool::gsc::vm::GetGscReader(vm); // moved
}

struct H32T7GSCExportReader : GSCExportReader {
Expand Down Expand Up @@ -3372,10 +3352,6 @@ int tool::gsc::gscinfo(Process& proc, int argc, const char* argv[]) {
return ret;
}

static int gscd(Process& proc, int argc, const char* argv[]) {
return gscinfo(proc, argc, argv);
}

ADD_TOOL(gscinfo, "gsc", " --help", "GSC decompiler/disassembler", nullptr, gscinfo);
ADD_TOOL(gscd, "gsc", " --help", "GSC decompiler/disassembler", nullptr, gscd);
ADD_TOOL(gscd, "gsc", " --help", "GSC decompiler/disassembler", nullptr, gscinfo);
ADD_TOOL(dds, "gsc", " [input=scriptparsetree] [output=dataset.csv]", "dump dataset from gscinfo", nullptr, dumpdataset);
1,784 changes: 2 additions & 1,782 deletions src/acts/tools/gsc_opcodes_load.cpp

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions src/acts/tools/gsc_vm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <includes.hpp>
#include "gsc_vm.hpp"
#include <tools/gsc.hpp>
#include <tools/gsc_opcodes.hpp>
#include <decrypt.hpp>


namespace tool::gsc::vm {
namespace {
std::unordered_map<byte, GscVm*>& GscReaders() {
static std::unordered_map<byte, GscVm*> gscReaders{};
return gscReaders;
}
std::vector<GscVmOpCode*>& GscOpCodes() {
static std::vector<GscVmOpCode*> gscOpCodes{};
return gscOpCodes;
}
}

GscVm::GscVm(byte vm, std::function<std::shared_ptr<GSCOBJHandler>(byte*, size_t)> func) : vm(vm), func(func) {
GscReaders()[vm] = this;
}

GscVmOpCode::GscVmOpCode(const char* id, std::function<void()> func, bool priv) : func(func), id(id), priv(priv) {
GscOpCodes().emplace_back(this);
}

std::function<std::shared_ptr<GSCOBJHandler>(byte*, size_t)>* GetGscReader(byte vm) {
auto& gscReaders{ GscReaders() };
auto it = gscReaders.find(vm);

if (it == gscReaders.end()) {
return nullptr;
}

return &it->second->func;
}
void RegisterVmOpCodes() {
auto& opcodes{ GscOpCodes() };
if (alogs::getlevel() <= alogs::loglevel::LVL_TRACE) {
std::ostringstream oss{};

for (GscVmOpCode* opcode : opcodes) {
oss << " " << opcode->id;
}

alogs::log(alogs::LVL_TRACE, std::format("Registering opcodes for{}", oss.str()));
}

// register public
for (GscVmOpCode* opcode : opcodes) {
if (!opcode->priv) {
opcode->func();
}
}
// register private, this is a lazy way to fix private private opcodes vm depending on public vm
for (GscVmOpCode* opcode : opcodes) {
if (opcode->priv) {
opcode->func();
}
}
}
}
Loading

0 comments on commit 9921383

Please sign in to comment.