forked from Factorio-Access/FactorioAccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfa_scenarios.py
59 lines (50 loc) · 1.85 KB
/
fa_scenarios.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
import re
from collections import defaultdict, namedtuple
import json
import fa_paths
import translations
from launch_and_monitor import launch_with_params
import fa_menu
Scenario = namedtuple('Scenario','order start_key name description')
def get_scenario_from_path(path):
with open(path,encoding='utf8') as fp:
json_desc=json.load(fp)
parts=translations.get_mod_path_parts(path)
key = parts[0]+'/'+parts[2]
locale=path.parent.joinpath('locale')
if not locale:
return None
temp_translations=defaultdict(dict)
for code in ['en',translations.code]:
sub=locale.joinpath(code)
for possible_cfg in sub.iterdir():
if not possible_cfg.name.endswith('.cfg'):
continue
with open(possible_cfg,encoding='utf8') as fp:
translations.read_cfg(fp,ret=temp_translations)
return Scenario(
json_desc['order'],
key,
temp_translations['']['scenario-name'],
temp_translations['']['description'])
def get_freeplay():
return get_scenario_from_path(fa_paths.READ_DIR.joinpath('base','scenarios','freeplay','description.json'))
def get_scenarios(m_scenario=None):
if m_scenario:
return m_scenario.name
scenarios=[]
for path in translations.iterate_over_mod_files('scenarios/.*/description.json',re.compile(r'FactorioAccess.*')):
scenario=get_scenario_from_path(path)
if scenario:
scenarios.append(scenario)
scenarios.sort()
return {s.name:s for s in scenarios}
def launch_scenario(scenario:Scenario):
return launch_with_params(['--load-scenario',scenario.start_key],)
def scenario_name(*args):
return args[-1].name
def scenario_desc(*args):
return args[-1].description
pre_launch_scenario = fa_menu.menu_item(get_scenarios,{("gui-new-game.play",):launch_scenario},scenario_desc)
if __name__ == "__main__":
print(get_scenarios())