Files
TexturePack/build.py
YoyoNow 869e8a847b
All checks were successful
SteamWarCI Build successful
Fix texture pack escape menu
2025-06-13 11:59:17 +02:00

95 lines
3.2 KiB
Python

import os
import shutil
import tempfile
import hashlib
import time
import zipfile
import importlib.util
import json
current_dir = os.getcwd()
def getVersions():
directories = [name for name in os.listdir(current_dir) if os.path.isdir(os.path.join(current_dir, name)) and not name.startswith(".")]
packVersions = []
for s in directories:
try:
num = int(s)
packVersions.append(num)
except ValueError:
pass
packVersions.sort()
return packVersions
def buildPack(dir, version):
current_path = os.path.join(current_dir, str(version))
copyFiles(current_path, dir)
buildScript = os.path.join(dir, "build.py")
if (os.path.isfile(buildScript)):
old_cwd = os.getcwd()
os.chdir(dir)
spec = importlib.util.spec_from_file_location('dynamic_module', buildScript)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
os.chdir(old_cwd)
os.remove(buildScript)
zip_folder(dir, current_path + ".zip")
# shutil.make_archive(current_path, "zip", dir)
current_path = current_path + ".zip"
timestamp = time.mktime(time.strptime('1970-01-01 00:00:00', '%Y-%m-%d %H:%M:%S'))
os.utime(current_path, (timestamp, timestamp))
sha1 = sha1_of_file(current_path)
os.rename(current_path, os.path.join(current_dir, str(version) + "_" + sha1 + ".zip"))
def copyFiles(src, dst):
for root, dirs, files in os.walk(src):
# Create corresponding path in destination
rel_path = os.path.relpath(root, src)
dest_path = os.path.join(dst, rel_path)
os.makedirs(dest_path, exist_ok=True)
for file in files:
src_file = os.path.join(root, file)
dst_file = os.path.join(dest_path, file)
shutil.copy2(src_file, dst_file) # Overwrites if exists
if dst_file.endswith(".json") or dst_file.endswith(".mcmeta"):
content = ""
with open(dst_file, "r") as file:
content = file.read()
content = json.loads(content)
content = json.dumps(content, separators=(',', ':'))
with open(dst_file, "w") as file:
file.write(content)
def zip_folder(folder_path, zip_file_path):
with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(folder_path):
for file in files:
abs_path = os.path.join(root, file)
rel_path = os.path.relpath(abs_path, folder_path)
zipf.write(abs_path, rel_path)
def sha1_of_file(filepath):
sha1 = hashlib.sha1()
with open(filepath, 'rb') as f:
while chunk := f.read(8192): # read in 8KB chunks
sha1.update(chunk)
return sha1.hexdigest()
if __name__ == "__main__":
versions=getVersions()
zipFiles = [name for name in os.listdir(current_dir) if os.path.isfile(os.path.join(current_dir, name)) and name.endswith(".zip") ]
for zipFile in zipFiles:
os.remove(os.path.join(current_dir, zipFile))
with tempfile.TemporaryDirectory() as tmpdirname:
for version in versions:
buildPack(tmpdirname, version)