VoidLoader/VoidLoaderCore/pipeline/head This commit looks good
Signed-off-by: Faumaray <faumaray@gmail.com>
158 lines
5.9 KiB
C++
158 lines
5.9 KiB
C++
#include <Mods/LoadLibrary.hpp>
|
|
#include <Logging/Level.hpp>
|
|
#include <Logging/Logger.hpp>
|
|
#include <Logging/LoggerManager.hpp>
|
|
#include <Logging/LoggerRegister.hpp>
|
|
#include <Mods/Loader.hpp>
|
|
#include <Agent.hpp>
|
|
#include <filesystem>
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace vl::Agent {
|
|
|
|
Agent::Agent(std::unordered_map<std::string, std::string> args) : m_modloader(std::make_unique<vl::mod::ModLoader>())
|
|
{
|
|
auto currentPath = std::filesystem::current_path();
|
|
if (!args.contains("gameDir"))
|
|
{
|
|
args["gameDir"] = currentPath.string();
|
|
}
|
|
std::filesystem::path gameDir = args["gameDir"];
|
|
std::filesystem::path dataDir = gameDir / "data";
|
|
std::filesystem::path logDir = gameDir / "logs";
|
|
std::filesystem::path modsDir = gameDir / "mods";
|
|
m_context = {}; // if defaultable
|
|
m_context.gameDir = std::move(gameDir);
|
|
m_context.dataDir = std::move(dataDir);
|
|
m_context.logDir = std::move(logDir);
|
|
m_context.apiVersion = {1,0,0};
|
|
auto mods = findMods(modsDir.string());
|
|
if (!mods.contains("VoidLoaderApi.vlmod"))
|
|
{
|
|
std::cerr << "FATAL: Could not locate API: mod loader is not loaded"<< std::endl;
|
|
return;
|
|
}
|
|
|
|
vl::mod::LoadModLibrary(mods.at("VoidLoaderApi.vlmod").string(), true);
|
|
mods.erase("VoidLoaderApi.vlmod");
|
|
mods.erase("VoidLoader.vlmod");
|
|
|
|
m_mods = mods;
|
|
m_isValid = true;
|
|
}
|
|
|
|
void JNICALL Agent::vmInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread)
|
|
{
|
|
|
|
}
|
|
void JNICALL Agent::vmDeath(jvmtiEnv* jvmti, JNIEnv* jni)
|
|
{
|
|
Agent::getInstance().m_isValid = false;
|
|
}
|
|
|
|
std::unordered_map<std::string, std::filesystem::path> Agent::findMods(const std::string& dir)
|
|
{
|
|
std::unordered_map<std::string, std::filesystem::path> result;
|
|
for (auto& entry : std::filesystem::directory_iterator(dir)) {
|
|
if (entry.path().extension() == ".vlmod") {
|
|
result.emplace(entry.path().filename().string(), std::filesystem::absolute(entry.path()));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
extern "C" JNIEXPORT jint JNICALL Java_nativeAgentInit(JNIEnv* env, jclass clazz, jobjectArray args, jobject instrumentation)
|
|
{
|
|
int stringCount = env->GetArrayLength(args);
|
|
std::unordered_map<std::string, std::string> argsMap;
|
|
|
|
for (int i=0; i<stringCount; i+=2) {
|
|
jstring argString = (jstring) (env->GetObjectArrayElement(args, i));
|
|
auto arg = std::string(env->GetStringUTFChars(argString, 0));
|
|
arg.erase(0, 2);
|
|
jstring valString = (jstring) (env->GetObjectArrayElement(args, i+1));
|
|
const char *val = env->GetStringUTFChars(valString, 0);
|
|
argsMap.emplace(arg, val);
|
|
}
|
|
|
|
auto& instance = vl::Agent::Agent::getInstance(argsMap);
|
|
try {
|
|
auto coreLogDir = (instance.m_context.logDir / "VoidLoader").string();
|
|
vl::logging::init_voidloader_logger();
|
|
instance.m_logger = vl::logging::LoggerManager::instance().createCombined("VoidLoader", coreLogDir.c_str(), 512'000, vl::logging::Level::Trace);
|
|
vl::logging::LoggerManager::instance().createConsole("VoidLoader")->setLevel(vl::logging::Level::Info);
|
|
}
|
|
catch (const std::exception& ex) {
|
|
fprintf(stderr, "[VoidLoaderCore] Logger initialization failed: %s\n", ex.what());
|
|
return 1;
|
|
}
|
|
|
|
auto error = env->GetJavaVM(&(instance.m_jvm));
|
|
if (error != JNI_OK) {
|
|
instance.m_logger->error("Unable to get JavaVM from JNIEnv");
|
|
return 1;
|
|
}
|
|
jint result = instance.m_jvm->GetEnv(reinterpret_cast<void**>(&(instance.m_jmti)), JVMTI_VERSION_21);
|
|
if (result != JNI_OK || instance.m_jmti == nullptr) {
|
|
instance.m_logger->error("Unable to access JVMTI");
|
|
return JNI_ERR;
|
|
}
|
|
instance.m_jni = env;
|
|
|
|
jvmtiEventCallbacks callbacks = {0};
|
|
callbacks.ClassFileLoadHook = &ClassTransformer::classFileLoadHook; // provide a static thunk
|
|
callbacks.VMInit = &vl::Agent::Agent::vmInit; // static
|
|
callbacks.VMDeath = &vl::Agent::Agent::vmDeath; // static
|
|
|
|
auto cerror = instance.m_jmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
|
|
if (cerror != JVMTI_ERROR_NONE) {
|
|
instance.m_logger->error("Unable to set JVMTI callbacks!");
|
|
return JNI_ERR;
|
|
}
|
|
|
|
// Enable events
|
|
instance.m_jmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, nullptr);
|
|
instance.m_jmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, nullptr);
|
|
instance.m_jmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, nullptr);
|
|
|
|
|
|
instance.m_logger->info("VoidLoader loaded");
|
|
instance.m_logger->info("Logging initialized");
|
|
|
|
return 0;
|
|
}
|
|
extern "C" JNIEXPORT jint JNICALL Java_nativeAgentStart(JNIEnv* env, jclass clazz)
|
|
{
|
|
auto& instance = vl::Agent::Agent::getInstance();
|
|
instance.m_logger->info("Loading mods...");
|
|
auto loaded = 0;
|
|
|
|
for (auto& [name, path] : instance.m_mods)
|
|
{
|
|
instance.m_logger->trace("Loading mod: {}", name);
|
|
loaded += instance.m_modloader->loadModule(path.string(), instance.m_context, false);
|
|
}
|
|
instance.m_logger->info("Mods loaded: {}, Starting", loaded);
|
|
|
|
instance.m_modloader->startAll();
|
|
return 0;
|
|
}
|
|
extern "C" JNIEXPORT jint JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentStart(JNIEnv* env, jclass clazz)
|
|
{
|
|
return Java_nativeAgentStart(env, clazz);
|
|
}
|
|
extern "C" JNIEXPORT jint JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentInit(JNIEnv* env, jclass jclass, jobject instrumentation, jobjectArray args)
|
|
{
|
|
return Java_nativeAgentInit(env,jclass,args,instrumentation);
|
|
}
|
|
|
|
extern "C" JNIEXPORT void JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeCleanup(JNIEnv* env, jclass jclass)
|
|
{
|
|
auto& instance = vl::Agent::Agent::getInstance();
|
|
instance.m_logger->info("Clearing up...");
|
|
instance.m_modloader->stopAll();
|
|
instance.m_logger->info("Mods stopped");
|
|
}
|