Added --default flag

This commit is contained in:
James David Clarke 2024-01-10 03:31:45 +00:00 committed by the-real-herowl
parent e05e46bc59
commit 1147a9715a
2 changed files with 89 additions and 7 deletions

@ -12,9 +12,9 @@ import shutil, csv, os, tempfile, sys, argparse, glob
from PIL import Image from PIL import Image
from collections import Counter from collections import Counter
from libtextureconverter.utils import detect_pixel_size, target_dir, colorize, colorize_alpha from libtextureconverter.utils import detect_pixel_size, target_dir, colorize, colorize_alpha, handle_default_minecraft_texture
from libtextureconverter.convert import convert_textures from libtextureconverter.convert import convert_textures
from libtextureconverter.config import SUPPORTED_MINECRAFT_VERSION, working_dir, mineclone2_path, appname from libtextureconverter.config import SUPPORTED_MINECRAFT_VERSION, working_dir, mineclone2_path, appname, home
# Argument parsing # Argument parsing
description_text = f"""This is the official MineClone 2 Texture Converter. description_text = f"""This is the official MineClone 2 Texture Converter.
@ -24,11 +24,12 @@ description_text = f"""This is the official MineClone 2 Texture Converter.
Supported Minecraft version: {SUPPORTED_MINECRAFT_VERSION} (Java Edition) Supported Minecraft version: {SUPPORTED_MINECRAFT_VERSION} (Java Edition)
""" """
parser = argparse.ArgumentParser(description=description_text) parser = argparse.ArgumentParser(description=description_text)
parser.add_argument("-i", "--input", required=True, help="Directory of Minecraft resource pack to convert") 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("-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("-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("-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("-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")
args = parser.parse_args() args = parser.parse_args()
### SETTINGS ### ### SETTINGS ###
@ -43,8 +44,8 @@ verbose = args.verbose
# If False, textures will be put into MineClone 2 directories. # If False, textures will be put into MineClone 2 directories.
make_texture_pack = True # Adjust as needed make_texture_pack = True # Adjust as needed
if PXSIZE is None: if args.default:
PXSIZE = detect_pixel_size(base_dir) base_dir = handle_default_minecraft_texture(home, output_dir)
if base_dir == None: if base_dir == None:
print( print(
@ -52,7 +53,7 @@ if base_dir == None:
Mind-reading has not been implemented yet. Mind-reading has not been implemented yet.
Try this: Try this:
"""+appname+""" -i <path to resource pack> -p <texture size> """+appname+""" -i <path to resource pack>
For the full help, use: For the full help, use:
"""+appname+""" -h""") """+appname+""" -h""")
@ -60,6 +61,9 @@ For the full help, use:
### END OF SETTINGS ### ### END OF SETTINGS ###
if PXSIZE is None:
PXSIZE = detect_pixel_size(base_dir)
tex_dir = base_dir + "/assets/minecraft/textures" tex_dir = base_dir + "/assets/minecraft/textures"
# Get texture pack name (from directory name) # Get texture pack name (from directory name)

@ -1,4 +1,4 @@
import shutil, csv, os, tempfile, sys, argparse, glob import shutil, csv, os, tempfile, sys, argparse, glob, re, zipfile
from PIL import Image from PIL import Image
from collections import Counter from collections import Counter
@ -26,3 +26,81 @@ def colorize(colormap, source, colormap_pixel, texture_size, destination, tempfi
def colorize_alpha(colormap, source, colormap_pixel, texture_size, destination, tempfile2_name): def colorize_alpha(colormap, source, colormap_pixel, texture_size, destination, tempfile2_name):
colorize(colormap, source, colormap_pixel, texture_size, destination, tempfile2_name) colorize(colormap, source, colormap_pixel, texture_size, destination, tempfile2_name)
os.system("composite -compose Dst_In "+source+" "+tempfile2_name+" -alpha Set "+destination) os.system("composite -compose Dst_In "+source+" "+tempfile2_name+" -alpha Set "+destination)
def find_highest_minecraft_version(home):
version_pattern = re.compile(r"1\.20\.\d+")
versions_dir = os.path.join(home, ".minecraft", "versions")
highest_version = None
if os.path.isdir(versions_dir):
for folder in os.listdir(versions_dir):
if version_pattern.match(folder):
if not highest_version or folder > highest_version:
highest_version = folder
return highest_version
def handle_default_minecraft_texture(home, output_dir):
version = find_highest_minecraft_version(home)
if not version:
print("No suitable Minecraft version found.")
sys.exit(1)
jar_file = os.path.join(home, ".minecraft", "versions", version, f"{version}.jar")
if not os.path.isfile(jar_file):
print("Minecraft JAR file not found.")
sys.exit(1)
temp_zip = f"/tmp/mc-default-{version.replace('.', '')}.zip"
shutil.copy2(jar_file, temp_zip)
extract_folder = temp_zip.replace(".zip", "")
with zipfile.ZipFile(temp_zip, 'r') as zip_ref:
zip_ref.extractall(extract_folder)
if not os.path.exists(extract_folder):
print(f"Extraction failed, folder not found: {extract_folder}")
sys.exit(1)
# Normalize the extract folder path
extract_folder = os.path.normpath(extract_folder)
# Define the textures directory and normalize it
textures_directory = os.path.normpath(f"{extract_folder}/assets/minecraft/textures")
# Using glob to find all files
all_files = glob.glob(f"{extract_folder}/**/*.*", recursive=True)
# Remove all non-png files except pack.mcmeta and pack.png in the root
for file_path in all_files:
if not file_path.endswith('.png') and not file_path.endswith('pack.mcmeta') and not file_path.endswith('pack.png'):
#print(f"Removing file: {file_path}")
os.remove(file_path)
# Remove all directories in the root except 'assets'
for item in os.listdir(extract_folder):
item_path = os.path.join(extract_folder, item)
if os.path.isdir(item_path) and item != "assets":
#print(f"Removing directory: {item_path}")
shutil.rmtree(item_path, ignore_errors=True)
# Remove directories in 'minecraft' except for 'textures'
minecraft_directory = os.path.normpath(f"{extract_folder}/assets/minecraft")
for item in os.listdir(minecraft_directory):
item_path = os.path.join(minecraft_directory, item)
if os.path.isdir(item_path) and item != "textures":
print(f"Removing directory: {item_path}")
shutil.rmtree(item_path, ignore_errors=True)
# Copy the textures directory to the output directory
output_textures_directory = os.path.join(output_dir, 'assets/minecraft/textures')
if os.path.exists(textures_directory) and not os.path.exists(output_textures_directory):
os.makedirs(os.path.dirname(output_textures_directory), exist_ok=True)
shutil.copytree(textures_directory, output_textures_directory, dirs_exist_ok=True)
# Copy pack.mcmeta and pack.png file if exists
for file_name in ['pack.mcmeta', 'pack.png']:
file_path = os.path.join(extract_folder, file_name)
if os.path.exists(file_path):
shutil.copy(file_path, output_dir)
print(f"Filtered and extracted to: {extract_folder}")
return extract_folder