comparison engine/python/fife/extensions/serializers/xmlobject.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 e3140f01749d
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 # -*- coding: utf-8 -*-
2
3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team
5 # http://www.fifengine.de
6 # This file is part of FIFE.
7 #
8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # ####################################################################
23
24 from fife import fife
25 from fife.extensions.serializers import *
26 from fife.extensions.fife_utils import *
27
28 class ObjectLocation(fife.ResourceLocation):
29 def __init__(self, file, node=None):
30 fife.ResourceLocation.__init__(self, file)
31 self.node = node
32
33 class XMLObjectLoader(fife.ResourceLoader):
34 def __init__(self, image_pool, anim_pool, model, vfs=None):
35 self.image_pool = image_pool
36 self.anim_pool = anim_pool
37 self.model = model
38 self.vfs = vfs
39 self.source = None
40 self.filename = ''
41
42 def loadResource(self, location):
43 self.source = location
44 self.filename = self.source.getFilename()
45 self.node = None
46 self.file = None
47 if hasattr(location, 'node'):
48 self.node = location.node
49 else:
50 isobjectfile = True
51 f = self.vfs.open(self.filename)
52 f.thisown = 1
53
54 obj_identifier = '<?fife type="object"?>'
55 try:
56 s = f.readString(len(obj_identifier))
57 except fife.IndexOverflow:
58 isobjectfile = False
59
60 if isobjectfile and s != obj_identifier:
61 isobjectfile = False
62
63 if not isobjectfile:
64 raise WrongFileType('Tried to open non-object file %s with XMLObjectLoader.' % self.filename)
65 self.do_load_resource(f)
66
67 def do_load_resource(self, file):
68 if file:
69 tree = ET.parse(file)
70 self.node = tree.getroot()
71 self.parse_object(self.node)
72
73 def parse_object(self, object):
74 if self.node.tag != 'object':
75 raise InvalidFormat('Expected <object> tag, but found <%s>.' % self.node.tag)
76
77 id = object.get('id')
78 if not id:
79 raise InvalidFormat('<object> declared without an id attribute.')
80
81 nspace = object.get('namespace')
82 if not nspace:
83 raise InvalidFormat('<object> %s declared without a namespace attribute.' % str(id))
84
85 obj = None
86 parent = object.get('parent', None)
87 if parent:
88 query = self.metamodel.getObjects('id', str(parent))
89 if len(query) == 0:
90 raise NotFound('No objects found with identifier %s.' % str(parent))
91 elif len(query) > 1:
92 raise NameClash('%d objects found with identifier %s.' % (len(query), str(parent)))
93 parent = query[0]
94
95 try:
96 obj = self.model.createObject(str(id), str(nspace), parent)
97 except RuntimeError, e:
98 if is_fife_exc(fife.NameClash, e):
99 raise NameClash('Tried to create already existing object, ignoring')
100 raise
101
102 obj.setResourceLocation(self.source)
103 fife.ObjectVisual.create(obj)
104 obj.setBlocking(bool( int(object.get('blocking', False)) ))
105 obj.setStatic(bool( int(object.get('static', False)) ))
106
107 pather = object.get('pather', 'RoutePather')
108 obj.setPather( self.model.getPather(pather) )
109
110 self.parse_images(object, obj)
111 self.parse_actions(object, obj)
112
113 def parse_images(self, objelt, object):
114 for image in objelt.findall('image'):
115 source = image.get('source')
116 if not source:
117 raise InvalidFormat('<image> declared without a source attribute.')
118
119 # paths are relative to this resource's path
120 path = self.filename.split('/')
121 path.pop()
122 path.append(str(source))
123
124 image_location = fife.ImageLocation('/'.join(path))
125 image_location .setXShift(int( image.get('x_offset', 0) ))
126 image_location .setYShift(int( image.get('y_offset', 0) ))
127 id = self.image_pool.addResourceFromLocation(image_location)
128 object.get2dGfxVisual().addStaticImage(int( image.get('direction', 0) ), id)
129 #img = self.image_pool.getImage(id)
130
131 def parse_actions(self, objelt, object):
132 for action in objelt.findall('action'):
133 id = action.get('id')
134 if not id:
135 raise InvalidFormat('<action> declared without an id attribute.')
136
137 act_obj = object.createAction(str(id))
138 fife.ActionVisual.create(act_obj)
139 self.parse_animations(action, act_obj)
140
141 def parse_animations(self, actelt, action):
142 for anim in actelt.findall('animation'):
143 source = anim.get('source')
144 if not source:
145 raise InvalidFormat('Animation declared with no source location.')
146
147 # animation paths are relative to this resource's path
148 path = self.filename.split('/')
149 path.pop()
150 path.append(str(source))
151
152 anim_id = self.anim_pool.addResourceFromFile('/'.join(path))
153 animation = self.anim_pool.getAnimation(anim_id)
154 action.get2dGfxVisual().addAnimation(int( anim.get('direction', 0) ), anim_id)
155 action.setDuration(animation.getDuration())