Skip to content

Commit

Permalink
Important save detection + LiveSplit commands fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Dec 9, 2023
1 parent f3e3911 commit ab6dbdc
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $arguments = @(
'--splash=res/splash.png',
# The install script should ensure that these are not installed
# But we'll still include unused dependencies that would be picked up by PyInstaller
# if requirements.txt was used directly to help ensure consistency when buildign locally.
# if requirements.txt was used directly to help ensure consistency when building locally.
#
# Installed by PyAutoGUI
'--exclude=pyscreeze',
Expand Down
2 changes: 1 addition & 1 deletion scripts/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Pillow>=10.0 # Python 3.12 support
psutil>=5.9.6 # Python 3.12 fixes
PyAutoGUI
PyWinCtl>=0.0.42 # py.typed
# When needed, dev builds can be found at https://download.qt.io/snapshots/ci/pyside/dev
# When needed, dev builds can be found at https://download.qt.io/snapshots/ci/pyside/dev?C=M;O=D
PySide6-Essentials>=6.6.0 # Python 3.12 support
requests>=2.28.2 # charset_normalizer 3.x update
toml
Expand Down
11 changes: 4 additions & 7 deletions src/AutoSplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import signal
import sys
from collections.abc import Callable
from copy import deepcopy
from time import time
from types import FunctionType
from typing import NoReturn
Expand Down Expand Up @@ -50,8 +51,6 @@
open_file,
)

DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = 2

# Needed when compiled, along with the custom hook-requests PyInstaller hook
os.environ["REQUESTS_CA_BUNDLE"] = certifi.where()
myappid = f"Toufool.AutoSplit.v{AUTOSPLIT_VERSION}"
Expand Down Expand Up @@ -93,18 +92,16 @@ def __init__(self): # noqa: PLR0915
# Initialize a few attributes
self.hwnd = 0
"""Window Handle used for Capture Region"""
self.last_saved_settings = DEFAULT_PROFILE
self.last_saved_settings = deepcopy(DEFAULT_PROFILE)
self.similarity = 0.0
self.split_image_number = 0
self.split_images_and_loop_number: list[tuple[AutoSplitImage, int]] = []
self.split_groups: list[list[int]] = []
self.capture_method = CaptureMethodBase(self)
self.is_running = False

# Last loaded settings empty and last successful loaded settings file path to None until we try to load them
self.last_loaded_settings = DEFAULT_PROFILE
self.last_successfully_loaded_settings_file_path: str | None = None
"""For when a file has never loaded, but you successfully "Save File As"."""
self.last_successfully_loaded_settings_file_path = ""
"""Path of the settings file to default to. `None` until we try to load once."""

# Automatic timer start
self.highest_similarity = 0.0
Expand Down
1 change: 1 addition & 0 deletions src/capture_method/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class CaptureMethodEnum(Enum, metaclass=CaptureMethodMeta):
def __repr__(self):
return self.value

