-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsavegame.py
45 lines (44 loc) · 2.16 KB
/
savegame.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
#load libraries for writing out save files
import zlib
import cerealizer
import settings #import game settings so they can be saved
class SaveGame: #class to manage a savegame
def __init__(self, g): #initialize ourselves
self.g = g #store globals
self.obj_props = {} #dictionary for object properties
self.game_props = {} #dictionary for game state properties (for less ID conflicts)
def new(self): #create a new save file
self.obj_props = {} #empty dictionaries
self.game_props = {}
def load(self, filename): #load savegame data
datafileobj = open(filename, "rb")
data = datafileobj.read()#read in save data
datafileobj.close()
data = zlib.decompress(data) #decompress it
data = cerealizer.loads(data)
self.obj_props = data[0] #get object properties
self.game_props = data[1] #and game properties
def save(self, filename): #write savegame data
f = open(filename, "wb") #open file to write out data
data = cerealizer.dumps([self.obj_props, self.game_props]) #cerealize object data
data = zlib.compress(data, 9) #compress pickled data
f.write(data) #write out data
f.close() #and close written file
def set_prop(self, id, prop, value): #set an object property
if id not in self.obj_props: #if this id hasn't been used yet
self.obj_props[id] = {} #initialize it
self.obj_props[id][prop] = value #store value
def get_prop(self, id, prop, default=None): #get an object property
try:
return self.obj_props[id][prop] #attempt to get value
except: #if it couldn't be done
return default #return default instead
def set_game_prop(self, id, prop, value): #set a game property
if id not in self.game_props: #if this id hasn't been seen yet
self.game_props[id] = {} #initialize it
self.game_props[id][prop] = value #store value
def get_game_prop(self, id, prop, default=None): #get a game property
try:
return self.game_props[id][prop] #attempt to get value
except:
return default #return default if it didn't exist