17 files changed
+451
-421
No files matched your search
+159
@@ -0,0 +1,159 @@
|
||||
#include <Logging/Level.hpp>
|
||||
#include <Logging/Logger.hpp>
|
||||
#include <Logging/LoggerManager.hpp>
|
||||
#include <Logging/LoggerRegister.hpp>
|
||||
#include <Mods/Loader.hpp>
|
||||
#include <Agent.hpp>
|
||||
#include <dlfcn.h>
|
||||
#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;
|
||||
}
|
||||
auto gameDir = args["gameDir"];
|
||||
auto dataDir = args["gameDir"] + std::filesystem::path::preferred_separator + "data";
|
||||
auto logDir = args["gameDir"] + std::filesystem::path::preferred_separator + "logs";
|
||||
auto modsDir = args["gameDir"] + std::filesystem::path::preferred_separator + "mods";
|
||||
m_context = {
|
||||
.gameDir = std::move(gameDir),
|
||||
.dataDir = std::move(dataDir),
|
||||
.logDir = std::move(logDir),
|
||||
.apiVersion = {1,0,0},
|
||||
};
|
||||
auto mods = findMods(modsDir);
|
||||
if (!mods.contains("VoidLoaderApi.vlmod"))
|
||||
{
|
||||
std::cerr << "FATAL: Could not locate API: mod loader is not loaded"<< std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
dlopen(mods.at("VoidLoaderApi.vlmod").c_str(), RTLD_NOW | RTLD_GLOBAL);
|
||||
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(), 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 = std::string(instance.m_context.logDir) + std::filesystem::path::preferred_separator + "VoidLoader";
|
||||
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 = &instance.m_transformer->classFileLoadHook;
|
||||
callbacks.VMInit = &instance.vmInit;
|
||||
callbacks.VMDeath = &instance.vmDeath;
|
||||
|
||||
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, instance.m_context);
|
||||
}
|
||||
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");
|
||||
}
|
||||
Reference in new issue
Block a user