2023-01-11 08:40:57 +13:00
|
|
|
import json
|
|
|
|
import logging
|
2023-01-13 04:32:03 +13:00
|
|
|
from enum import IntEnum
|
2023-01-11 08:40:57 +13:00
|
|
|
|
2023-12-26 05:21:41 +13:00
|
|
|
from PyQt5.QtCore import QObject, pyqtSignal, QTimer, pyqtSlot, Qt
|
2023-01-11 08:40:57 +13:00
|
|
|
from PyQt5.QtNetwork import QLocalSocket
|
|
|
|
from PyQt5.QtWidgets import QMessageBox
|
2023-01-15 07:56:46 +13:00
|
|
|
from legendary.models.game import Game
|
2023-01-11 08:40:57 +13:00
|
|
|
|
|
|
|
from rare.models.launcher import ErrorModel, Actions, FinishedModel, StateChangedModel
|
|
|
|
|
|
|
|
logger = logging.getLogger("GameProcess")
|
|
|
|
|
|
|
|
|
|
|
|
class GameProcess(QObject):
|
2023-01-13 04:32:03 +13:00
|
|
|
# int: code
|
|
|
|
launched = pyqtSignal(int)
|
|
|
|
# int: code
|
|
|
|
finished = pyqtSignal(int)
|
|
|
|
|
|
|
|
class Code(IntEnum):
|
|
|
|
SUCCESS = 0
|
2023-01-25 02:28:01 +13:00
|
|
|
ERROR = 1
|
2023-01-13 04:32:03 +13:00
|
|
|
TIMEOUT = -1
|
|
|
|
ON_STARTUP = -1234
|
2023-01-11 08:40:57 +13:00
|
|
|
|
2023-01-15 07:56:46 +13:00
|
|
|
def __init__(self, game: Game):
|
2023-01-11 08:40:57 +13:00
|
|
|
super(GameProcess, self).__init__()
|
2023-01-15 07:56:46 +13:00
|
|
|
self.game = game
|
2023-01-13 04:32:03 +13:00
|
|
|
self.on_startup = False
|
2023-01-11 08:40:57 +13:00
|
|
|
self.tried_connections = 0
|
2023-01-13 04:32:03 +13:00
|
|
|
|
2023-03-13 11:04:11 +13:00
|
|
|
self.timer = QTimer(self)
|
2023-01-13 04:32:03 +13:00
|
|
|
self.timer.timeout.connect(self.__connect)
|
2023-01-11 08:40:57 +13:00
|
|
|
self.socket = QLocalSocket()
|
2023-01-15 07:56:46 +13:00
|
|
|
self.socket_name = f"rare_{self.game.app_name}"
|
|
|
|
|
|
|
|
self.socket.connected.connect(self.__on_connected)
|
2023-01-11 08:40:57 +13:00
|
|
|
try:
|
2023-01-15 07:56:46 +13:00
|
|
|
self.socket.errorOccurred.connect(self.__on_error)
|
2023-01-11 08:40:57 +13:00
|
|
|
except AttributeError:
|
2023-01-13 04:32:03 +13:00
|
|
|
# QTimer.singleShot(
|
|
|
|
# 100,
|
|
|
|
# lambda: self._error_occurred(QLocalSocket.UnknownSocketError) if self.socket.error() else None
|
|
|
|
# )
|
2023-01-11 08:40:57 +13:00
|
|
|
logger.warning("Do not handle errors on QLocalSocket, because of an old qt version")
|
2023-01-15 07:56:46 +13:00
|
|
|
self.socket.readyRead.connect(self.__on_message)
|
2023-01-13 04:32:03 +13:00
|
|
|
self.socket.disconnected.connect(self.__close)
|
2023-01-11 08:40:57 +13:00
|
|
|
|
2023-01-21 13:15:06 +13:00
|
|
|
def connect_to_server(self, on_startup: bool):
|
2023-01-13 04:32:03 +13:00
|
|
|
self.on_startup = on_startup
|
|
|
|
self.timer.start(200)
|
2023-01-11 08:40:57 +13:00
|
|
|
|
2023-01-13 04:32:03 +13:00
|
|
|
@pyqtSlot()
|
|
|
|
def __close(self):
|
|
|
|
try:
|
|
|
|
self.socket.close()
|
|
|
|
except RuntimeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def __connect(self):
|
|
|
|
self.socket.connectToServer(self.socket_name)
|
2023-01-11 08:40:57 +13:00
|
|
|
self.tried_connections += 1
|
|
|
|
|
|
|
|
if self.tried_connections > 50: # 10 seconds
|
2024-01-10 03:33:45 +13:00
|
|
|
QMessageBox.warning(
|
|
|
|
None,
|
|
|
|
self.tr("Error - {}").format(self.game.app_title),
|
|
|
|
self.tr(
|
|
|
|
"Connection to game launcher for <b>{}</b> failed due to timeout.\n"
|
|
|
|
"This is usually do it the game or Rare's game launcher already running"
|
|
|
|
).format(self.game.app_name)
|
|
|
|
)
|
2023-01-11 08:40:57 +13:00
|
|
|
self.timer.stop()
|
2024-01-28 08:20:17 +13:00
|
|
|
self.tried_connections = 0
|
2023-01-13 04:32:03 +13:00
|
|
|
self.finished.emit(GameProcess.Code.TIMEOUT)
|
2023-01-11 08:40:57 +13:00
|
|
|
|
|
|
|
@pyqtSlot()
|
2023-01-15 07:56:46 +13:00
|
|
|
def __on_message(self):
|
2023-01-11 08:40:57 +13:00
|
|
|
message = self.socket.readAll().data()
|
|
|
|
if not message.startswith(b"{"):
|
|
|
|
logger.error(f"Received unsupported message: {message.decode('utf-8')}")
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
data = json.loads(message)
|
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
logger.error(e)
|
|
|
|
logger.error("Could not load json data")
|
|
|
|
return
|
|
|
|
|
2023-01-13 04:32:03 +13:00
|
|
|
action = data.get("action", False)
|
2023-01-11 08:40:57 +13:00
|
|
|
|
2023-01-13 04:32:03 +13:00
|
|
|
if not action:
|
2023-01-11 08:40:57 +13:00
|
|
|
logger.error("Got unexpected action")
|
|
|
|
elif action == Actions.finished:
|
2023-01-15 07:56:46 +13:00
|
|
|
logger.info(f"{self.game.app_name} {self.game.app_title} finished")
|
2023-01-11 08:40:57 +13:00
|
|
|
model = FinishedModel.from_json(data)
|
|
|
|
self.socket.close()
|
2023-01-15 07:56:46 +13:00
|
|
|
self.__game_finished(model.exit_code)
|
2023-01-11 08:40:57 +13:00
|
|
|
elif action == Actions.error:
|
|
|
|
model = ErrorModel.from_json(data)
|
2023-01-15 07:56:46 +13:00
|
|
|
logger.error(f"Error in game {self.game.app_title}: {model.error_string}")
|
2023-01-11 08:40:57 +13:00
|
|
|
self.socket.close()
|
2023-01-25 02:28:01 +13:00
|
|
|
self.__game_finished(GameProcess.Code.ERROR)
|
2023-01-11 08:40:57 +13:00
|
|
|
QMessageBox.warning(None, "Error", self.tr(
|
2023-01-15 07:56:46 +13:00
|
|
|
"Error in game {}:\n{}").format(self.game.app_title, model.error_string))
|
2023-01-11 08:40:57 +13:00
|
|
|
elif action == Actions.state_update:
|
|
|
|
model = StateChangedModel.from_json(data)
|
|
|
|
if model.new_state == StateChangedModel.States.started:
|
|
|
|
logger.info("Launched Game")
|
2023-01-13 04:32:03 +13:00
|
|
|
self.launched.emit(GameProcess.Code.SUCCESS)
|
2023-01-11 08:40:57 +13:00
|
|
|
|
|
|
|
@pyqtSlot()
|
2023-01-15 07:56:46 +13:00
|
|
|
def __on_connected(self):
|
2023-01-11 08:40:57 +13:00
|
|
|
self.timer.stop()
|
2023-01-15 07:56:46 +13:00
|
|
|
logger.info(f"Connection established for {self.game.app_name} ({self.game.app_title})")
|
2023-01-11 08:40:57 +13:00
|
|
|
if self.on_startup:
|
2023-01-15 07:56:46 +13:00
|
|
|
logger.info(f"Found {self.game.app_name} ({self.game.app_title}) running at startup")
|
2023-01-13 04:32:03 +13:00
|
|
|
self.launched.emit(GameProcess.Code.ON_STARTUP)
|
2023-01-11 08:40:57 +13:00
|
|
|
|
|
|
|
@pyqtSlot(QLocalSocket.LocalSocketError)
|
2023-01-15 07:56:46 +13:00
|
|
|
def __on_error(self, _: QLocalSocket.LocalSocketError):
|
2023-01-11 08:40:57 +13:00
|
|
|
if self.on_startup:
|
2023-01-13 04:32:03 +13:00
|
|
|
self.timer.stop()
|
2023-01-15 07:56:46 +13:00
|
|
|
self.__close()
|
|
|
|
self.__game_finished(GameProcess.Code.ON_STARTUP) # 1234 is exit code for startup
|
2023-12-06 21:52:21 +13:00
|
|
|
else:
|
|
|
|
logger.error(f"{self.game.app_name} ({self.game.app_title}): {self.socket.errorString()}")
|
2023-01-11 08:40:57 +13:00
|
|
|
|
2023-01-15 07:56:46 +13:00
|
|
|
def __game_finished(self, exit_code: int):
|
2023-01-13 04:32:03 +13:00
|
|
|
self.finished.emit(exit_code)
|