From 16cdc9fd1216932530cc814f6bbbcd5283e44d06 Mon Sep 17 00:00:00 2001 From: James David Clarke Date: Wed, 10 Jan 2024 07:03:16 +0000 Subject: [PATCH] Seperated common convert code from CLI and GUI so they can both use it --- tools/Texture_Converter.py | 124 +++++++--------------------- tools/libtextureconverter/cli.py | 0 tools/libtextureconverter/common.py | 10 +-- 3 files changed, 30 insertions(+), 104 deletions(-) delete mode 100644 tools/libtextureconverter/cli.py diff --git a/tools/Texture_Converter.py b/tools/Texture_Converter.py index 7d01b82eb..2e47baf28 100755 --- a/tools/Texture_Converter.py +++ b/tools/Texture_Converter.py @@ -1,110 +1,42 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Texture Converter. -# Converts Minecraft resource packs to Minetest texture packs. -# See README.md. +# cli.py -__author__ = "Wuzzy" -__license__ = "MIT License" -__status__ = "Development" - -import shutil, csv, os, tempfile, sys, argparse, glob -from PIL import Image -from collections import Counter - -from libtextureconverter.utils import detect_pixel_size, target_dir, colorize, colorize_alpha, handle_default_minecraft_texture, find_all_minecraft_resourcepacks -from libtextureconverter.convert import convert_textures -from libtextureconverter.config import SUPPORTED_MINECRAFT_VERSION, working_dir, mineclone2_path, appname, home +import argparse +import sys from libtextureconverter.gui import main as launch_gui +from libtextureconverter.config import SUPPORTED_MINECRAFT_VERSION, working_dir, appname, home +from libtextureconverter.utils import handle_default_minecraft_texture, find_all_minecraft_resourcepacks +from libtextureconverter.common import convert_resource_packs -# Argument parsing -description_text = f"""This is the official MineClone 2 Texture Converter. - This will convert textures from Minecraft resource packs to - a Minetest texture pack. +def main(): + make_texture_pack = True + parser = argparse.ArgumentParser(description=f"This is the official MineClone 2 Texture Converter. This will convert textures from Minecraft resource packs to a Minetest texture pack. Supported Minecraft version: {SUPPORTED_MINECRAFT_VERSION} (Java Edition)") + parser.add_argument("-i", "--input", help="Directory of Minecraft resource pack to convert") + parser.add_argument("-o", "--output", default=working_dir, help="Directory in which to put the resulting Minetest texture pack") + parser.add_argument("-p", "--pixel-size", type=int, help="Size (in pixels) of the original textures") + parser.add_argument("-d", "--dry-run", action="store_true", help="Pretend to convert textures without changing any files") + parser.add_argument("-v", "--verbose", action="store_true", help="Print out all copying actions") + parser.add_argument("-def", "--default", action="store_true", help="Use the default Minecraft texture pack") + parser.add_argument("-a", "--all", action="store_true", help="Convert all known Minecraft texturepacks") + args = parser.parse_args() - Supported Minecraft version: {SUPPORTED_MINECRAFT_VERSION} (Java Edition) - """ -parser = argparse.ArgumentParser(description=description_text) -parser.add_argument("-i", "--input", help="Directory of Minecraft resource pack to convert") -parser.add_argument("-o", "--output", default=working_dir, help="Directory in which to put the resulting Minetest texture pack") -parser.add_argument("-p", "--pixelsize", type=int, help="Size (in pixels) of the original textures") -parser.add_argument("-d", "--dry_run", action="store_true", help="Pretend to convert textures without changing any files") -parser.add_argument("-v", "--verbose", action="store_true", help="Print out all copying actions") -parser.add_argument("-def", "--default", action="store_true", help="Use the default Minecraft texture pack") -parser.add_argument("-a", "--all", action="store_true", help="Convert all known Minecraft texturepacks") -args = parser.parse_args() - -### SETTINGS ### -base_dir = args.input -output_dir = args.output -PXSIZE = args.pixelsize -# If True, will only make console output but not convert anything. -dry_run = args.dry_run -# If True, prints all copying actions -verbose = args.verbose -# If True, textures will be put into a texture pack directory structure. -# If False, textures will be put into MineClone 2 directories. -make_texture_pack = True # Adjust as needed - -if __name__ == "__main__": if len(sys.argv) == 1: - # No arguments supplied, launch the GUI launch_gui() else: - if args.default: - base_dir = handle_default_minecraft_texture(home, output_dir) - - if base_dir == None and not args.all: - print( - """ERROR: You didn't tell me the path to the Minecraft resource pack. - Mind-reading has not been implemented yet. - - Try this: - """+appname+""" -i - - For the full help, use: - """+appname+""" -h""") - sys.exit(2); - - ### END OF SETTINGS ### - - resource_packs = [] - - if args.all: - for resource_path in find_all_minecraft_resourcepacks(): - resource_packs.append(resource_path) - - if make_texture_pack and args.input: + if args.default: + resource_packs.append(handle_default_minecraft_texture(home, args.output)) + elif args.all: + resource_packs.extend(find_all_minecraft_resourcepacks()) + elif args.input: resource_packs.append(args.input) - for base_dir in resource_packs: - tex_dir = base_dir + "/assets/minecraft/textures" + if not resource_packs: + print(f"ERROR: No valid resource packs specified. Use '{appname} -h' for help.") + sys.exit(2) - # Get texture pack name (from directory name) - bdir_split = base_dir.split("/") - output_dir_name = bdir_split[-1] - if len(output_dir_name) == 0: - if len(bdir_split) >= 2: - output_dir_name = base_dir.split("/")[-2] - else: - # Fallback - output_dir_name = "New_MineClone_2_Texture_Pack" + convert_resource_packs(resource_packs, args.output, args.pixelsize, args.dry_run, args.verbose, make_texture_pack) - # ENTRY POINT - if make_texture_pack and not os.path.isdir(output_dir+"/"+output_dir_name): - os.mkdir(output_dir+"/"+output_dir_name) - - # If, set to convert all resourcepacks, then autodetect pixel size - if args.all: - PXSIZE = None - - if PXSIZE is None: - PXSIZE = detect_pixel_size(base_dir) - tempfile1 = tempfile.NamedTemporaryFile() - tempfile2 = tempfile.NamedTemporaryFile() - - convert_textures(make_texture_pack, dry_run, verbose, base_dir, tex_dir, tempfile1, tempfile2, output_dir, output_dir_name, mineclone2_path, PXSIZE) - - tempfile1.close() - tempfile2.close() +if __name__ == "__main__": + main() diff --git a/tools/libtextureconverter/cli.py b/tools/libtextureconverter/cli.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tools/libtextureconverter/common.py b/tools/libtextureconverter/common.py index 6e69775e7..e486dd310 100644 --- a/tools/libtextureconverter/common.py +++ b/tools/libtextureconverter/common.py @@ -7,7 +7,7 @@ from libtextureconverter.convert import convert_textures from libtextureconverter.config import SUPPORTED_MINECRAFT_VERSION, working_dir, mineclone2_path, appname, home from libtextureconverter.gui import main as launch_gui -def convert_resource_packs(resource_packs, output_dir, PXSIZE): +def convert_resource_packs(resource_packs, output_dir, PXSIZE, dry_run, verbose, make_texture_pack): for base_dir in resource_packs: print(f"Converting resource pack: {base_dir}") @@ -33,13 +33,7 @@ def convert_resource_packs(resource_packs, output_dir, PXSIZE): try: # Perform the actual conversion - convert_textures( - base_dir=base_dir, - tex_dir=tex_dir, - temp_files=(tempfile1.name, tempfile2.name), - output_dir=output_path, - pixel_size=pixel_size - ) + convert_textures(make_texture_pack, dry_run, verbose, base_dir, tex_dir, tempfile1, tempfile2,output_dir, output_dir_name, mineclone2_path, pixel_size) finally: # Clean up temporary files tempfile1.close()