init commit

Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2025-10-15 17:41:37 +03:00
commit a78c6a3bb6
21 files changed
+1238

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This project uses @Incubating APIs which are subject to change.
*/
plugins {
`cpp-library`
}
library {
targetMachines.add(machines.linux.x86_64)
baseName.set(rootProject.name)
}
version = "1.0.0"
+115
View File
@@ -0,0 +1,115 @@
#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 << "[FabricPP] 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 << "[FabricPP] 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 << "[FabricPP] 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 << "[FabricPP] 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 << "[FabricPP] C++ Agent loaded successfully!" << std::endl;
return JNI_OK;
}
// Called from Java agent - NON-BLOCKING
extern "C" JNIEXPORT void JNICALL Java_org_faumaray_FabricPPAgent_nativeAgentStart(JNIEnv* env, jclass clazz) {
std::cout << "[FabricPP] Native agent started from Java" << std::endl;
// Don't do any heavy work here - just initialization
}
extern "C" JNIEXPORT void JNICALL Java_org_faumaray_FabricPPAgent_nativeAgentInit(JNIEnv* env, jclass clazz, jstring args, jobject instrumentation) {
std::cout << "[FabricPP] 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 << "[FabricPP] 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_FabricPPAgent_nativeCleanup(JNIEnv* env, jclass clazz) {
std::cout << "[FabricPP] 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 << "[FabricPP] 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, &copy);
if (error == JVMTI_ERROR_NONE && copy != nullptr) {
std::memcpy(copy, original_data, original_len);
*new_data = copy;
*new_len = original_len;
std::cout << "[FabricPP] Successfully copied class (no transformation applied)" << std::endl;
return true;
}
return false;
}
+16
View File
@@ -0,0 +1,16 @@
#include "jvmti_handler.h"
void JNICALL JvmtiHandler::vmInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
std::cout << "[FabricPP] 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 << "[FabricPP] Agent ready for class transformation" << std::endl;
}
void JNICALL JvmtiHandler::vmDeath(jvmtiEnv* jvmti, JNIEnv* jni) {
std::cout << "[FabricPP] VM Shutting down" << std::endl;
JvmtiHandler::setAgentInitialized(false);
}
+174
View File
@@ -0,0 +1,174 @@
#include "mod_manager.h"
#include <iostream>
#include <fstream>
#include <sstream>
ModManager::ModManager() {
std::cout << "[FabricPP] ModManager initialized" << std::endl;
}
ModManager::~ModManager() {
std::cout << "[FabricPP] ModManager destroyed" << std::endl;
}
// Discover mods in the mods directory
void ModManager::discoverMods(const std::string& mods_dir) {
namespace fs = std::filesystem;
std::cout << "[FabricPP] 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 << "[FabricPP] Created mods directory: " << mods_dir << std::endl;
// Create a README
std::ofstream readme(mods_dir + "/README.txt");
readme << "FabricPP 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 << "[FabricPP] Discovered " << mod_count << " mod file(s)" << std::endl;
std::cout << "[FabricPP] Loaded " << loaded_mods.size() << " mod(s) successfully" << std::endl;
// Print loaded mods
if (!loaded_mods.empty()) {
std::cout << "\n[FabricPP] 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 << "[FabricPP] 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 << "[FabricPP] 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 << "[FabricPP] Loaded: " << mod.name << " v" << mod.version << std::endl;
} catch (const std::exception& e) {
std::cerr << "[FabricPP] 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 << "[FabricPP] 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 << "[FabricPP] 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 << "[FabricPP] 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 << "[FabricPP] Mod '" << mod_name << "' "
<< (enabled ? "enabled" : "disabled") << std::endl;
return;
}
}
}
@@ -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);
};
+23
View File
@@ -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
};
+55
View File
@@ -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);
};