1 local format = string.format; |
1 local format = string.format; |
2 local setmetatable, type = setmetatable, type; |
2 local setmetatable, type = setmetatable, type; |
3 local pairs, ipairs = pairs, ipairs; |
3 local pairs, ipairs = pairs, ipairs; |
4 local char = string.char; |
4 local char = string.char; |
5 local loadfile, setfenv, pcall = loadfile, setfenv, pcall; |
5 local loadfile, setfenv, pcall = loadfile, setfenv, pcall; |
6 local log = log; |
6 local log = require "util.logger".init("datamanager"); |
7 local io_open = io.open; |
7 local io_open = io.open; |
8 local os_remove = os.remove; |
8 local os_remove = os.remove; |
9 local tostring, tonumber = tostring, tonumber; |
9 local tostring, tonumber = tostring, tonumber; |
10 local error = error; |
10 local error = error; |
11 local next = next; |
11 local next = next; |
68 end |
68 end |
69 end |
69 end |
70 |
70 |
71 ------- API ------------- |
71 ------- API ------------- |
72 |
72 |
|
73 function set_data_path(path) |
|
74 data_path = path; |
|
75 end |
|
76 |
73 function getpath(username, host, datastore, ext) |
77 function getpath(username, host, datastore, ext) |
74 ext = ext or "dat"; |
78 ext = ext or "dat"; |
75 if username then |
79 if username then |
76 return format("data/%s/%s/%s.%s", encode(host), datastore, encode(username), ext); |
80 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext); |
77 elseif host then |
81 elseif host then |
78 return format("data/%s/%s.%s", encode(host), datastore, ext); |
82 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext); |
79 else |
83 else |
80 return format("data/%s.%s", datastore, ext); |
84 return format("%s/%s.%s", data_path, datastore, ext); |
81 end |
85 end |
82 end |
86 end |
83 |
87 |
84 function load(username, host, datastore) |
88 function load(username, host, datastore) |
85 local data, ret = loadfile(getpath(username, host, datastore)); |
89 local data, ret = loadfile(getpath(username, host, datastore)); |