forked from sharpobject/panel-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave.lua
More file actions
235 lines (212 loc) · 6.91 KB
/
save.lua
File metadata and controls
235 lines (212 loc) · 6.91 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
local sep = package.config:sub(1, 1) --determines os directory separator (i.e. "/" or "\")
function write_key_file() pcall(function()
local file = love.filesystem.newFile("keys.txt")
file:open("w")
file:write(json.encode(K))
file:close()
end) end
function read_key_file() pcall(function()
local K=K
local file = love.filesystem.newFile("keys.txt")
file:open("r")
local teh_json = file:read(file:getSize())
local user_conf = json.decode(teh_json)
file:close()
-- TODO: remove this later, it just converts the old format.
if #user_conf == 0 then
local new_conf = {}
for k,v in pairs(user_conf) do
new_conf[k:sub(3)] = v
end
user_conf = {new_conf, {}, {}, {}}
end
for k,v in ipairs(user_conf) do
K[k]=v
end
end) end
function read_txt_file(path_and_filename)
local s
pcall(function()
local file = love.filesystem.newFile(path_and_filename)
file:open("r")
s = file:read(file:getSize())
file:close()
end)
if not s then
s = "Failed to read file"..path_and_filename
else
s = s:gsub('\r\n?', '\n')
end
return s or "Failed to read file"
end
function write_conf_file() pcall(function()
local file = love.filesystem.newFile("conf.json")
file:open("w")
file:write(json.encode(config))
file:close()
end) end
function has_any_custom_character()
local function belong_to_characters_ids(character_id)
for _,v in pairs(default_characters_ids) do
if v == character_id then
return true
end
end
return false
end
local raw_dir_list = love.filesystem.getDirectoryItems("characters")
for _,v in ipairs(raw_dir_list) do
local start_of_v = string.sub(v,0,string.len(prefix_of_ignored_dirs))
if start_of_v ~= prefix_of_ignored_dirs and not belong_to_characters_ids(v) then
return true
end
end
return false
end
function read_conf_file() pcall(function()
local file = love.filesystem.newFile("conf.json")
file:open("r")
local teh_json = file:read(file:getSize())
for k,v in pairs(json.decode(teh_json)) do
config[k] = v
end
if love.filesystem.getInfo("assets/"..config.assets_dir) == nil then
config.assets_dir = default_assets_dir
end
if love.filesystem.getInfo("panels/"..config.panels_dir_when_not_using_set_from_assets_folder) == nil then
config.panels_dir_when_not_using_set_from_assets_folder = default_panels_dir
end
if love.filesystem.getInfo("sounds/"..config.sounds_dir) == nil then
config.sounds_dir = default_sounds_dir
end
if config.use_panels_from_assets_folder == nil then
config.use_panels_from_assets_folder = true
end
if config.use_panels_from_assets_folder then
config.panels_dir = config.assets_dir
else
config.panels_dir = config.panels_dir_when_not_using_set_from_assets_folder
end
if love.filesystem.getInfo("assets/"..config.assets_dir.."/lip")
and not has_any_custom_character() then
print("retrocompatibility applied!")
config.use_default_characters = true
end
love.window.setVSync(config.vsync and 1 or 0)
-- do stuff regarding version compatibility here, before we patch it
config.version = VERSION
file:close()
end) end
function read_replay_file() pcall(function()
local file = love.filesystem.newFile("replay.txt")
file:open("r")
local teh_json = file:read(file:getSize())
replay = json.decode(teh_json)
if type(replay.in_buf) == "table" then
replay.in_buf=table.concat(replay.in_buf)
write_replay_file()
end
end) end
function write_replay_file(path, filename) pcall(function()
local file
if path and filename then
love.filesystem.createDirectory(path)
file = love.filesystem.newFile(path.."/"..filename)
else
file = love.filesystem.newFile("replay.txt")
end
file:open("w")
file:write(json.encode(replay))
file:close()
end) end
function write_user_id_file() pcall(function()
love.filesystem.createDirectory("servers/"..connected_server_ip)
local file = love.filesystem.newFile("servers/"..connected_server_ip.."/user_id.txt")
file:open("w")
file:write(tostring(my_user_id))
file:close()
end) end
function read_user_id_file() pcall(function()
local file = love.filesystem.newFile("servers/"..connected_server_ip.."/user_id.txt")
file:open("r")
my_user_id = file:read()
file:close()
end) end
function write_puzzles() pcall(function()
love.filesystem.createDirectory("puzzles")
local file = love.filesystem.newFile("puzzles/stock (example).txt")
file:open("w")
file:write(json.encode(puzzle_sets))
file:close()
end) end
function read_puzzles() pcall(function()
-- if type(replay.in_buf) == "table" then
-- replay.in_buf=table.concat(replay.in_buf)
-- end
puzzle_packs = love.filesystem.getDirectoryItems("puzzles") or {}
print("loading custom puzzles...")
for _,filename in pairs(puzzle_packs) do
print(filename)
if love.filesystem.getInfo("puzzles/"..filename)
and filename ~= "stock (example).txt"
and filename ~= "README.txt" then
print("loading custom puzzle set: "..(filename or "nil"))
local current_set = {}
local file = love.filesystem.newFile("puzzles/"..filename)
file:open("r")
local teh_json = file:read(file:getSize())
current_set = json.decode(teh_json) or {}
for set_name, puzzle_set in pairs(current_set) do
puzzle_sets[set_name] = puzzle_set
end
print("loaded above set")
end
end
end) end
function print_list(t)
for i, v in ipairs(t) do
print(v)
end
end
function copy_file(source, destination)
local lfs = love.filesystem
local source_file = lfs.newFile(source)
source_file:open("r")
local source_size = source_file:getSize()
temp = source_file:read(source_size)
source_file:close()
local new_file = lfs.newFile(destination)
new_file:open("w")
local success, message = new_file:write(temp, source_size)
new_file:close()
end
function recursive_copy(source, destination)
local lfs = love.filesystem
local names = lfs.getDirectoryItems(source)
local temp
for i, name in ipairs(names) do
if lfs.isDirectory(source.."/"..name) then
print("calling recursive_copy(source".."/"..name..", ".. destination.."/"..name..")")
recursive_copy(source.."/"..name, destination.."/"..name)
elseif lfs.isFile(source.."/"..name) then
if not lfs.isDirectory(destination) then
love.filesystem.createDirectory(destination)
end
print("copying file: "..source.."/"..name.." to "..destination.."/"..name)
local source_file = lfs.newFile(source.."/"..name)
source_file:open("r")
local source_size = source_file:getSize()
temp = source_file:read(source_size)
source_file:close()
local new_file = lfs.newFile(destination.."/"..name)
new_file:open("w")
local success, message = new_file:write(temp, source_size)
new_file:close()
if not success then
print(message)
end
else
print("name: "..name.." isn't a directory or file?")
end
end
end