wesnoth/data/tools/upconvert

203 lines
8.0 KiB
Python
Executable File

#!/usr/bin/env python
#
# Up-convert UMC between versions.
import sys, os, re, getopt
filemoves = {
# Older includes all previous to 1.3.1.
"older" : (
("creepy.ogg", "underground.ogg"),
("eagle.wav", "gryphon-shriek-1.ogg"),
("lightning.wav", "lightning.ogg"), # Bug fix
),
"1.3.1" : (
# Peasant images moved to a new directory
("human-loyalists/peasant.png", "human-peasants/peasant.png"),
("human-loyalists/peasant-attack.png", "human-peasants/peasant-attack.png"),
("human-loyalists/peasant-attack2.png", "human-peasants/peasant-attack2.png"),
("human-loyalists/peasant-ranged.png", "human-peasants/peasant-ranged.png"),
("human-loyalists/peasant-idle-1.png", "human-peasants/peasant-idle-1.png"),
("human-loyalists/peasant-idle-2.png", "human-peasants/peasant-idle-2.png"),
("human-loyalists/peasant-idle-3.png", "human-peasants/peasant-idle-3.png"),
("human-loyalists/peasant-idle-4.png", "human-peasants/peasant-idle-4.png"),
("human-loyalists/peasant-idle-5.png", "human-peasants/peasant-idle-5.png"),
("human-loyalists/peasant-idle-6.png", "human-peasants/peasant-idle-6.png"),
("human-loyalists/peasant-idle-7.png", "human-peasants/peasant-idle-7.png"),
# All Great Mage attacks were renamed
("great-mage-attack-magic1.png", "great-mage-attack-magic-1.png"),
("great-mage-attack-magic2.png", "great-mage-attack-magic-2.png"),
("great-mage+female-attack-magic1.png", "great-mage+female-attack-magic-1.png"),
("great-mage+female-attack-magic2.png", "great-mage+female-attack-magic-2.png"),
("great-mage-attack-staff1.png", "great-mage-attack-staff-1.png"),
("great-mage-attack-staff2.png", "great-mage-attack-staff-2.png"),
("great-mage+female-attack-staff1.png", "great-mage+female-attack-staff-1.png"),
("great-mage+female-attack-staff2.png", "great-mage+female-attack-staff-2.png"),
# All Arch Mage attacks were renamed
("arch-mage-attack-magic1.png", "arch-mage-attack-magic-1.png"),
("arch-mage-attack-magic2.png", "arch-mage-attack-magic-2.png"),
("arch-mage+female-attack-magic1.png", "arch-mage+female-attack-magic-1.png"),
("arch-mage+female-attack-magic2.png", "arch-mage+female-attack-magic-2.png"),
("arch-mage-attack-staff1.png", "arch-mage-attack-staff-1.png"),
("arch-mage-attack-staff2.png", "arch-mage-attack-staff-2.png"),
("arch-mage+female-attack-staff1.png", "arch-mage+female-attack-staff-1.png"),
("arch-mage+female-attack-staff2.png", "arch-mage+female-attack-staff-2.png"),
# All Red Mage attacks were renamed
("red-mage-attack-magic1.png", "red-mage-attack-magic-1.png"),
("red-mage-attack-magic2.png", "red-mage-attack-magic-2.png"),
("red-mage+female-attack-magic1.png", "red-mage+female-attack-magic-1.png"),
("red-mage+female-attack-magic2.png", "red-mage+female-attack-magic-2.png"),
("red-mage-attack-staff1.png", "red-mage-attack-staff-1.png"),
("red-mage-attack-staff2.png", "red-mage-attack-staff-2.png"),
("red-mage+female-attack-staff1.png", "red-mage+female-attack-staff-1.png"),
("red-mage+female-attack-staff2.png", "red-mage+female-attack-staff-2.png"),
# Timothy Pinkham supplied titles for two of his music files.
# Zhaytee supplied a title for wesnoth-1.ogg
# gameplay03.ogg, and and wesnoth-[25].ogg already had titles.
("gameplay01.ogg", "knolls.ogg"),
("gameplay02.ogg", "wanderer.ogg"),
("gameplay03.ogg", "battle.ogg"),
("wesnoth-1.ogg", "revelation.ogg"),
("wesnoth-2.ogg", "loyalists.ogg"),
("wesnoth-5.ogg", "northerners.ogg"),
),
# An empty sentinel value at end is required.
# Always have the current version here.
"1.3.2" : (),
}
# 1.3.1 -> 1.3.2 terrain conversions
terrain_conversions = {
"Bww|" : "Ww^Bw|",
"Bww/" : "Ww^Bw/",
"Bww\\" : "Ww^Bw\\",
"Bwo|" : "Wo^Bw|",
"Bwo/" : "Wo^Bw/",
"Bwo\\" : "Wo^Bw\\",
"Bss|" : "Ss^Bw|",
"Bss/" : "Ss^Bw/",
"Bss\\" : "Ss^Bw\\",
"Dc" : "Dd^Dc",
"Dr" : "Dd^Dr",
"Do" : "Dd^Do",
"Fa" : "Aa^Fpa",
"Fet" : "Gg^Fet",
"Ff" : "Gs^Fp",
"Ft" : "Gs^Ft",
"Rfvs" : "Re^Gvs",
"Uf" : "Uu^Uf",
"Uui" : "Uu^Ii",
"Uhi" : "Uh^Ii",
"Vda" : "Dd^Vda",
"Vdt" : "Dd^Vdt",
"Vea" : "Aa^Vea",
"Veg" : "Gg^Ve",
"Vha" : "Aa^Vha",
"Vhg" : "Gg^Vh",
"Vhh" : "Hh^Vhh",
"Vhha" : "Ha^Vhha",
"Vhm" : "Mm^Vhh",
"Vht" : "Gs^Vht",
"Vu" : "Uu^Vu",
"Vud" : "Uu^Vud",
"Vwm" : "Ww^Vm",
"Vs" : "Ss^Vhs",
"Vsm" : "Ss^Vm",
"Xm" : "Mm^Xm"
}
def allcfgfiles(dir):
"Get the names of all .cfg files under dir, ignoring .svn directories."
datafiles = []
os.path.walk(dir,
lambda arg, dir, names: datafiles.extend(map(lambda x: os.path.normpath(os.path.join(dir, x)), names)),
None)
datafiles = filter(lambda x: ".svn" not in x, datafiles)
datafiles = filter(lambda x: x.endswith(".cfg") or ('maps' in x and os.path.isfile(x)), datafiles)
return datafiles
def help():
sys.stderr.write("""\
Usage: upconvert [options]
Convert Battle of Wesnoth WML from older versions to newer ones.
Options may be any of these:
-h, --help Emit this help message and quit
-d, --dryrun List changes but don't perform them.
-o, --oldversion Specify version to begin with.
""")
def mapconvert2(mapline):
"Convert a map line from 1.3.1 multiletter format to 1.3.2 format."
for (old, new) in terrain_conversions.items():
mapline = re.sub(r"\b" + re.escape(old) + r"\b", new, mapline)
return mapline
if __name__ == '__main__':
(options, arguments) = getopt.getopt(sys.argv[1:], "o:", [
"help",
"oldversion=",
"dryrun",
])
oldversion = 'older'
dryrun = False
for (switch, val) in options:
if switch in ('-h', '--help'):
help()
sys.exit(0)
elif switch in ('-o', '--oldversion'):
oldversion = val
elif switch in ('-d', '--dryrun'):
dryrun = True
# Compute the series of version upgrades to perform, and describe it.
versions = filemoves.keys()
versions.sort()
versions = [versions[-1]] + versions[:-1] # Move 'older' to front
if oldversion in versions:
versions = versions[versions.index(oldversion):]
else:
print >>sys.stderr, "upconvert: unrecognized version."
sys.exit(1)
explain = "Performing upgrades for:"
for i in range(len(versions)-1):
explain += " %s -> %s," % (versions[i], versions[i+1])
sys.stdout.write(explain[:-1] + ".\n")
fileconversions = map(lambda x: filemoves[x], versions[:-1])
# Perform resource file substitutions
ofp = None
for fn in allcfgfiles("."):
if dryrun:
ifp = open(fn)
else:
os.rename(fn, fn + "-bak")
ifp = open(fn + "-bak")
ofp = open(fn, "w")
modified = False
for (i, line) in enumerate(ifp):
transformed = line
# Filename conversions
if ".cfg" in fn:
for step in fileconversions:
for (old, new) in step:
transformed = transformed.replace(old, new)
# Map-format conversions (not yet ready for production)
#if "1.3.2" in versions:
# transformed = mapconvert2(transformed)
if ofp:
ofp.write(transformed)
if transformed != line:
if not 'maps' in fn:
print "%s, line %d: %s -> %s" % \
(fn, i+1, line.strip(), transformed.strip())
modified = True
if ofp:
ofp.close()
if not modified:
# Nothing changed, move the backup file back into place.
os.rename(fn + "-bak", fn)
elif 'maps' in fn:
print "%s modified."
# upconvert ends here