typegraph/FixProjectDirectories.py

45 lines
1.7 KiB
Python

#!/usr/bin/env python
"""Python script to fix a GMS1.4 project with subfolders"""
import sys
from xml.etree import ElementTree
from pathlib import Path
from os import replace, mkdir
cwd = Path.cwd()
projectFile = cwd / (cwd.stem + ".project.gmx")
def moveResources(elementTree, directory, dname, resourceType, fileSuffix):
for entry in elementTree.findall(resourceType):
resName = Path(entry.text).name
filename = resName + fileSuffix
old = cwd / (resourceType+"s") / filename
new = directory / old.name
if Path.exists(old):
replace(old, new)
if resourceType == "sprite":
for frame in ElementTree.parse(new).findall(".//frame"):
oldImg = cwd / (resourceType+"s") / frame.text
newImg = directory / frame.text
if not Path.exists(newImg.parent):
mkdir(newImg.parent)
if Path.exists(oldImg):
replace(oldImg, newImg)
entry.text = dname + "\\" + resName
for group in elementTree.findall(resourceType+"s"):
name = group.get("name")
newDir = directory / name
subname = name if dname == "" else dname+"\\"+name
if not Path.exists(newDir):
mkdir(newDir)
moveResources(group, newDir, subname, resourceType, fileSuffix)
project = ElementTree.parse(projectFile)
root = project.getroot()
for (res, suffix) in [("background", ".background.gmx"), ("object", ".object.gmx"), ("path", ".path.gmx"), ("room", ".room.gmx"), ("script", ""), ("shader", ".shader.gmx"), ("sound", ".sound.gmx"), ("sprite", ".sprite.gmx")]:
moveResources(root, cwd, "", res, suffix)
project.write(projectFile)