diff options
author | Andrei Karas <akaras@inbox.ru> | 2011-02-05 01:02:40 +0200 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2011-02-05 01:04:55 +0200 |
commit | dadc358c2e804c52c29d7852bbb54fb7cb0990e1 (patch) | |
tree | d5fda2754c2f2647de0c45c228a53546547e366a /spritesdedup/spritesdedup.py | |
parent | 93bf4a215c7a22c15993d41711d24e9d483d5402 (diff) | |
download | evol-tools-dadc358c2e804c52c29d7852bbb54fb7cb0990e1.tar.gz evol-tools-dadc358c2e804c52c29d7852bbb54fb7cb0990e1.tar.bz2 evol-tools-dadc358c2e804c52c29d7852bbb54fb7cb0990e1.tar.xz evol-tools-dadc358c2e804c52c29d7852bbb54fb7cb0990e1.zip |
Add new tool spritesdedup.
This tool delete duplicate frames in xml files.
Diffstat (limited to 'spritesdedup/spritesdedup.py')
-rwxr-xr-x | spritesdedup/spritesdedup.py | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/spritesdedup/spritesdedup.py b/spritesdedup/spritesdedup.py new file mode 100755 index 0000000..fc89137 --- /dev/null +++ b/spritesdedup/spritesdedup.py @@ -0,0 +1,101 @@ +#! /usr/bin/env python2.6 +# -*- coding: utf8 -*- +# +# Copyright (C) 2011 Evol Online + +import os +import re + +parentDir = "../../clientdata" +filt = re.compile(".+[.](xml|tmx)", re.IGNORECASE) +filtframe = re.compile(".+(<frame |<sequence )", re.IGNORECASE) +redelay = re.compile(".+delay=[\"](?P<delay>[^\"]+)[\"]") + +def haveXml(dir): + if not os.path.isdir(dir) or not os.path.exists(dir): + return False + for file in os.listdir(dir): + if filt.search(file): + return True + return False + + +def detectClientData(dirs): + global parentDir + for dir in dirs: + if haveXml(dir): + print "Detected client data directory in: " + dir + parentDir = dir + return True + print "Cant detect client data directory" + exit(1) + + +def processXmls(spritesDir): + files = os.listdir(spritesDir) + for file1 in files: + if file1[0] == ".": + continue + file2 = os.path.abspath(spritesDir + os.path.sep + file1) + if not os.path.isfile(file2): + processXmls(file2) + elif filt.search(file1): + processFile(file2, file1) + + +def processFile(file2, file1): + arr = list() + with open(file2, "r") as f: + for line in f: + arr.append(line) + + idx = 0 + firstIdx = 0 + lastLine = "" + changed = False + while idx < len(arr): + if filtframe.search(arr[idx]): + firstIdx = idx + lastLine = arr[idx] + dat = findOtherLine(arr, idx) + + idx = dat[0] + delay = dat[1] + if delay == 0 or firstIdx + 1 >= idx: + idx = firstIdx + 1 + continue + m = redelay.search(lastLine) + string = lastLine[0:m.start(1)] + str(delay) + lastLine[m.end(1):len(lastLine)] + arr[firstIdx:idx] = string + changed = True + idx = firstIdx + + idx = idx + 1 + + if changed == True: + print "Fixing: " + file1 + with open(file2, "w") as f: + for line in arr: + f.write(line) + + +def findOtherLine(arr, idx): + firstIdx = idx + delay = 0 + while idx < len(arr): + if arr[idx] != arr[firstIdx]: + return (idx, delay) + else: + m = redelay.search(arr[idx]) + if m is None: + return (idx, delay) + delay = delay + int(m.group("delay")) + idx = idx + 1 + + return (firstIdx, 0) + + +print "Detecting clientdata dir" +detectClientData([".", "..", parentDir]) +processXmls(parentDir + "/graphics/sprites/") + |