From b86295cfd8d6f55684957640c11f157e153f6e87 Mon Sep 17 00:00:00 2001 From: Bytequill Date: Mon, 3 Feb 2025 19:04:43 +0100 Subject: [PATCH] Migrate to `PluginBase` --- .../plugins/discordRPC/discordRPCplugin.cpp | 180 +++++------------- .../plugins/discordRPC/discordRPCplugin.hpp | 57 ++---- 2 files changed, 64 insertions(+), 173 deletions(-) diff --git a/src/tools/ecode/plugins/discordRPC/discordRPCplugin.cpp b/src/tools/ecode/plugins/discordRPC/discordRPCplugin.cpp index 45dc8642a..65cdea4aa 100644 --- a/src/tools/ecode/plugins/discordRPC/discordRPCplugin.cpp +++ b/src/tools/ecode/plugins/discordRPC/discordRPCplugin.cpp @@ -1,8 +1,11 @@ #include "discordRPCplugin.hpp" -#include -#include using json = nlohmann::json; +#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined ( __EMSCRIPTEN_PTHREADS__ ) +#define dcRPC_THREADED 1 +#else +#define dcRPC_THREADED 0 +#endif namespace ecode { @@ -14,38 +17,27 @@ Plugin* DiscordRPCplugin::NewSync( PluginManager* pluginManager) { return eeNew( DiscordRPCplugin, ( pluginManager, true ) ); } -DiscordRPCplugin::DiscordRPCplugin( PluginManager* pluginManager, bool sync) : Plugin( pluginManager ) { - //if ( sync ) { // Does not need to be multithreaded - load( pluginManager ); - //} +DiscordRPCplugin::DiscordRPCplugin( PluginManager* pluginManager, bool sync ) : + PluginBase( pluginManager ) { + if ( sync ) { + load( pluginManager ); + } else { +#if defined( dcRPC_THREADED ) && dcRPC_THREADED == 1 + mThreadPool->run( [this, pluginManager] { load( pluginManager ); } ); +#else + load( pluginManager ); +#endif + } } DiscordRPCplugin::~DiscordRPCplugin() { waitUntilLoaded(); mShuttingDown = true; - mManager->unsubscribeMessages( this ); - unsubscribeFileSystemListener(); - - for ( const auto& editor : mEditors ) { - for ( auto& kb : mKeyBindings ) { - editor.first->getKeyBindings().removeCommandKeybind( kb.first ); - if ( editor.first->hasDocument() ) - editor.first->getDocument().removeCommand( kb.first ); - } - for ( auto listener : editor.second ) - editor.first->removeEventListener( listener ); - editor.first->unregisterPlugin( this ); - } -} +} void DiscordRPCplugin::load( PluginManager* pluginManager ) { Clock clock; - //mClock = Clock(); AtomicBoolScopedOp loading( mLoading, true ); - pluginManager->subscribeMessages( this, - [this]( const auto& notification ) -> PluginRequestHandle { - return processMessage( notification ); - } ); std::vector paths; std::string path( pluginManager->getResourcesPath() + "plugins/discordRPC.json" ); @@ -57,13 +49,34 @@ void DiscordRPCplugin::load( PluginManager* pluginManager ) { mConfigPath = path; paths.emplace_back( path ); } - if (paths.empty() ) - return; - for ( const auto& tpath : paths ) { - try { - loadConfig( tpath, mConfigPath == tpath ); - } catch ( const json::exception& e ) { - Log::error( "DiscordRPCplugin::load - Parsing config \"%s\" failed:\n%s", tpath.c_str(), e.what() ); + std::string data; + if ( !FileSystem::fileGet( path, data ) ) + return; + mConfigHash = String::hash( data ); + + json j; + try { + j = json::parse( data, nullptr, true, true ); + } catch ( const json::exception& e ) { + Log::error( "DiscordRPCplugin::load - Error parsing config from path %s, error: %s, config " + "file content:\n%s", + path.c_str(), e.what(), data.c_str() ); + // Recreate it + j = json::parse( "{\n \"config\":{},\n \"keybindings\":{},\n}\n", nullptr, true, true ); + } + + bool updateConfigFile = false; + + if ( j.contains( "config" ) ) { + auto& config = j["config"]; + + } + + if ( updateConfigFile ) { + std::string newData = j.dump( 2 ); + if ( newData != data ) { + FileSystem::fileWrite( path, newData ); + mConfigHash = String::hash( newData ); } } @@ -72,107 +85,4 @@ void DiscordRPCplugin::load( PluginManager* pluginManager ) { setReady( clock.getElapsedTime() ); } -void DiscordRPCplugin::loadConfig( const std::string& path, bool updateConfigFile ) { - std::string data; - if ( !FileSystem::fileGet( path, data ) ) - return; - json j; - try { - j = json::parse( data, nullptr, true, true); - } catch ( const json::exception& e) { - Log::error( "DiscordRPCplugin::loadConfig - Error parsing config from", - "path %s, error: %s, config file content:\n%s", - path.c_str(), e.what(), data.c_str()); - if ( !updateConfigFile ) - return; - // TODO: Create a default config - // See: LinterPlugin:L100 - } - - if ( updateConfigFile ) { - mConfigHash = String::hash( data ); - } - - if (j.contains( "config" ) ) { - auto& config = j["config"]; - // TODO: Figure out config options - } - - if ( mKeyBindings.empty() ) { - // mKeyBindings["command-name"] = "mod+x"; // Syntax example - - } - - auto& kb = j["keybindings"]; - auto list = {"command-name"}; // { "command-name", "command2-name" } - for (const auto& key : list ) { - if ( kb.contains( key ) ) { - if ( !kb[key].empty() ) - mKeyBindings[key] = kb[key]; - } else if ( updateConfigFile ) - kb[key] = mKeyBindings[key]; - } -} - -void DiscordRPCplugin::onRegister( UICodeEditor* editor ) { - Lock l( mDocMutex ); - std::vector listeners; - - // Use this pattern to register your event listeners - // TODO: Add a link to where all the events are defined -// listeners.push_back( -// editor->addEventListener( Event::OnX, [this]( const Event* event ) { -// -// } -// ) - - if ( editor->hasDocument() ) { - auto& doc = editor->getDocument(); - -// doc.setCommand( "command-name", [this]( TextDocument::Client* client ) { -// // Action behavior -// } - } - - mEditors.insert( { editor, listeners } ); - mDocs.insert( editor->getDocumentRef().get() ); - mEditorDocs[editor] = editor->getDocumentRef().get(); -} - -void DiscordRPCplugin::onUnregister( UICodeEditor* editor ) { - if ( mShuttingDown ) - return; - - Lock l( mDocMutex ); - TextDocument* doc = mEditorDocs[editor]; - auto cbs = mEditors[editor]; - for ( auto listener : cbs ) - editor->removeEventListener( listener ); - mEditors.erase( editor ); - mEditorDocs.erase( editor ); - for ( auto editorIt : mEditorDocs ) - if ( editorIt.second == doc ) - return; - - for ( auto& kb : mKeyBindings ) { - editor->getKeyBindings().removeCommandKeybind( kb.first ); - if ( editor->hasDocument() ) - editor->getDocument().removeCommand( kb.first ); - } - - mDocs.erase( doc ); - mDirtyDoc.erase( doc ); -} - -void DiscordRPCplugin::update( UICodeEditor* editor ) { - if (mClock.getElapsedTime().asMilliseconds() >= 500) { - - mClock.restart(); - } -} - -PluginRequestHandle DiscordRPCplugin::processMessage( const PluginMessage& notification ) { - return {}; -} - } // namespace ecode \ No newline at end of file diff --git a/src/tools/ecode/plugins/discordRPC/discordRPCplugin.hpp b/src/tools/ecode/plugins/discordRPC/discordRPCplugin.hpp index 5ca8555ef..27a3268f1 100644 --- a/src/tools/ecode/plugins/discordRPC/discordRPCplugin.hpp +++ b/src/tools/ecode/plugins/discordRPC/discordRPCplugin.hpp @@ -3,68 +3,49 @@ #include "../plugin.hpp" #include "../pluginmanager.hpp" -#include -#include -#include + #include -#include +#include +#include #include +#include + using namespace EE; using namespace EE::System; -using namespace EE::UI; +//using namespace EE::UI; + namespace ecode { -class DiscordRPCplugin : public Plugin { +class DiscordRPCplugin : public PluginBase { public: static PluginDefinition Definition() { return { - "discrdrpc", // ID - "Discord Rich Presence", // Title - Human name - "Show your friends what you are up to through the discord Rich Presence system", // Description - DiscordRPCplugin::New, // Creator function - { 0, 0, 0 }, // Version, + "discrdrpc", + "Discord Rich Presence", + "Show your friends what you are up to through the discord Rich Presence system", + DiscordRPCplugin::New, + { 0, 0, 0 }, DiscordRPCplugin::NewSync }; } static Plugin* New( PluginManager* pluginManager ); - + static Plugin* NewSync( PluginManager* pluginManager ); - + virtual ~DiscordRPCplugin(); - - std::string getId() { return Definition().id; } - - std::string getTitle() { return Definition().name; } - - std::string getDescription() { return Definition().description; } - virtual String::HashType getConfigFileHash() { return mConfigHash; } + std::string getId() override { return Definition().id; } - void onRegister( UICodeEditor* ); + std::string getTitle() override { return Definition().name; } - void onUnregister( UICodeEditor* ); - - void update( UICodeEditor* ); + std::string getDescription() override { return Definition().description; } protected: - std::unordered_map> mEditors; - std::unordered_set mDocs; - std::unordered_map mEditorDocs; - std::unordered_map> mDirtyDoc; - std::map mKeyBindings; /* cmd, shortcut */ - String::HashType mConfigHash{ 0 }; - Mutex mDocMutex; - Clock mClock; + void load ( PluginManager* pluginManager ); DiscordRPCplugin( PluginManager* pluginManager, bool sync ); - - void load( PluginManager* pluginManager ); - - void loadConfig( const std::string& path, bool updateConfigFile ); - - PluginRequestHandle processMessage( const PluginMessage& notification ); }; } // namespace ecode