Factorio: make apworld compatible(#935)

This commit is contained in:
Fabian Dill 2022-08-18 01:33:40 +02:00 committed by GitHub
parent a1aa9c17ff
commit 0d61192c67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 7 deletions

View File

@ -78,9 +78,14 @@ def generate_mod(world, output_directory: str):
global data_final_template, locale_template, control_template, data_template, settings_template
with template_load_lock:
if not data_final_template:
mod_template_folder = os.path.join(os.path.dirname(__file__), "data", "mod_template")
def load_template(name: str):
import pkgutil
data = pkgutil.get_data(__name__, "data/mod_template/" + name).decode()
return data, name, lambda: False
template_env: Optional[jinja2.Environment] = \
jinja2.Environment(loader=jinja2.FileSystemLoader([mod_template_folder]))
jinja2.Environment(loader=jinja2.FunctionLoader(load_template))
data_template = template_env.get_template("data.lua")
data_final_template = template_env.get_template("data-final-fixes.lua")
locale_template = template_env.get_template(r"locale/en/locale.cfg")
@ -158,7 +163,21 @@ def generate_mod(world, output_directory: str):
mod_dir = os.path.join(output_directory, mod_name + "_" + Utils.__version__)
en_locale_dir = os.path.join(mod_dir, "locale", "en")
os.makedirs(en_locale_dir, exist_ok=True)
shutil.copytree(os.path.join(os.path.dirname(__file__), "data", "mod"), mod_dir, dirs_exist_ok=True)
if world.zip_path:
# Maybe investigate read from zip, write to zip, without temp file?
with zipfile.ZipFile(world.zip_path) as zf:
for file in zf.infolist():
if not file.is_dir() and "/data/mod/" in file.filename:
path_part = Utils.get_text_after(file.filename, "/data/mod/")
target = os.path.join(mod_dir, path_part)
os.makedirs(os.path.split(target)[0], exist_ok=True)
with open(target, "wb") as f:
f.write(zf.read(file))
else:
shutil.copytree(os.path.join(os.path.dirname(__file__), "data", "mod"), mod_dir, dirs_exist_ok=True)
with open(os.path.join(mod_dir, "data.lua"), "wt") as f:
f.write(data_template_code)
with open(os.path.join(mod_dir, "data-final-fixes.lua"), "wt") as f:

View File

@ -1,7 +1,7 @@
from typing import Dict, List, Set
from collections import deque
from worlds.factorio.Options import TechTreeLayout
from .Options import TechTreeLayout
funnel_layers = {TechTreeLayout.option_small_funnels: 3,
TechTreeLayout.option_medium_funnels: 4,

View File

@ -19,8 +19,8 @@ pool = ThreadPoolExecutor(1)
def load_json_data(data_name: str) -> Union[List[str], Dict[str, Any]]:
with open(os.path.join(source_folder, f"{data_name}.json")) as f:
return json.load(f)
import pkgutil
return json.loads(pkgutil.get_data(__name__, "data/" + data_name + ".json").decode())
techs_future = pool.submit(load_json_data, "techs")

View File

@ -1,7 +1,7 @@
import collections
import typing
from ..AutoWorld import World, WebWorld
from worlds.AutoWorld import World, WebWorld
from BaseClasses import Region, Entrance, Location, Item, RegionType, Tutorial, ItemClassification
from .Technologies import base_tech_table, recipe_sources, base_technology_table, \