2024-01-10 08:16:36 +00:00
from . special_convert_cases import convert_map_textures , convert_armor_textures , convert_chest_textures , convert_rail_textures , convert_banner_overlays , convert_grass_textures
2024-01-09 17:28:43 +00:00
from . utils import target_dir , colorize , colorize_alpha
2024-01-10 08:16:36 +00:00
import shutil
import csv
import os
import tempfile
import sys
import argparse
import glob
def convert_standard_textures (
make_texture_pack ,
dry_run ,
verbose ,
base_dir ,
tex_dir ,
tempfile1 ,
tempfile2 ,
output_dir ,
output_dir_name ,
mineclone2_path ,
PXSIZE ) :
2024-01-13 16:25:03 +00:00
failed_conversions = 0
with open ( " Conversion_Table.csv " , newline = " " ) as csvfile :
reader = csv . reader ( csvfile , delimiter = " , " , quotechar = ' " ' )
first_row = True
for row in reader :
# Skip first row
if first_row :
first_row = False
continue
src_dir = row [ 0 ]
src_filename = row [ 1 ]
dst_dir = ' ./textures '
dst_filename = row [ 2 ]
if row [ 4 ] != " " :
xs = int ( row [ 3 ] )
ys = int ( row [ 4 ] )
xl = int ( row [ 5 ] )
yl = int ( row [ 6 ] )
xt = int ( row [ 7 ] )
yt = int ( row [ 8 ] )
else :
xs = None
blacklisted = row [ 9 ]
2024-01-09 17:28:43 +00:00
2024-01-13 16:25:03 +00:00
if blacklisted == " y " :
# Skip blacklisted files
continue
2024-01-09 17:28:43 +00:00
2024-01-13 16:25:03 +00:00
if make_texture_pack == False and dst_dir == " " :
# If destination dir is empty, this texture is not supposed to be used in MCL2
# (but maybe an external mod). It should only be used in texture packs.
# Otherwise, it must be ignored.
# Example: textures for mcl_supplemental
continue
2024-01-09 17:28:43 +00:00
2024-01-13 16:25:03 +00:00
src_file = base_dir + src_dir + " / " + src_filename # source file
src_file_exists = os . path . isfile ( src_file )
dst_file = target_dir ( dst_dir , make_texture_pack , output_dir , output_dir_name ,
2024-01-10 08:16:36 +00:00
mineclone2_path ) + " / " + dst_filename # destination file
2024-01-09 17:28:43 +00:00
2024-01-13 16:25:03 +00:00
if src_file_exists == False :
print ( " WARNING: Source file does not exist: " + src_file )
failed_conversions = failed_conversions + 1
continue
if xs != None :
# Crop and copy images
if not dry_run :
crop_width = int ( xl )
crop_height = int ( yl )
offset_x = int ( xs )
offset_y = int ( ys )
with Image ( filename = src_file ) as img :
# Crop the image
img . crop ( left = offset_x , top = offset_y , width = crop_width , height = crop_height )
# Save the result
img . save ( filename = dst_file )
if verbose :
print ( src_file + " → " + dst_file )
else :
2024-01-09 17:28:43 +00:00
# Copy image verbatim
2024-01-13 16:25:03 +00:00
if not dry_run :
shutil . copy2 ( src_file , dst_file )
if verbose :
print ( src_file + " → " + dst_file )
return failed_conversions
2024-01-09 17:28:43 +00:00
2024-01-10 08:16:36 +00:00
def convert_textures ( make_texture_pack , dry_run , verbose , base_dir , tex_dir , tempfile1 , tempfile2 , output_dir , output_dir_name , mineclone2_path , PXSIZE ) :
print ( " Texture conversion BEGINS NOW! " )
2024-01-09 17:28:43 +00:00
2024-01-10 08:16:36 +00:00
# Convert textures listed in the Conversion_Table.csv
failed_conversions = convert_standard_textures ( make_texture_pack , dry_run , verbose , base_dir , tex_dir ,
tempfile1 , tempfile2 , output_dir , output_dir_name , mineclone2_path , PXSIZE )
2024-01-09 17:28:43 +00:00
2024-01-10 08:16:36 +00:00
# Conversion of map backgrounds
convert_map_textures ( make_texture_pack , dry_run , verbose , base_dir , tex_dir ,
tempfile1 , tempfile2 , output_dir , output_dir_name , mineclone2_path , PXSIZE )
2024-01-09 17:28:43 +00:00
2024-01-10 08:16:36 +00:00
# Convert armor textures
convert_armor_textures ( make_texture_pack , dry_run , verbose , base_dir , tex_dir , tempfile1 , tempfile2 , output_dir , output_dir_name , mineclone2_path , PXSIZE )
2024-01-09 17:28:43 +00:00
2024-01-10 08:16:36 +00:00
# Convert chest textures
convert_chest_textures ( make_texture_pack , dry_run , verbose , base_dir , tex_dir , tempfile1 , tempfile2 , output_dir , output_dir_name , mineclone2_path , PXSIZE )
2024-01-09 17:28:43 +00:00
2024-01-10 08:16:36 +00:00
# Generate railway crossings and t-junctions
convert_rail_textures ( make_texture_pack , dry_run , verbose , base_dir , tex_dir , tempfile1 , tempfile2 , output_dir , output_dir_name , mineclone2_path , PXSIZE )
2024-01-09 17:28:43 +00:00
2024-01-10 08:16:36 +00:00
# Convert banner overlays
convert_banner_overlays ( make_texture_pack , dry_run , verbose , base_dir , tex_dir , tempfile1 , tempfile2 , output_dir , output_dir_name , mineclone2_path , PXSIZE )
2024-01-09 17:28:43 +00:00
2024-01-10 08:16:36 +00:00
# Convert grass and related textures
convert_grass_textures ( make_texture_pack , dry_run , verbose , base_dir , tex_dir , tempfile1 , tempfile2 , output_dir , output_dir_name , mineclone2_path , PXSIZE )
2024-01-09 17:28:43 +00:00
2024-01-10 08:16:36 +00:00
# Metadata
if make_texture_pack :
# Create description file
description = " Texture pack for MineClone 2. Automatically converted from a Minecraft resource pack by the MineClone 2 Texture Converter. Size: " + str ( PXSIZE ) + " × " + str ( PXSIZE )
description_file = open ( target_dir ( " / " , make_texture_pack , output_dir , output_dir_name , mineclone2_path ) + " /description.txt " , " w " )
description_file . write ( description )
description_file . close ( )
2024-01-09 17:28:43 +00:00
2024-04-29 15:50:32 +00:00
# Create override file
shutil . copyfile ( " override.txt " , target_dir ( " / " , make_texture_pack , output_dir , output_dir_name , mineclone2_path ) + " /override.txt " )
2024-01-10 08:16:36 +00:00
# Create preview image (screenshot.png)
os . system ( " convert -size 300x200 canvas:transparent " + target_dir ( " / " , make_texture_pack , output_dir , output_dir_name , mineclone2_path ) + " /screenshot.png " )
os . system ( " composite " + base_dir + " /pack.png " + target_dir ( " / " , make_texture_pack , output_dir , output_dir_name , mineclone2_path ) + " /screenshot.png -gravity center " + target_dir ( " / " , make_texture_pack , output_dir , output_dir_name , mineclone2_path ) + " /screenshot.png " )
2024-01-09 17:28:43 +00:00
2024-01-10 08:16:36 +00:00
print ( " Textures conversion COMPLETE! " )
if failed_conversions > 0 :
print ( " WARNING: Number of missing files in original resource pack: " + str ( failed_conversions ) )
print ( " NOTE: Please keep in mind this script does not reliably convert all the textures yet. " )
if make_texture_pack :
print ( " You can now retrieve the texture pack in " + output_dir + " / " + output_dir_name + " / " )