Created
May 11, 2025 08:27
-
-
Save friedkeenan/f2742080d571d1f7ffcc4a9fc0cf68c1 to your computer and use it in GitHub Desktop.
Script for getting objects out of the Minecraft launcher database, like sounds
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| import fnmatch | |
| import json | |
| import shutil | |
| from pathlib import Path | |
| def indexes(assets_dir): | |
| return [ | |
| p.stem for p in Path(assets_dir, "indexes").glob("*.json") | |
| ] | |
| def object_hashes(index_path, object_pattern): | |
| with Path(index_path).open("r") as f: | |
| index = json.load(f) | |
| return { | |
| object_path: description["hash"] | |
| for object_path, description in index["objects"].items() | |
| if fnmatch.fnmatch(object_path, object_pattern) | |
| } | |
| def object_paths(index_path, object_pattern): | |
| index_path = Path(index_path) | |
| return { | |
| object_path: Path(index_path.parent.parent, "objects", hash[:2], hash) | |
| for object_path, hash in | |
| object_hashes(index_path, object_pattern).items() | |
| } | |
| def main(args): | |
| if args.list_indexes: | |
| for index in indexes(args.assets_dir): | |
| print(index) | |
| else: | |
| index_path = Path(args.assets_dir, "indexes", args.index + ".json") | |
| objects = object_paths(index_path, args.pattern) | |
| if len(objects) <= 0: | |
| print("Unable to find any objects!") | |
| return | |
| args.output_dir.mkdir(parents=True, exist_ok=True) | |
| for object_path, file_path in objects.items(): | |
| result_path = args.output_dir / object_path.replace("/", "_") | |
| shutil.copyfile(file_path, result_path) | |
| if __name__ == "__main__": | |
| import sys | |
| import argparse | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--assets-dir", type=Path, help="The base assets directory", default=Path()) | |
| parser.add_argument("-l", "--list-indexes", help="List the possible indexes", action="store_true") | |
| # NOTE: I don't enjoy this. | |
| listing_indexes = ( | |
| "--list-indexes" in sys.argv[1:] or | |
| "-l" in sys.argv[1:] | |
| ) | |
| parser.add_argument("-o", "--output-dir", type=Path, help="The directory to place the objects", default=Path()) | |
| parser.add_argument("-i", "--index", help="The index for the objects", required=not listing_indexes) | |
| parser.add_argument("-p", "--pattern", help="The pattern for object paths to extract", required=not listing_indexes) | |
| main(parser.parse_args()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment