|
10 | 10 | from galaxy.api.consts import Feature |
11 | 11 | from galaxy.api.errors import ImportInProgress, UnknownError |
12 | 12 | from galaxy.api.jsonrpc import ApplicationError, NotificationClient, Server |
13 | | -from galaxy.api.types import Achievement, Authentication, FriendInfo, Game, GameTime, LocalGame, NextStep |
| 13 | +from galaxy.api.types import Achievement, Authentication, FriendInfo, Game, GameTime, LocalGame, NextStep, GameLibrarySettings |
14 | 14 | from galaxy.task_manager import TaskManager |
15 | 15 |
|
16 | 16 | class JSONEncoder(json.JSONEncoder): |
@@ -46,6 +46,7 @@ def __init__(self, platform, version, reader, writer, handshake_token): |
46 | 46 |
|
47 | 47 | self._achievements_import_in_progress = False |
48 | 48 | self._game_times_import_in_progress = False |
| 49 | + self._game_library_settings_import_in_progress = False |
49 | 50 |
|
50 | 51 | self._persistent_cache = dict() |
51 | 52 |
|
@@ -109,6 +110,9 @@ def __init__(self, platform, version, reader, writer, handshake_token): |
109 | 110 | self._register_method("start_game_times_import", self._start_game_times_import) |
110 | 111 | self._detect_feature(Feature.ImportGameTime, ["get_game_time"]) |
111 | 112 |
|
| 113 | + self._register_method("start_game_library_settings_import", self._start_game_library_settings_import) |
| 114 | + self._detect_feature(Feature.ImportGameLibrarySettings, ["get_game_library_settings"]) |
| 115 | + |
112 | 116 | async def __aenter__(self): |
113 | 117 | return self |
114 | 118 |
|
@@ -402,6 +406,20 @@ def _game_time_import_failure(self, game_id: str, error: ApplicationError) -> No |
402 | 406 | def _game_times_import_finished(self) -> None: |
403 | 407 | self._notification_client.notify("game_times_import_finished", None) |
404 | 408 |
|
| 409 | + def _game_library_settings_import_success(self, game_library_settings: GameLibrarySettings) -> None: |
| 410 | + params = {"game_library_settings": game_library_settings} |
| 411 | + self._notification_client.notify("game_library_settings_import_success", params) |
| 412 | + |
| 413 | + def _game_library_settings_import_failure(self, game_id: str, error: ApplicationError) -> None: |
| 414 | + params = { |
| 415 | + "game_id": game_id, |
| 416 | + "error": error.json() |
| 417 | + } |
| 418 | + self._notification_client.notify("game_library_settings_import_failure", params) |
| 419 | + |
| 420 | + def _game_library_settings_import_finished(self) -> None: |
| 421 | + self._notification_client.notify("game_library_settings_import_finished", None) |
| 422 | + |
405 | 423 | def lost_authentication(self) -> None: |
406 | 424 | """Notify the client that integration has lost authentication for the |
407 | 425 | current user and is unable to perform actions which would require it. |
@@ -750,6 +768,63 @@ def game_times_import_complete(self) -> None: |
750 | 768 | (like updating cache). |
751 | 769 | """ |
752 | 770 |
|
| 771 | + async def _start_game_library_settings_import(self, game_ids: List[str]) -> None: |
| 772 | + if self._game_library_settings_import_in_progress: |
| 773 | + raise ImportInProgress() |
| 774 | + |
| 775 | + context = await self.prepare_game_library_settings_context(game_ids) |
| 776 | + |
| 777 | + async def import_game_library_settings(game_id, context_): |
| 778 | + try: |
| 779 | + game_library_settings = await self.get_game_library_settings(game_id, context_) |
| 780 | + self._game_library_settings_import_success(game_library_settings) |
| 781 | + except ApplicationError as error: |
| 782 | + self._game_library_settings_import_failure(game_id, error) |
| 783 | + except Exception: |
| 784 | + logging.exception("Unexpected exception raised in import_game_library_settings") |
| 785 | + self._game_library_settings_import_failure(game_id, UnknownError()) |
| 786 | + |
| 787 | + async def import_game_library_settings_set(game_ids_, context_): |
| 788 | + try: |
| 789 | + imports = [import_game_library_settings(game_id, context_) for game_id in game_ids_] |
| 790 | + await asyncio.gather(*imports) |
| 791 | + finally: |
| 792 | + self._game_library_settings_import_finished() |
| 793 | + self._game_library_settings_import_in_progress = False |
| 794 | + self.game_library_settings_import_complete() |
| 795 | + |
| 796 | + self._external_task_manager.create_task( |
| 797 | + import_game_library_settings_set(game_ids, context), |
| 798 | + "game library settings import", |
| 799 | + handle_exceptions=False |
| 800 | + ) |
| 801 | + self._game_library_settings_import_in_progress = True |
| 802 | + |
| 803 | + async def prepare_game_library_settings_context(self, game_ids: List[str]) -> Any: |
| 804 | + """Override this method to prepare context for get_game_library_settings. |
| 805 | + This allows for optimizations like batch requests to platform API. |
| 806 | + Default implementation returns None. |
| 807 | +
|
| 808 | + :param game_ids: the ids of the games for which game time are imported |
| 809 | + :return: context |
| 810 | + """ |
| 811 | + return None |
| 812 | + |
| 813 | + async def get_game_library_settings(self, game_id: str, context: Any) -> GameLibrarySettings: |
| 814 | + """Override this method to return the game library settings for the game |
| 815 | + identified by the provided game_id. |
| 816 | + This method is called by import task initialized by GOG Galaxy Client. |
| 817 | +
|
| 818 | + :param game_id: the id of the game for which the game time is returned |
| 819 | + :param context: the value returned from :meth:`prepare_game_library_settings_context` |
| 820 | + :return: GameLibrarySettings object |
| 821 | + """ |
| 822 | + raise NotImplementedError() |
| 823 | + |
| 824 | + def game_library_settings_import_complete(self) -> None: |
| 825 | + """Override this method to handle operations after game times import is finished |
| 826 | + (like updating cache). |
| 827 | + """ |
753 | 828 |
|
754 | 829 | def create_and_run_plugin(plugin_class, argv): |
755 | 830 | """Call this method as an entry point for the implemented integration. |
|
0 commit comments