FoxWorld-server/worldrw.py
2025-04-11 11:22:16 +03:00

27 lines
835 B
Python

import os
import os.path
CHUNK_SIZE = 16
FILE_VERSION = 0
def _get_chunk_path(x: int, z: int) -> str:
return f"world/{x}.{z}.fwc"
def get_chunk_data(x: int, z: int) -> bytes or None:
if os.path.exists(loc := _get_chunk_path(x, z)):
with open(loc, "rb") as fp:
if fp.read(3) != b"FWC":
raise RuntimeError(f"{loc} is not a FWC file!")
if (version := int.from_bytes(fp.read(2))) != FILE_VERSION:
raise RuntimeError(f"{loc} is wrong FWC version! Found {version}, expected {FILE_VERSION}.")
return fp.read()
def save_chunk_data(x: int, z: int, data: bytes) -> None:
if not os.path.isdir("world"): os.mkdir("world")
with open(_get_chunk_path(x, z), "wb") as fp:
fp.write(b"FWC" + FILE_VERSION.to_bytes(2))
fp.write(data)
def chunk_exists(x: int, z: int) -> bool:
return os.path.exists(_get_chunk_path(x, z))