forked from BLCM/bl3mods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modlist_to_json.py
executable file
·124 lines (105 loc) · 3.81 KB
/
modlist_to_json.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
#!/usr/bin/env python3
# vim: set expandtab tabstop=4 shiftwidth=4:
# Copyright 2019-2020 Christopher J. Kucera
# <cj@apocalyptech.com>
# <http://apocalyptech.com/contact.php>
#
# This Borderlands 3 Hotfix Mod is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This Borderlands 3 Hotfix Mod is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this Borderlands 3 Hotfix Mod. If not, see
# <https://www.gnu.org/licenses/>.
import os
import sys
import json
import argparse
parser = argparse.ArgumentParser(
description='Converts modlist.txt style BL3 modsets to JSON compatible with c0dycode\'s injector',
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument('-m', '--modlist',
type=str,
default='modlist.txt',
help='File to read mod sets from',
)
parser.add_argument('-o', '--output',
type=str,
default='modlist.json',
help='JSON file to output',
)
parser.add_argument('-f', '--force',
action='store_true',
help='Overwrite output file without prompting',
)
parser.add_argument('-v', '--verbose',
action='store_true',
help='Verbose output (show modfile contents as it goes)',
)
args = parser.parse_args()
if os.path.exists(args.output) and not args.force:
sys.stdout.write('WARNING: {} already exists. Overwrite [y/N]? '.format(args.output))
sys.stdout.flush()
response = sys.stdin.readline().strip().lower()
if len(response) == 0 or response[0] != 'y':
print('Aborting!')
sys.exit(1)
print('')
if not os.path.exists(args.modlist):
print('ERROR: {} does not exist'.format(args.modlist))
sys.exit(1)
def process_modfile(modpath, prefix, verbose=False):
to_ret = []
with open(modpath, encoding='utf-8') as mod_df:
hf_counter = 0
for linenum, modline in enumerate(mod_df):
modline = modline.strip()
if verbose:
print('{} line {}: {}'.format(modpath, linenum+1, modline))
if modline == '' or modline.startswith('#'):
continue
# Check for prefix
hf_counter += 1
hftype, hf = modline.split(',', 1)
key = '{}-Apoc{}-{}'.format(hftype, prefix, hf_counter)
to_ret.append((key, hf))
if verbose:
print('')
return to_ret
# Start constructing
json_out = {
'service_name': 'Micropatch',
'configuration_group': 'OakDefault',
'configuration_version': '1.0',
'parameters': [],
}
mod_count = 0
with open(args.modlist, encoding='utf-8') as modlist_df:
for line in modlist_df:
line = line.strip()
if line == '' or line.startswith('#'):
continue
modpath = '{}.txt'.format(line)
if not os.path.exists(modpath):
print('WARNING: {} was not found, skipping...'.format(modpath))
continue
print('Processing: {}'.format(modpath))
for (key, value) in process_modfile(modpath, '{:X}'.format(mod_count), verbose=args.verbose):
json_out['parameters'].append({'key': key, 'value': value})
mod_count += 1
# Write out
with open(args.output, 'w') as json_df:
json.dump(json_out, json_df, indent=' ')
# Report!
print('Done! Wrote {} mods (with {} total hotfixes) to {}'.format(
mod_count,
len(json_out['parameters']),
args.output,
))