Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cli commands for extract pkgs and replacing files in pkgs #214

Merged
merged 2 commits into from
Aug 29, 2024
Merged
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: 50 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

Check warning on line 13 in src/mercury_engine_data_structures/cli.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/cli.py#L13

Added line #L13 was not covered by tests
from mercury_engine_data_structures.game_check import Game


Expand Down Expand Up @@ -73,6 +73,22 @@
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(

Check warning on line 78 in src/mercury_engine_data_structures/cli.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/cli.py#L76-L78

Added lines #L76 - L78 were not covered by tests
"--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")

Check warning on line 82 in src/mercury_engine_data_structures/cli.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/cli.py#L81-L82

Added lines #L81 - L82 were not covered by tests

replace_files_parser = subparser.add_parser(

Check warning on line 84 in src/mercury_engine_data_structures/cli.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/cli.py#L84

Added line #L84 was not covered by tests
"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")

Check warning on line 90 in src/mercury_engine_data_structures/cli.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/cli.py#L87-L90

Added lines #L87 - L90 were not covered by tests

return parser


Expand Down Expand Up @@ -232,6 +248,35 @@
print(m)


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

Check warning on line 253 in src/mercury_engine_data_structures/cli.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/cli.py#L251-L253

Added lines #L251 - L253 were not covered by tests

pkg_editor = FileTreeEditor(root, args.game)

Check warning on line 255 in src/mercury_engine_data_structures/cli.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/cli.py#L255

Added line #L255 was not covered by tests

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))

Check warning on line 263 in src/mercury_engine_data_structures/cli.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/cli.py#L257-L263

Added lines #L257 - L263 were not covered by tests


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

Check warning on line 269 in src/mercury_engine_data_structures/cli.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/cli.py#L266-L269

Added lines #L266 - L269 were not covered by tests

pkg_editor = FileTreeEditor(root, args.game)

Check warning on line 271 in src/mercury_engine_data_structures/cli.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/cli.py#L271

Added line #L271 was not covered by tests

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())

Check warning on line 275 in src/mercury_engine_data_structures/cli.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/cli.py#L273-L275

Added lines #L273 - L275 were not covered by tests

pkg_editor.save_modifications(output, OutputFormat.PKG)

Check warning on line 277 in src/mercury_engine_data_structures/cli.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/cli.py#L277

Added line #L277 was not covered by tests


def main():
logging.basicConfig(level=logging.INFO)
args = create_parser().parse_args()
Expand All @@ -246,5 +291,9 @@
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)

Check warning on line 297 in src/mercury_engine_data_structures/cli.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/cli.py#L294-L297

Added lines #L294 - L297 were not covered by tests
else:
raise ValueError(f"Unknown command: {args.command}")