-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.py
61 lines (49 loc) · 2.04 KB
/
files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Pyhog file handler"""
"""I might get rid of this, unless I use it for the save files."""
"""Version declaration"""
__BRANCH__ = 0x01 # 0x01 is the master branch 0x02 is the prelease branch and 0x03 is the dev branch
__REALYEAR__ = 2022
__YEAR__ = (__REALYEAR__ // 256, __REALYEAR__ % 256)
__MONTH__ = 0x00
__DAY__ = 13
VER = f"{chr(__BRANCH__)}{chr(__YEAR__[0])}{chr(__YEAR__[1])}{chr(__MONTH__)}{chr(__DAY__)}"
engine_version = 0
engine_version = int("".join([str(ord(_)) for _ in VER]))
"""Header building"""
HeaderLen = 0x08
PADDING = 0x7f
FILE_TYPES = {0x00: "state", 0x01: "save"}
MODES = {0x00: "menu", 0x01: "level"}
HEADER = bytes(f"{chr(HeaderLen)}{VER}{chr(PADDING)}".encode())
class VersionError(Exception):
def __init__(self, msg):
super().__init__(msg)
# This is currently unused.
def verify_version(file, data):
"""Verifies that the data's version is the same as the above Version"""
file_ver = "".join(str(num) for num in data[1:5])
# FIXME: I return an incorrect number
version_to_check = int(file_ver)
version_delta = engine_version - version_to_check
if version_delta != 0:
raise VersionError(f"Uh oh, looks like this file is {version_delta} versions behind.")
return True
# def get_state() -> str:
# """Gets the current state of the game with an external file"""
# with open("state.phg", "rb") as f:
# data = f.read()
# # if verify_version(f"state.phg", data):
# """verision verification temporarily removed"""
# return MODES[data[-2]], data[-1]
def set_state(mode, ID:int) -> None:
"""Sets the current state of the game with an external file"""
with open("state.phg", "wb") as f:
f.write(HEADER + bytes(f"{chr(mode)}{chr(ID)}".encode()))
def save(fname, data:str):
with open(f"{fname}.phg", 'wb') as f:
f.write(f"{VER} {data}".encode())
def load(fname):
with open(f"{fname}.phg", "rb") as f:
full = f.read()
if __name__ != "__main__":
set_state(0, 0)