Skip to content

Commit

Permalink
add utilities to retrieve script modules from game projects
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Dec 23, 2023
1 parent d0abb85 commit 83590b7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/agstoolbox/core/ags/get_script_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from __future__ import annotations # for python 3.8

import os.path

import defusedxml.ElementTree as ETree
from xml.etree.ElementTree import Element

from agstoolbox.core.ags.game_project import GameProject
from agstoolbox.core.ags.script_module import ScriptModule


def module_from_game_project(game_project: GameProject, module_name: str) -> ScriptModule | None:
tree = ETree.parse(game_project.path)
root = tree.getroot()
scripts_and_header_htags = root.findall('.//ScriptAndHeader')
script_htag: Element | None = None
for sah_htag in scripts_and_header_htags:
sah_script_htag = sah_htag.find('ScriptAndHeader_Script')
script_htag = sah_script_htag.find('Script')
script_name_htag = script_htag.find('Name')
if script_name_htag.text == module_name:
break
script_fname_htag = script_htag.find('FileName')
if script_fname_htag.text[:-4] == module_name:
break
script_htag = None

if script_htag is None:
return None

script_filename_htag = script_htag.find('FileName')
module_filename = script_filename_htag.text[:-4]
header_filename = module_filename + ".ash"
script_filename = module_filename + ".asc"
full_header_filename = os.path.abspath(os.path.join(game_project.directory, header_filename))
full_script_filename = os.path.abspath(os.path.join(game_project.directory, script_filename))

sm: ScriptModule = ScriptModule()
sm.name = script_htag.find('Name').text
sm.version = script_htag.find('Version').text
sm.author = script_htag.find('Author').text
sm.unique_key = script_htag.find('Key').text
sm.description = script_htag.find('Description').text

with open(full_header_filename, "r") as f:
sm.header = f.read()

with open(full_script_filename, "r") as f:
sm.script = f.read()

return sm
16 changes: 16 additions & 0 deletions src/agstoolbox/core/ags/script_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import annotations # for python 3.8


MODULE_FILE_SIGNATURE = b'AGSScriptModule\0'
MODULE_FILE_SECTION = 0xb4f76a65
MODULE_FILE_TRAILER = 0xb4f76a66


class ScriptModule:
name: str | None = None
version: str | None = None
author: str | None = None
description: str | None = None
unique_key: str | None = None
script: str | None = None
header: str | None = None
17 changes: 17 additions & 0 deletions tests/test_ags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
from pathlib import Path


# TODO: fix to not need this (in Windows, MacOS and Linux)
if os.path.isdir(os.path.join(".", "src")) and os.path.isfile(os.path.join(".", "setup.py")):
sys.path.append(os.path.realpath("src"))
Expand All @@ -14,6 +15,7 @@
text_file_starts_with_xml_Windows1252, list_game_projects_in_dir, \
get_unique_game_project_in_path
from agstoolbox.core.ags.game_project import GameProject
from agstoolbox.core.ags.get_script_module import module_from_game_project

cur_dir = Path(__file__).resolve().parent
file_path01 = join_paths_as_posix(cur_dir, 'resources/fakedir2/Game.agf')
Expand Down Expand Up @@ -53,3 +55,18 @@ def test_get_unique_game_project_in_path():
game_project: GameProject = get_unique_game_project_in_path(file_path03)
assert game_project.name == "CopyGameTitle"
assert game_project.ags_editor_version.as_str == "3.5.1.14"


def test_script_module_from_game_file():
game_proj03 = get_unique_game_project_in_path(file_path03)
game_proj04 = get_unique_game_project_in_path(file_path04)
sm03 = module_from_game_project(game_proj03, "GlobalScript")
assert sm03 is not None
assert sm03.unique_key == "764688079"
assert sm03.header.startswith("// Main header script") is True
assert sm03.script.startswith("// main global script file") is True
sm04 = module_from_game_project(game_proj04, "GlobalScript")
assert sm04 is not None
assert sm04.unique_key == "764688079"
assert sm04.header.startswith("// Main header script") is True
assert sm04.script.startswith("// main global script file") is True

0 comments on commit 83590b7

Please sign in to comment.