Add custom theme support, token refresh settings and check account login status.
Now launch log viewer has a new look. Fix an issue that causes credentials method option not working. Add log view to settings page.
This commit is contained in:
1 parent
6e29fe1d5c
commit
1bafc60d95
25 files changed
+1149
-199
No files matched your search
@@ -1,4 +1,4 @@
|
||||
__version__ = '0.1.0'
|
||||
__version__ = '0.1.1'
|
||||
__description__ = """
|
||||
The core library of the *Launcher*
|
||||
|
||||
|
||||
@@ -12,6 +12,15 @@ class LauncherException(Exception):
|
||||
if user_message is not None:
|
||||
self.user_message = user_message
|
||||
|
||||
class LauncherHTTPException(LauncherException):
|
||||
user_message = "Launcher error occurred."
|
||||
|
||||
def __init__(self, message=None, status_code=None,*, user_message=None):
|
||||
super().__init__(message or user_message or self.user_message)
|
||||
if user_message is not None:
|
||||
self.user_message = user_message
|
||||
self.status_code = status_code
|
||||
|
||||
class HashMismatchException(Exception):
|
||||
def __init__(self, hash_name, got_hash, expected_hash, filepath: str | Path):
|
||||
if type(filepath) == Path: filepath = Path(filepath).absolute().as_posix()
|
||||
@@ -109,7 +118,7 @@ class AuthenticationException(LauncherException):
|
||||
class AuthenticationConnectException(LauncherException):
|
||||
user_message = "An error occurred while connecting to authentication server."
|
||||
|
||||
class AuthenticationSessionException(LauncherException):
|
||||
class AuthenticationSessionException(LauncherHTTPException):
|
||||
user_message = "Authentication failed. Session may expire."
|
||||
|
||||
class ThirdPartyAPIException(LauncherException):
|
||||
|
||||
@@ -166,7 +166,7 @@ def _check_account(account: dict, account_id: str | None = None, fix_wrong: bool
|
||||
)
|
||||
|
||||
# Check all required key is exiting
|
||||
if any(not isinstance(account.get(key), str) or not account[key] for key in required_strings):
|
||||
if any(not isinstance(account.get(key), str) or key not in account for key in required_strings):
|
||||
return False
|
||||
|
||||
# Check account id is match as provided
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
"""
|
||||
Store all function for microsoft authentication
|
||||
Store all function for Microsoft authentication
|
||||
|
||||
WARNING: This module is vibed! With code reviewed!
|
||||
"""
|
||||
@@ -11,7 +11,7 @@ from typing import Callable
|
||||
import requests
|
||||
|
||||
from core_lib.common.exception import AuthenticationSessionException, AuthenticationConnectException, \
|
||||
AuthenticationException
|
||||
AuthenticationException, LauncherHTTPException
|
||||
|
||||
MICROSOFT_DEVICE_CODE_URL = "https://login.microsoftonline.com/consumers/oauth2/v2.0/devicecode"
|
||||
MICROSOFT_TOKEN_URL = "https://login.microsoftonline.com/consumers/oauth2/v2.0/token"
|
||||
@@ -462,3 +462,39 @@ def refresh_microsoft_token(client_id: str, refresh_token: str) -> tuple[str, st
|
||||
result,
|
||||
)
|
||||
|
||||
def is_access_token_valid(minecraft_access_token: str) -> bool:
|
||||
"""
|
||||
Check if the access token is valid
|
||||
:param minecraft_access_token:
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
response = requests.get(
|
||||
MINECRAFT_PROFILE_URL,
|
||||
headers={"Authorization": f"Bearer {minecraft_access_token}"},
|
||||
timeout=10,
|
||||
)
|
||||
except Exception as e:
|
||||
raise AuthenticationSessionException(
|
||||
status_code=None,
|
||||
user_message="An error occurred while connecting to Minecraft API.",
|
||||
) from e
|
||||
|
||||
status_code = response.status_code
|
||||
|
||||
if status_code == 400:
|
||||
raise AuthenticationException(
|
||||
user_message="Got bad request response from Minecraft API. Client request format may outdated.",
|
||||
)
|
||||
elif status_code == 401:
|
||||
# Session is invalid
|
||||
return False
|
||||
|
||||
if status_code != 200:
|
||||
raise AuthenticationSessionException(
|
||||
"Get HTTP status code {} while fetch profile data for verifying access token".format(status_code),
|
||||
status_code=response.status_code,
|
||||
user_message="Unable to verify access token. An server error occurred. ({})".format(status_code),
|
||||
)
|
||||
|
||||
return True
|
||||
@@ -490,3 +490,26 @@ def update_profile(profiles: dict, profile_id: str, profile_data: dict, fix_wron
|
||||
|
||||
return profiles, profile_id
|
||||
|
||||
def set_last_used_time(profiles: dict, profile_id: str, time: datetime.datetime):
|
||||
"""
|
||||
Set profile last used time.
|
||||
:param profiles: Profiles data
|
||||
:param profile_id: Target profile ID.
|
||||
:param time: The time to set
|
||||
:return:
|
||||
"""
|
||||
if not is_profile_exists(profiles, profile_id):
|
||||
raise ProfileException(
|
||||
"Specified profile id {} not found.".format(profile_id)
|
||||
)
|
||||
|
||||
profiles["profiles"][profile_id]["lastUsed"] = time.isoformat().replace("+00:00", "Z")
|
||||
|
||||
def set_last_used_time_to_current(profiles: dict, profile_id: str):
|
||||
"""
|
||||
Set profile last used time to the current time.
|
||||
:param profiles: Profiles data
|
||||
:param profile_id: Target profile ID.
|
||||
:return:
|
||||
"""
|
||||
set_last_used_time(profiles, profile_id, datetime.datetime.now(datetime.timezone.utc))
|
||||
+34
-1
@@ -1,6 +1,8 @@
|
||||
import logging
|
||||
import sys
|
||||
from collections import deque
|
||||
|
||||
from PySide6.QtCore import QObject, Signal
|
||||
from colorlog import ColoredFormatter
|
||||
|
||||
DEFAULT_FORMAT = '%(asctime)s:%(name)s:%(levelname)s: %(message)s'
|
||||
@@ -20,4 +22,35 @@ def get_colorful_handler():
|
||||
datefmt=DEFAULT_DATE_FORMAT
|
||||
)
|
||||
handler.setFormatter(formatter)
|
||||
return handler
|
||||
return handler
|
||||
|
||||
class LogEmitter(QObject):
|
||||
message_emitted = Signal(str)
|
||||
record_emitted = Signal(object)
|
||||
|
||||
|
||||
class LogCaptureHandler(logging.Handler):
|
||||
def __init__(self, max_messages: int = 5000):
|
||||
super().__init__()
|
||||
self.messages = deque(maxlen=max_messages)
|
||||
self.records = deque(maxlen=max_messages)
|
||||
self.emitter = LogEmitter()
|
||||
|
||||
def emit(self, record: logging.LogRecord) -> None:
|
||||
try:
|
||||
message = self.format(record)
|
||||
|
||||
self.records.append(record)
|
||||
self.messages.append(message)
|
||||
|
||||
self.emitter.message_emitted.emit(message)
|
||||
self.emitter.record_emitted.emit(record)
|
||||
except Exception:
|
||||
self.handleError(record)
|
||||
|
||||
def get_messages(self) -> list[str]:
|
||||
return list(self.messages)
|
||||
|
||||
def clear(self) -> None:
|
||||
self.messages.clear()
|
||||
self.records.clear()
|
||||
Reference in new issue
Block a user