Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
12 files changed
+647
-17
No files matched your search
-15
@@ -2,21 +2,6 @@
|
||||
# Ignore build outputs from performing a nix-build or `nix build` command
|
||||
result
|
||||
result-*
|
||||
|
||||
# ---> VirtualEnv
|
||||
# Virtualenv
|
||||
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
|
||||
.Python
|
||||
[Bb]in
|
||||
[Ii]nclude
|
||||
[Ll]ib
|
||||
[Ll]ib64
|
||||
[Ll]ocal
|
||||
[Ss]cripts
|
||||
pyvenv.cfg
|
||||
.venv
|
||||
pip-selfcheck.json
|
||||
|
||||
# ---> C++
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
project(VoidLoaderCore LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Options
|
||||
# ----------------------------------------------------------
|
||||
option(BUILD_LINUX "Build shared lib for Linux" ON)
|
||||
option(BUILD_WINDOWS "Cross-compile shared lib for Windows" ON)
|
||||
set(VERSION "1.0.1")
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Locate JDK and JNI
|
||||
# ----------------------------------------------------------
|
||||
find_package(JNI REQUIRED)
|
||||
|
||||
if (JNI_FOUND)
|
||||
message(STATUS "✅ JNI include dirs: ${JNI_INCLUDE_DIRS}")
|
||||
include_directories(${JNI_INCLUDE_DIRS})
|
||||
else()
|
||||
message(FATAL_ERROR "❌ Could not locate JNI (ensure JAVA_HOME or JDK installed)")
|
||||
endif()
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Toolchain autodetection
|
||||
# ----------------------------------------------------------
|
||||
find_program(GCC_PATH g++)
|
||||
find_program(MINGW_PATH x86_64-w64-mingw32-g++)
|
||||
|
||||
message(STATUS "🔍 Toolchains detected:")
|
||||
message(STATUS " Linux → ${GCC_PATH}")
|
||||
message(STATUS " Win64 → ${MINGW_PATH}")
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Source structure
|
||||
# ----------------------------------------------------------
|
||||
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
||||
include_directories("include")
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Linux target
|
||||
# ----------------------------------------------------------
|
||||
if (BUILD_LINUX AND GCC_PATH)
|
||||
add_library(VoidLoader_Linux SHARED ${SOURCES})
|
||||
set_target_properties(VoidLoader_Linux PROPERTIES
|
||||
OUTPUT_NAME "VoidLoader"
|
||||
SUFFIX ".so"
|
||||
VERSION ${VERSION}
|
||||
)
|
||||
target_compile_options(VoidLoader_Linux PRIVATE -O2 -Wall -fPIC)
|
||||
target_include_directories(VoidLoader_Linux PRIVATE ${JNI_INCLUDE_DIRS})
|
||||
target_link_libraries(VoidLoader_Linux PRIVATE ${JNI_LIBRARIES})
|
||||
|
||||
# Post-build: bundle dependencies
|
||||
add_custom_command(TARGET VoidLoader_Linux POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -DINPUT_LIB=$<TARGET_FILE:VoidLoader_Linux>
|
||||
-DOUT_DIR=$<TARGET_FILE_DIR:VoidLoader_Linux>
|
||||
-P ${CMAKE_SOURCE_DIR}/cmake/bundle_linux.cmake
|
||||
)
|
||||
endif()
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Windows target
|
||||
# ----------------------------------------------------------
|
||||
if (BUILD_WINDOWS AND MINGW_PATH)
|
||||
add_library(VoidLoader_Windows SHARED ${SOURCES})
|
||||
set_target_properties(VoidLoader_Windows PROPERTIES
|
||||
OUTPUT_NAME "VoidLoader"
|
||||
SUFFIX ".dll"
|
||||
VERSION ${VERSION}
|
||||
)
|
||||
target_compile_options(VoidLoader_Windows PRIVATE
|
||||
-O2 -Wall -static-libstdc++ -static-libgcc
|
||||
)
|
||||
target_include_directories(VoidLoader_Windows PRIVATE ${JNI_INCLUDE_DIRS})
|
||||
|
||||
add_custom_command(TARGET VoidLoader_Windows POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -DINPUT_LIB=$<TARGET_FILE:VoidLoader_Windows>
|
||||
-DOUT_DIR=$<TARGET_FILE_DIR:VoidLoader_Windows>
|
||||
-P ${CMAKE_SOURCE_DIR}/cmake/bundle_windows.cmake
|
||||
)
|
||||
endif()
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Combined target
|
||||
# ----------------------------------------------------------
|
||||
add_custom_target(buildAll
|
||||
DEPENDS
|
||||
$<$<BOOL:${BUILD_LINUX}>:VoidLoader_Linux>
|
||||
$<$<BOOL:${BUILD_WINDOWS}>:VoidLoader_Windows>
|
||||
)
|
||||
|
||||
message(STATUS "✅ Configured VoidLoader JNI agent build")
|
||||
|
||||
Vendored
+2
-2
@@ -4,8 +4,8 @@ pipeline {
|
||||
stage('Build') {
|
||||
steps {
|
||||
sh 'rm -rf build'
|
||||
sh 'cmake -B build -S .'
|
||||
sh 'cmake --build build'
|
||||
sh 'cmake -B build -S . -DBUILD_WINDOWS=ON -DBUILD_LINUX=ON'
|
||||
sh 'cmake --build build --target buildAll'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
# Copy non-system dependencies using ldd
|
||||
execute_process(
|
||||
COMMAND ldd ${INPUT_LIB}
|
||||
OUTPUT_VARIABLE LDD_OUT
|
||||
)
|
||||
string(REGEX MATCHALL "\t[^ ]+ => (/[^ ]+)" MATCHES "${LDD_OUT}")
|
||||
|
||||
foreach(M ${MATCHES})
|
||||
string(REGEX REPLACE ".*=> " "" M_PATH ${M})
|
||||
if (NOT "${M_PATH}" MATCHES "^/usr/lib" AND NOT "${M_PATH}" MATCHES "^/lib")
|
||||
message(STATUS "📦 Copying ${M_PATH}")
|
||||
file(COPY ${M_PATH} DESTINATION ${OUT_DIR})
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# Copy referenced DLLs using objdump
|
||||
execute_process(
|
||||
COMMAND objdump -p ${INPUT_LIB}
|
||||
OUTPUT_VARIABLE OBJDUMP_OUT
|
||||
)
|
||||
string(REGEX MATCHALL "DLL Name: ([A-Za-z0-9_\\.-]+)" DLLS "${OBJDUMP_OUT}")
|
||||
|
||||
set(MINGW_ROOTS
|
||||
"/usr/x86_64-w64-mingw32/bin"
|
||||
"/usr/lib/gcc/x86_64-w64-mingw32/"
|
||||
)
|
||||
|
||||
foreach(DLL ${DLLS})
|
||||
string(REGEX REPLACE "DLL Name: " "" DLL_NAME ${DLL})
|
||||
foreach(ROOT ${MINGW_ROOTS})
|
||||
file(GLOB CAND "${ROOT}/${DLL_NAME}")
|
||||
if (CAND)
|
||||
message(STATUS "📦 Copying ${CAND}")
|
||||
file(COPY ${CAND} DESTINATION ${OUT_DIR})
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
endforeach()
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include <jvmti.h>
|
||||
#include <string>
|
||||
|
||||
class ClassTransformer {
|
||||
public:
|
||||
static void JNICALL classFileLoadHook(jvmtiEnv* jvmti, JNIEnv* jni,
|
||||
jclass class_being_redefined,
|
||||
jobject loader, const char* name,
|
||||
jobject protection_domain,
|
||||
jint class_data_len,
|
||||
const unsigned char* class_data,
|
||||
jint* new_class_data_len,
|
||||
unsigned char** new_class_data);
|
||||
|
||||
private:
|
||||
static bool shouldTransform(const std::string& class_name);
|
||||
static bool transformClass(jvmtiEnv* jvmti,
|
||||
const unsigned char* original_data,
|
||||
jint original_len,
|
||||
unsigned char** new_data,
|
||||
jint* new_len);
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include <jvmti.h>
|
||||
#include <jni.h>
|
||||
#include <iostream>
|
||||
|
||||
class JvmtiHandler {
|
||||
public:
|
||||
static void JNICALL vmInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread);
|
||||
static void JNICALL vmDeath(jvmtiEnv* jvmti, JNIEnv* jni);
|
||||
|
||||
// Accessor methods for global variables
|
||||
static jvmtiEnv* getJvmti();
|
||||
static JavaVM* getJavaVM();
|
||||
static bool isAgentInitialized();
|
||||
|
||||
// Setter methods
|
||||
static void setJvmti(jvmtiEnv* jvmti);
|
||||
static void setJavaVM(JavaVM* jvm);
|
||||
static void setAgentInitialized(bool initialized);
|
||||
|
||||
private:
|
||||
// No global variable declarations here - they'll be in agent.cpp
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
#include <jvmti.h>
|
||||
|
||||
enum class ModType {
|
||||
JAR, // Java/bytecode mod
|
||||
NATIVE // Native C++ mod
|
||||
};
|
||||
|
||||
struct ModInfo {
|
||||
std::string name;
|
||||
std::string version;
|
||||
std::string file_path;
|
||||
ModType type;
|
||||
bool enabled;
|
||||
|
||||
// Mod metadata (from fabric.mod.json or similar)
|
||||
std::string author;
|
||||
std::string description;
|
||||
std::vector<std::string> dependencies;
|
||||
};
|
||||
|
||||
class ModManager {
|
||||
public:
|
||||
ModManager();
|
||||
~ModManager();
|
||||
|
||||
// Discover and load mods from directory
|
||||
void discoverMods(const std::string& mods_dir);
|
||||
|
||||
// Load a single mod
|
||||
void loadMod(const std::filesystem::path& mod_path);
|
||||
|
||||
// Apply transformations from all mods to a class
|
||||
void applyTransformations(
|
||||
const std::string& class_name,
|
||||
const unsigned char* class_data,
|
||||
jint class_data_len
|
||||
);
|
||||
|
||||
// Get list of loaded mods
|
||||
const std::vector<ModInfo>& getLoadedMods() const;
|
||||
|
||||
// Enable/disable a mod
|
||||
void setModEnabled(const std::string& mod_name, bool enabled);
|
||||
|
||||
private:
|
||||
std::vector<ModInfo> loaded_mods;
|
||||
|
||||
// Parse mod metadata
|
||||
void parseJarMod(const std::filesystem::path& jar_path, ModInfo& mod);
|
||||
void parseNativeMod(const std::filesystem::path& native_path, ModInfo& mod);
|
||||
};
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
#include <jvmti.h>
|
||||
#include <jni.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "jvmti_handler.h"
|
||||
#include "class_transformer.h"
|
||||
#include "mod_manager.h"
|
||||
|
||||
// Global variables defined ONLY here
|
||||
namespace {
|
||||
jvmtiEnv* g_jvmti = nullptr;
|
||||
JavaVM* g_jvm = nullptr;
|
||||
bool g_agent_initialized = false;
|
||||
ModManager g_modManager;
|
||||
}
|
||||
|
||||
// Implementation of JvmtiHandler accessors
|
||||
jvmtiEnv* JvmtiHandler::getJvmti() {
|
||||
return g_jvmti;
|
||||
}
|
||||
|
||||
JavaVM* JvmtiHandler::getJavaVM() {
|
||||
return g_jvm;
|
||||
}
|
||||
|
||||
bool JvmtiHandler::isAgentInitialized() {
|
||||
return g_agent_initialized;
|
||||
}
|
||||
|
||||
void JvmtiHandler::setJvmti(jvmtiEnv* jvmti) {
|
||||
g_jvmti = jvmti;
|
||||
}
|
||||
|
||||
void JvmtiHandler::setJavaVM(JavaVM* jvm) {
|
||||
g_jvm = jvm;
|
||||
}
|
||||
|
||||
void JvmtiHandler::setAgentInitialized(bool initialized) {
|
||||
g_agent_initialized = initialized;
|
||||
}
|
||||
|
||||
// JVMTI Agent Entry Point
|
||||
extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
|
||||
std::cout << "[VoidLoader] C++ Agent_OnLoad called!" << std::endl;
|
||||
|
||||
JvmtiHandler::setJavaVM(vm);
|
||||
|
||||
// Get JVMTI environment
|
||||
jint result = vm->GetEnv(reinterpret_cast<void**>(&g_jvmti), JVMTI_VERSION_1_2);
|
||||
if (result != JNI_OK || g_jvmti == nullptr) {
|
||||
std::cerr << "[VoidLoader] ERROR: Unable to access JVMTI!" << std::endl;
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
JvmtiHandler::setJvmti(g_jvmti);
|
||||
|
||||
// Set JVMTI capabilities
|
||||
jvmtiCapabilities capabilities = {0};
|
||||
capabilities.can_retransform_classes = 1;
|
||||
capabilities.can_retransform_any_class = 1;
|
||||
capabilities.can_generate_all_class_hook_events = 1;
|
||||
capabilities.can_get_bytecodes = 1;
|
||||
|
||||
jvmtiError error = g_jvmti->AddCapabilities(&capabilities);
|
||||
if (error != JVMTI_ERROR_NONE) {
|
||||
std::cerr << "[VoidLoader] ERROR: Unable to set JVMTI capabilities!" << std::endl;
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
// Set event callbacks
|
||||
jvmtiEventCallbacks callbacks = {0};
|
||||
callbacks.ClassFileLoadHook = &ClassTransformer::classFileLoadHook;
|
||||
callbacks.VMInit = &JvmtiHandler::vmInit;
|
||||
callbacks.VMDeath = &JvmtiHandler::vmDeath;
|
||||
|
||||
error = g_jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
|
||||
if (error != JVMTI_ERROR_NONE) {
|
||||
std::cerr << "[VoidLoader] ERROR: Unable to set JVMTI callbacks!" << std::endl;
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
// Enable events
|
||||
g_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, nullptr);
|
||||
g_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, nullptr);
|
||||
g_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, nullptr);
|
||||
|
||||
std::cout << "[VoidLoader] C++ Agent loaded successfully!" << std::endl;
|
||||
return JNI_OK;
|
||||
}
|
||||
|
||||
// Called from Java agent - NON-BLOCKING
|
||||
extern "C" JNIEXPORT void JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentStart(JNIEnv* env, jclass clazz) {
|
||||
std::cout << "[VoidLoader] Native agent started from Java" << std::endl;
|
||||
// Don't do any heavy work here - just initialization
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentInit(JNIEnv* env, jclass clazz, jstring args, jobject instrumentation) {
|
||||
std::cout << "[VoidLoader] Native agent initialized with Instrumentation" << std::endl;
|
||||
// Initialize mod manager - but don't block
|
||||
g_modManager.discoverMods("mods");
|
||||
JavaVM* vm;
|
||||
auto error = env->GetJavaVM(&vm);
|
||||
if (error != JNI_OK) {
|
||||
std::cerr << "[VoidLoader] FATAL: Unable to get JavaVM from JNIEnv!" << std::endl;
|
||||
return;
|
||||
}
|
||||
Agent_OnLoad(vm, nullptr, nullptr);
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL Java_nativeAgentStart(JNIEnv* env, jclass clazz) {
|
||||
std::cout << "[VoidLoader] Native agent started from Java" << std::endl;
|
||||
// Don't do any heavy work here - just initialization
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL Java_nativeAgentInit(JNIEnv* env, jclass clazz, jstring args, jobject instrumentation) {
|
||||
std::cout << "[VoidLoader] Native agent initialized with Instrumentation" << std::endl;
|
||||
// Initialize mod manager - but don't block
|
||||
g_modManager.discoverMods("mods");
|
||||
JavaVM* vm;
|
||||
auto error = env->GetJavaVM(&vm);
|
||||
if (error != JNI_OK) {
|
||||
std::cerr << "[VoidLoader] FATAL: Unable to get JavaVM from JNIEnv!" << std::endl;
|
||||
return;
|
||||
}
|
||||
Agent_OnLoad(vm, nullptr, nullptr);
|
||||
}
|
||||
|
||||
// Cleanup native resources
|
||||
extern "C" JNIEXPORT void JNICALL Java_org_faumaray_VoidLoaderAgent_nativeCleanup(JNIEnv* env, jclass clazz) {
|
||||
std::cout << "[VoidLoader] Native cleanup called" << std::endl;
|
||||
// Cleanup any native resources if needed
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#include "class_transformer.h"
|
||||
#include "jvmti_handler.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <set>
|
||||
|
||||
// Only target specific Minecraft classes, exclude everything else
|
||||
static const std::set<std::string> TARGET_CLASSES = {
|
||||
"net/minecraft/client/Minecraft",
|
||||
"net/minecraft/client/main/Main",
|
||||
"net/minecraft/server/MinecraftServer"
|
||||
};
|
||||
|
||||
// Exclude all launcher and system classes
|
||||
static const std::set<std::string> EXCLUDED_PREFIXES = {
|
||||
"java/", "javax/", "sun/", "jdk/", "com/sun/",
|
||||
"org/prismlauncher/", "org/gtm/", "com/google/",
|
||||
"org/apache/", "org/slf4j/", "org/lwjgl/", "org/objectweb/",
|
||||
"io/", "okhttp3/", "okio/", "kotlin/", "scala/",
|
||||
"org/json/", "com/mojang/", "org/spongepowered/",
|
||||
"org/reflections/", "oshi/", "net/minecrell/", "org/checkerframework/"
|
||||
};
|
||||
|
||||
void JNICALL ClassTransformer::classFileLoadHook(jvmtiEnv* jvmti, JNIEnv* jni,
|
||||
jclass class_being_redefined,
|
||||
jobject loader, const char* name,
|
||||
jobject protection_domain,
|
||||
jint class_data_len,
|
||||
const unsigned char* class_data,
|
||||
jint* new_class_data_len,
|
||||
unsigned char** new_class_data) {
|
||||
|
||||
// Early return for null class name
|
||||
if (name == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string class_name(name);
|
||||
|
||||
// Don't transform system classes or launcher classes
|
||||
for (const auto& excluded_prefix : EXCLUDED_PREFIXES) {
|
||||
if (class_name.find(excluded_prefix) == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Only transform specific Minecraft classes
|
||||
if (TARGET_CLASSES.find(class_name) != TARGET_CLASSES.end()) {
|
||||
std::cout << "[VoidLoader] Transforming class: " << class_name << std::endl;
|
||||
transformClass(jvmti, class_data, class_data_len, new_class_data, new_class_data_len);
|
||||
}
|
||||
}
|
||||
|
||||
bool ClassTransformer::shouldTransform(const std::string& class_name) {
|
||||
return TARGET_CLASSES.find(class_name) != TARGET_CLASSES.end();
|
||||
}
|
||||
|
||||
bool ClassTransformer::transformClass(jvmtiEnv* jvmti,
|
||||
const unsigned char* original_data,
|
||||
jint original_len,
|
||||
unsigned char** new_data,
|
||||
jint* new_len) {
|
||||
if (jvmti == nullptr || original_data == nullptr || original_len == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// For now, just copy without modification to avoid issues
|
||||
unsigned char* copy = nullptr;
|
||||
jvmtiError error = jvmti->Allocate(original_len, ©);
|
||||
|
||||
if (error == JVMTI_ERROR_NONE && copy != nullptr) {
|
||||
std::memcpy(copy, original_data, original_len);
|
||||
*new_data = copy;
|
||||
*new_len = original_len;
|
||||
|
||||
std::cout << "[VoidLoader] Successfully copied class (no transformation applied)" << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include "jvmti_handler.h"
|
||||
|
||||
void JNICALL JvmtiHandler::vmInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
|
||||
std::cout << "[VoidLoader] VM Initialized - Minecraft starting soon" << std::endl;
|
||||
|
||||
JvmtiHandler::setJvmti(jvmti);
|
||||
JvmtiHandler::setAgentInitialized(true);
|
||||
|
||||
// The main application is now starting - our agent is ready
|
||||
std::cout << "[VoidLoader] Agent ready for class transformation" << std::endl;
|
||||
}
|
||||
|
||||
void JNICALL JvmtiHandler::vmDeath(jvmtiEnv* jvmti, JNIEnv* jni) {
|
||||
std::cout << "[VoidLoader] VM Shutting down" << std::endl;
|
||||
JvmtiHandler::setAgentInitialized(false);
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
#include "mod_manager.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
ModManager::ModManager() {
|
||||
std::cout << "[VoidLoader] ModManager initialized" << std::endl;
|
||||
}
|
||||
|
||||
ModManager::~ModManager() {
|
||||
std::cout << "[VoidLoader] ModManager destroyed" << std::endl;
|
||||
}
|
||||
|
||||
// Discover mods in the mods directory
|
||||
void ModManager::discoverMods(const std::string& mods_dir) {
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
std::cout << "[VoidLoader] Scanning for mods in: " << mods_dir << std::endl;
|
||||
|
||||
try {
|
||||
// Create mods directory if it doesn't exist
|
||||
if (!fs::exists(mods_dir)) {
|
||||
fs::create_directories(mods_dir);
|
||||
std::cout << "[VoidLoader] Created mods directory: " << mods_dir << std::endl;
|
||||
|
||||
// Create a README
|
||||
std::ofstream readme((std::filesystem::path(mods_dir) / "README.txt").string());
|
||||
|
||||
readme << "VoidLoader Mods Directory\n";
|
||||
readme << "=======================\n\n";
|
||||
readme << "Place your .jar mod files here.\n";
|
||||
readme << "The mod loader will automatically detect and load them.\n";
|
||||
readme.close();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Scan for mod files
|
||||
int mod_count = 0;
|
||||
for (const auto& entry : fs::directory_iterator(mods_dir)) {
|
||||
if (entry.is_regular_file()) {
|
||||
std::string ext = entry.path().extension().string();
|
||||
|
||||
// Support .jar mods (Java mods) and .dll/.so (native mods)
|
||||
if (ext == ".jar" || ext == ".dll" || ext == ".so") {
|
||||
loadMod(entry.path());
|
||||
mod_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "[VoidLoader] Discovered " << mod_count << " mod file(s)" << std::endl;
|
||||
std::cout << "[VoidLoader] Loaded " << loaded_mods.size() << " mod(s) successfully" << std::endl;
|
||||
|
||||
// Print loaded mods
|
||||
if (!loaded_mods.empty()) {
|
||||
std::cout << "\n[VoidLoader] Loaded Mods:" << std::endl;
|
||||
for (const auto& mod : loaded_mods) {
|
||||
std::cout << " - " << mod.name << " v" << mod.version
|
||||
<< " (" << mod.file_path << ")" << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "[VoidLoader] Error discovering mods: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Load a single mod
|
||||
void ModManager::loadMod(const std::filesystem::path& mod_path) {
|
||||
std::string filename = mod_path.filename().string();
|
||||
std::cout << "[VoidLoader] Loading mod: " << filename << std::endl;
|
||||
|
||||
try {
|
||||
ModInfo mod;
|
||||
mod.file_path = mod_path.string();
|
||||
mod.name = filename;
|
||||
mod.version = "unknown";
|
||||
mod.enabled = true;
|
||||
|
||||
// Parse mod metadata based on file type
|
||||
if (mod_path.extension() == ".jar") {
|
||||
parseJarMod(mod_path, mod);
|
||||
} else {
|
||||
// Native mod (.dll/.so)
|
||||
parseNativeMod(mod_path, mod);
|
||||
}
|
||||
|
||||
loaded_mods.push_back(mod);
|
||||
std::cout << "[VoidLoader] Loaded: " << mod.name << " v" << mod.version << std::endl;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "[VoidLoader] Failed to load mod " << filename << ": " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Parse JAR mod metadata (fabric.mod.json or similar)
|
||||
void ModManager::parseJarMod(const std::filesystem::path& jar_path, ModInfo& mod) {
|
||||
// TODO: Implement JAR parsing
|
||||
// This would:
|
||||
// 1. Open the JAR file as a ZIP
|
||||
// 2. Extract fabric.mod.json or mod.json
|
||||
// 3. Parse JSON for mod metadata
|
||||
// 4. Extract transformation rules/mixins
|
||||
|
||||
// For now, extract basic info from filename
|
||||
std::string filename = jar_path.stem().string();
|
||||
|
||||
// Try to parse version from filename (e.g., "modname-1.0.0.jar")
|
||||
size_t dash_pos = filename.find_last_of('-');
|
||||
if (dash_pos != std::string::npos) {
|
||||
mod.name = filename.substr(0, dash_pos);
|
||||
mod.version = filename.substr(dash_pos + 1);
|
||||
} else {
|
||||
mod.name = filename;
|
||||
}
|
||||
|
||||
mod.type = ModType::JAR;
|
||||
|
||||
std::cout << "[VoidLoader] Parsed JAR mod: " << mod.name << " v" << mod.version << std::endl;
|
||||
}
|
||||
|
||||
// Parse native mod metadata
|
||||
void ModManager::parseNativeMod(const std::filesystem::path& native_path, ModInfo& mod) {
|
||||
mod.name = native_path.stem().string();
|
||||
mod.type = ModType::NATIVE;
|
||||
|
||||
// TODO: Load the native library and query for metadata
|
||||
// This would dlopen/LoadLibrary the .dll/.so and call an exported function
|
||||
// like "getModInfo()" to retrieve metadata
|
||||
|
||||
std::cout << "[VoidLoader] Parsed native mod: " << mod.name << std::endl;
|
||||
}
|
||||
|
||||
// Apply transformations from mods to a class
|
||||
void ModManager::applyTransformations(
|
||||
const std::string& class_name,
|
||||
const unsigned char* class_data,
|
||||
jint class_data_len) {
|
||||
|
||||
// Check each mod for transformations targeting this class
|
||||
for (const auto& mod : loaded_mods) {
|
||||
if (!mod.enabled) continue;
|
||||
|
||||
// TODO: Check if mod has transformations for this class
|
||||
// This would:
|
||||
// 1. Look up transformation rules from mod metadata
|
||||
// 2. Check if any mixins target this class
|
||||
// 3. Queue transformations to be applied
|
||||
|
||||
// For now, just log
|
||||
if (class_name.find("net/minecraft/client/Minecraft") == 0) {
|
||||
std::cout << "[VoidLoader] Mod '" << mod.name << "' checking transformations for: "
|
||||
<< class_name << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get list of loaded mods
|
||||
const std::vector<ModInfo>& ModManager::getLoadedMods() const {
|
||||
return loaded_mods;
|
||||
}
|
||||
|
||||
// Enable/disable a mod
|
||||
void ModManager::setModEnabled(const std::string& mod_name, bool enabled) {
|
||||
for (auto& mod : loaded_mods) {
|
||||
if (mod.name == mod_name) {
|
||||
mod.enabled = enabled;
|
||||
std::cout << "[VoidLoader] Mod '" << mod_name << "' "
|
||||
<< (enabled ? "enabled" : "disabled") << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user