# Allow direct comparison with strings
@override
def __eq__(self, other: object):
if isinstance(other, str):
Expand Down
4 changes: 2 additions & 2 deletions src/hotkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def send_command(autosplit: "AutoSplit", command: Commands):
# having the reset image check be active at all time would be a better, more organic solution,
# but that is dependent on migrating to an observer pattern (#219) and being able to reload all images.
match command:
case _ if autosplit.settings_dict["start_also_resets"]:
case _ if autosplit.is_auto_controlled:
if command == "start" and autosplit.settings_dict["start_also_resets"]:
print("reset", flush=True)
print(command, flush=True)
Expand Down Expand Up @@ -164,7 +164,7 @@ def __get_hotkey_name(names: list[str]):
Uses keyboard.get_hotkey_name but works with non-english modifiers and keypad
See: https://github.com/boppreh/keyboard/issues/516 .
"""
if not names:
if not names: # 0-length
return ""

if len(names) == 1:
Expand Down
2 changes: 1 addition & 1 deletion src/split_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __value_from_filename(
raise ValueError("delimiters parameter must contain exactly 2 characters")
try:
string_value = filename.split(delimiters[0], 1)[1].split(delimiters[1])[0]
value: T = type(default_value)(string_value)
value = type(default_value)(string_value)
except (IndexError, ValueError):
return default_value
else:
Expand Down
19 changes: 12 additions & 7 deletions src/user_profile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
from copy import deepcopy
from typing import TYPE_CHECKING, TypedDict, cast

import toml
from PySide6 import QtCore, QtWidgets
from typing_extensions import deprecated, override

import error_messages
from capture_method import CAPTURE_METHODS, CaptureMethodEnum, Region, change_capture_method
Expand Down Expand Up @@ -40,6 +42,10 @@ class UserProfileDict(TypedDict):
captured_window_title: str
capture_region: Region

@override # pyright: ignore
@deprecated("Use `copy.deepcopy` instead")
def copy(): return super().copy()


DEFAULT_PROFILE = UserProfileDict(
split_hotkey="",
Expand Down Expand Up @@ -70,10 +76,7 @@ class UserProfileDict(TypedDict):


def have_settings_changed(autosplit: "AutoSplit"):
return (
autosplit.settings_dict != autosplit.last_saved_settings
or autosplit.settings_dict != autosplit.last_loaded_settings
)
return autosplit.settings_dict != autosplit.last_saved_settings


def save_settings(autosplit: "AutoSplit"):
Expand All @@ -95,6 +98,7 @@ def save_settings_as(autosplit: "AutoSplit"):
or os.path.join(auto_split_directory, "settings.toml"),
"TOML (*.toml)",
)[0]

# If user cancels save destination window, don't save settings
if not save_settings_file_path:
return ""
Expand All @@ -103,10 +107,10 @@ def save_settings_as(autosplit: "AutoSplit"):


def __save_settings_to_file(autosplit: "AutoSplit", save_settings_file_path: str):
autosplit.last_saved_settings = autosplit.settings_dict
# Save settings to a .toml file
with open(save_settings_file_path, "w", encoding="utf-8") as file:
toml.dump(autosplit.last_saved_settings, file)
toml.dump(autosplit.settings_dict, file)
autosplit.last_saved_settings = deepcopy(autosplit.settings_dict)
autosplit.last_successfully_loaded_settings_file_path = save_settings_file_path
return save_settings_file_path

Expand All @@ -120,9 +124,10 @@ def __load_settings_from_file(autosplit: "AutoSplit", load_settings_file_path: s
# Casting here just so we can build an actual UserProfileDict once we're done validating
# Fallback to default settings if some are missing from the file. This happens when new settings are added.
loaded_settings = DEFAULT_PROFILE | cast(UserProfileDict, toml.load(file))

# TODO: Data Validation / fallbacks ?
autosplit.settings_dict = UserProfileDict(**loaded_settings)
autosplit.last_loaded_settings = autosplit.settings_dict
autosplit.last_saved_settings = deepcopy(autosplit.settings_dict)

autosplit.x_spinbox.setValue(autosplit.settings_dict["capture_region"]["x"])
autosplit.y_spinbox.setValue(autosplit.settings_dict["capture_region"]["y"])
Expand Down
6 changes: 3 additions & 3 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
DWMWA_EXTENDED_FRAME_BOUNDS = 9
MAXBYTE = 255
BGR_CHANNEL_COUNT = 3
"""How many channels in an RGB image"""
"""How many channels in a BGR image"""
BGRA_CHANNEL_COUNT = 4
"""How many channels in an RGBA image"""
"""How many channels in a BGRA image"""


class ImageShape(IntEnum):
Expand Down Expand Up @@ -68,7 +68,7 @@ def is_valid_image(image: MatLike | None) -> TypeGuard[MatLike]:
return image is not None and bool(image.size)


def is_valid_hwnd(hwnd: int) -> bool:
def is_valid_hwnd(hwnd: int):
"""Validate the hwnd points to a valid window and not the desktop or whatever window obtained with `""`."""
if not hwnd:
return False
Expand Down

0 comments on commit ab6dbdc

Please sign in to comment.