From 8e1b8b38e42926e8f8f857bcc8a72ce3be67a49b Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Mon, 17 Aug 2026 18:54:36 +0800 Subject: [PATCH] Refactor: auto-register services and inline BytecodeService - add ServiceRegistry and JUICEAGENT_REGISTER_SERVICE macro so each service self-registers at static init; initializeAll() no longer includes concrete service headers - move BytecodeService class entirely into its .cpp (no header needed, only accessed via IService) - split ClassFileLoadHook handling into captureBytecodes and patchBytecodes listeners, registered in order --- src/libagent/includes/services.hpp | 3 +- .../includes/services/bytecode_service.hpp | 24 -------- .../includes/services/service_manager.hpp | 60 +++++++++++++++++++ src/libagent/services/BytecodeService.cpp | 57 +++++++++++------- 4 files changed, 98 insertions(+), 46 deletions(-) delete mode 100644 src/libagent/includes/services/bytecode_service.hpp diff --git a/src/libagent/includes/services.hpp b/src/libagent/includes/services.hpp index 1771fe7..6e81c14 100644 --- a/src/libagent/includes/services.hpp +++ b/src/libagent/includes/services.hpp @@ -2,14 +2,13 @@ #include #include -#include namespace JuiceAgent { namespace Services { inline bool initializeAll() { auto& manager = ServiceManager::getInstance(); - manager.registerService(); + ServiceRegistry::getInstance().instantiateAll(manager); return manager.initializeAll(); } diff --git a/src/libagent/includes/services/bytecode_service.hpp b/src/libagent/includes/services/bytecode_service.hpp deleted file mode 100644 index d96b6bc..0000000 --- a/src/libagent/includes/services/bytecode_service.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include -#include - -namespace JuiceAgent { -namespace Services { - -class BytecodeService : public IService { -public: - static BytecodeService& getInstance(); - - const char* name() const override { return "BytecodeService"; } - - bool onInitialize() override; - - void onShutdown() override; - -private: - void onClassFileLoad(const EventClassFileLoadHook& event); -}; - -} -} diff --git a/src/libagent/includes/services/service_manager.hpp b/src/libagent/includes/services/service_manager.hpp index 6d50389..ecf4f72 100644 --- a/src/libagent/includes/services/service_manager.hpp +++ b/src/libagent/includes/services/service_manager.hpp @@ -3,7 +3,9 @@ #include #include +#include #include +#include #include namespace JuiceAgent { @@ -28,6 +30,13 @@ public: return raw; } + IService* createService(std::unique_ptr service) { + IService* raw = service.get(); + spdlog::debug("[ServiceManager] Registering service: {}", raw->name()); + services_.push_back(std::move(service)); + return raw; + } + template ServiceT* getService() const { const std::type_info& target = typeid(ServiceT); @@ -67,5 +76,56 @@ private: std::vector> services_; }; +// Static self-registration: each service's .cpp uses JUICEAGENT_REGISTER_SERVICE. +class ServiceRegistry { +public: + using Factory = std::function()>; + + static ServiceRegistry& getInstance() { + static ServiceRegistry instance; + return instance; + } + + void addFactory(Factory factory) { + std::lock_guard lock(mutex_); + factories_.push_back(std::move(factory)); + } + + void instantiateAll(ServiceManager& manager) { + std::lock_guard lock(mutex_); + for (auto& f : factories_) { + manager.createService(f()); + } + } + +private: + ServiceRegistry() = default; + std::mutex mutex_; + std::vector factories_; +}; + +template +class ServiceRegistrar { +public: + ServiceRegistrar() { + ServiceRegistry::getInstance().addFactory( + []() -> std::unique_ptr { + return std::make_unique(); + } + ); + } +}; + } } + +#define JUICEAGENT_SERVICE_CONCAT_IMPL(a, b) a##b +#define JUICEAGENT_SERVICE_CONCAT(a, b) JUICEAGENT_SERVICE_CONCAT_IMPL(a, b) + +// Put at file scope in the service's .cpp: +// JUICEAGENT_REGISTER_SERVICE(juiceagent::services::MyService) +#define JUICEAGENT_REGISTER_SERVICE(ServiceT) \ + namespace { \ + const ::JuiceAgent::Services::ServiceRegistrar \ + JUICEAGENT_SERVICE_CONCAT(_juiceagent_service_registrar_, __LINE__); \ + } diff --git a/src/libagent/services/BytecodeService.cpp b/src/libagent/services/BytecodeService.cpp index 429ad08..c4981fb 100644 --- a/src/libagent/services/BytecodeService.cpp +++ b/src/libagent/services/BytecodeService.cpp @@ -1,32 +1,49 @@ -#include +#include +#include +#include #include namespace JuiceAgent { namespace Services { -BytecodeService& BytecodeService::getInstance() { - static BytecodeService instance; - return instance; -} +class BytecodeService : public IService { +public: + const char* name() const override { return "BytecodeService"; } -bool BytecodeService::onInitialize() { - listen( - EventId::ClassFileLoadHook, - &BytecodeService::onClassFileLoad - ); + bool onInitialize() override { + listen( + EventId::ClassFileLoadHook, + &BytecodeService::captureBytecodes + ); - spdlog::info("[BytecodeService] Initialized"); - return true; -} + listen( + EventId::ClassFileLoadHook, + &BytecodeService::patchBytecodes + ); -void BytecodeService::onShutdown() { - spdlog::info("[BytecodeService] Shutdown"); -} + spdlog::info("[BytecodeService] Initialized"); + return true; + } -void BytecodeService::onClassFileLoad(const EventClassFileLoadHook& event) { - spdlog::trace("[BytecodeService] ClassFileLoadHook: {}", - event.name ? event.name : ""); -} + void onShutdown() override { + spdlog::info("[BytecodeService] Shutdown"); + } + +private: + void captureBytecodes(const EventClassFileLoadHook& e) { + // TODO: Capture original bytes + spdlog::trace("[BytecodeService] Capture original bytes: {}", + e.name ? e.name : ""); + } + + void patchBytecodes(const EventClassFileLoadHook& e) { + // TODO: Apply patches + spdlog::trace("[BytecodeService] Apply patches: {}", + e.name ? e.name : ""); + } +}; } } + +JUICEAGENT_REGISTER_SERVICE(::JuiceAgent::Services::BytecodeService)