This repository has been archived by the owner on Jul 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
210 lines (171 loc) · 6.59 KB
/
__init__.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
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
import unrealsdk
import enum
import webbrowser
from typing import Dict, Iterable, List, Optional, cast
from Mods.ModMenu import (
EnabledSaveType,
Game,
Keybind,
KeybindManager,
Mods,
ModTypes,
RegisterMod,
SDKMod,
ServerMethod,
)
# thank you apple :)
try:
from Mods.EridiumLib import (
checkLibraryVersion,
checkModVersion,
getCurrentWorldInfo,
isClient,
log,
)
from Mods.EridiumLib.keys import KeyBinds
except ImportError:
webbrowser.open(
"https://github.com/DAmNRelentless/bl2-eridiumlib/blob/main/docs/TROUBLESHOOTING.md"
)
raise
if __name__ == "__main__":
import importlib
import sys
importlib.reload(sys.modules["Mods.EridiumLib"])
importlib.reload(sys.modules["Mods.EridiumLib.keys"])
# See https://github.com/bl-sdk/PythonSDK/issues/68
try:
raise NotImplementedError
except NotImplementedError:
__file__ = sys.exc_info()[-1].tb_frame.f_code.co_filename # type: ignore
class MissionStatus(enum.IntEnum):
NotStarted = 0
Active = 1
RequiredObjectivesComplete = 2
ReadyToTurnIn = 3
Complete = 4
Failed = 5
MAX = 6
def canBeActivated(self) -> bool:
"""Returns true if the status is either ReadyToTurnIn or Active."""
return self in [
MissionStatus.ReadyToTurnIn,
MissionStatus.Active,
]
class MissionSelector(SDKMod):
Name: str = "Mission Selector"
Author: str = "Chronophylos, Relentless"
Description: str = "Switch through missions with hotkeys.\nInspired by Borderlands 3."
Version: str = "1.3.2"
_EridiumVersion: str = "0.4.2"
SupportedGames: Game = Game.BL2 | Game.TPS
Types: ModTypes = ModTypes.Utility
SaveEnabledState: EnabledSaveType = EnabledSaveType.LoadWithSettings
SettingsInputs: Dict[str, str] = {
KeyBinds.Enter.value: "Enable",
KeyBinds.G.value: "GitHub",
}
def __init__(self) -> None:
super().__init__()
self.Keybinds = [
Keybind(
"Select next Mission", KeyBinds.RightBracket.value, True, OnPress=self.nextMission
),
Keybind(
"Select previous Mission",
KeyBinds.LeftBracket.value,
True,
OnPress=self.prevMission,
),
]
def Enable(self) -> None:
super().Enable()
if not checkLibraryVersion(self._EridiumVersion):
raise RuntimeWarning("Incompatible EridiumLib version!")
checkModVersion(self, "DAmNRelentless/bl2-missionselector")
def SettingsInputPressed(self, action: str) -> None:
if action == "GitHub":
webbrowser.open("https://github.com/DAmNRelentless/bl2-missionselector")
else:
super().SettingsInputPressed(action)
def nextMission(self, event: KeybindManager.InputEvent) -> None:
if event != KeybindManager.InputEvent.Pressed:
return
missionTracker = self.getMissionTracker()
activeMissions = cast(List[unrealsdk.UObject], self.getActiveMissions(missionTracker))
index = self.getActiveMissionIndex(missionTracker, activeMissions)
nextMission = None
if index < len(activeMissions) - 1:
nextMission = activeMissions[index + 1]
else:
nextMission = activeMissions[0]
self.setActiveMission(nextMission.MissionDef)
def prevMission(self, event: KeybindManager.InputEvent) -> None:
if event != KeybindManager.InputEvent.Pressed:
return
missionTracker = self.getMissionTracker()
activeMissions = cast(List[unrealsdk.UObject], self.getActiveMissions(missionTracker))
index = self.getActiveMissionIndex(missionTracker, activeMissions)
nextMission = activeMissions[index - 1]
self.setActiveMission(nextMission.MissionDef)
@staticmethod
def getActiveMissionIndex(
missionTracker: unrealsdk.UObject, missions: Iterable[unrealsdk.UObject]
) -> int:
"""Returns the index of the current active mission in missions."""
activeMission = missionTracker.ActiveMission
for index, mission in enumerate(missions):
if mission.MissionDef.MissionNumber == activeMission.MissionNumber:
return index
return -1
@staticmethod
def getMissionTracker() -> unrealsdk.UObject:
return cast(unrealsdk.UObject, getCurrentWorldInfo().GRI.MissionTracker)
@staticmethod
def getActiveMissions(
missionTracker: unrealsdk.UObject,
) -> Iterable[unrealsdk.UObject]:
"""Returns all active missions sorted by their MissionNumber.
For a definition of active see `MissionStatus.isActive`-
"""
activeMissions = sorted(
[m for m in missionTracker.MissionList if MissionStatus(m.Status).canBeActivated()],
key=lambda m: int(m.MissionDef.MissionNumber),
)
return activeMissions
def setActiveMission(self, mission: unrealsdk.UObject) -> None:
"""Set the currently tracked mission to mission."""
if isClient():
self._serverSetActiveMission(mission.MissionNumber)
else:
self._setActiveMission(mission.MissionNumber)
def getMissionByNumber(
self, missionTracker: unrealsdk.UObject, number: int
) -> unrealsdk.UObject:
"""Returns the mission with the MissionNumber equal to number.
Raises an IndexError if the mission was not found.
"""
for mission in missionTracker.MissionList:
if mission.MissionDef.MissionNumber == number:
return cast(unrealsdk.UObject, mission)
raise IndexError(f"There is nomission with the mission number {number}")
@ServerMethod
def _serverSetActiveMission(self, number: int, PC: Optional[unrealsdk.UObject] = None) -> None:
self._setActiveMission(number, PC)
def _setActiveMission(self, number: int, PC: Optional[unrealsdk.UObject] = None) -> None:
missionTracker = self.getMissionTracker()
mission = self.getMissionByNumber(missionTracker, number)
missionTracker.SetActiveMission(mission.MissionDef, True, PC)
instance = MissionSelector()
if __name__ == "__main__":
log(instance, "Manually loaded")
for mod in Mods:
if mod.Name == instance.Name:
if mod.IsEnabled:
mod.Disable()
Mods.remove(mod)
log(instance, "Removed last instance")
# Fixes inspect.getfile()
instance.__class__.__module__ = mod.__class__.__module__
break
RegisterMod(instance)