Skip to content

Commit

Permalink
Fix config load.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpartanJ committed Feb 12, 2025
1 parent e2f8bae commit 163056c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 35 deletions.
4 changes: 2 additions & 2 deletions bin/assets/plugins/discordRPC.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"config": {
"appID": "1335730393948749898",
"appID": "1339026777158455336",
"doLanguageIcons": true,
"iconBindings": {
".htaccess": "https://github.com/vyfor/icons/raw/master/icons/onyx/gear.png",
Expand Down Expand Up @@ -107,4 +107,4 @@
}
},
"keybindings": {}
}
}
78 changes: 52 additions & 26 deletions src/tools/ecode/plugins/discordRPC/discordRPCplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ using json = nlohmann::json;

namespace ecode {

static const auto DEFAULT_CLIENT_ID = "1339026777158455336";

Plugin* DiscordRPCplugin::New( PluginManager* pluginManager ) {
return eeNew( DiscordRPCplugin, ( pluginManager, false ) );
}
Expand All @@ -32,8 +34,11 @@ DiscordRPCplugin::DiscordRPCplugin( PluginManager* pluginManager, bool sync ) :

DiscordRPCplugin::~DiscordRPCplugin() {
waitUntilLoaded();
if ( mIPC.isConnected() )
mIPC.clearActivity();
mShuttingDown = true;
}

void DiscordRPCplugin::load( PluginManager* pluginManager ) {
Clock clock;
AtomicBoolScopedOp loading( mLoading, true );
Expand All @@ -53,10 +58,38 @@ void DiscordRPCplugin::load( PluginManager* pluginManager ) {
mConfigPath = path;
paths.emplace_back( path );
}

if ( paths.empty() )
return;

for ( const auto& ipath : paths ) {
try {
loadDiscordRPCConfig( ipath, mConfigPath == ipath );
} catch ( const json::exception& e ) {
Log::error( "Parsing DiscordRPCplugin config \"%s\" failed:\n%s", ipath.c_str(),
e.what() );
}
}

if ( getUISceneNode() ) {
mIPC.UIReady = true;
initIPC();
} else {
mIPC.IsReconnectScheduled = true;
}

mReady = true;
fireReadyCbs();
setReady( clock.getElapsedTime() );
}

void DiscordRPCplugin::loadDiscordRPCConfig( const std::string& path, bool updateConfigFile ) {
std::string data;
if ( !FileSystem::fileGet( path, data ) )
return;
mConfigHash = String::hash( data );

if ( updateConfigFile )
mConfigHash = String::hash( data );

json j;
try {
Expand All @@ -69,23 +102,27 @@ void DiscordRPCplugin::load( PluginManager* pluginManager ) {
j = json::parse( "{\n \"config\":{},\n \"keybindings\":{},\n}\n", nullptr, true, true );
}

bool updateConfigFile = false;

if ( j.contains( "config" ) ) {
auto& config = j["config"];
mcLangBindings = config["iconBindings"];
if ( config.contains( "appID" ) ) {
mIPC.ClientID = config.value( "appID", "1335730393948749898" );
} else {
mIPC.ClientID = "1335730393948749898";
updateConfigFile = true;
}
if ( config.contains( "doLanguageIcons" ) ) {

if ( config.contains( "iconBindings" ) )
mcLangBindings = config["iconBindings"];
else
config["iconBindings"] = mcLangBindings;

if ( config.contains( "appID" ) )
mIPC.ClientID = config.value( "appID", DEFAULT_CLIENT_ID );
else
config["appID"] = DEFAULT_CLIENT_ID;

if ( config.contains( "doLanguageIcons" ) )
mcDoLangIcon = config.value( "doLanguageIcons", true );
} else {
mIPC.ClientID = true;
updateConfigFile = true;
}
else
config["doLanguageIcons"] = true;

if ( updateConfigFile && config.contains( "iconBindings" ) &&
config["iconBindings"].is_null() )
config["iconBindings"] = nlohmann::json::object();
}

if ( updateConfigFile ) {
Expand All @@ -95,17 +132,6 @@ void DiscordRPCplugin::load( PluginManager* pluginManager ) {
mConfigHash = String::hash( newData );
}
}

if ( getUISceneNode() ) {
mIPC.UIReady = true;
initIPC();
} else {
mIPC.IsReconnectScheduled = true;
}

mReady = true;
fireReadyCbs();
setReady( clock.getElapsedTime() );
}

void DiscordRPCplugin::initIPC() {
Expand Down
2 changes: 2 additions & 0 deletions src/tools/ecode/plugins/discordRPC/discordRPCplugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class DiscordRPCplugin : public PluginBase {

DiscordRPCplugin( PluginManager* pluginManager, bool sync );

void loadDiscordRPCConfig( const std::string& path, bool updateConfigFile );

void initIPC();
};

Expand Down
14 changes: 7 additions & 7 deletions src/tools/ecode/plugins/discordRPC/sdk/ipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,17 @@ void DiscordIPC::sendPacket( DiscordIPCOpcodes opcode, json j ) {
} bytes;

bytes.value = opcode;
for ( int i = 0; i <= 3; ++i ) {
for ( int i = 0; i <= 3; ++i )
data.push_back( bytes.bytes[i] );
}

bytes.value = packet.length();
for ( int i = 0; i <= 3; ++i ) {
for ( int i = 0; i <= 3; ++i )
data.push_back( bytes.bytes[i] );
}

for ( char c : packet ) {
for ( char c : packet )
data.push_back( static_cast<uint8_t>( c ) );
}

Log::debug( "dcIPC: sending packet: %s", packet );

#if defined( EE_PLATFORM_POSIX )

Expand All @@ -276,7 +275,6 @@ void DiscordIPC::sendPacket( DiscordIPCOpcodes opcode, json j ) {
char buffer[1024];
recv( mSocket, buffer, sizeof( buffer ), 0 );

// return bytesRead;
#elif EE_PLATFORM == EE_PLATFORM_WIN
DWORD bytesSent;
if ( !WriteFile( mSocket, data.data(), data.size(), &bytesSent, nullptr ) ) {
Expand All @@ -298,6 +296,8 @@ void DiscordIPC::sendPacket( DiscordIPCOpcodes opcode, json j ) {
return;
}

Log::debug( "dcIPC - received sendPacket response: %s", buffer );

#endif
}

Expand Down

0 comments on commit 163056c

Please sign in to comment.