Asset prog

This commit is contained in:
2025-08-25 10:16:55 -05:00
parent 947f21cac7
commit 8af2f044ed
23 changed files with 290 additions and 31 deletions

View File

@@ -1,6 +1,9 @@
import sys, os
import xml.etree.ElementTree as ET
from assethelpers import getAssetRelativePath
from args import args
from constants import ASSET_FILE_NAME_MAX_LENGTH
from processimage import processPalette, processImage
def processTileset(assetPath):
# Process the tileset file
@@ -25,18 +28,58 @@ def processTileset(assetPath):
return
# Exactly one image element is required
images = root.findall('image')
if len(images) != 1:
imagesNode = root.findall('image')
if len(imagesNode) != 1:
print(f"Error: Tileset {assetPath} must have exactly one image element.")
return
image = images[0]
if 'source' not in image.attrib:
imageNode = imagesNode[0]
if 'source' not in imageNode.attrib:
print(f"Error: Tileset {assetPath} is missing image source.")
return
imageSource = image.attrib['source']
imageSource = imageNode.attrib['source']
directory = os.path.dirname(assetPath)
image = processImage(os.path.join(directory, imageSource))
# Build
relative = getAssetRelativePath(assetPath)
print(f"Relative path: {relative}")
relativeFile = getAssetRelativePath(assetPath)
relativeDir = os.path.dirname(relativeFile)
data = "DTF" # Header for Dusk Tileset Format
# Write width (int32_t)
data += tilewidth.to_bytes(4, byteorder='little').decode('latin1')
# Write height (int32_t)
data += tileheight.to_bytes(4, byteorder='little').decode('latin1')
# Write tilecount (int32_t)
data += tilecount.to_bytes(4, byteorder='little').decode('latin1')
# Write column count (int32_t)
data += columns.to_bytes(4, byteorder='little').decode('latin1')
# Write row count (int32_t)
rows = (tilecount + columns - 1) // columns
data += rows.to_bytes(4, byteorder='little').decode('latin1')
# Write image source file name, padd to ASSET_FILE_NAME_MAX_LENGTH
imageSourceBytes = image["outputFile"].encode('utf-8')
data += len(imageSourceBytes).to_bytes(4, byteorder='little').decode('latin1')
data += imageSourceBytes.decode('latin1')
paddingLength = max(0, ASSET_FILE_NAME_MAX_LENGTH - len(imageSourceBytes))
data += '\x00' * paddingLength
# Write to output file
fileNameWithoutExt = os.path.splitext(os.path.basename(assetPath))[0]
outputFilePath = os.path.join(args.output_assets, relativeDir, f"{fileNameWithoutExt}.dtf")
os.makedirs(os.path.dirname(outputFilePath), exist_ok=True)
with open(outputFilePath, 'wb') as f:
f.write(data.encode('latin1'))
return {
"outputFile": os.path.relpath(outputFilePath, args.output_assets),
"tileWidth": tilewidth,
"tileHeight": tileheight,
"tileCount": tilecount,
"columns": columns,
"rows": rows,
"image": image
}