-
Notifications
You must be signed in to change notification settings - Fork 0
/
lprofile.lua
60 lines (47 loc) · 1.75 KB
/
lprofile.lua
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
---Wrapper module for the Profile interface; LOVE-friendly.
-- Provides save/loading ability through love.filesystem.
local Profile = require 'profile'
local serialize = require 'libs.Ser.ser'
---Load saved data from a serialized table as a file.
-- @param filename string Filename, must be found in requirepath to load;
-- '.lua' extension is stripped internally.
-- @param dbg boolean Iff true, prints any error while loading.
-- @return Table iff load is successful; else nil.
local function getSavedData(filename, dbg)
if not filename or (type(filename) ~= 'string') then
return nil
end
if string.sub(filename, #filename-3) == '.lua' then
filename = string.sub(filename, 1, #filename-4)
end
local data
local _,err = pcall(function() data = require(filename) end)
if err and dbg then print(err) end
return data
end
---(Profile):save()
-- Write savedata to file
-- @error if internal 'savename' is nil
local save = function(self)
assert (self.filename, "file is nil!")
assert (type(self.filename) == 'string',
"file is not a string but a " .. type(self.filename))
love.filesystem.write(self.filename, serialize(self.data))
end
---Profile.init(filename)
-- Initialize a profile with data in the save file (if any)
-- @param filename string; Filename directed to LOVE's saveDirectory;
-- if nil (or error while loading), a default 'empty' profile
-- is created.
-- @return New Profile instance.
local function init(filename)
local p = Profile.init()
local savedata = getSavedData(filename)
p.filename = filename
if savedata then
p.data = savedata
end
p.save = save
return p
end
return {init = init}