-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_manager.py
104 lines (82 loc) · 3.4 KB
/
save_manager.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from entity.player import Player
from logger.log_manager import LogManager
import file_utils
def get_bytes(value) -> bytes:
"""Convert a specific datatype to bytes for saving"""
if isinstance(value, str):
return value.encode()
elif isinstance(value, int):
return value.to_bytes(4, "big")
elif isinstance(value, bool):
return value.to_bytes(2, "big")
def get_data(value: bytes, data_type: type):
"""Convert bytes to a specific datatype for loading"""
if data_type == str:
return value.decode()
elif data_type == int:
return int.from_bytes(value, "big")
elif data_type == bool:
return bool.from_bytes(value, "big")
def get_saves() -> list[str]:
"""Get the names of the players saved in the saves directory"""
save_files = file_utils.get_files_in_directory(file_utils.get_saves_directory())
player_names = []
for file in save_files:
if file.find(".eq") == -1:
continue
player_names.append(file[:file.find(".eq")])
return player_names
def save(player: Player):
"""Save a player to a binary file at \"[player_name].eq\"
player: The player to save"""
save_file_name = file_utils.get_file_path(f"saves/{player.name}.eq")
with open(save_file_name, "wb") as save_file:
save_data = b""
player_data = player.__dict__
for key, value in player_data.items():
save_data += key.encode()
save_data += b":"
save_data += get_bytes(value)
save_data += b"\n"
save_file.write(save_data)
def load(name: str) -> Player | None:
"""Load a player from a binary file
name: The name of the player to attempt to load"""
save_file_name = file_utils.get_file_path(f"saves/{name}.eq")
loaded_data = dict()
# Read in everything from the file to populate the dictionary
try:
with open(save_file_name, "rb") as save_file:
for line in save_file.readlines():
line_data = line.split(b":")
key = line_data[0].decode()
value = line_data[1]
loaded_data[key] = value
except FileNotFoundError:
LogManager.get_logger().error(f"Could not open save file: {save_file_name} - The file does not exist.")
return None
# Get all the attributes from the player class
loaded_player = Player(loaded_data["name"].decode())
player_data = loaded_player.__dict__
# Match the stuff from the file to the player class
for key, value in loaded_data.items():
# Make sure the key from the file exists in the player class
if key in player_data.keys():
# Convert the binary data to whatever datatype the class member is
# We need to trim the end of the value by 1 to get rid of the newline character
player_data[key] = get_data(value[:-1], type(player_data[key]))
else:
LogManager.get_logger().warn(f"Key `{key}` found in save file with value `{value}`. This has most likely "
f"been deprecated and will be removed the next time the player is saved.")
return loaded_player
if __name__ == "__main__":
test = Player("test")
test.gold = 14345
test.hp = 200
test.maxhp = 200
test.level = 4
print(test.__dict__)
save(test)
loaded_test = load("test")
print(loaded_test.__dict__)
print(get_saves())