-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.py
executable file
·719 lines (600 loc) · 20.3 KB
/
cli.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
#!/usr/bin/env python3
import math
import os
import re
from typing import Union
from requests import Timeout
from manager import ModPackages, Package
ModPackages.init()
def _menu(
title: str,
options: Union[tuple, list],
quit: bool = False,
clear: bool = False,
back: bool = False,
default=None
):
"""
Render a menu for the user
Parameters
----------
title : str
Title to display at the top of the menu
options : list,tuple
List of menu options (option label, return value / return method)
If the second parameter of the selected option is a function, that function is called when selected.
quit : bool
Display a "quit" option to immediately exit the application
clear : bool
Clear the display prior to displaying the menu, useful for the main menu
back : bool
Display a "back" option, will return None when selected
default : str
Default option to use if the user simply presses 'Enter' without selecting anything
Note, this is what the user _would_ enter by default, so the 0th index will be '1'.
"""
if clear:
os.system('clear')
print(title + '\n')
space = math.floor(math.log10(len(options))) + 1
c = 0
for i in options:
c += 1
s = space - math.floor(math.log10(c))
print(str(c) + ':' + (' ' * s) + i[0])
if back:
print('B: Go Back')
if quit:
print('Q: Quit Application')
print('')
opt = input('Enter 1-' + str(len(options)) + ': ')
if opt == '':
opt = default
if quit and opt is not None and opt.lower() == 'q':
print('Bye bye')
exit()
if back and opt is not None and opt.lower() == 'b':
return None
if clear:
os.system('clear')
else:
print('')
c = 0
sel = None
for i in options:
c += 1
if opt == str(c):
sel = i[1]
if hasattr(sel, '__call__'):
return sel()
else:
return sel
def _wait():
"""
Just wait until the user presses enter, useful for displaying information
"""
print('')
input('Press ENTER to continue')
def menu_main():
"""
Present the user with the main menu of this application
"""
run = _menu(
'Valheim Mod Manager',
(
('List Mods Installed', list_installed),
('Install New Mod', install_new),
('Check For Updates', check_updates),
('List Mods Removed', list_removed),
('Uninstall Mod', remove),
('Revert Modifications', rollback),
# ('Sync Game Mods [Local Game]', sync_game),
# ('Import Game Mods [Local Game]', import_existing),
('Export/Package Mods', export_package)
),
quit=True, clear=True
)
if run == 'wait':
_wait()
def check_environment():
"""
Check the environment on starting to allow the user to sync existing mods easily
Nothing is returned, but if the user selects the default option, `import_existing` will be executed
"""
print('Checking manager environment...')
if not ModPackages.check_packages_fresh():
print('Thunderstore packages cache not fresh, downloading new copy...')
try:
ModPackages.download_packages()
except ConnectionError:
print('Unable to connect to Thunderstore! Please verify your internet connectivity.')
except Timeout:
print('Thunderstore took too long to respond, skipping package update')
print('Loading manager...')
ModPackages.load_caches()
print('Checking local game environment...')
mods = []
diff = False
game = ModPackages.get_synced_packages()
for pkg in game:
if pkg.installed_version is None:
# Mod in game directory is not registered as installed
print(pkg.name + ' found in game but is not registered yet')
diff = True
elif pkg.installed_version != pkg.selected_version:
# Mod installed, but versions differ
print(pkg.name + ' ' + pkg.selected_version + ' found in game directory differs from registered version')
diff = True
mods.append(pkg.name)
local = ModPackages.get_installed_packages()
for pkg in local:
# Skip auto-generated system mods
if pkg.name == 'BepInExPack_Valheim' or pkg.name == 'HookGenPatcher':
continue
if pkg.name not in mods:
print(pkg.name + ' registered but is not installed in game yet')
diff = True
if diff:
_menu(
title='Changes detected',
options=(
('Sync changes', sync_existing),
('Continue without syncing', 'skip')
),
default='1'
)
def list_installed() -> str:
"""
List installed mods (and provide a UI to manage them)
:return: str
"""
return _list_mods('installed')
def list_removed() -> str:
"""
List recently removed mods (and provide a UI to manage them)
:return: str
"""
return _list_mods('removed')
def _list_mods(mode: str) -> str:
"""
Display a list of mods
Returns
-------
str
'wait' is returned to indicate that the user needs to press 'Enter' to continue
"""
max_len = {
'name': 0,
'vers': 0,
'date': 0,
'author': 0
}
def print_row(row: dict):
print(
'| ' +
' | '.join((
row['id'].rjust(2, ' '),
row['name'].ljust(max_len['name'], ' '),
row['version'].ljust(max_len['vers'], ' '),
row['date'].ljust(max_len['date'], ' '),
row['rating'].rjust(6, ' '),
row['author'].ljust(max_len['author'], ' ')
)) +
' |'
)
def print_sep():
print(
'|-' +
'-|-'.join((
'-'.rjust(2, '-'),
'-'.ljust(max_len['name'], '-'),
'-'.ljust(max_len['vers'], '-'),
'-'.ljust(max_len['date'], '-'),
'-'.rjust(6, '-'),
'-'.ljust(max_len['author'], '-')
)) +
'-|'
)
sorting = 'n'
while True:
os.system('clear')
if mode == 'installed':
mods = ModPackages.get_installed_packages()
else:
mods = ModPackages.get_removed_packages()
if len(mods) == 0:
if mode == 'installed':
print(
'No mods are installed! '
'Try running "Import Game Mods" to import your existing mods or "Install New Mod" to start!'
)
else:
print('No mods recently removed')
return 'wait'
max_len = {
'name': 8,
'vers': 7,
'date': 7,
'author': 6
}
for pkg in mods:
max_len['name'] = max(max_len['name'], len(pkg.name))
if pkg.installed_version is not None:
max_len['vers'] = max(max_len['vers'], len(pkg.installed_version))
max_len['date'] = max(max_len['date'], len(pkg.update.strftime('%Y-%m-%d')))
max_len['author'] = max(max_len['author'], len(pkg.owner))
if mode == 'installed':
print('Installed Mods')
else:
print('Removed Mods')
print('')
print_row({
'id': '#',
'name': 'Mod Name',
'version': 'Version',
'date': 'Updated',
'rating': 'Rating',
'author': 'Author'
})
print_sep()
if sorting == 'v':
mods = sorted(mods, key=lambda item: item.installed_version if item.installed_version is not None else '')
elif sorting == 'd':
mods = sorted(mods, key=lambda item: item.update)
elif sorting == 'r':
mods = sorted(mods, key=lambda item: item.rating)
elif sorting == 'a':
mods = sorted(mods, key=lambda item: item.owner)
else:
mods = sorted(mods, key=lambda item: item.name)
counter = 1
for pkg in mods:
print_row({
'id': str(counter),
'name': pkg.name,
'version': pkg.installed_version if pkg.installed_version is not None else 'N/A',
'date': pkg.update.strftime('%Y.%m.%d'),
'rating': str(pkg.rating),
'author': pkg.owner
})
counter += 1
print('')
print('Change sorting by entering [n]ame, [v]ersion, [d]ate, [r]ating, or [a]uthor,')
print('view or manage a mod with 1 - ' + str(counter-1) + ', or ENTER to return')
opt = input('(n, v, d, r, a, 1-' + str(counter-1) + '): ').lower()
if opt == '':
# Return to menu
return ''
elif re.match('^[0-9]*$', opt) is not None and counter > int(opt) > 0:
# Number, select a specific mod
_manage_mod(mods[int(opt)-1])
else:
sorting = opt
def install_new():
"""
Provide a UI to install a new mod from a search field
Returns
-------
str|None
'wait' is returned on changes to allow the user to see results,
or None if nothing performed
"""
print('Install New Mod')
print('')
opt = input('Enter the mod name or URL to install (or ENTER to return): ')
if opt == '':
return
mod = None
mods = ModPackages.search(opt)
if len(mods) == 0:
print('No mods found!')
_wait()
return install_new()
elif len(mods) > 1:
mods.sort(reverse=True, key=lambda mod: mod.rating)
opts = []
for m in mods:
opts.append((m.name + ' by ' + m.owner + ' last updated ' + m.update.strftime('%Y-%m-%d'), m))
opt = _menu(title='Multiple mods found', options=opts, back=True, default='b')
if opt is None:
return install_new()
else:
mod = opt
else:
mod = mods[0]
opts = []
for v in mod.versions:
opts.append((v.version + ' released ' + v.created.strftime('%Y-%m-%d'), v))
vers = _menu(title='Select Version (or ENTER to auto select newest)', options=opts, back=True, default='1')
if vers is None:
return
mod.selected_version = vers.version
print('Installing ' + mod.name + ' v' + vers.version)
print(vers.description)
print('')
try:
opt = input('ENTER to resume, CTRL+C to stop: ')
except KeyboardInterrupt:
opt = 'n'
if opt == '':
print('Installing mod...')
mod.install()
print('Deploying to local game client...')
ModPackages.sync_game()
print('Mod installed')
return 'wait'
else:
print('not installing')
def export_package():
"""
Export all changes to ZIP files for deployment
Returns
-------
str
'wait' is returned to indicate that the user needs to press 'Enter' to continue
"""
print('Exporting mod packages...')
full = ModPackages.export_full()
updates = ModPackages.export_updates()
changelog = ModPackages.export_changelog()
modlist = ModPackages.export_modlist()
try:
if ModPackages.config['sftp_host'] != '':
print('Uploading server packages to ' + ModPackages.config['sftp_host'] + '...')
ModPackages.export_server_sftp()
else:
print('No SFTP host set, skipping server upload')
except KeyError:
print('No SFTP host set, skipping server upload')
ModPackages.commit_changes()
print('Created bundles:')
print('Full: ' + full)
print('Updates: ' + updates)
print('Changelog: ' + changelog)
print('Modlist: ' + modlist)
return 'wait'
def check_updates() -> str:
"""
Check if mods have updates and provide the user with an option to install them
Returns
-------
str
'wait' is returned to indicate that the user needs to press 'Enter' to continue
"""
print('Checking for updates...')
print('')
updates_available = False
opts = [('Install all updates', 'ALL')]
for pkg in ModPackages.get_installed_packages():
updates = pkg.check_update_available()
v1 = pkg.get_installed_version().version
v2 = pkg.get_highest_version().version
if updates:
opts.append((pkg.name + ' ' + v1 + ' update available to ' + v2, pkg))
updates_available = True
if not updates_available:
print('No mod updates are available!')
return 'wait'
opt = _menu(title='Select an update to perform or ENTER to update all', options=opts, default='1', back=True)
if opt is None:
# User opted to not perform any updates
return ''
elif opt == 'ALL':
# User opted to perform ALL updates
for pkg in ModPackages.get_installed_packages():
if pkg.check_update_available():
pkg.upgrade()
print('Updated ' + pkg.name)
ModPackages.sync_game()
else:
# Specific package to update
opt.upgrade()
ModPackages.sync_game()
print('Updated ' + opt.name)
return 'wait'
def rollback() -> str:
"""
Revert / rollback pending changes prior to deployment, useful for borked mods
Returns
-------
str
'wait' is returned to indicate that the user needs to press 'Enter' to continue
"""
print('Checking for changes...')
print('')
updates_available = False
opts = []
pkgs = []
opts.append(('Rollback everything', 'ALL'))
for pkg in ModPackages.get_installed_packages():
try:
changes = ModPackages.changed[pkg.uuid]
if changes['old'] == changes['new']:
# Changes recorded, but must have already been rolled back
continue
elif changes['old'] is None:
# New record
opts.append(('Remove ' + pkg.name + ' ' + pkg.installed_version, pkg))
pkgs.append(pkg)
updates_available = True
elif changes['new'] is None:
# Removed record
opts.append(('Reinstall ' + pkg.name + ' ' + changes['old'], pkg))
pkgs.append(pkg)
updates_available = True
else:
# Updated
opts.append(('Revert ' + pkg.name + ' from ' + changes['new'] + ' to ' + changes['old'], pkg))
pkgs.append(pkg)
updates_available = True
except KeyError:
# No changes recorded, nothing to perform
pass
if not updates_available:
print('No changes found')
return 'wait'
opt = _menu(title='Select an update to revert or ENTER to rollback everything', options=opts, default='1',
back=True)
if opt is None:
# User opted to not perform any updates
return ''
elif opt == 'ALL':
# User opted to perform ALL updates
for pkg in pkgs:
pkg.rollback()
print('Reverted ' + pkg.name)
ModPackages.sync_game()
else:
# Specific package to update
opt.rollback()
ModPackages.sync_game()
print('Reverted ' + opt.name)
return 'wait'
def remove() -> str:
"""
Provide a UI for the user to remove an installed mod
Returns
-------
str
'wait' is returned to indicate that the user needs to press 'Enter' to continue
"""
pkgs = ModPackages.get_installed_packages()
opts = []
c = -1
for pkg in pkgs:
c += 1
opts.append((pkg.name + ' ' + pkg.installed_version, c))
if len(opts) == 0:
print('No mods installed, nothing to remove.')
return 'wait'
opts.append(('**REMOVE ALL MODS**', '_ALL_'))
opt = _menu(title='Uninstalling Mod', options=opts, back=True, default='b')
if opt is None:
return ''
if opt == '_ALL_':
for pkg in pkgs:
print('Removing mod ' + pkg.name + '...')
pkg.remove()
else:
print('Removing mod...')
pkgs[opt].remove()
print('Removing files from game client...')
ModPackages.sync_game()
print('Selected mod has been removed')
return 'wait'
def import_existing() -> str:
"""
Load all currently installed game mods into the manager, useful on first run and if a mod is manually installed
Returns
-------
str
'wait' is returned to indicate that the user needs to press 'Enter' to continue
"""
print('Scanning for current packages...')
packages = ModPackages.get_synced_packages()
check = []
dupes = []
for p in packages:
if p.name in check and p.name not in dupes:
dupes.append(p.name)
else:
check.append(p.name)
if len(dupes) > 0:
# The manifest doesn't contain all data to uniquely identify the source package,
# and some authors will fork projects to publish under the same name.
for d in dupes:
opts = []
for p in packages:
if p.name == d:
opts.append((p.name + ' by ' + p.owner + ' last updated ' + p.update.strftime('%Y-%m-%d'), p))
opt = _menu(title='Duplicates found for package, please select the one to install', options=opts)
# Since we can't modify a list while iterating over, (and modifying it will change the keys),
# create a copy list and copy valid entries over
p1 = []
for p in packages:
if p.name != d or p == opt:
p1.append(p)
packages = p1
print('')
for p in packages:
print('* ' + p.name + ' ' + p.selected_version)
if len(packages) > 0:
try:
opt = input('ENTER to load current mods, CTRL+C to stop: ')
except KeyboardInterrupt:
opt = 'n'
else:
opt = 'n'
if opt == '':
for p in packages:
print('Installing ' + p.name + ' ' + p.selected_version + '...')
p.install()
return 'wait'
def sync_existing() -> str:
"""
Import any existing game mod and push any registered mod
:return:
"""
import_existing()
ModPackages.sync_game()
return ''
def _manage_mod(mod: Package):
os.system('clear')
if mod.installed_version is not None:
print(mod.name + ' ' + mod.installed_version)
else:
print(mod.name)
print('')
if mod.check_update_available():
print('Status: Update Available!')
elif mod.installed_version is not None:
print('Status: Installed, Up to date')
else:
print('Status: Not installed')
print('Author: ' + mod.owner)
print('Updated: ' + mod.update.strftime('%Y-%m-%d'))
print('Rating: ' + str(mod.rating))
print('Categories: ' + ', '.join(mod.categories))
print('URL: ' + mod.url)
print('')
if mod.installed_version is not None:
print(mod.get_installed_version().description)
else:
print(mod.get_highest_version().description)
print('')
if mod.check_update_available():
options = ('r', 'u')
print('Actions: [r]emove, [u]pdate, or ENTER to return')
elif mod.installed_version is not None:
options = ('r',)
print('Actions: [r]emove or ENTER to return')
else:
options = ('i',)
print('Actions: [i]nstall for the latest version or ENTER to return')
opt = input('(' + ', '.join(options) + ', or ENTER): ').lower()
if opt == 'r' and opt in options:
print('Removing mod...')
mod.remove()
print('Removing files from game client...')
ModPackages.sync_game()
print('Selected mod has been removed')
_wait()
elif opt == 'u' and opt in options:
print('Updating mod...')
mod.upgrade()
print('Syncing game client...')
ModPackages.sync_game()
print('Updated ' + mod.name)
_wait()
elif opt == 'i' and opt in options:
print('Installing mod...')
mod.install()
print('Syncing game client...')
ModPackages.sync_game()
print('Mod installed')
_wait()
# Check if the user performed manual changes first (also useful on first run)
check_environment()
# Run the main menu until they quit.
while True:
menu_main()