source: trunk/game/editor/loaders.py @ 309

Revision 309, 2.4 KB checked in by orlandov, 10 years ago (diff)

Ticket #111: Patch by or1andov, fixes[s:trac, t:111]

  • Create a wrapper around the FIFE editor that will allow us to load and save PARPG maps
  • Attributes for instances with id's on a map should pass through on save
  • Port savers to use the etree XML api instead of SAX
Line 
1#!python
2
3#   This file is part of PARPG.
4#   PARPG is free software: you can redistribute it and/or modify
5#   it under the terms of the GNU General Public License as published by
6#   the Free Software Foundation, either version 3 of the License, or
7#   (at your option) any later version.
8#
9#   PARPG is distributed in the hope that it will be useful,
10#   but WITHOUT ANY WARRANTY; without even the implied warranty of
11#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12#   GNU General Public License for more details.
13#
14#   You should have received a copy of the GNU General Public License
15#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
16
17import fife
18
19from xmlmap import XMLMapLoader
20from serializers import WrongFileType, NameClash
21from serializers.xmlobject import XMLObjectLoader
22
23fileExtensions = ('xml',)
24
25class AttributeInterceptor(object):
26    """
27    The purpose of this class is to intercept createObject calls by the PARPG
28    XMLMapLoader and record the attributes of instances so that they can be
29    re-applied to objects on save.
30    """
31
32    def __init__(self):
33        self.objects = {}
34
35    def createObject(self, layer, object_dict, inst):
36        if object_dict.get('id') is not None:
37            self.objects[object_dict['id']] = object_dict
38
39data = None
40def loadMapFile(path, engine, callback=None):
41    global data
42    data = AttributeInterceptor()
43    map_loader = XMLMapLoader(engine, data, callback)
44    map = map_loader.loadResource(fife.ResourceLocation(path))
45    return map
46
47def loadImportFile(path, engine):
48    object_loader = XMLObjectLoader(engine.getImagePool(), engine.getAnimationPool(), engine.getModel(), engine.getVFS())
49    res = None
50    try:
51        res = object_loader.loadResource(fife.ResourceLocation(path))
52        print 'imported object file ' + path
53    except WrongFileType:
54        pass
55#        print 'ignored non-object file ' + path
56    except NameClash:
57        pass
58#        print 'ignored already loaded file ' + path
59    return res
60
61def loadImportDir(path, engine):
62    for file in filter(
63            lambda f: f.split('.')[-1] == 'xml', engine.getVFS().listFiles(path)
64        ):
65        loadImportFile('/'.join([path, file]), engine)
66
67def loadImportDirRec(path, engine):
68    loadImportDir(path, engine)
69
70    for dir in filter(lambda d: not d.startswith('.'), engine.getVFS().listDirectories(path)):
71        loadImportDirRec('/'.join([path, dir]), engine)
Note: See TracBrowser for help on using the repository browser.