diff options
-rw-r--r-- | src/FbTk/LResourceHelper.lua | 95 |
1 files changed, 66 insertions, 29 deletions
diff --git a/src/FbTk/LResourceHelper.lua b/src/FbTk/LResourceHelper.lua index e2899f8..5171c43 100644 --- a/src/FbTk/LResourceHelper.lua +++ b/src/FbTk/LResourceHelper.lua | |||
@@ -22,7 +22,9 @@ local function newindex(table, key, value) | |||
22 | local meta = getmetatable(table); | 22 | local meta = getmetatable(table); |
23 | local t, mt = check_arg(table, key); | 23 | local t, mt = check_arg(table, key); |
24 | 24 | ||
25 | if type(value) == 'table' then | 25 | if mt == res_magic then |
26 | write_resource(t, value); | ||
27 | elseif type(value) == 'table' then | ||
26 | if mt == res_magic then | 28 | if mt == res_magic then |
27 | myerror(table, '"' .. key .. '" is a resource.'); | 29 | myerror(table, '"' .. key .. '" is a resource.'); |
28 | end; | 30 | end; |
@@ -35,8 +37,6 @@ local function newindex(table, key, value) | |||
35 | else | 37 | else |
36 | if mt ~= nil and mt._magic == cat_magic and mt._state == 1 then | 38 | if mt ~= nil and mt._magic == cat_magic and mt._state == 1 then |
37 | myerror(table, '"' .. key .. '" is a category.'); | 39 | myerror(table, '"' .. key .. '" is a category.'); |
38 | elseif mt == res_magic then | ||
39 | write_resource(t, value); | ||
40 | else | 40 | else |
41 | meta[key] = value; | 41 | meta[key] = value; |
42 | end; | 42 | end; |
@@ -101,39 +101,76 @@ local function register_resource(root, name, object) | |||
101 | return register_resource(t, tail, object); | 101 | return register_resource(t, tail, object); |
102 | end; | 102 | end; |
103 | 103 | ||
104 | local function dump_(key, value, fd) | 104 | function value_to_string(key, value) |
105 | if type(value) == 'table' then | 105 | if type(value) == 'string' then |
106 | fd:write(key, ' = {}\n'); | 106 | return string.format('%q', value); |
107 | for k, v in pairs(value) do | 107 | elseif type(value) == 'number' then |
108 | k = append_name(key, k); | 108 | return string.format('%g', value); |
109 | 109 | elseif type(value) == 'boolean' then | |
110 | local mt = getmetatable(v); | 110 | if value then |
111 | if mt ~= nil and mt._magic == cat_magic then | 111 | return "true"; |
112 | v = mt; | 112 | else |
113 | elseif mt == res_magic then | 113 | return "false"; |
114 | v = read_resource(v); | 114 | end; |
115 | else | ||
116 | error('Unsupported value type for ' .. key .. ': ' .. type(value)); | ||
117 | end; | ||
118 | end; | ||
119 | |||
120 | local function dump_res(key, value, fd) | ||
121 | local first = true; | ||
122 | for k, v in pairs(value) do | ||
123 | if append_name(key, k) ~= nil then | ||
124 | if first ~= true then | ||
125 | fd:write(','); | ||
115 | end; | 126 | end; |
127 | fd:write(' '); | ||
128 | first = false; | ||
116 | 129 | ||
117 | if k ~= nil then | 130 | if type(k) == 'string' then |
118 | dump_(k, v, fd); | 131 | fd:write(k, ' = '); |
132 | end; | ||
133 | |||
134 | if type(v) == 'table' then | ||
135 | fd:write('{'); | ||
136 | dump_res(k, v, fd); | ||
137 | fd:write('} '); | ||
138 | else | ||
139 | fd:write(value_to_string(k, v)); | ||
119 | end; | 140 | end; |
120 | end; | 141 | end; |
121 | fd:write('\n'); | 142 | end; |
122 | else | 143 | if first ~= true then |
123 | if type(value) == 'string' then | 144 | fd:write(' '); |
124 | value = string.format('%q', value); | 145 | end; |
125 | elseif type(value) == 'number' then | 146 | end; |
126 | value = string.format('%g', value); | 147 | |
127 | elseif type(value) == 'boolean' then | 148 | local function dump_(key, value, fd) |
128 | if value then | 149 | for k, v in pairs(value) do |
129 | value = "true"; | 150 | k = append_name(key, k); |
151 | |||
152 | local mt = getmetatable(v); | ||
153 | if mt ~= nil and mt._magic == cat_magic then | ||
154 | v = mt; | ||
155 | elseif mt == res_magic then | ||
156 | v = read_resource(v); | ||
157 | end; | ||
158 | |||
159 | if k ~= nil then | ||
160 | if type(v) == 'table' then | ||
161 | fd:write(k, ' = {'); | ||
162 | if mt == res_magic then | ||
163 | dump_res(k, v, fd); | ||
164 | fd:write('}\n'); | ||
165 | else | ||
166 | fd:write('}\n'); | ||
167 | dump_(k, v, fd); | ||
168 | fd:write('\n'); | ||
169 | end; | ||
130 | else | 170 | else |
131 | value = "false"; | 171 | fd:write(k, ' = ', value_to_string(k, v), '\n'); |
132 | end; | 172 | end; |
133 | else | ||
134 | error('Unsupported value type for ' .. key .. ': ' .. type(value)); | ||
135 | end; | 173 | end; |
136 | fd:write(key, ' = ', value, '\n'); | ||
137 | end; | 174 | end; |
138 | end; | 175 | end; |
139 | 176 | ||