feat: refactor module structure to use ModuleBase class and improve initialization logic

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
xiaozhou233andCopilot committed 2026-04-29 16:45:13 +08:00
1 parent 28804c0af3
commit 2493a9243f
6 files changed
+151 -130

No files matched your search

@@ -12,6 +12,9 @@ public:
virtual bool init() = 0;
virtual bool start() = 0;
virtual void stop() = 0;
virtual bool initialized() const = 0;
virtual bool running() const = 0;
};
}
@@ -0,0 +1,74 @@
#pragma once
#include <modules/IModule.hpp>
namespace JuiceAgent::Core::Modules {
class ModuleBase : public IModule {
public:
virtual ~ModuleBase() = default;
bool init() override {
if (_initialized) {
return true;
}
if (!on_init()) {
return false;
}
_initialized = true;
return true;
}
bool start() override {
if (!_initialized) {
return false;
}
if (_running) {
return true;
}
if (!on_start()) {
return false;
}
_running = true;
return true;
}
void stop() override {
if (!_running) {
return;
}
on_stop();
_running = false;
}
bool initialized() const override {
return _initialized;
}
bool running() const override {
return _running;
}
protected:
virtual bool on_init() {
return true;
}
virtual bool on_start() {
return true;
}
virtual void on_stop() {
}
private:
bool _initialized = false;
bool _running = false;
};
}
+18 -32
View File
@@ -1,4 +1,5 @@
#include <modules/ModuleRegistry.hpp>
#include <modules/ModuleBase.hpp>
#include <JuiceAgent/Logger.hpp>
#include <libagent.hpp>
#include <event/eventbus.hpp>
@@ -21,17 +22,16 @@ struct DumperConfig {
static DumperConfig config;
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
class Dumper : public IModule {
class Dumper : public ModuleBase {
using super = ModuleBase;
public:
std::string name() const override {
return "Dumper";
}
bool init() override {
if (_initialized) {
return true;
}
protected:
bool on_init() override {
auto& cfg = agent.get_config();
config.enabled = cfg.get<bool>(
@@ -45,19 +45,10 @@ public:
true
);
_initialized = true;
return true;
}
bool start() override {
if (!_initialized) {
return false;
}
if (_running) {
return true;
}
bool on_start() override {
if (!config.enabled) {
return true;
}
@@ -70,27 +61,30 @@ public:
}
);
_running = true;
return true;
}
void stop() override {
if (!_running) {
void on_stop() override {
if (!config.enabled) {
return;
}
auto& bus = agent.get_eventbus();
bus.unsubscribe<EventClassFileLoadHook>(_classFileToken);
_running = false;
if (_classFileToken != 0) {
bus.unsubscribe<EventClassFileLoadHook>(_classFileToken);
_classFileToken = 0;
}
}
private:
void on_class_file_load_hook(const EventClassFileLoadHook& event) {
if (!event.classbytes || event.class_data_len <= 0) {
return;
}
std::string_view name = event.name ? event.name : "";
if (name.empty()) {
return;
}
@@ -104,6 +98,7 @@ public:
}
std::ofstream file(output, std::ios::binary);
if (!file.is_open()) {
PLOGW << "Dumper: Failed to open file: " << output.string();
return;
@@ -113,28 +108,19 @@ public:
reinterpret_cast<const char*>(event.classbytes),
static_cast<std::streamsize>(event.class_data_len)
);
file.close();
} catch (const std::exception& e) {
PLOGE << "Dumper: " << e.what();
}
}
private:
fs::path build_class_path(std::string_view className) {
fs::path build_class_path(std::string_view class_name) {
fs::path path(config.DumpDir);
// JVM name format: cn/xiaozhou233/Test
path /= fs::path(className);
path /= fs::path(class_name);
path += ".class";
return path;
}
private:
bool _initialized = false;
bool _running = false;
EventBus::Token _classFileToken = 0;
std::mutex _mutex;
};
+13 -25
View File
@@ -1,4 +1,5 @@
#include <modules/ModuleRegistry.hpp>
#include <modules/ModuleBase.hpp>
#include <JuiceAgent/Logger.hpp>
#include <libagent.hpp>
#include <JuiceAgent/Utils.hpp>
@@ -16,20 +17,21 @@ struct JarLoaderConfig {
static JarLoaderConfig config;
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
static const char* MODULE_CLASS = "cn/xiaozhou233/juiceagent/api/modules/JarLoader";
static const char* MODULE_CLASS =
"cn/xiaozhou233/juiceagent/api/modules/JarLoader";
static const char* MODULE_METHOD = "loadJar";
class JarLoaderModule : public IModule {
class JarLoaderModule : public ModuleBase {
using super = ModuleBase;
public:
std::string name() const override {
return "JarLoader";
}
bool init() override {
if (_initialized) {
return true;
}
protected:
bool on_init() override {
auto& cfg = agent.get_config();
config.enabled = cfg.get<bool>(
@@ -62,20 +64,16 @@ public:
);
PLOGI << "JarLoader init";
_initialized = true;
return true;
}
bool start() override {
if (!_initialized) {
return false;
}
if (_running || !config.enabled) {
bool on_start() override {
if (!config.enabled) {
return true;
}
JuiceAgent::Utils::Serializer ser;
ser.add_kv("InjectionDir", config.injection_dir);
ser.add_kv("JarPath", config.jar_path);
ser.add_kv("EntryClass", config.entry_class);
@@ -91,22 +89,12 @@ public:
);
PLOGI << "JarLoader start";
_running = true;
return true;
}
void stop() override {
if (!_running) {
return;
}
void on_stop() override {
PLOGI << "JarLoader stop";
_running = false;
}
private:
bool _initialized = false;
bool _running = false;
};
}
+34 -34
View File
@@ -1,4 +1,5 @@
#include <modules/ModuleRegistry.hpp>
#include <modules/ModuleBase.hpp>
#include <JuiceAgent/Logger.hpp>
#include <libagent.hpp>
#include <event/event_type.hpp>
@@ -17,7 +18,9 @@ struct RemapperConfig {
static RemapperConfig config;
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
class RemapperModule : public IModule {
class RemapperModule : public ModuleBase {
using super = ModuleBase;
struct _Remapper {
jclass handler_class = nullptr;
jmethodID onClassFileLoadMethod = nullptr;
@@ -30,11 +33,8 @@ public:
return "Remapper";
}
bool init() override {
if (_initialized) {
return true;
}
protected:
bool on_init() override {
auto& cfg = agent.get_config();
config.enabled = cfg.get<bool>(
@@ -49,7 +49,6 @@ public:
);
if (!config.enabled) {
_initialized = true;
return true;
}
@@ -114,21 +113,11 @@ public:
return false;
}
_initialized = true;
return true;
}
bool start() override {
if (!_initialized) {
return false;
}
if (_running) {
return true;
}
bool on_start() override {
if (!config.enabled) {
_running = true;
return true;
}
@@ -152,26 +141,38 @@ public:
}
);
_running = true;
return true;
}
void stop() override {
if (!_running) {
void on_stop() override {
if (!config.enabled) {
return;
}
auto& bus = agent.get_eventbus();
bus.unsubscribe<EventClassFileLoadHook>(_classFileToken);
bus.unsubscribe<EventMethodEntry>(_methodEntryToken);
bus.unsubscribe<EventMethodExit>(_methodExitToken);
if (_classFileToken != 0) {
bus.unsubscribe<EventClassFileLoadHook>(_classFileToken);
}
if (_methodEntryToken != 0) {
bus.unsubscribe<EventMethodEntry>(_methodEntryToken);
}
if (_methodExitToken != 0) {
bus.unsubscribe<EventMethodExit>(_methodExitToken);
}
_classFileToken = 0;
_methodEntryToken = 0;
_methodExitToken = 0;
_running = false;
JNIEnv* env = agent.get_env();
if (env != nullptr && Remapper.handler_class != nullptr) {
env->DeleteGlobalRef(Remapper.handler_class);
Remapper.handler_class = nullptr;
}
}
private:
@@ -184,15 +185,16 @@ private:
JNIEnv* env = event.jni_env;
std::string className(name);
jstring jName = env->NewStringUTF(className.c_str());
if (jName == nullptr) {
std::string class_name(name);
jstring j_name = env->NewStringUTF(class_name.c_str());
if (j_name == nullptr) {
return;
}
jbyteArray input = env->NewByteArray(event.class_data_len);
if (input == nullptr) {
env->DeleteLocalRef(jName);
env->DeleteLocalRef(j_name);
return;
}
@@ -209,7 +211,7 @@ private:
Remapper.onClassFileLoadMethod,
event.class_being_redefined,
event.loader,
jName,
j_name,
event.protection_domain,
event.class_data_len,
input
@@ -238,8 +240,9 @@ private:
}
env->DeleteLocalRef(input);
env->DeleteLocalRef(jName);
env->DeleteLocalRef(j_name);
}
void on_method_entry_hook(const EventMethodEntry& event) {
// event.jni_env->CallStaticVoidMethod(
// Remapper.handler_class,
@@ -265,9 +268,6 @@ private:
}
private:
bool _initialized = false;
bool _running = false;
EventBus::Token _classFileToken = 0;
EventBus::Token _methodEntryToken = 0;
EventBus::Token _methodExitToken = 0;
+9 -39
View File
@@ -1,58 +1,28 @@
#include <modules/ModuleRegistry.hpp>
#include <modules/ModuleBase.hpp>
#include <JuiceAgent/Logger.hpp>
namespace JuiceAgent::Core::Modules {
class TemplateModule : public IModule {
class TemplateModule : public ModuleBase {
public:
std::string name() const override {
return "Template";
}
bool init() override {
if (_initialized) {
return true;
}
protected:
bool on_init() override {
// load config / prepare resource
_initialized = true;
return true;
}
bool start() override {
if (!_initialized) {
return false;
}
if (_running) {
return true;
}
// start logic
_running = true;
bool on_start() override {
// start module
return true;
}
void stop() override {
if (!_running) {
return;
}
// cleanup logic
_running = false;
void on_stop() override {
// stop module
}
private:
bool _initialized = false;
bool _running = false;
};
}
JUICEAGENT_REGISTER_MODULE(
JuiceAgent::Core::Modules::TemplateModule,
Template
)
}