mirror of
https://github.com/SpartanJ/eepp.git
synced 2026-08-18 06:55:48 +03:00
Migrate to PluginBase
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
#include "discordRPCplugin.hpp"
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <eepp/system/filesystem.hpp>
|
||||
|
||||
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<std::string> 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<Uint32> 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
|
||||
@@ -3,68 +3,49 @@
|
||||
|
||||
#include "../plugin.hpp"
|
||||
#include "../pluginmanager.hpp"
|
||||
#include <eepp/config.hpp>
|
||||
#include <eepp/system/mutex.hpp>
|
||||
#include <eepp/system/process.hpp>
|
||||
|
||||
#include <eepp/system/threadpool.hpp>
|
||||
#include <eepp/ui/uicodeeditor.hpp>
|
||||
#include <eepp/system/mutex.hpp>
|
||||
#include <eepp/system/filesystem.hpp>
|
||||
#include <eepp/system/scopedop.hpp>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
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<UICodeEditor*, std::vector<Uint32>> mEditors;
|
||||
std::unordered_set<TextDocument*> mDocs;
|
||||
std::unordered_map<UICodeEditor*, TextDocument*> mEditorDocs;
|
||||
std::unordered_map<TextDocument*, std::unique_ptr<Clock>> mDirtyDoc;
|
||||
std::map<std::string, std::string> 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
|
||||
|
||||
Reference in New Issue
Block a user