comparison tools/object_generator.py @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children 81641655bc38
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 #!/usr/bin/python
2 from util_scripts.path import path
3 import os, sys
4
5 DIRNAME = 'clients/rio_de_hola/objects/nature/trees'
6 NAMESPACE = 'http://www.fifengine.de/xml/rio_de_hola'
7 STATIC = 1
8 BLOCKING = 1
9
10 _sep = os.path.sep
11
12 def dirpath(p):
13 return _sep.join(p.split(_sep)[:-1]) + _sep
14
15 def filename(p):
16 return p.split(_sep)[-1]
17
18 class Animation(object):
19 def __init__(self, path_to_xml):
20 self.path = path_to_xml
21 self.direction = int(dirpath(path_to_xml).split(_sep)[-2])
22 self.action_name = dirpath(path_to_xml).split(_sep)[-3]
23
24 def __str__(self):
25 return self.path
26
27 def relpath(self, rootpath):
28 return self.path.replace(rootpath, '')
29
30 class StaticImage(object):
31 def __init__(self, path_to_image):
32 self.path = path_to_image
33 self.direction = int(filename(path_to_image).split('.')[0])
34
35 def __str__(self):
36 return self.path
37
38 def relpath(self, rootpath):
39 return self.path.replace(rootpath, '')
40
41 OBJECT_HEADER = '<object id="%s" namespace="%s" blocking="%d" static="%d">'
42 ACTION_HEADER = '\t<action id="%s">'
43 ANIMATION_LINE = '\t\t<animation source="%s" direction="%d" />'
44 ACTION_FOOTER = '\t</action>'
45 IMAGE_LINE = '\t<image source="%s" direction="%d" />'
46 OBJECT_FOOTER = '</object>\n'
47
48 class Obj(object):
49 def __init__(self, path_to_xml):
50 self.path = path_to_xml
51 self.dirpath = dirpath(path_to_xml)
52 self.name = self.dirpath.split(_sep)[-2]
53 try:
54 num = int(self.name)
55 self.name = self.dirpath.split(_sep)[-3] + ':' + self.name
56 except ValueError:
57 pass # ok, object is not named as plain integer
58 self.actions = {}
59 self.images = []
60
61 def animation_is_part_of(self, anim):
62 return anim.path.find(self.dirpath) != -1
63
64 def image_is_part_of(self, img):
65 return (img.path.find(self.dirpath) != -1) and (img.relpath(self.dirpath).find(_sep) == -1)
66
67 def add_animation(self, animation):
68 if not self.actions.has_key(animation.action_name):
69 self.actions[animation.action_name] = []
70 self.actions[animation.action_name].append(animation)
71
72 def add_image(self, image):
73 self.images.append(image)
74
75 def write_xml(self):
76 lines = []
77 a = lines.append
78 a(OBJECT_HEADER % (self.name, NAMESPACE, BLOCKING, STATIC))
79 self.images.sort(lambda x, y: cmp(str(x), str(y)))
80 for i in self.images:
81 a(IMAGE_LINE % (i.relpath(self.dirpath), i.direction))
82
83 for action_name in sorted(self.actions.keys()):
84 a(ACTION_HEADER % action_name)
85 self.actions[action_name].sort(lambda x, y: cmp(str(x), str(y)))
86 for animation in self.actions[action_name]:
87 a(ANIMATION_LINE % (animation.relpath(self.dirpath), animation.direction))
88 a(ACTION_FOOTER)
89 a(OBJECT_FOOTER)
90
91 print 'FILE : ', self.path
92 print '\n'.join(lines)
93 print ''
94 open(self.path, 'w').write('\n'.join(lines))
95
96 def __str__(self):
97 return self.name
98
99 p = path(DIRNAME)
100 obj_files = [str(f) for f in p.walkfiles('object.xml')]
101 anim_files = [str(f) for f in p.walkfiles('animation.xml')]
102 images = [str(f) for f in p.walkfiles('*.png')]
103
104 objects = []
105 # iterate all object.xml files
106 for o in obj_files:
107 obj = Obj(str(o))
108 objects.append(obj)
109
110 for a in anim_files:
111 anim = Animation(str(a))
112 if obj.animation_is_part_of(anim):
113 obj.add_animation(anim)
114 for i in images:
115 img = StaticImage(str(i))
116 if obj.image_is_part_of(img):
117 obj.add_image(img)
118
119 for o in objects:
120 o.write_xml()
121
122 print "all done"