mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-03-22 15:05:03 +01:00
Move DiscordRPC to separate threaad
This commit is contained in:
14
Globals.h
14
Globals.h
@@ -17,6 +17,8 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
#include "utilities.h"
|
#include "utilities.h"
|
||||||
#include "motiontelemetry.h"
|
#include "motiontelemetry.h"
|
||||||
#include "ref/discord-rpc/include/discord_rpc.h"
|
#include "ref/discord-rpc/include/discord_rpc.h"
|
||||||
|
#include <map>
|
||||||
|
|
||||||
#ifdef WITH_UART
|
#ifdef WITH_UART
|
||||||
#include "uart.h"
|
#include "uart.h"
|
||||||
#endif
|
#endif
|
||||||
@@ -24,17 +26,15 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
#include "zmq_input.h"
|
#include "zmq_input.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct DiscordData
|
|
||||||
{
|
|
||||||
DiscordRichPresence dcRcp;
|
|
||||||
char *scnName = "";
|
|
||||||
};
|
|
||||||
|
|
||||||
struct global_settings {
|
struct global_settings {
|
||||||
// members
|
// members
|
||||||
// data items
|
// data items
|
||||||
// TODO: take these out of the settings
|
// TODO: take these out of the settings
|
||||||
DiscordData dcData;
|
|
||||||
|
/// <summary>
|
||||||
|
/// Mapa z watkami w formacie <std::string nazwa, std::thread watek>
|
||||||
|
/// </summary>
|
||||||
|
std::map<std::string, std::thread> threads = {};
|
||||||
bool shiftState{ false }; //m7todo: brzydko
|
bool shiftState{ false }; //m7todo: brzydko
|
||||||
bool ctrlState{ false };
|
bool ctrlState{ false };
|
||||||
bool altState{ false };
|
bool altState{ false };
|
||||||
|
|||||||
108
application.cpp
108
application.cpp
@@ -176,6 +176,60 @@ int eu07_application::run_crashgui()
|
|||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
void eu07_application::DiscordRPCService()
|
||||||
|
{
|
||||||
|
// initialize discord-rpc
|
||||||
|
WriteLog("Initializing Discord Rich Presence...");
|
||||||
|
static const char *discord_app_id = "1343662664504840222";
|
||||||
|
DiscordEventHandlers handlers;
|
||||||
|
memset(&handlers, 0, sizeof(handlers));
|
||||||
|
Discord_Initialize(discord_app_id, &handlers, 1, nullptr);
|
||||||
|
|
||||||
|
std::string rpcScnName = Global.SceneryFile;
|
||||||
|
if (rpcScnName[0] == '$')
|
||||||
|
rpcScnName.erase(0, 1);
|
||||||
|
rpcScnName.erase(rpcScnName.size() - 4, 4);
|
||||||
|
if (rpcScnName.find('_') != std::string::npos)
|
||||||
|
{
|
||||||
|
std::replace(rpcScnName.begin(), rpcScnName.end(), '_', ' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate startup timestamp
|
||||||
|
auto now = std::chrono::system_clock::now();
|
||||||
|
auto now_c = std::chrono::system_clock::to_time_t(now);
|
||||||
|
|
||||||
|
// Init RPC object
|
||||||
|
static DiscordRichPresence discord_rpc;
|
||||||
|
memset(&discord_rpc, 0, sizeof(discord_rpc));
|
||||||
|
// realworld timestamp from datetime
|
||||||
|
discord_rpc.startTimestamp = static_cast<int64_t>(now_c);
|
||||||
|
static std::string state = "Sceneria: " + rpcScnName;
|
||||||
|
discord_rpc.state = state.c_str();
|
||||||
|
discord_rpc.details = "Ładowanie scenerii";
|
||||||
|
discord_rpc.largeImageKey = "logo";
|
||||||
|
discord_rpc.largeImageText = "MaSzyna";
|
||||||
|
|
||||||
|
// First RPC upload
|
||||||
|
Discord_UpdatePresence(&discord_rpc);
|
||||||
|
|
||||||
|
// run loop
|
||||||
|
while (!glfwWindowShouldClose(m_windows.front()) && !m_modestack.empty())
|
||||||
|
{
|
||||||
|
// Discord RPC updater
|
||||||
|
if (simulation::is_ready)
|
||||||
|
{
|
||||||
|
std::string PlayerVehicle = simulation::Train->name();
|
||||||
|
// make to upper
|
||||||
|
for (auto &c : PlayerVehicle)
|
||||||
|
c = toupper(c);
|
||||||
|
|
||||||
|
PlayerVehicle = "Prowadzi: " + PlayerVehicle;
|
||||||
|
discord_rpc.details = PlayerVehicle.c_str();
|
||||||
|
Discord_UpdatePresence(&discord_rpc);
|
||||||
|
}
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(5000)); // update RPC every 5 secs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
eu07_application::init( int Argc, char *Argv[] ) {
|
eu07_application::init( int Argc, char *Argv[] ) {
|
||||||
@@ -241,46 +295,15 @@ eu07_application::init( int Argc, char *Argv[] ) {
|
|||||||
if (!init_network())
|
if (!init_network())
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
// initialize discord-rpc
|
// Run DiscordRPC service
|
||||||
WriteLog("Initializing Discord Rich Presence...");
|
std::thread sDiscordRPC(&eu07_application::DiscordRPCService, this);
|
||||||
static const char *discord_app_id = "1343662664504840222";
|
Global.threads.emplace("DiscordRPC", std::move(sDiscordRPC));
|
||||||
DiscordEventHandlers handlers;
|
|
||||||
memset(&handlers, 0, sizeof(handlers));
|
|
||||||
Discord_Initialize(discord_app_id, &handlers, 1, nullptr);
|
|
||||||
|
|
||||||
std::string rpcScnName = Global.SceneryFile;
|
|
||||||
if (rpcScnName[0] == '$')
|
|
||||||
rpcScnName.erase(0, 1);
|
|
||||||
rpcScnName.erase(rpcScnName.size() - 4, 4);
|
|
||||||
if (rpcScnName.find('_') != std::string::npos)
|
|
||||||
{
|
|
||||||
std::replace(rpcScnName.begin(), rpcScnName.end(), '_', ' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
// calculate startup timestamp
|
|
||||||
auto now = std::chrono::system_clock::now();
|
|
||||||
auto now_c = std::chrono::system_clock::to_time_t(now);
|
|
||||||
|
|
||||||
// Init RPC object
|
|
||||||
static DiscordRichPresence discord_rpc;
|
|
||||||
memset(&discord_rpc, 0, sizeof(discord_rpc));
|
|
||||||
// realworld timestamp from datetime
|
|
||||||
discord_rpc.startTimestamp = static_cast<int64_t>(now_c);
|
|
||||||
static std::string state = "Sceneria: " + rpcScnName;
|
|
||||||
discord_rpc.state = state.c_str();
|
|
||||||
discord_rpc.details = "Ładowanie scenerii";
|
|
||||||
discord_rpc.largeImageKey = "logo";
|
|
||||||
discord_rpc.largeImageText = "MaSzyna";
|
|
||||||
Global.dcData.dcRcp = discord_rpc;
|
|
||||||
|
|
||||||
// First RPC upload
|
|
||||||
Discord_UpdatePresence(&Global.dcData.dcRcp);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
double eu07_application::generate_sync() {
|
double eu07_application::generate_sync() {
|
||||||
if (Timer::GetDeltaTime() == 0.0)
|
if (Timer::GetDeltaTime() == 0.0)
|
||||||
return 0.0;
|
return 0.0;
|
||||||
@@ -321,19 +344,6 @@ eu07_application::run() {
|
|||||||
// main application loop
|
// main application loop
|
||||||
while (!glfwWindowShouldClose( m_windows.front() ) && !m_modestack.empty())
|
while (!glfwWindowShouldClose( m_windows.front() ) && !m_modestack.empty())
|
||||||
{
|
{
|
||||||
// Discord RPC updater
|
|
||||||
if (simulation::is_ready)
|
|
||||||
{
|
|
||||||
std::string PlayerVehicle = simulation::Train->name();
|
|
||||||
// make to upper
|
|
||||||
for (auto &c : PlayerVehicle) c = toupper(c);
|
|
||||||
|
|
||||||
PlayerVehicle = "Prowadzi: " + PlayerVehicle;
|
|
||||||
Global.dcData.dcRcp.details = PlayerVehicle.c_str();
|
|
||||||
Discord_UpdatePresence(&Global.dcData.dcRcp);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Timer::subsystem.mainloop_total.start();
|
Timer::subsystem.mainloop_total.start();
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ public:
|
|||||||
int
|
int
|
||||||
run();
|
run();
|
||||||
// issues request for a worker thread to perform specified task. returns: true if task was scheduled
|
// issues request for a worker thread to perform specified task. returns: true if task was scheduled
|
||||||
|
|
||||||
|
void eu07_application::DiscordRPCService(); // discord rich presence service function (runs as separate thread)
|
||||||
bool
|
bool
|
||||||
request( python_taskqueue::task_request const &Task );
|
request( python_taskqueue::task_request const &Task );
|
||||||
// ensures the main thread holds the python gil and can safely execute python calls
|
// ensures the main thread holds the python gil and can safely execute python calls
|
||||||
|
|||||||
Reference in New Issue
Block a user