Оптимизация и баги функции

lorgon

Известный
Автор темы
657
266
Версия MoonLoader
Другое
Нужен фидбек на счёт функций, которые переводят таблицу в строку / и обратно, включая переменные на mimgui. Что нужно поправить, добавить или убрать.
Lua:
function decodeTable(str)
    local im = require 'imgui'
    local new = im.new
    local env = {
        ImVec2 = im.ImVec2,
        ImVec4 = im.ImVec4,
        bool = new.bool,
        int = new.int,
        float = new.float,
    }
    local tab = load("return " .. str, nil, nil, env)()
end

function encodeTable(tbl)
    local result = {}
    local function str(a)
        local ffi = require 'ffi'
        if ffi.istype('ImVec2', a) then
            return string.format("ImVec2(%s, %s)", a.x, a.y)
        elseif ffi.istype('ImVec4', a) then
            return string.format("ImVec4(%s, %s, %s, %s)", a.x, a.y, a.z, a.w)
        elseif ffi.istype('int [1]', a) then
            return string.format("int(%s)", a[0])
        elseif ffi.istype('float [1]', a) then
            return string.format("float(%s)", a[0])
        elseif ffi.istype('bool [1]', a) then
            return string.format("bool(%s)", a[0])
        elseif type(a) == 'string' then
            return string.format("%q", a)
        else
            return tostring(a)
        end
    end
    for k, v in pairs(tbl) do
        if type(v) == "table" then
            v = encodeTable(v)
        else
            v = str(v)
        end
        if type(k) == "string" then
            k = string.format("[%q]", k)
        else
            k = string.format("[%s]", k)
        end
        table.insert(result, k .. " = " .. v)
    end
    return "{" .. table.concat(result, ", ") .. "}"
end