Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f5e3d0aabb | ||
|
|
aa434a233c | ||
|
|
d377702c17 | ||
|
|
71bfa7b32d | ||
|
|
df54abb9ad | ||
|
|
8588f10ea0 | ||
|
|
4ee04b3e7b | ||
|
|
fff614c871 | ||
|
|
ff3dc58cd7 | ||
|
|
29a095639f | ||
|
|
742ab14fdd | ||
|
|
a0b5c64a84 | ||
|
|
30dbdc93c5 | ||
|
|
e9af2df47a | ||
|
|
01ee9d7a38 | ||
|
|
3338f2ace8 | ||
|
|
2506a33f7d | ||
|
|
6ea34bf00b | ||
|
|
de940dab27 | ||
|
|
65b91ff8b2 | ||
|
|
de538e00b6 | ||
|
|
c9711dc5fd | ||
|
|
8c4bcf125b |
No files matched your search
Executable
+19
@@ -0,0 +1,19 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -e
|
||||||
|
if [[ ! -d "/home/faumaray/Project/personal/VoidLoaderCore" ]]; then
|
||||||
|
echo "Cannot find source directory; Did you move it?"
|
||||||
|
echo "(Looking for "/home/faumaray/Project/personal/VoidLoaderCore")"
|
||||||
|
echo 'Cannot force reload with this script - use "direnv reload" manually and then try again'
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# rebuild the cache forcefully
|
||||||
|
_nix_direnv_force_reload=1 direnv exec "/home/faumaray/Project/personal/VoidLoaderCore" true
|
||||||
|
|
||||||
|
# Update the mtime for .envrc.
|
||||||
|
# This will cause direnv to reload again - but without re-building.
|
||||||
|
touch "/home/faumaray/Project/personal/VoidLoaderCore/.envrc"
|
||||||
|
|
||||||
|
# Also update the timestamp of whatever profile_rc we have.
|
||||||
|
# This makes sure that we know we are up to date.
|
||||||
|
touch -r "/home/faumaray/Project/personal/VoidLoaderCore/.envrc" "/home/faumaray/Project/personal/VoidLoaderCore/.direnv"/*.rc
|
||||||
+3
-2
@@ -5,13 +5,14 @@ result-*
|
|||||||
# ---> C++
|
# ---> C++
|
||||||
# Prerequisites
|
# Prerequisites
|
||||||
*.d
|
*.d
|
||||||
|
.direnv
|
||||||
|
.direnv/*
|
||||||
# Compiled Object files
|
# Compiled Object files
|
||||||
*.slo
|
*.slo
|
||||||
*.lo
|
*.lo
|
||||||
*.o
|
*.o
|
||||||
*.obj
|
*.obj
|
||||||
|
build
|
||||||
# Precompiled Headers
|
# Precompiled Headers
|
||||||
*.gch
|
*.gch
|
||||||
*.pch
|
*.pch
|
||||||
|
|||||||
+31
-6
@@ -4,10 +4,14 @@ project(VoidLoader LANGUAGES CXX)
|
|||||||
# ============================================================
|
# ============================================================
|
||||||
# Project settings
|
# Project settings
|
||||||
# ============================================================
|
# ============================================================
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
|
set(BUILD_SHARED_LIBS OFF)
|
||||||
|
include(FetchContent)
|
||||||
|
|
||||||
|
add_compile_options(-ffunction-sections -fdata-sections)
|
||||||
|
add_link_options(-Wl,--gc-sections)
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# Source / includes
|
# Source / includes
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@@ -33,6 +37,14 @@ if (DEFINED ENV{JAVA_HOME})
|
|||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
FetchContent_Declare(
|
||||||
|
VoidLoaderApi
|
||||||
|
GIT_REPOSITORY https://git.faumaray.ru/VoidLoader/VoidLoaderApi.git
|
||||||
|
GIT_TAG main # release-1.10.0
|
||||||
|
)
|
||||||
|
|
||||||
|
FetchContent_MakeAvailable(VoidLoaderApi)
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# Platform flags
|
# Platform flags
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@@ -42,29 +54,42 @@ else()
|
|||||||
add_compile_options(-fPIC)
|
add_compile_options(-fPIC)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if (MINGW OR WIN32)
|
||||||
|
list(FILTER SOURCES EXCLUDE REGEX "LoadLibraryUnix\\.cpp$")
|
||||||
|
else()
|
||||||
|
list(FILTER SOURCES EXCLUDE REGEX "LoadLibraryWin\\.cpp$")
|
||||||
|
endif()
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# Build shared library
|
# Build shared library
|
||||||
# ============================================================
|
# ============================================================
|
||||||
add_library(${PROJECT_NAME} SHARED ${SOURCES})
|
add_library(${PROJECT_NAME} SHARED ${SOURCES})
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME} PRIVATE ${JNI_INCLUDE_DIRS})
|
target_include_directories(${PROJECT_NAME} PUBLIC ${JNI_INCLUDE_DIRS})
|
||||||
target_link_libraries(${PROJECT_NAME} PRIVATE ${JNI_LIBRARIES})
|
target_link_libraries(${PROJECT_NAME} PUBLIC JNI::JNI JNI::JVM VoidLoaderApiInterface)
|
||||||
|
|
||||||
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
|
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
|
||||||
target_link_options(${PROJECT_NAME} PRIVATE -static-libstdc++ -static-libgcc)
|
target_link_options(${PROJECT_NAME} PRIVATE
|
||||||
|
-Wl,--no-undefined
|
||||||
|
-Wl,--as-needed
|
||||||
|
-static-libstdc++ # pull libstdc++ into .so
|
||||||
|
-static-libgcc # pull libgcc into .so
|
||||||
|
-Wl,-O1
|
||||||
|
-Wl,--gc-sections
|
||||||
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|
||||||
# Output name: YourAgent.dll / libYourAgent.so
|
# Output name: YourAgent.dll / libYourAgent.so
|
||||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||||
PREFIX ""
|
PREFIX ""
|
||||||
|
SUFFIX ".vlmod"
|
||||||
OUTPUT_NAME "VoidLoader"
|
OUTPUT_NAME "VoidLoader"
|
||||||
)
|
)
|
||||||
|
|
||||||
if(UNIX AND NOT APPLE)
|
if(UNIX AND NOT APPLE)
|
||||||
target_link_options(${PROJECT_NAME} PRIVATE -Wl,--enable-new-dtags -Wl,-rpath,\$ORIGIN)
|
target_link_options(${PROJECT_NAME} PRIVATE -Wl,--enable-new-dtags -Wl,-rpath,\$ORIGIN)
|
||||||
set_target_properties(${PROJECT_NAME} PROPERTIES BUILD_RPATH "\$ORIGIN" INSTALL_RPATH "\$ORIGIN")
|
set_target_properties(${PROJECT_NAME} PROPERTIES BUILD_RPATH "\$ORIGIN" INSTALL_RPATH "\$ORIGIN")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# Dep bundler (Option B: auto-scan & copy non-system deps)
|
# Dep bundler (Option B: auto-scan & copy non-system deps)
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|||||||
Vendored
+84
-43
@@ -1,87 +1,128 @@
|
|||||||
pipeline {
|
pipeline {
|
||||||
agent any
|
agent {
|
||||||
|
docker { image 'faumaray/jdk-mingw-cpp:latest' }
|
||||||
|
}
|
||||||
|
|
||||||
|
parameters {
|
||||||
|
string(name: 'TAG_NAME', defaultValue: 'latest', description: 'Tag name for Gitea release')
|
||||||
|
string(name: 'RELEASE_NAME', defaultValue: 'latest', description: 'Release name for Gitea release')
|
||||||
|
}
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
stage('Build-Linux') {
|
stage('Build-Linux') {
|
||||||
tools {
|
tools { jdk 'JDK21' }
|
||||||
jdk 'JDK21'
|
|
||||||
}
|
|
||||||
steps {
|
steps {
|
||||||
sh 'java -version'
|
sh '''
|
||||||
sh 'echo $JAVA_HOME'
|
java -version
|
||||||
sh 'rm -rf build/linux'
|
echo $JAVA_HOME
|
||||||
sh 'mkdir -p build/linux'
|
rm -rf build/linux
|
||||||
sh 'cmake -B build/linux -DCMAKE_BUILD_TYPE=Release'
|
mkdir -p build/linux
|
||||||
sh 'cmake --build build/linux'
|
cmake -B build/linux -DCMAKE_BUILD_TYPE=Release
|
||||||
|
cmake --build build/linux
|
||||||
|
'''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Build-Windows') {
|
stage('Build-Windows') {
|
||||||
tools {
|
tools { jdk 'JDK21DOCKERWIN' }
|
||||||
jdk 'JDK21-WIN'
|
|
||||||
}
|
|
||||||
steps {
|
steps {
|
||||||
sh 'java -version'
|
sh '''
|
||||||
sh 'echo $JAVA_HOME'
|
export PATH="$JAVA_HOME/bin:$PATH"
|
||||||
sh 'rm -rf build/windows'
|
java -version
|
||||||
sh 'mkdir -p build/windows'
|
echo $JAVA_HOME
|
||||||
sh 'cmake -B build/windows -DCMAKE_TOOLCHAIN_FILE=mingw.cmake -DCMAKE_BUILD_TYPE=Release'
|
rm -rf build/windows
|
||||||
sh 'cmake --build build/windows'
|
mkdir -p build/windows
|
||||||
|
cmake -B build/windows -DCMAKE_TOOLCHAIN_FILE=mingw.cmake -DCMAKE_BUILD_TYPE=Release
|
||||||
|
cmake --build build/windows
|
||||||
|
'''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Package-Artifacts') {
|
stage('Package-Artifacts') {
|
||||||
steps {
|
steps {
|
||||||
sh '''
|
sh '''
|
||||||
rm -rf dist
|
rm -rf dist
|
||||||
mkdir -p dist/native/windows
|
rm -rf VoidLoader.zip
|
||||||
mkdir -p dist/native/linux
|
mkdir -p dist/native/windows dist/native/linux
|
||||||
|
|
||||||
# copy linux .so and .so.* files
|
# collect libs
|
||||||
find build/linux -type f -name "*.so*" -exec cp {} dist/native/linux/ \\;
|
find build/linux -type f -name "*.vlmod" -exec cp {} dist/native/linux/ \\;
|
||||||
|
find build/windows -type f -name "*.vlmod" -exec cp {} dist/native/windows/ \\;
|
||||||
|
|
||||||
# copy windows dll files
|
|
||||||
find build/windows -type f -name "*.dll" -exec cp {} dist/native/windows/ \\;
|
|
||||||
|
|
||||||
# create zip
|
|
||||||
cd dist
|
cd dist
|
||||||
zip -r ../VoidLoader.zip native
|
zip -r ../VoidLoader.zip native
|
||||||
'''
|
'''
|
||||||
}
|
}
|
||||||
|
|
||||||
post {
|
post {
|
||||||
success {
|
success {
|
||||||
archiveArtifacts artifacts: 'VoidLoader.zip', fingerprint: true
|
archiveArtifacts artifacts: 'VoidLoader.zip', fingerprint: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Publish-To-Gitea') {
|
stage('Publish-To-Gitea') {
|
||||||
steps {
|
steps {
|
||||||
withCredentials([string(credentialsId: 'GITEA_TOKEN', variable: 'GITEA_TOKEN')]) {
|
withCredentials([string(credentialsId: 'GITEA_TOKEN', variable: 'GITEA_TOKEN')]) {
|
||||||
sh '''
|
sh '''
|
||||||
|
set -e
|
||||||
GITEA_URL="https://git.faumaray.ru"
|
GITEA_URL="https://git.faumaray.ru"
|
||||||
REPO="VoidLoader/VoidLoaderCore"
|
REPO="VoidLoader/VoidLoaderCore"
|
||||||
TAG="build-${BUILD_NUMBER}"
|
TAG="${TAG_NAME:-latest}"
|
||||||
|
RELEASE="${RELEASE_NAME:-latest}"
|
||||||
|
|
||||||
# Create release if it does not exist
|
echo "📦 Checking release: $RELEASE ($TAG)"
|
||||||
curl -X POST -H "Authorization: token $GITEA_TOKEN" \
|
|
||||||
|
# Query existing release (if any)
|
||||||
|
EXISTING_RELEASE=$(curl -s -H "Authorization: token $GITEA_TOKEN" \
|
||||||
|
"$GITEA_URL/api/v1/repos/$REPO/releases/tags/$TAG" || true)
|
||||||
|
|
||||||
|
RELEASE_ID=$(echo "$EXISTING_RELEASE" | jq -r '.id // empty')
|
||||||
|
PRERELEASE=$(echo "$EXISTING_RELEASE" | jq -r '.prerelease // false')
|
||||||
|
|
||||||
|
if [ -n "$RELEASE_ID" ] && [ "$PRERELEASE" = "true" ]; then
|
||||||
|
echo "⚠️ Existing prerelease found (ID $RELEASE_ID), removing..."
|
||||||
|
|
||||||
|
# Delete release
|
||||||
|
curl -s -X DELETE -H "Authorization: token $GITEA_TOKEN" \
|
||||||
|
"$GITEA_URL/api/v1/repos/$REPO/releases/$RELEASE_ID" >/dev/null || true
|
||||||
|
|
||||||
|
# Delete tag (safe to ignore if not present)
|
||||||
|
curl -s -X DELETE -H "Authorization: token $GITEA_TOKEN" \
|
||||||
|
"$GITEA_URL/api/v1/repos/$REPO/tags/$TAG" >/dev/null || true
|
||||||
|
|
||||||
|
echo "🧹 Old prerelease removed."
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "🚀 Creating new release..."
|
||||||
|
|
||||||
|
CREATE_RESPONSE=$(curl -s -X POST -H "Authorization: token $GITEA_TOKEN" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "{\\"tag_name\\": \\"$TAG\\", \\"name\\": \\"$TAG\\"}" \
|
-d "{
|
||||||
$GITEA_URL/api/v1/repos/$REPO/releases || true
|
\\"tag_name\\": \\"$TAG\\",
|
||||||
|
\\"name\\": \\"$RELEASE\\",
|
||||||
|
\\"prerelease\\": true
|
||||||
|
}" \
|
||||||
|
"$GITEA_URL/api/v1/repos/$REPO/releases")
|
||||||
|
|
||||||
# Get release ID
|
RELEASE_ID=$(echo "$CREATE_RESPONSE" | jq -r '.id')
|
||||||
RELEASE_ID=$(curl -s -H "Authorization: token $GITEA_TOKEN" \
|
if [ -z "$RELEASE_ID" ] || [ "$RELEASE_ID" = "null" ]; then
|
||||||
"$GITEA_URL/api/v1/repos/$REPO/releases/tags/$TAG" | jq -r '.id')
|
echo "❌ Failed to create release!"
|
||||||
|
echo "$CREATE_RESPONSE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Release ID = $RELEASE_ID"
|
echo "✅ New release created (ID $RELEASE_ID)"
|
||||||
|
|
||||||
# Upload asset
|
# Upload artifact
|
||||||
curl -X POST -H "Authorization: token $GITEA_TOKEN" \
|
echo "⬆️ Uploading VoidLoader.zip ..."
|
||||||
|
curl -s -X POST -H "Authorization: token $GITEA_TOKEN" \
|
||||||
-F "attachment=@VoidLoader.zip" \
|
-F "attachment=@VoidLoader.zip" \
|
||||||
"$GITEA_URL/api/v1/repos/$REPO/releases/$RELEASE_ID/assets"
|
"$GITEA_URL/api/v1/repos/$REPO/releases/$RELEASE_ID/assets" >/dev/null
|
||||||
|
|
||||||
|
echo "🎉 Upload complete: $GITEA_URL/$REPO/releases/tag/$TAG"
|
||||||
'''
|
'''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Generated
+61
@@ -0,0 +1,61 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"flake-utils": {
|
||||||
|
"inputs": {
|
||||||
|
"systems": "systems"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1731533236,
|
||||||
|
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1760524057,
|
||||||
|
"narHash": "sha256-EVAqOteLBFmd7pKkb0+FIUyzTF61VKi7YmvP1tw4nEw=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "544961dfcce86422ba200ed9a0b00dd4b1486ec5",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-utils": "flake-utils",
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"systems": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1681028828,
|
||||||
|
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
{
|
||||||
|
description = "Gradle C++ cross-compile (Linux → Windows/Mac)";
|
||||||
|
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||||
|
flake-utils.url = "github:numtide/flake-utils";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs =
|
||||||
|
{
|
||||||
|
self,
|
||||||
|
nixpkgs,
|
||||||
|
flake-utils,
|
||||||
|
}:
|
||||||
|
flake-utils.lib.eachDefaultSystem (
|
||||||
|
system:
|
||||||
|
let
|
||||||
|
pkgs = import nixpkgs { inherit system; };
|
||||||
|
in
|
||||||
|
{
|
||||||
|
devShells.default = pkgs.mkShell {
|
||||||
|
name = "CppShell";
|
||||||
|
|
||||||
|
nativeBuildInputs = with pkgs; [
|
||||||
|
gcc15Stdenv
|
||||||
|
gcc15
|
||||||
|
ninja
|
||||||
|
jdk
|
||||||
|
cmake
|
||||||
|
clang-tools
|
||||||
|
];
|
||||||
|
|
||||||
|
buildInputs = with pkgs; [
|
||||||
|
];
|
||||||
|
|
||||||
|
shellHook = '''';
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <class_transformer.h>
|
||||||
|
#include <Logging/Logger.hpp>
|
||||||
|
#include <Mod/Context.hpp>
|
||||||
|
#include <Mods/Loader.hpp>
|
||||||
|
#include <atomic>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <jvmti.h>
|
||||||
|
#include <jni.h>
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
JNIEXPORT jint JNICALL Java_nativeAgentInit(JNIEnv*, jclass, jobjectArray, jobject);
|
||||||
|
JNIEXPORT jint JNICALL Java_nativeAgentStart(JNIEnv*, jclass);
|
||||||
|
JNIEXPORT jint JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentStart(JNIEnv*, jclass);
|
||||||
|
JNIEXPORT jint JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentInit(JNIEnv*, jclass, jobject, jobjectArray);
|
||||||
|
JNIEXPORT void JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeCleanup(JNIEnv*, jclass);
|
||||||
|
}
|
||||||
|
namespace vl::Agent {
|
||||||
|
|
||||||
|
class Agent {
|
||||||
|
public:
|
||||||
|
static void JNICALL vmInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread);
|
||||||
|
static void JNICALL vmDeath(jvmtiEnv* jvmti, JNIEnv* jni);
|
||||||
|
|
||||||
|
static Agent& getInstance(const std::unordered_map<std::string, std::string>& args) {
|
||||||
|
std::call_once(initFlag_, [&] {
|
||||||
|
instance_.reset(new Agent(args));
|
||||||
|
});
|
||||||
|
return *instance_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Accessor that just retrieves the existing instance
|
||||||
|
static Agent& getInstance() {
|
||||||
|
if (!instance_) {
|
||||||
|
throw std::runtime_error("Agent not initialized — call getInstance(args) first.");
|
||||||
|
}
|
||||||
|
return *instance_;
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit Agent(std::unordered_map<std::string, std::string> args);
|
||||||
|
|
||||||
|
Agent(const Agent&) = delete;
|
||||||
|
Agent& operator=(const Agent&) = delete;
|
||||||
|
Agent(Agent&&) = delete;
|
||||||
|
Agent& operator=(Agent&&) = delete;
|
||||||
|
|
||||||
|
bool isValid() const noexcept
|
||||||
|
{
|
||||||
|
return m_isValid;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unordered_map<std::string, std::filesystem::path> findMods(const std::string& dir);
|
||||||
|
friend jint ::Java_nativeAgentInit(JNIEnv*, jclass, jobjectArray, jobject);
|
||||||
|
friend jint ::Java_nativeAgentStart(JNIEnv*, jclass);
|
||||||
|
friend jint ::Java_ru_faumaray_VoidLoaderAgent_nativeAgentStart(JNIEnv*, jclass);
|
||||||
|
friend jint ::Java_ru_faumaray_VoidLoaderAgent_nativeAgentInit(JNIEnv*, jclass, jobject, jobjectArray);
|
||||||
|
friend void ::Java_ru_faumaray_VoidLoaderAgent_nativeCleanup(JNIEnv*, jclass);
|
||||||
|
friend class ClassTransformer;
|
||||||
|
friend class vl::mod::ModLoader;
|
||||||
|
private:
|
||||||
|
std::unordered_map<std::string, std::filesystem::path> m_mods;
|
||||||
|
jvmtiEnv* m_jmti = nullptr;
|
||||||
|
JavaVM* m_jvm = nullptr;
|
||||||
|
JNIEnv* m_jni = nullptr;
|
||||||
|
std::unique_ptr<vl::mod::ModLoader> m_modloader;
|
||||||
|
std::unique_ptr<ClassTransformer> m_transformer;
|
||||||
|
vl::mod::Context m_context;
|
||||||
|
std::atomic<bool> m_isValid{false};
|
||||||
|
std::shared_ptr<vl::logging::Logger> m_logger;
|
||||||
|
static std::unique_ptr<Agent> instance_;
|
||||||
|
static std::once_flag initFlag_;
|
||||||
|
};
|
||||||
|
inline std::unique_ptr<Agent> Agent::instance_ = nullptr;
|
||||||
|
inline std::once_flag Agent::initFlag_;
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Logging/Sink.hpp>
|
||||||
|
#include <Logging/Message.hpp>
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <thread>
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
#include <queue>
|
||||||
|
#include <stop_token>
|
||||||
|
|
||||||
|
namespace vl::logging {
|
||||||
|
|
||||||
|
class AsyncWorker {
|
||||||
|
public:
|
||||||
|
static AsyncWorker& instance();
|
||||||
|
|
||||||
|
struct Item {
|
||||||
|
std::shared_ptr<Sink> sink;
|
||||||
|
Message msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
void enqueue(Item&& item);
|
||||||
|
|
||||||
|
private:
|
||||||
|
AsyncWorker();
|
||||||
|
~AsyncWorker();
|
||||||
|
void run(std::stop_token st);
|
||||||
|
|
||||||
|
std::mutex mtx_;
|
||||||
|
std::condition_variable_any cv_;
|
||||||
|
std::queue<Item> queue_;
|
||||||
|
std::jthread thread_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Logging/Sink.hpp>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace vl::logging {
|
||||||
|
|
||||||
|
class CombinedSink final : public Sink {
|
||||||
|
public:
|
||||||
|
CombinedSink(std::shared_ptr<Sink> a, std::shared_ptr<Sink> b)
|
||||||
|
: a_(std::move(a)), b_(std::move(b)) {}
|
||||||
|
|
||||||
|
void write(const Message& msg) override {
|
||||||
|
a_->write(msg);
|
||||||
|
b_->write(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<Sink> a_, b_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Logging/Sink.hpp>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
namespace vl::logging {
|
||||||
|
|
||||||
|
class ConsoleSink final : public Sink {
|
||||||
|
public:
|
||||||
|
void write(const Message& msg) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::mutex coutMutex_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Logging/Sink.hpp>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace vl::logging {
|
||||||
|
|
||||||
|
// File rotation: R3 (daily + index), plus optional size cap
|
||||||
|
class FileSink final : public Sink {
|
||||||
|
public:
|
||||||
|
explicit FileSink(std::string basePath, std::uintmax_t maxBytes);
|
||||||
|
|
||||||
|
void write(const Message& msg) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string base_;
|
||||||
|
std::uintmax_t maxBytes_;
|
||||||
|
std::ofstream out_;
|
||||||
|
std::string currentDate_; // YYYY-MM-DD
|
||||||
|
int index_{1};
|
||||||
|
std::uintmax_t written_{0};
|
||||||
|
|
||||||
|
void ensureOpen(const Message& msg);
|
||||||
|
void openCurrent();
|
||||||
|
void rotateToDate(std::string newDate);
|
||||||
|
|
||||||
|
static std::string dateOf(const std::chrono::system_clock::time_point& tp);
|
||||||
|
static std::string filenameFor(const std::string& base, std::string_view date, int index);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Logging/Logger.hpp>
|
||||||
|
#include <Logging/Sink.hpp>
|
||||||
|
#include <Logging/AsyncWorker.hpp>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace vl::logging {
|
||||||
|
|
||||||
|
class LoggerInternal final : public Logger {
|
||||||
|
public:
|
||||||
|
LoggerInternal(std::string prefix, std::shared_ptr<Sink> sink, Level lvl)
|
||||||
|
: prefix_(std::move(prefix)), sink_(std::move(sink)), level_(lvl) {}
|
||||||
|
|
||||||
|
std::string_view prefix() const noexcept override { return prefix_; }
|
||||||
|
void setLevel(Level l) noexcept override { level_ = l; }
|
||||||
|
//FIXME: make possible to override rotation
|
||||||
|
void setMaxBytes(std::uintmax_t bytes) noexcept override { };
|
||||||
|
Level level() const noexcept override { return level_; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void enqueue(Message&& m) override {
|
||||||
|
AsyncWorker::instance().enqueue({ sink_, std::move(m) });
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string prefix_;
|
||||||
|
std::shared_ptr<Sink> sink_;
|
||||||
|
Level level_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Logging/Provider.hpp>
|
||||||
|
#include <Logging/Sink.hpp>
|
||||||
|
#include <mutex>
|
||||||
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
namespace vl::logging {
|
||||||
|
|
||||||
|
class LoggerProvider final : public Provider {
|
||||||
|
public:
|
||||||
|
std::shared_ptr<Logger> createConsole(std::string prefix, Level lvl) override;
|
||||||
|
std::shared_ptr<Logger> createFile(std::string prefix, std::string basePath,
|
||||||
|
std::uintmax_t maxBytes, Level lvl) override;
|
||||||
|
std::shared_ptr<Logger> createCombined(std::string prefix, std::string basePath,
|
||||||
|
std::uintmax_t maxBytes, Level lvl) override;
|
||||||
|
|
||||||
|
std::shared_ptr<Logger> get(std::string_view prefix) const override;
|
||||||
|
bool exists(std::string_view prefix) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<Logger> emplaceOrGet_(std::string prefix, std::shared_ptr<Sink> sink, Level lvl);
|
||||||
|
|
||||||
|
mutable std::mutex mtx_;
|
||||||
|
std::unordered_map<std::string, std::shared_ptr<Logger>> byPrefix_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace vl::logging {
|
||||||
|
|
||||||
|
// Call during VoidLoaderCore startup after it is loaded by JVM/JNI.
|
||||||
|
void init_voidloader_logger();
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
namespace vl::mod {
|
||||||
|
|
||||||
|
void* LoadModLibrary(const std::string& path, bool isGlobal);
|
||||||
|
bool CloseModLibrary(void* handle);
|
||||||
|
void* LoadModSymbol(void* handle, const std::string& name);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Mod/Base.hpp>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace vl::mod {
|
||||||
|
|
||||||
|
class LoadedMod {
|
||||||
|
public:
|
||||||
|
LoadedMod(std::string name, std::string version,
|
||||||
|
std::unique_ptr<Base> instance, void* handle)
|
||||||
|
: name_(std::move(name)), version_(std::move(version)),
|
||||||
|
instance_(std::move(instance)), handle_(handle) {}
|
||||||
|
|
||||||
|
Base& mod() { return *instance_; }
|
||||||
|
const Base& mod() const { return *instance_; }
|
||||||
|
|
||||||
|
const std::string& name() const noexcept { return name_; }
|
||||||
|
const std::string& version() const noexcept { return version_; }
|
||||||
|
void* handle() const noexcept { return handle_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string name_;
|
||||||
|
std::string version_;
|
||||||
|
std::unique_ptr<Base> instance_;
|
||||||
|
void* handle_{};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Mod/Context.hpp>
|
||||||
|
#include <Mods/Loaded.hpp>
|
||||||
|
#include <Mods/Registry.hpp>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace vl::mod {
|
||||||
|
|
||||||
|
// Handles dlopen/dlsym, discovers mod registrations, constructs mods (C2)
|
||||||
|
class ModLoader {
|
||||||
|
public:
|
||||||
|
// Load a module (.so), discover CRTP-registered mod types, instantiate each with Context.
|
||||||
|
// Returns the number of instances created from this module.
|
||||||
|
std::size_t loadModule(const std::string& path, const Context& ctx, bool isGlobal);
|
||||||
|
|
||||||
|
// Start/stop all currently loaded instances.
|
||||||
|
void startAll();
|
||||||
|
void stopAll();
|
||||||
|
|
||||||
|
// Unload everything (calls stopAll first if not stopped).
|
||||||
|
void unloadAll();
|
||||||
|
|
||||||
|
const std::vector<LoadedMod>& instances() const noexcept { return instances_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<void*> handles_;
|
||||||
|
std::vector<LoadedMod> instances_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Mod/Registration.hpp>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace vl::mod {
|
||||||
|
|
||||||
|
class Registry {
|
||||||
|
public:
|
||||||
|
struct Entry {
|
||||||
|
std::string typeName;
|
||||||
|
std::string name; // from T::Name()
|
||||||
|
std::string version; // from T::Version()
|
||||||
|
std::function<std::unique_ptr<Base>(const Context&)> factory;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Read registration entries from a loaded module (ELF .so handle)
|
||||||
|
// Returns number of entries found.
|
||||||
|
static std::size_t addFromModule(void* soHandle);
|
||||||
|
|
||||||
|
// Enumerate all known entries from all modules loaded so far
|
||||||
|
static const std::vector<Entry>& all();
|
||||||
|
static std::vector<Entry>& mutableAll();
|
||||||
|
|
||||||
|
private:
|
||||||
|
static std::vector<Entry>& storage_();
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
#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
|
|
||||||
};
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
#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);
|
|
||||||
};
|
|
||||||
+13
@@ -1,4 +1,17 @@
|
|||||||
|
# toolchain-mingw64.cmake
|
||||||
set(CMAKE_SYSTEM_NAME Windows)
|
set(CMAKE_SYSTEM_NAME Windows)
|
||||||
|
set(CMAKE_SYSTEM_PROCESSOR x86_64)
|
||||||
|
|
||||||
|
# specify the cross compiler
|
||||||
set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
|
set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
|
||||||
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
|
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
|
||||||
set(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)
|
set(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)
|
||||||
|
|
||||||
|
# where is the target environment
|
||||||
|
set(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32)
|
||||||
|
|
||||||
|
# search for programs in the build host directories
|
||||||
|
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
||||||
|
# for libraries and headers in the target directories
|
||||||
|
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
||||||
|
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
||||||
+158
@@ -0,0 +1,158 @@
|
|||||||
|
#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");
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
#include <Logging/AsyncWorker.hpp>
|
||||||
|
|
||||||
|
namespace vl::logging {
|
||||||
|
|
||||||
|
AsyncWorker& AsyncWorker::instance() {
|
||||||
|
static AsyncWorker w;
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
AsyncWorker::AsyncWorker()
|
||||||
|
: thread_([this](std::stop_token st){ run(st); }) {}
|
||||||
|
|
||||||
|
AsyncWorker::~AsyncWorker() {
|
||||||
|
thread_.request_stop();
|
||||||
|
cv_.notify_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AsyncWorker::enqueue(Item&& item) {
|
||||||
|
{
|
||||||
|
std::scoped_lock lk(mtx_);
|
||||||
|
queue_.push(std::move(item));
|
||||||
|
}
|
||||||
|
cv_.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AsyncWorker::run(std::stop_token st) {
|
||||||
|
while (!st.stop_requested()) {
|
||||||
|
Item item;
|
||||||
|
{
|
||||||
|
std::unique_lock lk(mtx_);
|
||||||
|
cv_.wait(lk, st, [this]{ return !queue_.empty(); });
|
||||||
|
if (st.stop_requested()) break;
|
||||||
|
item = std::move(queue_.front());
|
||||||
|
queue_.pop();
|
||||||
|
}
|
||||||
|
if (item.sink) item.sink->write(item.msg);
|
||||||
|
}
|
||||||
|
// drain
|
||||||
|
for (;;) {
|
||||||
|
Item item;
|
||||||
|
{
|
||||||
|
std::scoped_lock lk(mtx_);
|
||||||
|
if (queue_.empty()) break;
|
||||||
|
item = std::move(queue_.front());
|
||||||
|
queue_.pop();
|
||||||
|
}
|
||||||
|
if (item.sink) item.sink->write(item.msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#include <Logging/ConsoleSink.hpp>
|
||||||
|
#include <Logging/Level.hpp>
|
||||||
|
#include <chrono>
|
||||||
|
#include <ctime>
|
||||||
|
#include <format>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace vl::logging {
|
||||||
|
|
||||||
|
static std::string formatTimestamp(const std::chrono::system_clock::time_point& tp) {
|
||||||
|
const auto tt = std::chrono::system_clock::to_time_t(tp);
|
||||||
|
std::tm tm{};
|
||||||
|
#if defined(_WIN32)
|
||||||
|
localtime_s(&tm, &tt);
|
||||||
|
#else
|
||||||
|
localtime_r(&tt, &tm);
|
||||||
|
#endif
|
||||||
|
char buf[32];
|
||||||
|
std::strftime(buf, sizeof(buf), "%F %T", &tm);
|
||||||
|
return std::string{buf};
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConsoleSink::write(const Message& msg) {
|
||||||
|
const auto line = std::format("{} {} {} {}\n",
|
||||||
|
formatTimestamp(msg.ts),
|
||||||
|
msg.prefix,
|
||||||
|
toString(msg.level),
|
||||||
|
msg.text);
|
||||||
|
std::scoped_lock lk(coutMutex_);
|
||||||
|
std::cout << line;
|
||||||
|
std::cout.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
#include <Logging/FileSink.hpp>
|
||||||
|
#include <Logging/Level.hpp>
|
||||||
|
#include <chrono>
|
||||||
|
#include <ctime>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <format>
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace vl::logging {
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
static std::string formatTimestamp(const std::chrono::system_clock::time_point& tp) {
|
||||||
|
const auto tt = std::chrono::system_clock::to_time_t(tp);
|
||||||
|
std::tm tm{};
|
||||||
|
#if defined(_WIN32)
|
||||||
|
localtime_s(&tm, &tt);
|
||||||
|
#else
|
||||||
|
localtime_r(&tt, &tm);
|
||||||
|
#endif
|
||||||
|
char buf[32];
|
||||||
|
std::strftime(buf, sizeof(buf), "%F %T", &tm);
|
||||||
|
return std::string{buf};
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string ymdOf(const std::chrono::system_clock::time_point& tp) {
|
||||||
|
const auto tt = std::chrono::system_clock::to_time_t(tp);
|
||||||
|
std::tm tm{};
|
||||||
|
#if defined(_WIN32)
|
||||||
|
localtime_s(&tm, &tt);
|
||||||
|
#else
|
||||||
|
localtime_r(&tt, &tm);
|
||||||
|
#endif
|
||||||
|
char buf[16];
|
||||||
|
std::strftime(buf, sizeof(buf), "%F", &tm); // YYYY-MM-DD
|
||||||
|
return std::string{buf};
|
||||||
|
}
|
||||||
|
|
||||||
|
FileSink::FileSink(std::string basePath, std::uintmax_t maxBytes)
|
||||||
|
: base_(std::move(basePath)), maxBytes_(maxBytes) {
|
||||||
|
fs::path p(base_);
|
||||||
|
if (p.has_parent_path()) fs::create_directories(p.parent_path());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string FileSink::dateOf(const std::chrono::system_clock::time_point& tp) { return ymdOf(tp); }
|
||||||
|
|
||||||
|
std::string FileSink::filenameFor(const std::string& base, std::string_view date, int index) {
|
||||||
|
// base_YYYY-MM-DD_index.log
|
||||||
|
return std::format("{}_{}_{}.log", base, date, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileSink::openCurrent() {
|
||||||
|
const auto name = filenameFor(base_, currentDate_, index_);
|
||||||
|
out_.close();
|
||||||
|
out_.clear();
|
||||||
|
out_.open(name, std::ios::out | std::ios::app | std::ios::binary);
|
||||||
|
if (!out_) {
|
||||||
|
throw std::runtime_error(std::format("FileSink: cannot open '{}'", name));
|
||||||
|
}
|
||||||
|
try { written_ = fs::file_size(name); } catch (...) { written_ = 0; }
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileSink::rotateToDate(std::string newDate) {
|
||||||
|
currentDate_ = std::move(newDate);
|
||||||
|
index_ = 1;
|
||||||
|
openCurrent();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileSink::ensureOpen(const Message& msg) {
|
||||||
|
const auto msgDate = dateOf(msg.ts);
|
||||||
|
if (!out_.is_open()) {
|
||||||
|
currentDate_ = msgDate;
|
||||||
|
index_ = 1;
|
||||||
|
openCurrent();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (msgDate != currentDate_) {
|
||||||
|
rotateToDate(msgDate);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (maxBytes_ > 0 && written_ >= maxBytes_) {
|
||||||
|
++index_;
|
||||||
|
openCurrent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileSink::write(const Message& msg) {
|
||||||
|
ensureOpen(msg);
|
||||||
|
const auto line = std::format("{} {} {} {}\n",
|
||||||
|
formatTimestamp(msg.ts),
|
||||||
|
msg.prefix,
|
||||||
|
toString(msg.level),
|
||||||
|
msg.text);
|
||||||
|
out_ << line;
|
||||||
|
written_ += static_cast<std::uintmax_t>(line.size());
|
||||||
|
out_.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
#include <Logging/LoggerProvider.hpp>
|
||||||
|
#include <Logging/LoggerInternal.hpp>
|
||||||
|
#include <Logging/ConsoleSink.hpp>
|
||||||
|
#include <Logging/FileSink.hpp>
|
||||||
|
#include <Logging/CombinedSink.hpp>
|
||||||
|
|
||||||
|
namespace vl::logging {
|
||||||
|
|
||||||
|
std::shared_ptr<Logger> LoggerProvider::emplaceOrGet_(std::string prefix,
|
||||||
|
std::shared_ptr<Sink> sink,
|
||||||
|
Level lvl) {
|
||||||
|
std::scoped_lock lk(mtx_);
|
||||||
|
if (auto it = byPrefix_.find(prefix); it != byPrefix_.end()) return it->second; // P3
|
||||||
|
auto lg = std::make_shared<LoggerInternal>(prefix, std::move(sink), lvl);
|
||||||
|
byPrefix_.emplace(std::move(prefix), lg);
|
||||||
|
return lg;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Logger> LoggerProvider::createConsole(std::string prefix, Level lvl) {
|
||||||
|
return emplaceOrGet_(std::move(prefix), std::make_shared<ConsoleSink>(), lvl);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Logger> LoggerProvider::createFile(std::string prefix, std::string basePath,
|
||||||
|
std::uintmax_t maxBytes, Level lvl) {
|
||||||
|
return emplaceOrGet_(std::move(prefix), std::make_shared<FileSink>(std::move(basePath), maxBytes), lvl);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Logger> LoggerProvider::createCombined(std::string prefix, std::string basePath,
|
||||||
|
std::uintmax_t maxBytes, Level lvl) {
|
||||||
|
auto a = std::make_shared<ConsoleSink>();
|
||||||
|
auto b = std::make_shared<FileSink>(std::move(basePath), maxBytes);
|
||||||
|
auto s = std::make_shared<CombinedSink>(std::move(a), std::move(b));
|
||||||
|
return emplaceOrGet_(std::move(prefix), std::move(s), lvl);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Logger> LoggerProvider::get(std::string_view prefix) const {
|
||||||
|
std::scoped_lock lk(mtx_);
|
||||||
|
auto it = byPrefix_.find(std::string(prefix));
|
||||||
|
if (it == byPrefix_.end()) throw std::runtime_error("LoggerProvider::get: unknown prefix");
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LoggerProvider::exists(std::string_view prefix) const {
|
||||||
|
std::scoped_lock lk(mtx_);
|
||||||
|
return byPrefix_.contains(std::string(prefix));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#include <Logging/LoggerRegister.hpp>
|
||||||
|
#include <Logging/LoggerManager.hpp>
|
||||||
|
#include <Logging/LoggerProvider.hpp>
|
||||||
|
|
||||||
|
namespace vl::logging {
|
||||||
|
|
||||||
|
static LoggerProvider g_coreProvider; // internal to the Core library
|
||||||
|
|
||||||
|
void init_voidloader_logger() {
|
||||||
|
LoggerManager::instance().setProvider(&g_coreProvider);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include <Mods/LoadLibrary.hpp>
|
||||||
|
#include <dlfcn.h>
|
||||||
|
namespace vl::mod {
|
||||||
|
void* LoadModLibrary(const std::string& path, bool isGlobal)
|
||||||
|
{
|
||||||
|
dlerror();
|
||||||
|
return dlopen(path.c_str(), isGlobal ? RTLD_NOW | RTLD_GLOBAL : RTLD_NOW);
|
||||||
|
}
|
||||||
|
bool CloseModLibrary(void* handle)
|
||||||
|
{
|
||||||
|
return dlclose(handle) == 0;
|
||||||
|
}
|
||||||
|
void* LoadModSymbol(void* handle, const std::string& name)
|
||||||
|
{
|
||||||
|
dlerror();
|
||||||
|
return dlsym(handle, name.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
#include <Mods/LoadLibrary.hpp>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
namespace vl::mod {
|
||||||
|
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
|
||||||
|
static int32_t __wtf8_decode1(const char** input) {
|
||||||
|
uint32_t code_point;
|
||||||
|
uint8_t b1;
|
||||||
|
uint8_t b2;
|
||||||
|
uint8_t b3;
|
||||||
|
uint8_t b4;
|
||||||
|
|
||||||
|
b1 = **input;
|
||||||
|
if (b1 <= 0x7F)
|
||||||
|
return b1; /* ASCII code point */
|
||||||
|
if (b1 < 0xC2)
|
||||||
|
return -1; /* invalid: continuation byte */
|
||||||
|
code_point = b1;
|
||||||
|
|
||||||
|
b2 = *++*input;
|
||||||
|
if ((b2 & 0xC0) != 0x80)
|
||||||
|
return -1; /* invalid: not a continuation byte */
|
||||||
|
code_point = (code_point << 6) | (b2 & 0x3F);
|
||||||
|
if (b1 <= 0xDF)
|
||||||
|
return 0x7FF & code_point; /* two-byte character */
|
||||||
|
|
||||||
|
b3 = *++*input;
|
||||||
|
if ((b3 & 0xC0) != 0x80)
|
||||||
|
return -1; /* invalid: not a continuation byte */
|
||||||
|
code_point = (code_point << 6) | (b3 & 0x3F);
|
||||||
|
if (b1 <= 0xEF)
|
||||||
|
return 0xFFFF & code_point; /* three-byte character */
|
||||||
|
|
||||||
|
b4 = *++*input;
|
||||||
|
if ((b4 & 0xC0) != 0x80)
|
||||||
|
return -1; /* invalid: not a continuation byte */
|
||||||
|
code_point = (code_point << 6) | (b4 & 0x3F);
|
||||||
|
if (b1 <= 0xF4) {
|
||||||
|
code_point &= 0x1FFFFF;
|
||||||
|
if (code_point <= 0x10FFFF)
|
||||||
|
return code_point; /* four-byte character */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* code point too large */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t wtf8_length_as_utf16(const char* source_ptr) {
|
||||||
|
size_t w_target_len = 0;
|
||||||
|
int32_t code_point;
|
||||||
|
|
||||||
|
do {
|
||||||
|
code_point = __wtf8_decode1(&source_ptr);
|
||||||
|
if (code_point < 0)
|
||||||
|
return -1;
|
||||||
|
if (code_point > 0xFFFF)
|
||||||
|
w_target_len++;
|
||||||
|
w_target_len++;
|
||||||
|
} while (*source_ptr++);
|
||||||
|
|
||||||
|
return w_target_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wtf8_to_utf16(const char* source_ptr,
|
||||||
|
WCHAR* w_target,
|
||||||
|
size_t w_target_len) {
|
||||||
|
int32_t code_point;
|
||||||
|
|
||||||
|
do {
|
||||||
|
code_point = __wtf8_decode1(&source_ptr);
|
||||||
|
/* uv_wtf8_length_as_utf16 should have been called and checked first. */
|
||||||
|
assert(code_point >= 0);
|
||||||
|
if (code_point > 0xFFFF) {
|
||||||
|
assert(code_point < 0x10FFFF);
|
||||||
|
*w_target++ = (((code_point - 0x10000) >> 10) + 0xD800);
|
||||||
|
*w_target++ = ((code_point - 0x10000) & 0x3FF) + 0xDC00;
|
||||||
|
w_target_len -= 2;
|
||||||
|
} else {
|
||||||
|
*w_target++ = code_point;
|
||||||
|
w_target_len -= 1;
|
||||||
|
}
|
||||||
|
} while (*source_ptr++);
|
||||||
|
|
||||||
|
(void)w_target_len;
|
||||||
|
assert(w_target_len == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* LoadModLibrary(const std::string& path, bool isGlobal)
|
||||||
|
{
|
||||||
|
WCHAR filename_w[32768];
|
||||||
|
ssize_t r;
|
||||||
|
void* handle = nullptr;
|
||||||
|
|
||||||
|
r = wtf8_length_as_utf16(path.c_str());
|
||||||
|
if (r < 0)
|
||||||
|
return nullptr;
|
||||||
|
if ((size_t) r > ARRAY_SIZE(filename_w))
|
||||||
|
return nullptr;
|
||||||
|
wtf8_to_utf16(path.c_str(), filename_w, r);
|
||||||
|
|
||||||
|
return LoadLibraryExW(filename_w, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
|
||||||
|
}
|
||||||
|
bool CloseModLibrary(void* handle)
|
||||||
|
{
|
||||||
|
return FreeLibrary(static_cast<HMODULE>(handle)) != 0;
|
||||||
|
}
|
||||||
|
void* LoadModSymbol(void* handle, const std::string& name)
|
||||||
|
{
|
||||||
|
return (void*)(uintptr_t) GetProcAddress(static_cast<HMODULE>(handle), name.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
#include <Mods/LoadLibrary.hpp>
|
||||||
|
#include <Mods/Loader.hpp>
|
||||||
|
#include <Mods/LoadLibrary.hpp>
|
||||||
|
#include <Agent.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace vl::mod {
|
||||||
|
|
||||||
|
std::size_t ModLoader::loadModule(const std::string& path, const Context& ctx, bool isGlobal) {
|
||||||
|
void* handle = LoadModLibrary(path, isGlobal);
|
||||||
|
if (!handle) throw std::runtime_error(std::string("dlopen failed: "));
|
||||||
|
handles_.push_back(handle);
|
||||||
|
|
||||||
|
const std::size_t before = Registry::all().size();
|
||||||
|
Registry::addFromModule(handle);
|
||||||
|
const std::size_t after = Registry::all().size();
|
||||||
|
const std::size_t added = after - before;
|
||||||
|
if (added > 0) {
|
||||||
|
auto& all = Registry::mutableAll();
|
||||||
|
for (std::size_t i = all.size() - added; i < all.size(); ++i) {
|
||||||
|
auto& e = all[i];
|
||||||
|
auto inst = e.factory(ctx);
|
||||||
|
auto name = std::string(inst->name());
|
||||||
|
auto version = std::string(inst->version());
|
||||||
|
e.name = std::move(name);
|
||||||
|
e.version = std::move(version);
|
||||||
|
instances_.emplace_back(e.name, e.version, std::move(inst), handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return added;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModLoader::startAll() {
|
||||||
|
auto& instance = vl::Agent::Agent::getInstance();
|
||||||
|
for (auto& lm : instances_)
|
||||||
|
{
|
||||||
|
auto name = std::string(lm.mod().name());
|
||||||
|
auto version = std::string(lm.mod().version());
|
||||||
|
instance.m_logger->info("Starting {}, Version: {}", name, version);
|
||||||
|
lm.mod().onStart();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModLoader::stopAll() {
|
||||||
|
for (auto it = instances_.rbegin(); it != instances_.rend(); ++it) it->mod().onStop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModLoader::unloadAll() {
|
||||||
|
stopAll();
|
||||||
|
instances_.clear();
|
||||||
|
for (auto* h : handles_) if (h) CloseModLibrary(h);
|
||||||
|
handles_.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#include <Mods/Registry.hpp>
|
||||||
|
#include <Mods/LoadLibrary.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace vl::mod {
|
||||||
|
|
||||||
|
std::vector<Registry::Entry>& Registry::storage_() {
|
||||||
|
static std::vector<Entry> s;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<Registry::Entry>& Registry::all() {
|
||||||
|
return storage_();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Registry::Entry>& Registry::mutableAll() {
|
||||||
|
return storage_();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t Registry::addFromModule(void* soHandle) {
|
||||||
|
#if defined(__ELF__)
|
||||||
|
using BoundPtr = const Registration*;
|
||||||
|
auto symStart = LoadModSymbol(soHandle, "__start_vlmods");
|
||||||
|
auto symStop = LoadModSymbol(soHandle, "__stop_vlmods");
|
||||||
|
if ((symStart == nullptr) || (symStop == nullptr))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
auto* begin = reinterpret_cast<BoundPtr>(symStart);
|
||||||
|
auto* end = reinterpret_cast<BoundPtr>(symStop);
|
||||||
|
|
||||||
|
std::size_t added = 0;
|
||||||
|
for (auto* it = begin; it && it < end; ++it) {
|
||||||
|
if (!it->factory || it->magic != kModRegMagic) continue;
|
||||||
|
|
||||||
|
Entry e;
|
||||||
|
e.typeName = std::string{"<unknown>"};
|
||||||
|
e.name = std::string{"<unnamed>"};
|
||||||
|
e.version = std::string{"0.0.0"};
|
||||||
|
e.factory = it->factory;
|
||||||
|
storage_().push_back(std::move(e));
|
||||||
|
++added;
|
||||||
|
}
|
||||||
|
return added;
|
||||||
|
#else
|
||||||
|
(void)soHandle;
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace vl::log
|
||||||
-133
@@ -1,133 +0,0 @@
|
|||||||
#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
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
#include "class_transformer.h"
|
#include "class_transformer.h"
|
||||||
#include "jvmti_handler.h"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
#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);
|
|
||||||
}
|
|
||||||
@@ -1,175 +0,0 @@
|
|||||||
#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