import os import zlib import json import constants def save_world(path, regions): checksums = {} region_list = [] if not os.path.exists(path): os.mkdir(path) if not os.path.exists(path + "/region"): os.mkdir(path + "/region") if not os.path.exists(path + "/playerdata"): os.mkdir(path + "/playerdata") for i in range(len(regions)): with open(path + "/region/" + str(i) + ".bcr", "wb") as f: checksums[str(i) + ".bcr"] = hex(zlib.crc32(str.encode(json.dumps(regions[i])))) region_list.append(str(i) + ".bcr") f.write(zlib.compress(str.encode(json.dumps(regions[i])))) with open(path + "/level.dat", "wb") as f: f.write(zlib.compress(str.encode(json.dumps({ "version": constants.VERSION, "checksums": checksums, "regions": region_list })))) def load_world(path): blocks = [] with open(path + "/level.dat", "rb") as f: data = json.loads(zlib.decompress(f.read())) print(data) for region in data['regions']: with open(path + "/region/" + region, "rb") as f: raw = zlib.decompress(f.read()) if hex(zlib.crc32(raw)) != data['checksums'][region]: raise Exception("Region file " + region + " is corrupted.") blocks.append(json.loads(raw)) return { "version": data['version'], "blocks": blocks }