/
MenuManager.py
436 lines (346 loc) · 16.4 KB
/
MenuManager.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import unrealsdk
import json
import os
import re
import sys
import webbrowser
from functools import cmp_to_key
from typing import Dict, List, Set, Tuple
from . import VERSION_MAJOR, VERSION_MINOR
from . import DeprecationHelper as dh
from . import KeybindManager, ModObjects
__all__: Tuple[str, ...] = ()
_MODS_EVENT_ID: int = 1417
_MODS_MENU_NAME: str = "MODS"
_FAVOURITES_FILE: str = os.path.join(os.path.dirname(os.path.realpath(__file__)), "favourites.json")
_current_mod_list: List[ModObjects.SDKMod] = []
_favourite_mods: Set[str] = set()
def GetOrderedModList() -> List[ModObjects.SDKMod]:
"""
Gets the list of mods properly ordered by favourite status, priority, and name.
The first section of the list contains the favourited mods, the second all others. Each section
is sorted by priority first, with higher priority appearing earlier in the list, and
alphabetically by name second.
Returns:
The ordered list of mods.
"""
def cmp(a: ModObjects.SDKMod, b: ModObjects.SDKMod) -> int:
if a.Name in _favourite_mods and b.Name not in _favourite_mods:
return -1
elif a.Name not in _favourite_mods and b.Name in _favourite_mods:
return 1
if a.Priority == b.Priority:
# Do some basic html stripping, so recolouring a name doesn't send the mod to the top
# You can definitely fool this but it should be good enough
a_stripped = re.sub("<.+?>", '', a.Name)
b_stripped = re.sub("<.+?>", '', b.Name)
if a_stripped < b_stripped:
return -1
elif a_stripped > b_stripped:
return 1
return 0
return b.Priority - a.Priority
return sorted(ModObjects.Mods, key=cmp_to_key(cmp))
class _General(ModObjects.SDKMod):
Name: str = "General"
Author: str = "PythonSDK"
Description: str = (
"Welcome to the Borderlands 2 SDK Mod Manager\n"
"\n"
"See below for options.\n"
)
Version: str = f"{VERSION_MAJOR}.{VERSION_MINOR}"
SupportedGames: ModObjects.Game = (
ModObjects.Game.BL2 | ModObjects.Game.TPS | ModObjects.Game.AoDK
)
Types: ModObjects.ModTypes = ModObjects.ModTypes.All
Status: str = ""
SettingsInputs: Dict[str, str] = {
"H": "Help",
"O": "Open Mods Folder",
}
def SettingsInputPressed(self, action: str) -> None:
if action == "Help":
webbrowser.open("http://bl-sdk.github.io/")
elif action == "Open Mods Folder":
os.startfile(os.path.join(os.path.dirname(sys.executable), "Mods"))
_general_instance = _General()
def _save_favourite_mods() -> None:
""" Saves all favourited mods to disk. """
if len(_favourite_mods) == 0:
os.remove(_FAVOURITES_FILE)
else:
with open(_FAVOURITES_FILE, "w") as file:
json.dump(list(_favourite_mods), file, indent=4)
def _load_favourite_mods() -> None:
""" Loads all favourited mods from disk. """
global _favourite_mods
try:
_favourite_mods = set()
with open(_FAVOURITES_FILE) as file:
names = json.load(file)
# This looks redundant, but it makes sure only loaded mods are in the list
for mod in ModObjects.Mods:
if mod.Name in names:
_favourite_mods.add(mod.Name)
except (FileNotFoundError, json.JSONDecodeError):
pass
def _update_mod_list_item(
mod: ModObjects.SDKMod,
item: unrealsdk.UObject,
movie: unrealsdk.UObject,
translation_context: unrealsdk.UObject
) -> None:
""" Updates a mod GFxObject to use all the latest fields from it's SDKMod. """
item.SetString(movie.Prop_contentTitleText, mod.Name, translation_context)
item.SetString(movie.Prop_costText, "By " + mod.Author, translation_context)
item.SetString(movie.Prop_descriptionText, mod.Description, translation_context)
item.SetString(
movie.Prop_statusText,
f"<font color=\"#A1E4EF\">{mod.Version}</font>", # Make this the same colour as author
translation_context
)
status = mod.Status
if mod.Status == "Enabled":
status = "<font color=\"#00FF00\">Enabled</font>"
elif mod.Status == "Disabled":
status = "<font color=\"#FF0000\">Disabled</font>"
item.SetString(movie.Prop_messageText, status, translation_context)
if isinstance(mod.Types, list):
dh.PrintWarning(
"Using lists for mod types is deprecated, combine types with bitwise or instead."
)
# For some odd reason these take in floats, but treat them as bools
item.SetFloat(movie.Prop_isCompatibility, float(ModObjects.ModTypes.Utility in mod.Types))
item.SetFloat(movie.Prop_isAddOn, float(ModObjects.ModTypes.Content in mod.Types))
item.SetFloat(movie.Prop_isSeasonPass, float(ModObjects.ModTypes.Gameplay in mod.Types))
item.SetFloat(movie.Prop_isMisc, float(ModObjects.ModTypes.Library in mod.Types))
item.SetFloat(movie.Prop_isNewOffer, float(mod.Name in _favourite_mods or mod == _general_instance))
def _FrontEndPopulate(caller: unrealsdk.UObject, function: unrealsdk.UFunction, params: unrealsdk.FStruct) -> bool:
"""
This function is called to create the front end menu. We use it to replace the DLC caption and
event id.
"""
def AddListItem(caller: unrealsdk.UObject, function: unrealsdk.UFunction, params: unrealsdk.FStruct) -> bool:
"""
This function is called every time an item is added to *any* menu list - we obviously can't
use a generic hook.
Using it cause it simplifies the code to replace the caption.
"""
if params.Caption == "$WillowMenu.WillowScrollingListDataProviderFrontEnd.DLC":
return False
inject_now = False
if unrealsdk.GetEngine().GetCurrentWorldInfo().NetMode == 3: # NM_Client
inject_now = params.Caption == "$WillowMenu.WillowScrollingListDataProviderFrontEnd.Disconnect"
else:
inject_now = params.Caption == "$WillowMenu.WillowScrollingListDataProviderFrontEnd.Quit"
if inject_now:
caller.AddListItem(_MODS_EVENT_ID, _MODS_MENU_NAME, False, False)
return True
unrealsdk.RunHook("WillowGame.WillowScrollingList.AddListItem", "ModMenu.MenuManager", AddListItem)
unrealsdk.DoInjectedCallNext()
caller.Populate(params.TheList)
unrealsdk.RemoveHook("WillowGame.WillowScrollingList.AddListItem", "ModMenu.MenuManager")
return False
def _RefreshDLC(caller: unrealsdk.UObject, function: unrealsdk.UFunction, params: unrealsdk.FStruct) -> bool:
"""
This function is called to refresh the DLC menu. We're mostly interested in it because Gearbox
has amazing code, and calls `OnDownloadableContentListRead` twice if you're offline.
This happens right at the end of the function, so it's easiest to just recreate it.
We can ignore the filter stuff, `OnDownloadableContentListRead` overwrites it right after, and
we can also remove the small meaningless loading message that some people get hung up on.
"""
if caller.bDelegateFired:
caller.ShowMarketplaceElements(False)
caller.SetShoppingTooltips(False, False, False, False, True)
caller.SetContentData()
caller.bDelegateFired = False
# Here's the issue: earlier they setup a delegate for this call, but when it fails they
# also manually call the delgate again - we just don't
caller.WPCOwner.OnlineSub.ContentInterface.ObjectPointer.ReadDownloadableContentList(
caller.WPCOwner.GetMyControllerId()
)
return False
def _OnDownloadableContentListRead(caller: unrealsdk.UObject, function: unrealsdk.UFunction, params: unrealsdk.FStruct) -> bool:
""" This function is called to fill in the dlc menu, we overwrite it with our mods instead. """
global _current_mod_list
_load_favourite_mods()
caller.ClearFilters()
caller.SetFilterFromString("compatibility", "Utility Mods", "isCompatibility:1")
caller.SetFilterFromString("addon", "Content Mods", "isAddOn:1")
caller.SetFilterFromString("seasonpass", "Gameplay Mods", "isSeasonPass:1")
caller.SetFilterFromString("misc", "Libraries", "isMisc:1")
caller.SetFilterFromStringAndSortNew("all", "All Mods", "")
caller.SetStoreHeader("Mods", False, "By Abahbob", "Mod Manager")
_current_mod_list = [_general_instance] + GetOrderedModList() # type: ignore
translation_context = unrealsdk.GetEngine().GamePlayers[0].GetTranslationContext()
for idx, mod in enumerate(_current_mod_list):
# This is weird and crashes if you don't have the second arg, but also crashes most the time
# when you try to access something on it - seems like a garbage pointer
item, _ = caller.CreateMarketplaceItem()
item.SetString(caller.Prop_offeringId, str(idx), translation_context)
_update_mod_list_item(mod, item, caller, translation_context)
caller.AddContentData(item)
caller.PostContentLoaded(True)
return False
def _ShopInputKey(caller: unrealsdk.UObject, function: unrealsdk.UFunction, params: unrealsdk.FStruct) -> bool:
"""
This function is called on pretty much all input in the dlc menu. We use it to add our own
custom binds, and to refresh mods after running them.
"""
global _filter_idx
key = params.ukey
event = params.uevent
controller_key_map = {
"Gamepad_LeftStick_Up": "Up",
"Gamepad_LeftStick_Down": "Down",
"XboxTypeS_A": "Enter",
"XboxTypeS_B": "Escape",
"XboxTypeS_Y": "Q",
"XboxTypeS_LeftTrigger": "PageUp",
"XboxTypeS_RightTrigger": "PageDown"
}
if key in controller_key_map:
key = controller_key_map[key]
if key in ("Escape", "Up", "Down", "W", "S"):
return True
if event in (KeybindManager.InputEvent.Pressed, KeybindManager.InputEvent.Repeat):
# These two are bugged on gearbox's end, we manually fix them
if key == "PageUp":
caller.ScrollDescription(True)
return False
elif key == "PageDown":
caller.ScrollDescription(False)
return False
item = caller.GetSelectedObject()
mod = _current_mod_list[int(item.GetString(caller.Prop_offeringId))]
def update_item() -> None:
_update_mod_list_item(
mod,
item,
caller,
unrealsdk.GetEngine().GamePlayers[0].GetTranslationContext()
)
# Unfortuantly it doesn't seem like there's a way to easily scroll back down to where you
# were, but we have to call this to actually update the visuals
# This also resets filters, and for some reason none of the filter functions want to work
# right now either :/
_RefreshDLC(caller, None, None)
if key == "Q":
if event == KeybindManager.InputEvent.Released:
if mod == _general_instance:
caller.CycleFilter()
else:
if mod.Name in _favourite_mods:
_favourite_mods.remove(mod.Name)
else:
_favourite_mods.add(mod.Name)
_save_favourite_mods()
update_item()
return False
elif key in mod.SettingsInputs:
if event == KeybindManager.InputEvent.Released:
mod.SettingsInputPressed(mod.SettingsInputs[key])
update_item()
return False
# These keys would try open the store if we let them continue
elif key in ("Enter", "E"):
return False
return True
def _extOnOfferingChanged(caller: unrealsdk.UObject, function: unrealsdk.UFunction, params: unrealsdk.FStruct) -> bool:
"""
This function is called when the currently selected mod is changed. We use it to update the
settings input tooltips.
"""
caller.PlayUISound('VerticalMovement')
try:
idx = int(params.Data.GetString(caller.Prop_offeringId))
# This seems to sometimes be called as we're refreshig the menu, making `GetString` return None
except TypeError:
return False
mod = _current_mod_list[idx]
on_left_column = False
left_column = ""
right_column = ""
if mod == _general_instance:
left_column = "[Q] Filter\n"
elif mod.Name in _favourite_mods:
left_column = "[Q] Unfavourite\n"
else:
left_column = "[Q] Favourite\n"
for key, action in mod.SettingsInputs.items():
entry = f"[{key}] {action}\n"
if on_left_column:
left_column += entry
else:
right_column += entry
on_left_column = not on_left_column
caller.SetTooltips(left_column, right_column)
return False
def _FrontEndHandleClick(caller: unrealsdk.UObject, function: unrealsdk.UFunction, params: unrealsdk.FStruct) -> bool:
"""
This function is called when you click on any option on the main menu. We use it to open the dlc
menu, even if you're offline or on epic.
"""
if params.EventID == _MODS_EVENT_ID:
movie = params.TheList.MyOwnerMovie
if not movie.IsOverlayMenuOpen():
movie.CheckDownloadableContentListCompleted(movie.WPCOwner.GetMyControllerId(), True)
return False
return True
def _SharedHandleInputKey(caller: unrealsdk.UObject, function: unrealsdk.UFunction, params: unrealsdk.FStruct) -> bool:
"""
This function is called on pretty much all key input events on the main menu. We use it to open
the dlc menu when you press "M".
"""
if (
params.ukey == "M"
and params.uevent == KeybindManager.InputEvent.Released
and not caller.IsOverlayMenuOpen()
):
caller.CheckDownloadableContentListCompleted(caller.WPCOwner.GetMyControllerId(), True)
return True
def _FrontEndUpdateTooltips(caller: unrealsdk.UObject, function: unrealsdk.UFunction, params: unrealsdk.FStruct) -> bool:
"""
This function is called to update the tooltips in the bottom right corner on the main menu. We
replace it with our own message, adding character/network mode for bl2, and mentioning our new
`M` for mods menu bind.
Unfortuantly doing so requires recreating most of the function, there's no nice var we can read
from - `SetVariableString` adds a bunch of extra formatting that's awkward to parse though if
you tried using `GetVariableString`.
"""
tooltip = caller.TooltipSpacing + caller.SelectTooltip
cancel = caller.CancelString
if caller.WPCOwner.WorldInfo.NetMode == 3:
# There's no easy len() :/
count = 0
if caller.TheList is not None:
for _ in caller.TheList.DataProviderStack:
count += 1 # noqa: SIM113
if count <= 1:
cancel = caller.DisconnectString
tooltip += caller.TooltipSpacing + caller.CancelTooltip.replace("%PLAYER1", cancel)
tooltip += "\n"
if caller.CanShowSpectatorControls():
tooltip += caller.TooltipSpacing + caller.SpectatorTooltip
if caller.CanShowCharacterSelect(-1):
tooltip += caller.TooltipSpacing + caller.CharacterSelectTooltip
if caller.WPCOwner.WorldInfo.NetMode != 3:
tooltip += caller.TooltipSpacing + caller.NetworkOptionsTooltip
# Only show on the main menu, not also the pause menu
if caller.Class.Name == "FrontendGFxMovie":
tooltip += caller.TooltipSpacing + "[M] Mods"
if caller.MyFrontendDefinition is not None:
caller.SetVariableString(
caller.MyFrontendDefinition.TooltipPath,
caller.ResolveDataStoreMarkup(tooltip)
)
return False
unrealsdk.RunHook("WillowGame.WillowScrollingListDataProviderFrontEnd.Populate", "ModMenu.MenuManager", _FrontEndPopulate)
unrealsdk.RunHook("WillowGame.MarketplaceGFxMovie.RefreshDLC", "ModMenu.MenuManager", _RefreshDLC)
unrealsdk.RunHook("WillowGame.MarketplaceGFxMovie.OnDownloadableContentListRead", "ModMenu.MenuManager", _OnDownloadableContentListRead)
unrealsdk.RunHook("WillowGame.MarketplaceGFxMovie.ShopInputKey", "ModMenu.MenuManager", _ShopInputKey)
unrealsdk.RunHook("WillowGame.MarketplaceGFxMovie.extOnOfferingChanged", "ModMenu.MenuManager", _extOnOfferingChanged)
unrealsdk.RunHook("WillowGame.WillowScrollingListDataProviderFrontEnd.HandleClick", "ModMenu.MenuManager", _FrontEndHandleClick)
unrealsdk.RunHook("WillowGame.FrontendGFxMovie.SharedHandleInputKey", "ModMenu.MenuManager", _SharedHandleInputKey)
unrealsdk.RunHook("WillowGame.FrontendGFxMovie.UpdateTooltips", "ModMenu.MenuManager", _FrontEndUpdateTooltips)