From 6858a35754544172caf2d821c47fe74831757e03 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Sun, 19 Apr 2026 18:57:32 +0800 Subject: [PATCH] Feat: Simple module system --- src/libagent/includes/modules/IModule.hpp | 17 +++++++++++++ src/libagent/includes/modules/Modules.hpp | 25 +++++++++++++++++++ src/libagent/libagent.cpp | 10 ++++++++ src/libagent/modules/JarLoader.cpp | 30 +++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 src/libagent/includes/modules/IModule.hpp create mode 100644 src/libagent/includes/modules/Modules.hpp create mode 100644 src/libagent/modules/JarLoader.cpp diff --git a/src/libagent/includes/modules/IModule.hpp b/src/libagent/includes/modules/IModule.hpp new file mode 100644 index 0000000..96226c0 --- /dev/null +++ b/src/libagent/includes/modules/IModule.hpp @@ -0,0 +1,17 @@ +#pragma once +#include + +namespace JuiceAgent::Core::Modules { + +class IModule { +public: + virtual ~IModule() = default; + + virtual std::string name() const = 0; + + virtual bool init() = 0; + virtual bool start() = 0; + virtual void stop() = 0; +}; + +} \ No newline at end of file diff --git a/src/libagent/includes/modules/Modules.hpp b/src/libagent/includes/modules/Modules.hpp new file mode 100644 index 0000000..f69f79e --- /dev/null +++ b/src/libagent/includes/modules/Modules.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include + +namespace JuiceAgent::Core::Modules { + +class JarLoaderModule : public IModule { +public: + JarLoaderModule() = default; + ~JarLoaderModule() override = default; + + std::string name() const override { + return "JarLoader"; + } + + bool init() override; + bool start() override; + void stop() override; + +private: + bool _initialized = false; + bool _running = false; +}; + +} \ No newline at end of file diff --git a/src/libagent/libagent.cpp b/src/libagent/libagent.cpp index 5fb8d3d..6ee56f5 100644 --- a/src/libagent/libagent.cpp +++ b/src/libagent/libagent.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace JuiceAgent { // ===== Singleton ===== @@ -40,6 +41,11 @@ namespace JuiceAgent { set_config(cfg); PLOGD << "env: " << env << ", jvmti: " << jvmti; + // temporary register modules + // TODO: remove this, implement a better way to register modules + JuiceAgent::Core::Modules::JarLoaderModule jarloader; + jarloader.init(); + // PreLoad Event eventbus.post(EventPreLoad{ @@ -81,6 +87,10 @@ namespace JuiceAgent { // register bytecodes_opt JuiceAgent::Bytecodes::register_bytecodes(); + // Temporary startup modules + // TODO: remove this, implement a better way to startup modules + jarloader.start(); + // Loaded Event eventbus.post(EventLoaded{ diff --git a/src/libagent/modules/JarLoader.cpp b/src/libagent/modules/JarLoader.cpp new file mode 100644 index 0000000..d4e6af7 --- /dev/null +++ b/src/libagent/modules/JarLoader.cpp @@ -0,0 +1,30 @@ +#include +#include + +namespace JuiceAgent::Core::Modules { + +bool JarLoaderModule::init() { + if (_initialized) return true; + + PLOGI << "JarLoader init"; + _initialized = true; + return true; +} + +bool JarLoaderModule::start() { + if (!_initialized) return false; + if (_running) return true; + + PLOGI << "JarLoader start"; + _running = true; + return true; +} + +void JarLoaderModule::stop() { + if (!_running) return; + + PLOGI << "JarLoader stop"; + _running = false; +} + +} \ No newline at end of file