summaryrefslogtreecommitdiff
path: root/src/main.py
blob: f417d94d134d1138106bd0922ae1363d56eda010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from common import *

class KP:
	@staticmethod
	def run():
		KP.app = QtGui.QApplication(sys.argv)

		KP.app.settings = QtCore.QSettings('Koopatlas', 'Newer Team')

		from mapdata import KPMap
		KP.map = KPMap()

		from ui import KPMainWindow
		
		KP.mainWindow = KPMainWindow()
		KP.mainWindow.show()

		KP.enumerateTilesets()

		KP.app.exec_()


	@classmethod
	def icon(cls, name):
		try:
			cache = cls.iconCache
		except AttributeError:
			cache = {}
			cls.iconCache = cache

		try:
			return cache[name]
		except KeyError:
			icon = QtGui.QIcon('Resources/%s.png' % name)
			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
		print "Loading set: %s" % name
		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]