Skip to content

Commit

Permalink
Add cli commands for extract pkgs and replacing files in pkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquegemignani committed Aug 28, 2024
1 parent c389ebc commit e0cd82c
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/mercury_engine_data_structures/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from mercury_engine_data_structures import formats
from mercury_engine_data_structures.construct_extensions.json import convert_to_raw_python
from mercury_engine_data_structures.file_tree_editor import FileTreeEditor
from mercury_engine_data_structures.file_tree_editor import FileTreeEditor, OutputFormat
from mercury_engine_data_structures.game_check import Game


Expand Down Expand Up @@ -73,6 +73,22 @@ def create_parser():
compare.add_argument("--limit", help="Limit the number of files to test", type=int)
compare.add_argument("input_path", type=Path, help="Path to the directory to glob")

extract_files_parser = subparser.add_parser("extract-files")
add_game_argument(extract_files_parser)
extract_files_parser.add_argument("--quiet", action="store_true", default=False,
help="Don't print files as they're extracted")
extract_files_parser.add_argument("root", type=Path, help="Path to the PKG files")
extract_files_parser.add_argument("output", type=Path, help="Path to where to place output files")

replace_files_parser = subparser.add_parser(
"replace-files",
help="Adds files to all pkgs in a folder. The results are written to a different path"
)
add_game_argument(replace_files_parser)
replace_files_parser.add_argument("root", type=Path, help="Path to the PKG files to read from")
replace_files_parser.add_argument("new_files", type=Path, help="Path to where the new files are located")
replace_files_parser.add_argument("output", type=Path, help="Path to where to place modified pkgs")

return parser


Expand Down Expand Up @@ -232,6 +248,38 @@ def apply_limit(it):
print(m)


def extract_files(args: argparse.Namespace) -> None:
root: Path = args.root
output_root: Path = args.output

pkg_editor = FileTreeEditor(root, args.game)

output_root.mkdir(parents=True, exist_ok=True)
for file_name in pkg_editor.all_asset_names():
if not args.quiet:
print(file_name)
out_path = output_root.joinpath(file_name)
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_bytes(pkg_editor.get_raw_asset(file_name))


def replace_files(args: argparse.Namespace) -> None:
root: Path = args.root
new_files: Path = args.new_files
output: Path = args.output

pkg_editor = FileTreeEditor(root, args.game)

for file_name in new_files.rglob("*"):
if file_name.is_file():
pkg_editor.replace_asset(
file_name.relative_to(new_files).as_posix(),
file_name.read_bytes()
)

pkg_editor.save_modifications(output, OutputFormat.PKG)


def main():
logging.basicConfig(level=logging.INFO)
args = create_parser().parse_args()
Expand All @@ -246,5 +294,9 @@ def main():
find_pkg_for(args)
elif args.command == "compare-files":
asyncio.run(compare_all_files_in_path(args))
elif args.command == "extract-files":
extract_files(args)
elif args.command == "replace-files":
replace_files(args)
else:
raise ValueError(f"Unknown command: {args.command}")

0 comments on commit e0cd82c

Please sign in to comment.