-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSaveHandler.gd
186 lines (133 loc) · 5.09 KB
/
SaveHandler.gd
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
extends Object
class_name SaveHandler
var filename :StringName = "world.xau"
var profile := 0
var data_dictionary := {}
var profile_data := {
"name": "Naraq",
"puzzles": 0,
"seconds": 0,
}
var screenshot :Image = preload("res://sprites/screenshot.png").get_image()
func load_data():
print("according to handler: ", profile)
if not DirAccess.dir_exists_absolute("user://profiles/" + str(profile)):
DirAccess.make_dir_recursive_absolute("user://profiles/" + str(profile))
if FileAccess.file_exists("user://profiles/%s/%s" % [str(profile), filename]):
var file := FileAccess.open("user://profiles/%s/%s" % [str(profile), filename], FileAccess.READ)
var test_json_conv = JSON.new()
if test_json_conv.parse(file.get_as_text()) == OK:
data_dictionary = test_json_conv.data
file.close()
load_profile_data()
func load_profile_data() -> void:
if not DirAccess.dir_exists_absolute("user://profiles/" + str(profile)):
DirAccess.make_dir_recursive_absolute("user://profiles/" + str(profile))
if FileAccess.file_exists("user://profiles/%s/.profile" % str(profile)):
var file := FileAccess.open("user://profiles/%s/.profile" % str(profile), FileAccess.READ)
var test_json_conv = JSON.new()
if test_json_conv.parse(file.get_as_text()) == OK:
profile_data = test_json_conv.data
file.close()
else:
save_profile_data()
func save():
if not DirAccess.dir_exists_absolute("user://profiles/" + str(profile)):
DirAccess.make_dir_recursive_absolute("user://profiles/" + str(profile))
var file := FileAccess.open("user://profiles/%s/%s" % [str(profile), filename], FileAccess.WRITE)
file.store_string(JSON.stringify(data_dictionary, "\t"))
file.close()
file = FileAccess.open("user://profiles/%s/.profile" % str(profile), FileAccess.WRITE)
file.store_string(JSON.stringify(profile_data, "\t"))
file.close()
func save_profile_data():
if not DirAccess.dir_exists_absolute("user://profiles/" + str(profile)):
DirAccess.make_dir_recursive_absolute("user://profiles/" + str(profile))
var file := FileAccess.open("user://profiles/%s/.profile" % str(profile), FileAccess.WRITE)
file.store_string(JSON.stringify(profile_data, "\t"))
file.close()
func get_value_in_dict(dict: Dictionary, key: StringName, default: Variant) -> Variant:
if not key in dict:
return default
var value = dict[key]
if value is Dictionary and "type" in value:
var type :StringName = value["type"]
match type:
"color":
return Color(value["value"])
"vector2":
return Vector2(value["value"][0], value["value"][1])
_:
return value
else:
return value
func get_value(key: StringName, default: Variant) -> Variant:
return get_value_in_dict(data_dictionary, key, default)
func save_value(key: StringName, value: Variant):
save_value_in_dict(data_dictionary, key, value)
func save_value_in_dict(dict: Dictionary, key: StringName, value: Variant):
if value is Color:
dict[key] = {
"type": "color",
"value": value.to_html()
}
elif value is Vector2:
dict[key] = {
"type": "vector2",
"value": [value.x, value.y]
}
else:
dict[key] = value
func vget_value(keys: Array[StringName], default: Variant) -> Variant:
var c_dict :Dictionary = data_dictionary
for key_index in keys.size() - 1:
var current_key :StringName = keys[key_index]
if c_dict is Dictionary:
if not current_key in c_dict:
c_dict[current_key] = {}
c_dict = c_dict[current_key]
else:
return default
return get_value_in_dict(c_dict, keys.pop_back(), default)
func vsave_value(keys: Array[StringName], value: Variant):
var c_dict :Dictionary = data_dictionary
for key_index in keys.size() - 1:
var current_key :StringName = keys[key_index]
if c_dict is Dictionary:
if current_key in c_dict:
c_dict = c_dict[current_key]
save_value_in_dict(c_dict, keys.pop_back(), value)
func store_screenshot():
if not DirAccess.dir_exists_absolute("user://profiles/" + str(profile)):
DirAccess.make_dir_recursive_absolute("user://profiles/" + str(profile))
var th := Thread.new()
th.start(screenshot.save_png.bind("user://profiles/%s/screenshot.png" % str(profile)))
await th.wait_to_finish()
func load_screenshot():
if not DirAccess.dir_exists_absolute("user://profiles/" + str(profile)):
DirAccess.make_dir_recursive_absolute("user://profiles/" + str(profile))
if FileAccess.file_exists("user://profiles/%s/screenshot.png" % str(profile)):
screenshot = Image.load_from_file("user://profiles/%s/screenshot.png" % str(profile))
else:
screenshot = await SaveData.take_screenshot()
store_screenshot()
func delete_profile():
var path := "user://profiles/%s" % str(profile)
if DirAccess.dir_exists_absolute(path):
var dir := DirAccess.open(path)
dir.include_hidden = true
for i in dir.get_files():
dir.remove(i)
DirAccess.remove_absolute(path)
func erase_key(key: StringName):
data_dictionary.erase(key)
func verase_key(keys: Array[StringName], key: Variant):
var c_dict :Dictionary = data_dictionary
for key_index in keys.size() - 1:
var current_key :StringName = keys[key_index]
if c_dict is Dictionary:
if current_key in c_dict:
c_dict = c_dict[current_key]
else:
return
c_dict.erase(key)