From c2f1ed6a85c3895483d36af0f64e2829c3fa3263 Mon Sep 17 00:00:00 2001 From: RaindropsSys Date: Fri, 1 Sep 2023 14:54:08 +0200 Subject: Updated 11 files and added 2 files (automated) --- src/save.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/save.py (limited to 'src/save.py') diff --git a/src/save.py b/src/save.py new file mode 100644 index 0000000..0e8e3d2 --- /dev/null +++ b/src/save.py @@ -0,0 +1,54 @@ +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 + } -- cgit