aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/LResourceHelper.lua
blob: 5171c436b6e02c4c242466c180932346f60d39d9 (plain)
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
read_resource, write_resource, res_magic = ...;

local cat_magic = {};

local function myerror(table, msg)
    error(getmetatable(table)._fullname .. ': ' .. msg);
end;

local function check_arg(table, key)
    if type(key) ~= 'string' and type(key) ~= 'number' then
        myerror(table, 'expecting strings as keys.');
    end;
    if string.match(key, "^_") then
        myerror(table, 'resource names must not begin with "_".');
    end;
    local t = getmetatable(table)[key];
    return t, getmetatable(t);
end;

local new_cat;
local function newindex(table, key, value)
    local meta = getmetatable(table);
    local t, mt = check_arg(table, key);

    if mt == res_magic then
        write_resource(t, value);
    elseif type(value) == 'table' then
        if mt == res_magic then
            myerror(table, '"' .. key .. '" is a resource.');
        end;
        if mt == nil or mt._magic ~= cat_magic then
            t = new_cat(table, key);
        end;
        for k,v in pairs(value) do
            t[k] = v;
        end;
    else
        if mt ~= nil and mt._magic == cat_magic and mt._state == 1 then
            myerror(table, '"' .. key .. '" is a category.');
        else
            meta[key] = value;
        end;
    end;
end;

local function index(table, key)
    local t, mt = check_arg(table, key);

    if mt == res_magic then
        return read_resource(t);
    end;

    return t;
end;

local function append_name(str, name)
    if type(name) == 'string' and string.match(name, '^%a%w*$') then
        return str .. '.' .. name;
    elseif type(name) == 'number' then
        return str .. '[' .. string.format('%g', name) .. ']';
    else
        return nil;
    end;
end;

new_cat = function(table, key)
    local meta = getmetatable(table);
    local mt = {
        __newindex = newindex, __index = index,
        _magic = cat_magic, _fullname = append_name(meta._fullname, key), _state = 0
    };
    meta[key] = setmetatable({}, mt);
    return meta[key];
end;

local function register_resource(root, name, object)
    local meta = getmetatable(root);
    meta._state = 1;

    local head, tail = string.match(name, '^(%a%w*)%.?(.*)');
    local t = meta[head];
    local mt = getmetatable(t);

    if tail == '' then
        if mt ~= nil and mt._magic == cat_magic then
            t = mt;
        end;
        if getmetatable(object) == res_magic then
            meta[head] = object;
            write_resource(object, t);
        else
            meta[head] = nil;
            root[head] = object;
        end;
        return t;
    end;

    if mt == nil or mt._magic ~= cat_magic then
        t = new_cat(root, head);
    end;
    return register_resource(t, tail, object);
end;

function value_to_string(key, value)
    if type(value) == 'string' then
        return string.format('%q', value);
    elseif type(value) == 'number' then
        return string.format('%g', value);
    elseif type(value) == 'boolean' then
        if value then
            return "true";
        else
            return "false";
        end;
    else
        error('Unsupported value type for ' .. key .. ': ' .. type(value));
    end;
end;

local function dump_res(key, value, fd)
    local first = true;
    for k, v in pairs(value) do
        if append_name(key, k) ~= nil then
            if first ~= true then
                fd:write(',');
            end;
            fd:write(' ');
            first = false;

            if type(k) == 'string' then
                fd:write(k, ' = ');
            end;

            if type(v) == 'table' then
                fd:write('{');
                dump_res(k, v, fd);
                fd:write('} ');
            else
                fd:write(value_to_string(k, v));
            end;
        end;
    end;
    if first ~= true then
        fd:write(' ');
    end;
end;

local function dump_(key, value, fd)
    for k, v in pairs(value) do
        k = append_name(key, k);

        local mt = getmetatable(v);
        if mt ~= nil and mt._magic == cat_magic then
            v = mt;
        elseif mt == res_magic then
            v = read_resource(v);
        end;

        if k ~= nil then
            if type(v) == 'table' then
                fd:write(k, ' = {');
                if mt == res_magic then
                    dump_res(k, v, fd);
                    fd:write('}\n');
                else
                    fd:write('}\n');
                    dump_(k, v, fd);
                    fd:write('\n');
                end;
            else
                fd:write(k, ' = ', value_to_string(k, v), '\n');
            end;
        end;
    end;
end;

local function dump(root, file)
    local fd = io.open(file, 'w');
    local meta = getmetatable(root);
    if not string.match(meta._fullname, "^[%a]+$") then
        error("Someone has been messing with metatables.");
    end;

    dump_(meta._fullname, meta, fd);
    fd:close();
end;

local function make_root(name)
    local t = {
        __newindex = newindex, __index = index,
        _magic = cat_magic, _fullname = name, _state = 0
    };
    getfenv()[name] = setmetatable({}, t);
end;

return make_root, register_resource, dump;