Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions openrac/tools/openrac_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,24 +485,55 @@ def play_plugins(data: Path, runtime: Path = RUNTIME) -> Path | None:
(DATA/plugins, where the launcher keeps the player's), and a list beside the player's
plugins.json, which it leaves as it is, with that mod on. None when this editor carries no
built mod (a checkout, not a download)."""
if not (runtime / "openrac-mod.json").is_file():
if not install_runtime(data, runtime):
return None
folder = data / "plugins"
installed = folder / RUNTIME_ID
play = folder / PLAY_LIST
write_mod_list(play, with_runtime(read_mod_list(folder / "plugins.json")))
return play


def install_runtime(data: Path, runtime: Path = RUNTIME) -> bool:
"""The editor's mod brought up to date in DATA/plugins; False when this editor carries no built
mod (a checkout, not a download)."""
if not (runtime / "openrac-mod.json").is_file():
return False
installed = data / "plugins" / RUNTIME_ID
if not same_tree(runtime, installed):
if installed.exists():
shutil.rmtree(installed)
shutil.copytree(runtime, installed)
mods: list = []
return True


def read_mod_list(path: Path) -> list:
try:
listed = json.loads((folder / "plugins.json").read_text(encoding="utf-8"))
mods = [m for m in listed.get("mods", []) if isinstance(m, dict)]
listed = json.loads(path.read_text(encoding="utf-8"))
return [m for m in listed.get("mods", []) if isinstance(m, dict)]
except (OSError, ValueError, AttributeError):
pass
mods = [m for m in mods if m.get("id") != RUNTIME_ID] + [{"id": RUNTIME_ID, "enabled": True}]
play = folder / PLAY_LIST
play.write_text(json.dumps({"format": 1, "mods": mods}, indent=2) + "\n", encoding="utf-8", newline="\n")
return play
return []


def with_runtime(mods: list) -> list:
return [m for m in mods if m.get("id") != RUNTIME_ID] + [{"id": RUNTIME_ID, "enabled": True}]


def write_mod_list(path: Path, mods: list) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps({"format": 1, "mods": mods}, indent=2) + "\n", encoding="utf-8", newline="\n")


def turn_on_runtime(data: Path, runtime: Path = RUNTIME) -> bool:
"""For a planet that needs it (a shared map's): the editor's mod installed in DATA/plugins and
on in the player's own list (plugins.json), their other mods as they were. False when this
editor carries no built mod."""
if not install_runtime(data, runtime):
return False
listed = data / "plugins" / "plugins.json"
mods = read_mod_list(listed)
if {"id": RUNTIME_ID, "enabled": True} not in mods:
write_mod_list(listed, with_runtime(mods))
return True


def same_tree(a: Path, b: Path) -> bool:
Expand Down
19 changes: 19 additions & 0 deletions openrac/tools/shared_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,19 @@ def import_map(path: Path, projects: Path, disc_path: Path | None = None) -> dic
# COMMAND LINE
# =============================================================================

def needs_runtime(project: Path, level: int) -> bool:
"""Whether PROJECT's level has what only the editor-runtime mod plays: simple moves (objects
of other levels brought for their look among them) or cutscenes."""
import behaviours
level_dir = project / "levels" / f"level_{level:02}"
scene = level_dir / f"level_{level:02}.tscn"
try:
moves = behaviours.given(scene.read_text(encoding="utf-8"))
except OSError:
moves = []
return bool(moves) or (level_dir / "cutscenes").is_dir()


def main(argv=None) -> int:
parser = argparse.ArgumentParser(prog="openrac_editor.py map", description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
Expand Down Expand Up @@ -782,6 +795,12 @@ def main(argv=None) -> int:
raise FormatError(str(exc), code=planet.code_of(exc)) from exc
print(f"Added it to your mods as the planet {added['name']}: turn on Mods, board the ship "
"and pick it in the ship's menu.")
if needs_runtime(done["project"], done["level"]):
import openrac_editor
if openrac_editor.turn_on_runtime(args.planet.resolve().parent):
print("Turned on the editor-runtime mod: this map's moving objects and cutscenes need it.")
else:
print(" check: this map needs the editor-runtime mod: turn it on in Mods.")
print(f"openrac-output: {done['project']}")
return 0
except (MapError, planet.PlanetError, FormatError, OSError, struct.error, zipfile.BadZipFile) as exc:
Expand Down
14 changes: 14 additions & 0 deletions openrac/tools/test_openrac_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,20 @@ def test_play_turns_the_editors_mod_on_and_keeps_the_players(self):
oe.play_plugins(data, runtime)
self.assertEqual((data / "plugins" / "editor-runtime" / "lib" / "editor_runtime.so").read_bytes(), b"v2")

def test_a_map_that_needs_it_turns_the_editors_mod_on_in_the_players_list(self):
with tempfile.TemporaryDirectory() as tmp:
runtime = Path(tmp) / "editor" / "mods" / "editor-runtime"
runtime.mkdir(parents=True)
(runtime / "openrac-mod.json").write_text('{"id": "editor-runtime"}')
data = Path(tmp) / "data"
self.assertFalse(oe.turn_on_runtime(data, Path(tmp) / "nothing"))
(data / "plugins").mkdir(parents=True)
(data / "plugins" / "plugins.json").write_text('{"format": 1, "mods": [{"id": "lock-strafe", "enabled": true}]}')
self.assertTrue(oe.turn_on_runtime(data, runtime))
self.assertEqual(json.loads((data / "plugins" / "plugins.json").read_text())["mods"],
[{"id": "lock-strafe", "enabled": True}, {"id": "editor-runtime", "enabled": True}])
self.assertTrue((data / "plugins" / "editor-runtime" / "openrac-mod.json").is_file())

def test_on_a_mac_the_game_draws_with_vulkan_like_the_launcher(self):
with mock.patch.object(oe.sys, "platform", "darwin"), mock.patch.dict(oe.os.environ, {}, clear=True):
self.assertEqual(oe.game_environment()["OPENRAC_GL_API"], "vulkan")
Expand Down
Loading