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/main.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) (limited to 'src/main.py') 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] + -- cgit v1.2.3