Exemplo n.º 1
0
    def save(self, object):
        """ saves the data of a fife.Object to its xml file
		
		@type	object:	fife.Object
		@param	object:	the object which should be saved
		@rtype	bool
		@return	flag whether the saving was successful or not
		"""
        self.change = False
        result = False

        file = object.getFilename()
        if not file:
            raise SerializerError("Object cannot be saved, no file found %s" %
                                  object)
            return result

        if not self.vfs.exists(file):
            raise NotFound("File not within vfs: %s" % file)
            return result

        file_handle = self.vfs.open(file)
        file_handle.thisown = 1
        tree = ET.parse(file_handle)
        root = tree.getroot()

        object_id = object.getId()
        blocking = object.isBlocking()
        static = object.isStatic()

        # @todo:
        # compat layer - remove as soon as cell_pathfinding branch
        # is merged back to trunk/
        if hasattr(object, 'getCostId'):
            cost_id = object.getCostId()
        else:
            cost_id = ''
        if hasattr(object, 'getCost'):
            cost = object.getCost()
        else:
            cost = 0.0
        if hasattr(object, 'getCellStackPosition'):
            cellstack_pos = object.getCellStackPosition()
        else:
            cellstack_pos = 0

        if self.debug:
            print "XML tree dump: (pre-save)"
            ET.dump(root)
            print "Object data: "
            print "\tid", object_id
            print "\tblocking", blocking
            print "\tstatic", static
            print "\tcost id", cost_id
            print "\tcost", cost

        # check for compat mode
        if root.tag != 'assets':
            self.compat = True

        # in compat mode tree is <object>
        if self.compat:
            objects = [
                root,
            ]
        # new XML structure has tree root <assets> which groups multiple objects
        else:
            objects = root.findall("object")

        for obj in objects:
            _id = obj.get("id")
            if _id != object_id:
                if self.debug:
                    print "...ommitting object %s " % _id
                continue

            if 'blocking' not in obj.attrib or int(
                    obj.attrib['blocking']) != int(blocking):
                self.change = True
            if 'static' not in obj.attrib or int(
                    obj.attrib['static']) != int(static):
                self.change = True
            if 'cost_id' not in obj.attrib or str(
                    obj.attrib['cost_id']) != str(cost_id):
                self.change = True
            if 'cost' not in obj.attrib or float(
                    obj.attrib['cost']) != float(cost):
                self.change = True
            if 'cellstack_position' not in obj.attrib or int(
                    obj.attrib['cellstack_position']) != int(cellstack_pos):
                self.change = True

            obj.attrib['blocking'] = str(int(blocking))
            obj.attrib['static'] = str(int(static))
            if cost_id and cost:
                obj.attrib['cost_id'] = str(cost_id)
                obj.attrib['cost'] = str(cost)
            obj.attrib['cellstack_position'] = str(cellstack_pos)

            if self.debug and self.change:
                print "\tSet new data in xml tree: "
                print "\t\tblocking: ", obj.attrib['blocking']
                print "\t\tstatic: ", obj.attrib['static']

            images = obj.findall("image")
            actions = obj.findall("action")

            if self.debug:
                print "\tAttempting to save image data: "
                print "\t...found these image elements: "
                print "\t", images
                print "object dump: "
                print ET.dump(obj)

            self.save_images(images, object)
            self.save_actions(actions, object)

        if not self.change:
            return result

        xmlcontent = ET.tostring(root)

        if self.debug:
            print "XML tree dump: (post-manipulation)"
            ET.dump(root)

        # save xml data beneath the <?fife type="object"?> definition into the object file
        file = open(file, 'w')
        file.write(XMLObjectSaver.PROCESSING_INSTRUCTION + '\n')
        file.write(xmlcontent + "\n")
        file.close()
        result = True
        return result
Exemplo n.º 2
0
	def save(self, object):
		""" saves the data of a fife.Object to its xml file

		@type	object:	fife.Object
		@param	object:	the object which should be saved
		@rtype	bool
		@return	flag whether the saving was successful or not
		"""
		self.change = False
		result = False

		file = object.getFilename()
		if not file:
			raise SerializerError("Object cannot be saved, no file found %s" % object)
			return result

		if not self.vfs.exists(file):
			raise NotFound("File not within vfs: %s" % file)
			return result

		file_handle = self.vfs.open(file)
		file_handle.thisown = 1
		tree = ET.parse(file_handle)
		root = tree.getroot()

		object_id = object.getId()
		blocking = object.isBlocking()
		static = object.isStatic()

		cost_id = object.getCostId()
		cost = object.getCost()
		cellstack_pos = object.getCellStackPosition()

		if self.debug:
			print("XML tree dump: (pre-save)")
			ET.dump(root)
			print("Object data: ")
			print("\tid", object_id)
			print("\tblocking", blocking)
			print("\tstatic", static)
			print("\tcost id", cost_id)
			print("\tcost", cost)

		# check for compat mode
		if root.tag != 'assets':
			self.compat = True

		# in compat mode tree is <object>
		if self.compat:
			objects = [root,]
		# new XML structure has tree root <assets> which groups multiple objects
		else:
			objects = root.findall("object")

		for obj in objects:
			_id = obj.get("id")
			if _id != object_id:
				if self.debug:
					print("...ommitting object %s " % _id)
				continue

			if int(obj.attrib['blocking']) != int(blocking):
				self.change = True
			if int(obj.attrib['static']) != int(static):
				self.change = True
			if str(obj.attrib['cost_id']) != str(cost_id):
				self.change = True
			if float(obj.attrib['cost']) != float(cost):
				self.change = True
			if int(obj.attrib['cellstack_position']) != int(cellstack_pos):
				self.change = True

			obj.attrib['blocking'] = str(int(blocking))
			obj.attrib['static'] = str(int(static))
			obj.attrib['cost_id'] = str(cost_id)
			obj.attrib['cost'] = str(cost)
			obj.attrib['cellstack_position'] = str(cellstack_pos)

			if self.debug and self.change:
				print("\tSet new data in xml tree: ")
				print("\t\tblocking: ", obj.attrib['blocking'])
				print("\t\tstatic: ", obj.attrib['static'])

			images = obj.findall("image")
			actions = obj.findall("action")

			if self.debug:
				print("\tAttempting to save image data: ")
				print("\t...found these image elements: ")
				print("\t", images)
				print("object dump: ")
				print(ET.dump(obj))

			self.save_images(images, object)
			self.save_actions(actions, object)

		if not self.change:
			return result

		xmlcontent = ET.tostring(root)

		if self.debug:
			print("XML tree dump: (post-manipulation)")
			ET.dump(root)

		# save xml data beneath the <?fife type="object"?> definition into the object file
		file = open(file, 'w')
		file.write(XMLObjectSaver.PROCESSING_INSTRUCTION+'\n')
		file.write(xmlcontent + "\n")
		file.close()
		result = True
		return result