forked from bl-sdk/mods_base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.py
121 lines (91 loc) · 3.27 KB
/
settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from __future__ import annotations
import json
from collections.abc import Mapping, Sequence
from typing import TYPE_CHECKING, TypedDict
from . import MODS_DIR
if TYPE_CHECKING:
from .mod import Mod
from .options import BaseOption
type JSON = Mapping[str, JSON] | Sequence[JSON] | str | int | float | bool | None
SETTINGS_DIR = MODS_DIR / "settings"
SETTINGS_DIR.mkdir(parents=True, exist_ok=True)
class BasicModSettings(TypedDict, total=False):
enabled: bool
options: dict[str, JSON]
keybinds: dict[str, str | None]
def load_options_dict(
options: Sequence[BaseOption],
settings: Mapping[str, JSON],
) -> None:
"""
Recursively loads options from their settings dict.
Args:
options: The list of options to load.
settings: The settings dict.
"""
for option in options:
if option.identifier not in settings:
continue
value = settings[option.identifier]
option._from_json(value) # type: ignore
def default_load_mod_settings(self: Mod) -> None:
"""Default implementation for Mod.load_settings."""
if self.settings_file is None:
return
settings: BasicModSettings
try:
with self.settings_file.open() as file:
settings = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
return
# No sense doing this if not defined
if "options" in settings:
load_options_dict(self.options, settings["options"])
if "keybinds" in settings:
saved_keybinds = settings["keybinds"]
for keybind in self.keybinds:
if keybind.identifier in saved_keybinds:
key = saved_keybinds[keybind.identifier]
if key is None:
keybind.key = None
else:
keybind.key = str(key)
if self.auto_enable and settings.get("enabled", False):
self.enable()
def create_options_dict(options: Sequence[BaseOption]) -> dict[str, JSON]:
"""
Creates an options dict from a list of options.
Args:
options: The list of options to save.
Returns:
The options' values in dict form.
"""
return {
option.identifier: child_json
for option in options
if (child_json := option._to_json()) is not ... # pyright: ignore[reportPrivateUsage]
}
def default_save_mod_settings(self: Mod) -> None:
"""Default implementation for Mod.save_settings."""
if self.settings_file is None:
return
settings: BasicModSettings = {}
if len(self.options) > 0:
option_settings = create_options_dict(self.options)
if len(option_settings) > 0:
settings["options"] = option_settings
if len(self.keybinds) > 0:
keybind_settings: dict[str, str | None] = {}
for keybind in self.keybinds:
if not keybind.is_rebindable:
continue
keybind_settings[keybind.identifier] = keybind.key
if len(keybind_settings) > 0:
settings["keybinds"] = keybind_settings
if self.auto_enable:
settings["enabled"] = self.is_enabled
if len(settings) == 0:
self.settings_file.unlink(missing_ok=True)
return
with self.settings_file.open("w") as file:
json.dump(settings, file, indent=4)