3 # Loads block files from sectors folders into map.sqlite database.
4 # The sectors folder should be safe to remove after this prints "Finished."
11 exit('You need to have the Python sqlite3 module.')
17 # sectors2 gets to try first
18 if os.path.isdir(path + 'sectors2/'):
19 paths.append('sectors2')
20 if os.path.isdir(path + 'sectors/'):
21 paths.append('sectors')
24 exit('Could not find sectors folder at ' + path + 'sectors2/ or ' + path + 'sectors/')
26 def parseSigned12bit(u):
28 return (u if u < 2**11 else u - 2**12)
30 def parseSigned16bit(u):
32 return (u if u < 2**15 else u - 2**16)
41 # Convert sector folder(s) to integer
42 def getSectorPos(dirname):
45 x = parseSigned16bit(dirname[:4])
46 z = parseSigned16bit(dirname[4:])
47 elif len(dirname) == 7:
49 x = parseSigned12bit(dirname[:3])
50 z = parseSigned12bit(dirname[4:])
52 print('Terrible sector at ' + dirname)
57 # Convert block file to integer position
58 def getBlockPos(sectordir, blockfile):
59 p2d = getSectorPos(sectordir)
64 if len(blockfile) != 4:
65 print("Invalid block filename: " + blockfile)
67 y = parseSigned16bit(blockfile)
69 return p2d[0], y, p2d[1]
71 # Convert location to integer
72 def getBlockAsInteger(p):
73 return int64(p[2]*16777216 + p[1]*4096 + p[0])
78 if not os.path.isfile(path + 'map.sqlite'):
81 conn = sqlite3.connect(path + 'map.sqlite')
84 exit('Could not open database.')
89 cur.execute("CREATE TABLE IF NOT EXISTS `blocks` (`pos` INT NOT NULL PRIMARY KEY, `data` BLOB);")
91 print('Created database at ' + path + 'map.sqlite')
101 elif base == 'sectors2':
104 print('Ignoring base ' + base)
107 for root, dirs, files in os.walk(path + base):
110 pos = getBlockAsInteger(getBlockPos(root[(-8 if v == 1 else -7 if v == 2 else 0):], block))
113 print('Ignoring broken path ' + root + '/' + block)
116 f = open(root+'/'+block, 'rb')
119 if sys.version_info.major == 2:
122 blob = memoryview(blob)
123 cur.execute('INSERT OR IGNORE INTO `blocks` VALUES(?, ?)', (pos, blob))
126 if(time.time() - t > 3):
128 print(str(count)+' blocks processed...')
132 print('Finished. (' + str(count) + ' blocks)')