-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__init__.py
More file actions
212 lines (178 loc) · 6.19 KB
/
__init__.py
File metadata and controls
212 lines (178 loc) · 6.19 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from __future__ import annotations
from enum import IntFlag, auto
from functools import wraps
from pathlib import Path
from typing import TYPE_CHECKING, Literal, overload
import unrealsdk
from .dot_sdkmod import open_in_mod_dir
# Need to define a few things first to avoid circular imports
__version_info__: tuple[int, int] = (1, 11)
__version__: str = f"{__version_info__[0]}.{__version_info__[1]}"
__author__: str = "bl-sdk"
# This avoids E402 for all the following imports
if True:
MODS_DIR: Path = (
_mod_dir
if (_mod_dir := Path(__file__).parent.parent).is_dir()
else _mod_dir.parent # If in a .sdkmod, need to go up an extra folder
)
del _mod_dir
from .command import (
AbstractCommand,
ArgParseCommand,
capture_next_console_line,
command,
remove_next_console_line_capture,
)
from .hook import HookType, bind_all_hooks, hook
from .html_to_plain_text import html_to_plain_text
from .keybinds import EInputEvent, KeybindType, keybind
from .mod import CoopSupport, Game, Library, Mod, ModType, RestartToDisable
from .mod_factory import build_mod
from .mod_list import (
deregister_mod,
get_ordered_mod_list,
register_mod,
)
from .options import (
JSON,
BaseOption,
BoolOption,
ButtonOption,
DropdownOption,
GroupedOption,
HiddenOption,
KeybindOption,
NestedOption,
SliderOption,
SpinnerOption,
ValueOption,
)
from .settings import SETTINGS_DIR
if TYPE_CHECKING:
from unrealsdk.unreal import UObject
__all__: tuple[str, ...] = (
"ENGINE",
"JSON",
"MODS_DIR",
"SETTINGS_DIR",
"AbstractCommand",
"ArgParseCommand",
"BaseOption",
"BoolOption",
"ButtonOption",
"CoopSupport",
"DropdownOption",
"EInputEvent",
"Game",
"GroupedOption",
"HiddenOption",
"HookType",
"KeybindOption",
"KeybindType",
"Library",
"Mod",
"ModType",
"NestedOption",
"RestartToDisable",
"SliderOption",
"SpinnerOption",
"ValueOption",
"__version__",
"__version_info__",
"bind_all_hooks",
"build_mod",
"capture_next_console_line",
"command",
"deregister_mod",
"get_ordered_mod_list",
"get_pc",
"hook",
"html_to_plain_text",
"keybind",
"open_in_mod_dir",
"register_mod",
"remove_next_console_line_capture",
)
ENGINE: UObject
@overload
def get_pc() -> UObject: ...
@overload
def get_pc(*, possibly_loading: Literal[True] = True) -> UObject | None: ...
def get_pc(*, possibly_loading: bool = False) -> UObject | None:
"""
Gets the main (local) player controller object.
Note that this may return None if called during a loading screen. Since hooks and keybinds
should never be able to trigger at this time, for convenience the default type hinting does not
include this possibility. If running on another thread however, this can happen, pass the
`possibly_loading` kwarg to update the type hinting.
Args:
possibly_loading: Changes the type hinting to possibly return None. No runtime impact.
Returns:
The player controller.
"""
raise NotImplementedError
class ObjectFlags(IntFlag):
"""
Useful values to assign to object flags, both for directly on an object and in construct_object.
Note that not all flags are known in all games - though the type hinting always contains them
all. Trying to use a flag in a game where it's not known will throw an attribute error.
"""
if TYPE_CHECKING:
# Prevents the object from getting garbage collected
KEEP_ALIVE = auto()
# Allows the object to be referenced by objects in other packages
ALLOW_CROSS_PACKAGE_REFERENCES = auto()
match Game.get_tree():
case Game.Willow1 | Game.Willow2:
ENGINE = unrealsdk.find_object( # pyright: ignore[reportConstantRedefinition]
"WillowGameEngine",
"Transient.WillowGameEngine_0",
)
_GAME_PLAYERS_PROP = ENGINE.Class._find_prop("GamePlayers")
_ACTOR_PROP = _GAME_PLAYERS_PROP.Inner.PropertyClass._find_prop("Actor") # type: ignore
@wraps(get_pc)
def get_pc_willow(*, possibly_loading: bool = False) -> UObject | None: # noqa: ARG001
return ENGINE._get_field(_GAME_PLAYERS_PROP)[0]._get_field(_ACTOR_PROP)
get_pc = get_pc_willow # type: ignore
@wraps(ObjectFlags, updated=())
class WillowObjectFlags(ObjectFlags): # type: ignore
ALLOW_CROSS_PACKAGE_REFERENCES = 0x4
KEEP_ALIVE = 0x4000
ObjectFlags = WillowObjectFlags # type: ignore
case Game.Oak | Game.Oak2:
if Game.get_tree() is Game.Oak:
ENGINE = unrealsdk.find_object( # pyright: ignore[reportConstantRedefinition]
"OakGameEngine",
"/Engine/Transient.OakGameEngine_0",
)
else:
try:
ENGINE = unrealsdk.find_object( # pyright: ignore[reportConstantRedefinition]
"OakGameEngine",
"/Engine/Transient.OakGameEngine_2147482611",
)
except ValueError:
# In case the number changes
ENGINE = next( # pyright: ignore[reportConstantRedefinition]
obj
for obj in unrealsdk.find_all("OakGameEngine")
if not obj.Name.startswith("Default__")
)
_GAME_INSTANCE_PROP = ENGINE.Class._find_prop("GameInstance")
_LOCAL_PLAYERS_PROP = _GAME_INSTANCE_PROP.PropertyClass._find_prop("LocalPlayers") # type: ignore
_PLAYER_CONTROLLER_PROP = _LOCAL_PLAYERS_PROP.Inner.PropertyClass._find_prop( # type: ignore
"PlayerController",
)
@wraps(get_pc)
def get_pc_oak(*, possibly_loading: bool = False) -> UObject | None: # noqa: ARG001
return (
ENGINE._get_field(_GAME_INSTANCE_PROP)
._get_field(_LOCAL_PLAYERS_PROP)[0]
._get_field(_PLAYER_CONTROLLER_PROP)
)
get_pc = get_pc_oak # type: ignore
@wraps(ObjectFlags, updated=())
class OakObjectFlags(ObjectFlags): # type: ignore
KEEP_ALIVE = 0x80
ObjectFlags = OakObjectFlags # type: ignore