summaryrefslogtreecommitdiff
path: root/spritesdedup/spritesdedup.py
blob: ab0fb8c4902a3ec47a95105f99329c2fb0f532ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#! /usr/bin/env python2.6
# -*- coding: utf8 -*-
#
# Copyright (C) 2011  Evol Online
# Author: Andrei Karas (4144)

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/")