-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add utilities to retrieve script modules from game projects
- Loading branch information
1 parent
d0abb85
commit 83590b7
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters