-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
354 lines (290 loc) · 11.9 KB
/
main.py
File metadata and controls
354 lines (290 loc) · 11.9 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import pygame
import urllib.request
import urllib.error
import json
import os
import threading
import sys
import psutil
import shared
import shutil
screensize_x = 500
screensize_y = 500
internet_is_working = True
realdata = {}
inprogress:list[str] = []
downloaded_from_web = 0
tile_size = 0
if not os.path.isdir("tiles"):
os.mkdir("tiles")
opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent', "Enderbyte-Programs/Maps")]
urllib.request.install_opener(opener)
def parse_size(data: int) -> str:
result:str = ""
if data < 0:
neg = True
data = -data
else:
neg = False
if data < 2000:
result = f"{data} bytes"
elif data > 2000000000:
result = f"{round(data/1000000000,2)} GB"
elif data > 2000000:
result = f"{round(data/1000000,2)} MB"
elif data > 2000:
result = f"{round(data/1000,2)} KB"
if neg:
result = "-"+result
return result
def get_memusage() -> int:
p = psutil.Process(os.getpid())
return p.memory_info().rss
def write_3d(z,y,x,val):
if not z in realdata:
realdata[z] = {}
if not y in realdata[z]:
realdata[z][y] = {}
realdata[z][y][x] = val
def grab_3d(z,y,x):
"""Attempt to load from this array, else return None"""
try:
return realdata[z][y][x]
except:
return None
def do_download(url:str,file:str):
global needsupdate, tile_size, downloaded_from_web, internet_is_working
print(url)
inprogress.append(url)
os.makedirs(os.path.dirname(file),exist_ok=True)
try:
msg = urllib.request.urlretrieve(url,file)[1]
except urllib.error.URLError:
internet_is_working = False
inprogress.remove(url)
needsupdate = True
return
internet_is_working = True
tile_size += len(msg.as_bytes())
downloaded_from_web += len(msg.as_bytes())
inprogress.remove(url)
needsupdate = True
print("Completed",file)
class Source:
def __init__(self,d:dict):
self.url:str = d["url"]
self.minzoom:int = d["minzoom"]
self.maxzoom:int = d["maxzoom"]
self.tilewidth:int = d["tilewidth"]
self.tileheight:int = d["tileheight"]
self.ext:str = d["ext"]
self.uses_standard_tile_url_rules:bool = d["stdrules"]
def load_url(self,z:int,y:int,x:int) -> pygame.Surface:
image = grab_3d(z,y,x)
if image is None:
fileoutpath = os.path.abspath("tiles/"+str(z)+"/"+str(y)+"/"+str(x)+"."+self.ext)
if not os.path.isfile(fileoutpath):
try:
#Do a check for impossibilities
if self.uses_standard_tile_url_rules:
if z < 0 or y < 0 or x < 0:
raise Exception("Nonzero")
if y > (2**z - 1) or x > (2**z - 1):
raise Exception("OOB")
if z > self.maxzoom:
raise Exception("Overzoomed")
print(z,y,x,"Downloading")
parsedurl = self.url.replace(r"{z}",str(z)).replace(r"{y}",str(y)).replace(r"{x}",str(x))
if not parsedurl in inprogress:
if internet_is_working:
print("NEW INIT")
threading.Thread(target=do_download,args=(parsedurl,fileoutpath)).start()
else:
return nointernetscreen
return loadingscreen
except:
return errorscreen
try:
image = pygame.image.load(fileoutpath).convert()
except:
#Technically this should not be possible but sometimes it happens anyway
return anomalyscreen
write_3d(z,y,x,image)
return image
pygame.init()
# Set up the drawing window
screen = pygame.display.set_mode([screensize_x, screensize_y],pygame.RESIZABLE)
font = pygame.font.SysFont("Arial",12)
pygame.display.set_caption("Maps")
pygame.display.set_icon(pygame.image.load("icon.png").convert())
xoffset = 0
yoffset = 0
zoom = 2
keyarray:dict = {}
errorscreen = pygame.image.load("assets/prefiles/error.png").convert()
loadingscreen = pygame.image.load("assets/prefiles/loading.png").convert()
anomalyscreen = pygame.image.load("assets/prefiles/anomaly.png").convert()
nointernetscreen = pygame.image.load("assets/prefiles/nointernet.png").convert()
mouse_startclickpos = (0,0)
isdragging = False
def get_from_keyarray(key):
if key in keyarray:
return keyarray[key]
else:
return False
with open("config.json") as f:
data = json.load(f)
currentsource = Source(data["sources"]["default"])
tile_size:int = data["tilesize"]
current_source_tiles_to_centre_x = round(screensize_x / 2 / currentsource.tilewidth)
current_source_tiles_to_centre_y = round(screensize_y / 2 / currentsource.tileheight)
# Run until the user asks to quit
running = True
clock = pygame.time.Clock()
needsupdate = True
mousestate = 0
ttk = 0
while running:
plusbutton = shared.Button((255,255,255),10,screensize_y - 25,20,20,"+")
minusbutton = shared.Button((255,255,255),40,screensize_y - 25,20,20,"-")
clearmembutton = shared.Button((255,255,255),70,screensize_y - 25,20,20,imagepath="assets/icons/clear-memory.png")
clearchachebutton = shared.Button((255,255,255),100,screensize_y - 25,20,20,imagepath="assets/icons/clear-everything.png")
reloadinetbutton = shared.Button((255,255,255),130,screensize_y - 25,20,20,imagepath="assets/icons/inetreload.png")
ttk += 1
# Did the user click the window close button?
ev = pygame.event.get()
for event in ev:
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
needsupdate = True
keyarray[event.key] = True
#Singleton fire events
if event.key == pygame.K_q:
running = False
if event.key == pygame.K_EQUALS:
zoom += 1
xoffset *= 2
xoffset += current_source_tiles_to_centre_x*currentsource.tilewidth
yoffset *= 2
yoffset += current_source_tiles_to_centre_y*currentsource.tileheight
if event.key == pygame.K_MINUS:
zoom -= 1
xoffset -= current_source_tiles_to_centre_x*currentsource.tilewidth
xoffset //= 2
yoffset -= current_source_tiles_to_centre_y*currentsource.tileheight
yoffset //= 2
if event.key == pygame.K_f:
needsupdate = True
if shared.confirm(screen,"Arial",24,"This will free up memory but \ndecrease loading speeds. \nDo you want to continue?"):
realdata = {}
if event.key == pygame.K_r:
needsupdate = True
if shared.confirm(screen,"Arial",24,"This wil free up disk space but\ndestroy all cached tiles.\nAre you sure you want to do this?"):
realdata = {}
tile_size = 0
shutil.rmtree("tiles")
os.mkdir("tiles")
elif event.type == pygame.KEYUP:
keyarray[event.key] = False
elif event.type == pygame.VIDEORESIZE:
screensize_x,screensize_y = event.size
current_source_tiles_to_centre_x = round(screensize_x / 2 / currentsource.tilewidth)
current_source_tiles_to_centre_y = round(screensize_y / 2 / currentsource.tileheight)
needsupdate = True
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
isdragging = True
mouse_startclickpos = pygame.mouse.get_pos()
elif event.type == pygame.MOUSEWHEEL:
if event.y == 1:
zoom += 1
xoffset *= 2
xoffset += current_source_tiles_to_centre_x*currentsource.tilewidth
yoffset *= 2
yoffset += current_source_tiles_to_centre_y*currentsource.tileheight
elif event.y == -1:
zoom -= 1
xoffset -= current_source_tiles_to_centre_x*currentsource.tilewidth
xoffset //= 2
yoffset -= current_source_tiles_to_centre_y*currentsource.tileheight
yoffset //= 2
needsupdate = True
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
isdragging = False
elif event.type == pygame.MOUSEMOTION:
if isdragging:
xdiff = pygame.mouse.get_pos()[0] - mouse_startclickpos[0]
ydiff = pygame.mouse.get_pos()[1] - mouse_startclickpos[1]
xoffset -= xdiff
yoffset -= ydiff
mouse_startclickpos = pygame.mouse.get_pos()
needsupdate = True
if get_from_keyarray(pygame.K_DOWN):
needsupdate = True
yoffset += 5
if get_from_keyarray(pygame.K_UP):
needsupdate = True
yoffset -= 5
if get_from_keyarray(pygame.K_LEFT):
needsupdate = True
xoffset -= 5
if get_from_keyarray(pygame.K_RIGHT):
needsupdate = True
xoffset += 5
#Update any stragglers
# Fill the background with white
if needsupdate:
pygame.mouse.set_cursor(pygame.cursors.Cursor(pygame.SYSTEM_CURSOR_SIZEALL))
screen.fill((255, 255, 255))
nearest_tile_x = xoffset // currentsource.tilewidth
nearest_tile_y = yoffset // currentsource.tileheight
last_tile_x = (xoffset + screensize_x) // currentsource.tilewidth + 1
last_tile_y = (yoffset + screensize_y) // currentsource.tileheight + 1
for y in range(nearest_tile_y,last_tile_y):
for x in range(nearest_tile_x,last_tile_x):
screen.blit(currentsource.load_url(zoom,y,x),(((x - nearest_tile_x) * currentsource.tilewidth) - (xoffset % currentsource.tilewidth),((y - nearest_tile_y) * currentsource.tileheight) - (yoffset % currentsource.tileheight)))
screen.blit(font.render(f"X: {xoffset} Y: {yoffset} Z: {zoom} || {round(clock.get_fps())} FPS || M: {parse_size(get_memusage())} D: {parse_size(downloaded_from_web)} W: {parse_size(tile_size)}",True,(0,0,0)),(0,0))
needsupdate = False
#Write UI
pygame.draw.rect(screen,(200,200,200),(0,screensize_y-30,screensize_x,30))
if plusbutton.isOver(ev):
zoom += 1
xoffset *= 2
xoffset += current_source_tiles_to_centre_x*currentsource.tilewidth
yoffset *= 2
yoffset += current_source_tiles_to_centre_y*currentsource.tileheight
needsupdate = True
elif minusbutton.isOver(ev):
zoom -= 1
xoffset -= current_source_tiles_to_centre_x*currentsource.tilewidth
xoffset //= 2
yoffset -= current_source_tiles_to_centre_y*currentsource.tileheight
yoffset //= 2
needsupdate = True
elif clearmembutton.isOver(ev):
needsupdate = True
if shared.confirm(screen,"Arial",24,"This will free up memory but \ndecrease loading speeds. \nDo you want to continue?"):
realdata = {}
elif clearchachebutton.isOver(ev):
needsupdate = True
if shared.confirm(screen,"Arial",24,"This wil free up disk space but\ndestroy all cached tiles.\nAre you sure you want to do this?"):
realdata = {}
tile_size = 0
shutil.rmtree("tiles")
os.mkdir("tiles")
elif reloadinetbutton.isOver(ev):
needsupdate = True
internet_is_working = True
reloadinetbutton.draw(screen)
plusbutton.draw(screen)
minusbutton.draw(screen)
clearchachebutton.draw(screen)
clearmembutton.draw(screen)
# Flip the display
clock.tick(30)
pygame.display.flip()
# Done! Time to quit.
pygame.quit()