* Use `vars()` instead of directly accessing `__dict__` * Remove `auto_update` from RareGame's metadata * Correct check for updating the Steam App ID (We want to keep any changes from the user) * Collect both Wine and Proton prefixes when removing overlay registry keys. * Add few convenience functions in config_helper and paths.
116 lines
3.8 KiB
Python
116 lines
3.8 KiB
Python
import platform
|
|
import time
|
|
from logging import getLogger
|
|
|
|
import pypresence.exceptions
|
|
from PyQt5.QtCore import QObject, QSettings
|
|
from pypresence import Presence
|
|
|
|
from rare.shared import LegendaryCoreSingleton, GlobalSignalsSingleton
|
|
from rare.models.options import options
|
|
|
|
client_id = "830732538225360908"
|
|
logger = getLogger("RPC")
|
|
|
|
|
|
class DiscordRPC(QObject):
|
|
def __init__(self):
|
|
super(DiscordRPC, self).__init__()
|
|
self.RPC = None
|
|
self.state = 1 # 0: game, 1: always active, 2: off
|
|
self.core = LegendaryCoreSingleton()
|
|
self.signals = GlobalSignalsSingleton()
|
|
|
|
self.settings = QSettings()
|
|
if self.settings.value(*options.rpc_enable) == 1: # show always
|
|
self.state = 2
|
|
self.set_discord_rpc()
|
|
|
|
self.signals.discord_rpc.set_title.connect(self.update_presence)
|
|
self.signals.discord_rpc.apply_settings.connect(self.changed_settings)
|
|
|
|
def update_presence(self, app_name):
|
|
self.set_discord_rpc(app_name)
|
|
|
|
def changed_settings(self, game_running: list = None):
|
|
value = self.settings.value(*options.rpc_enable)
|
|
if value == 2:
|
|
self.remove_rpc()
|
|
return
|
|
if not game_running and value == 0:
|
|
self.remove_rpc()
|
|
elif not game_running:
|
|
self.set_discord_rpc()
|
|
else:
|
|
self.set_discord_rpc(game_running[0])
|
|
|
|
def remove_rpc(self):
|
|
if self.settings.value(*options.rpc_enable) != 1:
|
|
if not self.RPC:
|
|
return
|
|
try:
|
|
self.RPC.close()
|
|
except Exception:
|
|
logger.warning("Already closed")
|
|
del self.RPC
|
|
self.RPC = None
|
|
logger.info("Remove RPC")
|
|
self.state = 2
|
|
else:
|
|
self.state = 1
|
|
self.set_discord_rpc()
|
|
|
|
def set_discord_rpc(self, app_name=None):
|
|
if not self.RPC:
|
|
try:
|
|
self.RPC = Presence(
|
|
client_id
|
|
) # Rare app: https://discord.com/developers/applications
|
|
self.RPC.connect()
|
|
except ConnectionRefusedError as e:
|
|
logger.warning(f"Discord is not active\n{e}")
|
|
self.RPC = None
|
|
return
|
|
except FileNotFoundError as e:
|
|
logger.warning(f"File not found error\n{e}")
|
|
self.RPC = None
|
|
return
|
|
except pypresence.exceptions.InvalidPipe as e:
|
|
logger.error(f"Is Discord running? \n{e}")
|
|
self.RPC = None
|
|
return
|
|
except Exception as e:
|
|
logger.error(str(e))
|
|
self.RPC = None
|
|
return
|
|
self.update_rpc(app_name)
|
|
|
|
def update_rpc(self, app_name=None):
|
|
if self.settings.value(*options.rpc_enable) == 2 or (
|
|
not app_name and self.settings.value(*options.rpc_enable) == 0
|
|
):
|
|
self.remove_rpc()
|
|
return
|
|
title = None
|
|
if not app_name:
|
|
self.RPC.update(
|
|
large_image="logo", details="https://github.com/RareDevs/Rare"
|
|
)
|
|
return
|
|
if self.settings.value(*options.rpc_name):
|
|
try:
|
|
title = self.core.get_installed_game(app_name).title
|
|
except AttributeError:
|
|
logger.error(f"Could not get title of game: {app_name}")
|
|
title = app_name
|
|
start = None
|
|
if self.settings.value(*options.rpc_time):
|
|
start = str(time.time()).split(".")[0]
|
|
os = None
|
|
if self.settings.value(*options.rpc_os):
|
|
os = f"via Rare on {platform.system()}"
|
|
|
|
self.RPC.update(
|
|
large_image="logo", details=title, large_text=title, state=os, start=start
|
|
)
|
|
self.state = 0
|