1
0
mirror of synced 2025-02-07 15:01:21 +01:00

Support splitting texturemaps into individual sprites.

This commit is contained in:
Jennifer Taylor 2020-11-29 00:47:25 +00:00
parent 31ad2856fd
commit 0c4cb8df5c

View File

@ -1676,6 +1676,12 @@ def main() -> int:
action="store_true",
help="Generate overlay images showing mappings",
)
extract_parser.add_argument(
"-s",
"--split-textures",
action="store_true",
help="Split textures into individual sprites",
)
update_parser = subparsers.add_parser('update', help='Update relevant textures in a file from a directory')
update_parser.add_argument(
@ -1711,12 +1717,19 @@ def main() -> int:
args = parser.parse_args()
if args.action == "extract":
if args.split_textures:
if args.write_raw:
raise Exception("Cannot write raw textures when splitting sprites!")
if args.generate_mapping_overlays:
raise Exception("Cannot generate mapping overlays when splitting sprites!")
with open(args.file, "rb") as bfp:
afpfile = AFPFile(bfp.read(), verbose=args.verbose)
# Actually place the files down.
os.makedirs(args.dir, exist_ok=True)
if not args.split_textures:
for texture in afpfile.textures:
filename = os.path.join(args.dir, texture.name)
@ -1751,6 +1764,7 @@ def main() -> int:
""").strip())
if args.write_mappings:
if not args.split_textures:
for i, name in enumerate(afpfile.regionmap.entries):
if i < 0 or i >= len(afpfile.texture_to_region):
raise Exception(f"Out of bounds region {i}")
@ -1826,6 +1840,43 @@ def main() -> int:
with open(filename, "wb") as bfp:
img.save(bfp, format='PNG')
if args.split_textures:
textures: Dict[str, Any] = {}
announced: Dict[str, bool] = {}
for i, name in enumerate(afpfile.regionmap.entries):
if i < 0 or i >= len(afpfile.texture_to_region):
raise Exception(f"Out of bounds region {i}")
region = afpfile.texture_to_region[i]
texturename = afpfile.texturemap.entries[region.textureno]
if texturename not in textures:
for tex in afpfile.textures:
if tex.name == texturename:
textures[texturename] = tex
break
else:
raise Exception("Could not find texture {texturename} to split!")
if textures[texturename].img:
# Grab the location in the image, save it out to a new file.
filename = f"{texturename}_{name}.png"
filename = os.path.join(args.dir, filename)
if args.pretend:
print(f"Would write {filename} sprite...")
else:
print(f"Writing {filename} sprite...")
sprite = textures[texturename].img.crop(
(region.left // 2, region.top // 2, region.right // 2, region.bottom // 2),
)
with open(filename, "wb") as bfp:
sprite.save(bfp, format='PNG')
else:
if not announced.get(texturename, False):
print(f"Cannot extract sprites from {texturename} because it is not a supported format!")
announced[texturename] = True
if args.action == "update":
# First, parse the file out
with open(args.file, "rb") as bfp: