summaryrefslogtreecommitdiff
path: root/src/save.py
diff options
context:
space:
mode:
authorRaindropsSys <contact@minteck.org>2023-09-01 14:54:08 +0200
committerRaindropsSys <contact@minteck.org>2023-09-01 14:54:08 +0200
commitc2f1ed6a85c3895483d36af0f64e2829c3fa3263 (patch)
tree6f6a8efbe62db8576a1abf5409569b37ccf261be /src/save.py
parent03afd42fdcd52e4a827016828c4ad286de320078 (diff)
downloadblocks-c2f1ed6a85c3895483d36af0f64e2829c3fa3263.tar.gz
blocks-c2f1ed6a85c3895483d36af0f64e2829c3fa3263.tar.bz2
blocks-c2f1ed6a85c3895483d36af0f64e2829c3fa3263.zip
Updated 11 files and added 2 files (automated)
Diffstat (limited to 'src/save.py')
-rw-r--r--src/save.py54
1 files changed, 54 insertions, 0 deletions
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
+ }