Advertisement
dachte

mcpack_convert_alpha.py

Mar 11th, 2021
1,151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import argparse
  4. import code # for code.interact(local=dict(globals(), **locals()) ) debugging
  5. import glob
  6. import json
  7. import os
  8. import re
  9. import shutil
  10. import tempfile
  11. from typing import Dict, List, Tuple # for mypy
  12.  
  13. ######################
  14. # programname
  15. #
  16. # What the program is for
  17.  
  18. def main():
  19.     cfg = handle_args()
  20.     orig_metadata = jload(os.path.join(cfg.src, 'manifest.json'))
  21.  
  22.     with tempfile.TemporaryDirectory() as td:
  23.         # In this context manager, we made a directory into which we'll place everything before we zip it
  24.         shutil.copy(src=os.path.join(cfg.src, 'pack_icon.png'), dst=(os.path.join(td, 'pack.png')))
  25.         with open(os.path.join(td, 'pack.mcmeta'), 'w') as mcmf:
  26.             data =  {
  27.                 "pack": {
  28.                     "pack_format": cfg.pack_ver,
  29.                     "description": orig_metadata['header']['description']
  30.                     }
  31.                 }
  32.             json.dump(data, mcmf)
  33.         os.makedirs(os.path.join(td, 'assets', 'minecraft', 'textures', 'block'))
  34.         for btfile in os.listdir(os.path.join(cfg.src, 'textures', 'blocks')):
  35.             if '_mer' in btfile:
  36.                 continue
  37.             if 'texture_set' in btfile:
  38.                 continue
  39.             if not os.path.isfile(os.path.join(cfg.src, 'textures', 'blocks', btfile)):
  40.                 continue # Embedded dir?
  41.             print(f"Processing texture {btfile}")
  42.             targfn = btfile
  43.             # TODO add more specific rules for remapping names here.
  44.             if btfile.startswith('wool_colored_'):
  45.                 targfn = re.sub('wool_colored_(.*).png', '\\1_wool.png', btfile)
  46.             # We may also bail for textures that are too different in format (e.g. lava flow), or figure out how to convert them
  47.             shutil.copy(src=os.path.join(cfg.src, 'textures', 'blocks', btfile), dst=os.path.join(td, 'assets', 'minecraft', 'textures', 'block', targfn))
  48.         shutil.make_archive(f"{orig_metadata['header']['name']}-{orig_metadata['header']['version'][0]}.{orig_metadata['header']['version'][1]}.{orig_metadata['header']['version'][2]}", 'zip', td)
  49.  
  50.  
  51. def handle_args():
  52.     parser = argparse.ArgumentParser(description="This does a thing")
  53.     parser.add_argument("--src",    default='dlight', type=str, help="source dir for bedrock pack")
  54.     parser.add_argument("--pack_ver",   default=6, type=int,    help="Pack version")
  55.     ret = parser.parse_args()
  56.     return ret
  57.  
  58. #####
  59.  
  60. def jload(fn:str):
  61.     with open(fn, 'r') as fh:
  62.         ret = json.load(fh)
  63.     return ret
  64.  
  65.  
  66. #####
  67. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement