def saveInPlaceGen(self):
        """
        Save all chunks to the database, and write the root_tag back to level.dat.
        """
        if DEBUG_PE:
            open(dump_fName, 'a').write("*** saveInPlaceGen\n")
        self.saving = True
        batch = leveldb_mcpe.WriteBatch()
        dirtyChunkCount = 0
        for c in self.chunksNeedingLighting:
            self.getChunk(*c).genFastLights()

        for chunkCoords in self._loadedChunks.keys():
            chunk = self._loadedChunks[chunkCoords]
            if chunk.dirty:
                dirtyChunkCount += 1
                self.worldFile.saveChunk(chunk, batch=batch)
                chunk.dirty = False
            yield

        with nbt.littleEndianNBT():
            for p in self.players:
                # The player data may not be in the cache if we have multi-player game.
                # So, accessing the cache using the player as key crashes the program...
                playerData = self.playerTagCache.get(p)
                if playerData is not None:
                    playerData = playerData.save(compressed=False)  # It will get compressed in the DB itself
                    self.worldFile.savePlayer(p, playerData, batch=batch)

        with self.worldFile.world_db() as db:
            wop = self.worldFile.writeOptions
            db.Write(wop, batch)

        self.saving = False
        logger.info(u"Saved {0} chunks to the database".format(dirtyChunkCount))
        path = os.path.join(self.worldFile.path, 'level.dat')
        with nbt.littleEndianNBT():
            rootTagData = self.root_tag["Data"].save(compressed=False)
#             if self.world_version == '1.plus':
#                 magic = 5
#             else:
#                 magic = 4

            magic = self.dat_world_version
            if isinstance(magic, (str, unicode)):
                magic = ord(magic)

            rootTagData = struct.Struct('<i').pack(magic) + struct.Struct('<i').pack(len(rootTagData)) + rootTagData
            with open(path, 'wb') as f:
                f.write(rootTagData)

        Save all chunks to the database, and write the root_tag back to level.dat.
        
pymclevel/leveldbpocket.py