This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx_save_load.py
138 lines (119 loc) · 4.25 KB
/
x_save_load.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import errno
import json
import base64
import abc
import y_helpers
class SaveLoadJSON(metaclass=abc.ABCMeta):
def __init__(self, filepath):
self.datatypes = (int, str, bool, float, dict, list, tuple)
self.key = '74zrm66FpKKkkZOb7YytlKaRk5vujK2Up5GTm9jG59Wbn5Pvm8aarpSXpKCUjNWbroWlpvHi'
self.isEncoding = True
self.protectedFields = ['protectedFields',
'filepath', 'datatypes', 'key', 'isEncoding']
self.filepath = filepath
def checkFolder(self):
if not os.path.exists(os.path.dirname(self.filepath)):
try:
os.makedirs(os.path.dirname(self.filepath))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
def saveGame(self):
if getattr(self, 'runManualUpdates'):
self.runManualUpdates()
obj = self.iterateObj()
self.checkFolder()
self.writeReadme()
with open(self.filepath, 'w') as fp:
if self.isEncoding:
json_obj = json.dumps(obj)
enc = self.encode(str(json_obj)).decode("utf-8")
fp.write(enc)
else:
json.dump(obj, fp, indent=4)
def writeReadme(self):
dirPath = os.path.dirname(self.filepath)
readmePath = dirPath + "/readme.txt"
with open(readmePath, 'w') as fp:
fp.write(
'Please do not try to manually edit any of the files in this folder.')
def iterateObj(self):
obj = {}
for key in self.__dict__:
isValid = self.isValidType(key)
if isValid:
obj[key] = self.__dict__[key]
return obj
def isValidType(self, key):
return self.isInbuilt(self.__dict__[key]) and not self.isProtected(key)
def isInbuilt(self, thing):
return type(thing) in self.datatypes
def isProtected(self, key):
return key in self.protectedFields
def loadGame(self):
if getattr(self, 'runManualUpdates'):
self.runManualUpdates()
json_data = self.get_text(self.filepath)
for key, val in json_data.items():
self.__dict__[key] = val
def get_text(self, filename):
json_data = None
with open(filename, encoding='utf-8-sig') as json_file:
text = json_file.read()
if self.isEncoding:
dec = self.decode(text).replace("'", '"')
else:
dec = text
json_data = json.loads(dec)
return json_data
def getSaveFileInfo(self, filename):
json_data = self.get_text(filename)
totalTime = 0
thisMap = 'Nowhere'
if 'currentMap' in json_data:
thisMap = json_data['currentMap']
if 'totalTimePlayed' in json_data:
totalTime = y_helpers.pretty_print_time(
json_data['totalTimePlayed'])
return (thisMap, totalTime)
def encode(self, clear):
key = self.key
enc = []
for i in range(len(clear)):
key_c = key[i % len(key)]
enc_c = (ord(clear[i]) + ord(key_c)) % 256
enc.append(enc_c)
return base64.urlsafe_b64encode(bytes(enc))
def decode(self, enc):
key = self.key
dec = []
enc = base64.urlsafe_b64decode(enc)
for i in range(len(enc)):
key_c = key[i % len(key)]
dec_c = chr((256 + enc[i] - ord(key_c)) % 256)
dec.append(dec_c)
return "".join(dec)
@abc.abstractmethod
def runManualUpdates(self):
raise NotImplementedError('subclass must define this method')
if __name__ == "__main__":
class GameThatSaves(SaveLoadJSON):
def __init__(self, filepath):
SaveLoadJSON.__init__(self, filepath)
self.x = 1.0
self.y = 2
self.z = 3
self.no = self.saveGame
self.data = {'a': 21, 'b': 22}
def runManualUpdates(self):
pass
s = GameThatSaves('./test/result.json')
s.saveGame()
print('s.x =', s.x)
for key in s.__dict__:
if not key in s.protectedFields:
s.__dict__[key] = None
print('s.x =', s.x)
s.loadGame()
print('s.x =', s.x)