util/datamanager.lua

Fri, 24 Oct 2008 07:27:36 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 24 Oct 2008 07:27:36 +0100
branch
s2s
changeset 148
4c0dcd245d34
parent 117
8e5c5e6a3240
child 129
0f119bece309
permissions
-rw-r--r--

s2s works! \o/ \o/

0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
1 local format = string.format;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
2 local setmetatable, type = setmetatable, type;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
3 local pairs = pairs;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
4 local char = string.char;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
5 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
6 local log = log;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
7 local io_open = io.open;
88
023320150c65 Minor fix
Waqas Hussain <waqas20@gmail.com>
parents: 87
diff changeset
8 local tostring = tostring;
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
9
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
10 module "datamanager"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
11
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
12
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
13 ---- utils -----
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
14 local encode, decode;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
15
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
16 local log = function (type, msg) return log(type, "datamanager", msg); end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
17
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
18 do
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
19 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
20
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
21 decode = function (s)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
22 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
23 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
24
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
25 encode = function (s)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
26 return s and (s:gsub("%W", function (c) return format("%%%x", c:byte()); end));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
27 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
28 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
29
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
30 local function basicSerialize (o)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
31 if type(o) == "number" or type(o) == "boolean" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
32 return tostring(o)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
33 else -- assume it is a string
88
023320150c65 Minor fix
Waqas Hussain <waqas20@gmail.com>
parents: 87
diff changeset
34 return format("%q", tostring(o))
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
35 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
36 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
37
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
38
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
39 local function simplesave (f, o)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
40 if type(o) == "number" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
41 f:write(o)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
42 elseif type(o) == "string" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
43 f:write(format("%q", o))
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
44 elseif type(o) == "table" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
45 f:write("{\n")
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
46 for k,v in pairs(o) do
82
cbf387f29d56 Fix for saving tables with non-string keys
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
47 f:write(" [", basicSerialize(k), "] = ")
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
48 simplesave(f, v)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
49 f:write(",\n")
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
50 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
51 f:write("}\n")
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
52 else
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
53 error("cannot serialize a " .. type(o))
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
54 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
55 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
56
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
57 ------- API -------------
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
58
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
59 function getpath(username, host, datastore)
84
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
60 if username then
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
61 return format("data/%s/%s/%s.dat", encode(host), datastore, encode(username));
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
62 elseif host then
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
63 return format("data/%s/%s.dat", encode(host), datastore);
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
64 else
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
65 return format("data/%s.dat", datastore);
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
66 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
67 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
68
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
69 function load(username, host, datastore)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
70 local data, ret = loadfile(getpath(username, host, datastore));
117
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
71 if not data then
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
72 log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or nil).."@"..(host or nil));
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
73 return nil;
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
74 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
75 setfenv(data, {});
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
76 local success, ret = pcall(data);
117
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
77 if not success then
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
78 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or nil).."@"..(host or nil));
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
79 return nil;
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
80 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
81 return ret;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
82 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
83
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
84 function store(username, host, datastore, data)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
85 local f, msg = io_open(getpath(username, host, datastore), "w+");
117
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
86 if not f then
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
87 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or nil).."@"..(host or nil));
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
88 return nil;
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
89 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
90 f:write("return ");
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
91 simplesave(f, data);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
92 f:close();
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
93 return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
94 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
95

mercurial