-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_factorio_data.py
executable file
·128 lines (103 loc) · 4.16 KB
/
extract_factorio_data.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
#!/usr/bin/env python
import json
import argparse
import os.path
import platform
import subprocess
import errno
import shutil
import logging
import sys
import ConfigParser
def parse_path(value):
"""Returns the given path with ~ and environment variables expanded."""
return os.path.expanduser(os.path.expandvars(value))
def export_recipes(factorio_data_dir, output_data_dir):
export_path = os.path.abspath('export')
recipes_dir_path = os.path.join(factorio_data_dir, 'base/prototypes/recipe')
recipe_files_paths = []
for root, dirs, files in os.walk(recipes_dir_path):
recipe_files_paths.extend(os.path.join(root, f) for f in files)
recipe_json_path = os.path.join(output_data_dir, 'recipes.json')
args = [os.path.join(export_path, 'export_recipes.lua')] + recipe_files_paths
with open(recipe_json_path, 'w') as recipe_json_file:
logging.info('Exporting recipes from %r to %r...',
recipes_dir_path,
recipe_json_path)
export = subprocess.Popen(
args=args,
cwd=export_path,
stdout=recipe_json_file.fileno())
if export.wait() != 0:
raise Exception('crap')
def export_icons(factorio_data_dir, output_data_dir):
source_icon_path = os.path.join(factorio_data_dir, 'base/graphics/icons')
target_icon_path = os.path.join(output_data_dir, 'icons')
if os.path.exists(target_icon_path):
if os.path.isdir(target_icon_path):
shutil.rmtree(target_icon_path)
else:
raise Exception('unexpected file at %r' % target_icon_path)
logging.info('Copying %r to %r...', source_icon_path, target_icon_path)
shutil.copytree(source_icon_path,
target_icon_path)
_THING_SECTION_NAMES = ['entity-name',
'equipment-name',
'fluid-name',
'item-name']
_RECIPE_SECTION_NAMES = ['recipe-name']
def export_names(factorio_data_dir, output_data_dir):
source_icon_path = os.path.join(factorio_data_dir, 'base/locale/en/base.cfg')
target_icon_path = os.path.join(output_data_dir, 'names.json')
config = ConfigParser.RawConfigParser()
config.read([source_icon_path])
thing_names = {}
for section_name in _THING_SECTION_NAMES:
thing_names.update(config.items(section_name))
recipe_names = {}
for section_name in _RECIPE_SECTION_NAMES:
recipe_names.update(config.items(section_name))
with open(target_icon_path, 'w') as target_file:
json.dump({'item-names': thing_names,
'recipe-names': recipe_names}, target_file,
sort_keys=True,
indent=2)
def create_output_data_dir(path):
try:
os.makedirs(path)
except OSError, e:
if e.errno == errno.EEXIST and os.path.isdir(path):
return
else:
raise e
def main():
if platform.system() == 'Darwin':
default_data_path = '/Applications/factorio.app/Contents/data'
else:
default_data_path = None
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument(
'--factorio_data_path', metavar='PATH',
default=default_data_path,
type=parse_path,
help='path to the data (datastore, blobstore, etc.) associated with the '
'application.')
parser.add_argument(
'--output_data_path', metavar='PATH',
default=os.path.abspath('factorio-data'),
type=parse_path,
help='path to the data (datastore, blobstore, etc.) associated with the '
'application.')
args = parser.parse_args()
factorio_data_path = os.path.abspath(args.factorio_data_path)
output_path = args.output_data_path
sys.stderr.write(
'Extracting data\nfrom: %s\nto: %s\n' % (factorio_data_path,
output_path))
create_output_data_dir(output_path)
export_recipes(factorio_data_path, output_path)
export_icons(factorio_data_path, output_path)
export_names(factorio_data_path, output_path)
sys.stderr.write('Done!\n')
if __name__ == '__main__':
main()