Mercurial > fife-parpg
comparison engine/extensions/serializers/xmlobject.py @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | 9a1529f9625e |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
1 import fife | |
2 from serializers import * | |
3 from fife_utils import * | |
4 from serializers import * | |
5 | |
6 class ObjectLocation(fife.ResourceLocation): | |
7 def __init__(self, file, node=None): | |
8 fife.ResourceLocation.__init__(self, file) | |
9 self.node = node | |
10 | |
11 class XMLObjectLoader(fife.ResourceLoader): | |
12 def __init__(self, image_pool, anim_pool, model, vfs=None): | |
13 self.image_pool = image_pool | |
14 self.anim_pool = anim_pool | |
15 self.model = model | |
16 self.vfs = vfs | |
17 self.source = None | |
18 self.filename = '' | |
19 | |
20 def loadResource(self, location): | |
21 self.source = location | |
22 self.filename = self.source.getFilename() | |
23 self.node = None | |
24 self.file = None | |
25 if hasattr(location, 'node'): | |
26 self.node = location.node | |
27 else: | |
28 isobjectfile = True | |
29 f = self.vfs.open(self.filename) | |
30 f.thisown = 1 | |
31 | |
32 obj_identifier = '<?fife type="object"?>' | |
33 try: | |
34 s = f.readString(len(obj_identifier)) | |
35 except fife.IndexOverflow: | |
36 isobjectfile = False | |
37 | |
38 if isobjectfile and s != obj_identifier: | |
39 isobjectfile = False | |
40 | |
41 if not isobjectfile: | |
42 raise WrongFileType('Tried to open non-object file %s with XMLObjectLoader.' % self.filename) | |
43 self.do_load_resource(f) | |
44 | |
45 def do_load_resource(self, file): | |
46 if file: | |
47 tree = ET.parse(file) | |
48 self.node = tree.getroot() | |
49 self.parse_object(self.node) | |
50 | |
51 def parse_object(self, object): | |
52 if self.node.tag != 'object': | |
53 raise InvalidFormat('Expected <object> tag, but found <%s>.' % self.node.tag) | |
54 | |
55 id = object.get('id') | |
56 if not id: | |
57 raise InvalidFormat('<object> declared without an id attribute.') | |
58 | |
59 nspace = object.get('namespace') | |
60 if not nspace: | |
61 raise InvalidFormat('<object> %s declared without a namespace attribute.' % str(id)) | |
62 | |
63 obj = None | |
64 parent = object.get('parent', None) | |
65 if parent: | |
66 query = self.metamodel.getObjects('id', str(parent)) | |
67 if len(query) == 0: | |
68 raise NotFound('No objects found with identifier %s.' % str(parent)) | |
69 elif len(query) > 1: | |
70 raise NameClash('%d objects found with identifier %s.' % (len(query), str(parent))) | |
71 parent = query[0] | |
72 | |
73 try: | |
74 obj = self.model.createObject(str(id), str(nspace), parent) | |
75 except RuntimeError, e: | |
76 if is_fife_exc(fife.NameClash, e): | |
77 raise NameClash('Tried to create already existing object, ignoring') | |
78 raise | |
79 | |
80 obj.setResourceLocation(self.source) | |
81 fife.ObjectVisual.create(obj) | |
82 obj.setBlocking(bool( object.get('blocking', False) )) | |
83 obj.setStatic(bool( object.get('static', False) )) | |
84 | |
85 pather = object.get('pather', 'RoutePather') | |
86 obj.setPather( self.model.getPather(pather) ) | |
87 | |
88 self.parse_images(object, obj) | |
89 self.parse_actions(object, obj) | |
90 | |
91 def parse_images(self, objelt, object): | |
92 for image in objelt.findall('image'): | |
93 source = image.get('source') | |
94 if not source: | |
95 raise InvalidFormat('<image> declared without a source attribute.') | |
96 | |
97 # paths are relative to this resource's path | |
98 path = self.filename.split('/') | |
99 path.pop() | |
100 path.append(str(source)) | |
101 | |
102 id = self.image_pool.addResourceFromFile('/'.join(path)) | |
103 object.get2dGfxVisual().addStaticImage(int( image.get('direction', 0) ), id) | |
104 img = self.image_pool.getImage(id) | |
105 img.setXShift(int( image.get('x_offset', 0) )) | |
106 img.setYShift(int( image.get('y_offset', 0) )) | |
107 | |
108 def parse_actions(self, objelt, object): | |
109 for action in objelt.findall('action'): | |
110 id = action.get('id') | |
111 if not id: | |
112 raise InvalidFormat('<action> declared without an id attribute.') | |
113 | |
114 act_obj = object.createAction(str(id)) | |
115 fife.ActionVisual.create(act_obj) | |
116 self.parse_animations(action, act_obj) | |
117 | |
118 def parse_animations(self, actelt, action): | |
119 for anim in actelt.findall('animation'): | |
120 source = anim.get('source') | |
121 if not source: | |
122 raise InvalidFormat('Animation declared with no source location.') | |
123 | |
124 # animation paths are relative to this resource's path | |
125 path = self.filename.split('/') | |
126 path.pop() | |
127 path.append(str(source)) | |
128 | |
129 anim_id = self.anim_pool.addResourceFromFile('/'.join(path)) | |
130 animation = self.anim_pool.getAnimation(anim_id) | |
131 action.get2dGfxVisual().addAnimation(int( anim.get('direction', 0) ), anim_id) | |
132 action.setDuration(animation.getDuration()) | |
133 |