From 93349b8e694ac31f0aba4f6bc25515bd5a531f7d Mon Sep 17 00:00:00 2001 From: Treeki Date: Tue, 27 Mar 2012 23:15:51 +0200 Subject: tilesets now loaded on-demand and not part of KPMap --- src/common.py | 3 +++ src/dialogs.py | 2 +- src/editorui/editormain.py | 4 ++-- src/main.py | 57 ++++++++++++++++++++++++++++++++++++++++++- src/mapdata.py | 60 ++-------------------------------------------- src/ui.py | 16 ++++++------- 6 files changed, 71 insertions(+), 71 deletions(-) diff --git a/src/common.py b/src/common.py index cc68e99..91cf0c7 100644 --- a/src/common.py +++ b/src/common.py @@ -1,4 +1,7 @@ import sys +import os +import os.path + from PyQt4 import QtCore, QtGui from PyQt4.QtCore import Qt diff --git a/src/dialogs.py b/src/dialogs.py index 66c84e9..945677c 100644 --- a/src/dialogs.py +++ b/src/dialogs.py @@ -11,7 +11,7 @@ class KPTilesetChooserDialog(QtGui.QDialog): # can't be assed to create a model self.chooser = QtGui.QListWidget() - self.nameList = KP.map.tilesets.keys() + self.nameList = KP.knownTilesets.keys() self.nameList.sort() # this piece of the API is kinda shitty but whatever diff --git a/src/editorui/editormain.py b/src/editorui/editormain.py index c20cc47..1cd2948 100644 --- a/src/editorui/editormain.py +++ b/src/editorui/editormain.py @@ -236,7 +236,7 @@ class KPMapScene(QtGui.QGraphicsScene): baseDestX = drawLeft * 24 rows = layer.cache - tileset = KP.map.loadedTilesets[layer.tileset] + tileset = KP.tileset(layer.tileset) tileList = tileset.tiles for y in xrange(drawTop, drawBottom): @@ -280,7 +280,7 @@ class KPMapScene(QtGui.QGraphicsScene): baseDestX = drawLeft * 24 rows = pnLayer.cache - tileset = KP.map.loadedTilesets[pnLayer.tileset] + tileset = KP.tileset(pnLayer.tileset) tileList = tileset.tiles for y in xrange(drawTop, drawBottom): diff --git a/src/main.py b/src/main.py index a429916..8d8d494 100644 --- a/src/main.py +++ b/src/main.py @@ -7,7 +7,6 @@ class KP: KP.app.settings = QtCore.QSettings('Koopatlas', 'Newer Team') - from mapdata import KPMap KP.map = KPMap() @@ -16,6 +15,8 @@ class KP: KP.mainWindow = KPMainWindow() KP.mainWindow.show() + KP.enumerateTilesets() + KP.app.exec_() @@ -34,3 +35,57 @@ class KP: cache[name] = icon return icon + + @classmethod + def enumerateTilesets(cls): + try: + registry = cls.knownTilesets + except AttributeError: + registry = {} + cls.knownTilesets = registry + cls.loadedTilesets = {} + + path = os.path.join(os.getcwd(), 'Tilesets') + if not os.path.exists(path): + os.mkdir(path) + + for file in os.listdir(path): + name = file[:-4] + + if file.endswith('.arc'): + filepath = os.path.join(path, file) + registry[name] = {'path': filepath} + + + @classmethod + def loadTileset(cls, name): + from hashlib import sha256 as sha + + if name in cls.loadedTilesets: + return + + filepath = cls.knownTilesets[name]['path'] + data = open(filepath, 'rb').read() + + tsInfo = cls.knownTilesets[name] + newHash = sha(data).hexdigest() + if 'hash' in tsInfo and tsInfo['hash'] == newHash: + # file hasn't changed + return + + tsInfo['hash'] = newHash + + from tileset import KPTileset + cls.loadedTilesets[name] = KPTileset.loadFromArc(data) + + + @classmethod + def tileset(cls, name): + cache = cls.loadedTilesets + + try: + return cache[name] + except KeyError: + cls.loadTileset(name) + return cache[name] + diff --git a/src/mapdata.py b/src/mapdata.py index 7346634..8469ad4 100644 --- a/src/mapdata.py +++ b/src/mapdata.py @@ -51,7 +51,7 @@ class KPObject(object): __dump_attribs__ = ('position', 'size', 'tileset', 'kind') def _load(self, mapObj, src): - self.kindObj = mapObj.loadedTilesets[self.tileset].objects[self.kind] + self.kindObj = KP.tileset(self.tileset).objects[self.kind] self.updateCache() def __init__(self): @@ -262,7 +262,7 @@ class KPPathTileLayer(KPLayer): def setTileset(self, tileset): self.tileset = tileset - tsObjects = KP.map.loadedTilesets[tileset] + tsObjects = KP.tileset(tileset) for obj in self.objects: obj.tileset = tileset @@ -542,11 +542,6 @@ class KPMap(object): self.doodadDefinitions = [] self.doodadModel = KPMap.DoodadModel(self.doodadDefinitions) - self.tilesets = {} - self.loadedTilesets = {} - - self.loadTilesets() - # LAYERS class LayerModel(QtCore.QAbstractListModel): @@ -740,55 +735,4 @@ class KPMap(object): def derefNode(self, ref): return self.pathLayer.nodes[ref] if (ref >= 0) else None - # TILESETS - def loadTilesets(self): - import os - from hashlib import sha256 as sha - - path = os.getcwd() + os.sep + 'Tilesets' - - if not os.path.exists(path): - os.mkdir(path) - - for file in os.listdir(path): - name = file[:-4] - - if file[-4:] == '.arc': - filepath = path + os.sep + file - - filehandler = open(filepath) - data = filehandler.read() - filehandler.close() - hash = sha(data).hexdigest() - - # this is a bit messy - try: - old_info = self.tilesets[name] - if old_info['hash'] == hash: - continue - except KeyError: - pass - - self.tilesets[name] = {'path': filepath, 'hash': hash} - self.loadedTilesets[name] = KPTileset.loadFromArc(filepath) - - - def reloadTileset(self, name): - # TODO: Re-enable this when it's fixed. - return - - from hashlib import sha256 as sha - - info = self.tilesets[name] - - filehandler = open(info['path']) - data = filehandler.read() - filehandler.close() - hash = sha(data).hexdigest() - - if info['hash'] != hash: - self.loadedTilesets[name] = KPTileset.loadFromArc(info['path']) - - KP.mapscene.update() - KP.mapscene.views()[0].update() diff --git a/src/ui.py b/src/ui.py index a7e71ba..fb8f4a4 100644 --- a/src/ui.py +++ b/src/ui.py @@ -821,10 +821,10 @@ class KPMainWindow(QtGui.QMainWindow): showDoodads = True elif isinstance(layer, KPTileLayer): - KP.map.reloadTileset(layer.tileset) + KP.loadTileset(layer.tileset) showObjects = True - self.objectSelector.setTileset(KP.map.loadedTilesets[layer.tileset]) + self.objectSelector.setTileset(KP.tileset(layer.tileset)) self.objectSelectorDock.setVisible(showObjects) self.doodadSelectorDock.setVisible(showDoodads) @@ -839,9 +839,9 @@ class KPMainWindow(QtGui.QMainWindow): self.scene.setCurrentLayer(layer) - KP.map.reloadTileset(layer.tileset) + KP.loadTileset(layer.tileset) - self.objectSelector.setTileset(KP.map.loadedTilesets[layer.tileset]) + self.objectSelector.setTileset(KP.tileset(layer.tileset)) self.objectSelectorDock.setVisible(True) self.doodadSelectorDock.setVisible(True) @@ -990,18 +990,16 @@ class KPMainWindow(QtGui.QMainWindow): if path: import shutil import os - from hashlib import sha256 as sha + # Todo: refactor this to use a KP method name = os.path.basename(path[:-4]) shutil.copy(path, 'Tilesets') filehandler = open(path) data = filehandler.read() filehandler.close() - hash = sha(data).hexdigest() - KP.map.tilesets[name] = {'path': path, 'hash': hash} - KP.map.loadedTilesets[name] = KPTileset.loadFromArc(path) + KP.knownTilesets[name] = {'path': path} @QtCore.pyqtSlot() @@ -1021,7 +1019,7 @@ class KPMainWindow(QtGui.QMainWindow): KPTileLayer.tileset = tilesetName - self.objectSelector.setTileset(KP.map.loadedTilesets[layer.tileset]) + self.objectSelector.setTileset(KP.tileset(layer.tileset)) # Animate -- cgit v1.2.3