-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathfbp.py
executable file
·199 lines (165 loc) · 5.77 KB
/
fbp.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
#!/usr/bin/env python3
# Written by Brian White <[email protected]>
# This file is covered by the CC0 license.
import argparse
import json
import os
import re
import sys
import bpcodec
ARGS = None
ACTIVE_INDEX = 'active_index'
BLUEPRINT = 'blueprint'
BLUEPRINTS = 'blueprints'
BLUEPRINT_BOOK = 'blueprint_book'
DECONSTRUCTION_PLANNER = 'deconstruction_planner'
UPGRADE_PLANNER = 'upgrade_planner'
ITEM = 'item'
META = '.metadata'
def SafeName(name):
return re.sub('[^A-Za-z0-9]+', '', name)
def SplitBlueprints(dir, blueprints):
for bp in blueprints:
metadata = {}
metafile = None
def WriteMetadata():
nonlocal metadata, metafile
if metadata:
assert metafile, f'Have metadata but no metafile ({metadata}).'
file = open(metafile, 'w')
file.write(bpcodec.Pretty(metadata))
file.close()
metadata = {}
metafile = None
for type, obj in bp.items():
#print("-", type, obj)
if type == BLUEPRINT_BOOK:
assert 'label' in obj, f'Object of type "{type}" has no label.'
print('Book:', obj['label'])
WriteMetadata()
subdir = SafeName(obj['label'])
newdir = dir + '/' + subdir
metafile = newdir + META
SplitBook(newdir, obj)
elif type == BLUEPRINT or type == DECONSTRUCTION_PLANNER or type == UPGRADE_PLANNER:
assert 'label' in obj, f'Object of type "{type}" has no label.'
print(f"- {type}: {obj['label']}")
WriteMetadata()
name = SafeName(obj['label'])
path = dir + '/' + name
metafile = path + META
file = open(path + '.json', 'w')
file.write(bpcodec.Pretty({ type: obj }))
file.close()
else:
metadata[type] = obj
WriteMetadata()
def SplitBook(dir, book):
try:
os.mkdir(dir, mode=0o755)
except FileExistsError:
pass
metadata = {}
for type, obj in book.items():
#print(type, obj)
if type == BLUEPRINTS:
SplitBlueprints(dir, obj)
else:
metadata[type] = obj
file = open(dir + '/' + META, 'w')
file.write(bpcodec.Pretty(metadata))
file.close()
def Split(outdir, infile):
assert outdir, '"split" requires an "outdir"'
assert infile, '"split" requires an "infile"'
file = open(infile, 'r')
text = file.read(1<<30) # 1GiB max
data = bpcodec.Decode(text)
#print(bpcodec.Pretty(data))
assert len(data) == 1, 'infile should have only one top-level object'
for type, book in data.items():
assert type == 'blueprint_book', f'Blueprint is not a book (was {type}).'
SplitBook(outdir, book)
def BuildBook(dir):
blueprints = []
#print('dir:', dir)
for entry in os.listdir(dir):
path = dir + '/' + entry
if entry[0] == '.':
continue
if entry.endswith(META):
continue
if os.path.isfile(path):
file = open(path, 'r')
data = json.load(file)
file.close()
print(f"- blueprint: {entry}")
blueprints.append(data)
elif os.path.isdir(path):
print(f"Book: {entry}")
data = BuildBook(path)
blueprints.append(data)
else:
assert False, f'Somehow "{path}" is neither a file nor directory.'
path = os.path.splitext(path)[0] + META
file = open(path, 'r')
meta = json.load(file)
file.close()
for key, val in meta.items():
blueprints[-1][key] = val
book = { BLUEPRINTS: blueprints }
file = open(dir + '/' + META, 'r')
data = file.read()
file.close()
meta = json.loads(data)
#print('meta:', meta)
for key, val in meta.items():
#print('book-meta:', key, val)
book[key] = val
return { BLUEPRINT_BOOK: book }
def Build(outfile, indir):
assert outfile, '"build" requires an "outfile"'
assert indir, '"build" requires an "indir"'
book = BuildBook(indir)
#print('top-book:', json.dumps(book))
file = open(outfile, 'w')
file.write(bpcodec.Encode(book))
file.close()
def Unpack(outfile, infile):
assert outfile, '"unpack" requires an "outfile"'
assert infile, '"unpack" requires an "infile"'
file = open(infile, 'r')
text = file.read(10<<20) # 10MiB max
file.close()
bp = bpcodec.Decode(text)
file = open(outfile, 'w')
file.write(bpcodec.Pretty(bp))
file.close()
def Pack(outfile, infile):
assert outfile, '"pack" requires an "outfile"'
assert infile, '"pack" requires an "infile"'
file = open(infile, 'r')
data = json.load(file)
file.close()
text = bpcodec.Encode(data)
file = open(outfile, 'w')
file.write(text)
file.close()
def main():
if ARGS.action == 'split':
Split(ARGS.outdir, ARGS.infile)
if ARGS.action == 'build':
Build(ARGS.outfile, ARGS.indir)
if ARGS.action == 'unpack':
Unpack(ARGS.outfile, ARGS.infile)
if ARGS.action == 'pack':
Pack(ARGS.outfile, ARGS.infile)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Factorio Blueprint Tools')
parser.add_argument('action', choices=['build', 'split', 'unpack', 'pack'], help='what action to perform')
parser.add_argument('--indir', help='directory of individual blueprints to read')
parser.add_argument('--outdir', help='directory of individual blueprints to write')
parser.add_argument('--infile', help='blueprint file to read')
parser.add_argument('--outfile', help='blueprint file to write')
ARGS = parser.parse_args(sys.argv[1:])
main()