ConfigInfo.lua 2.05 KB
cc.exports.ConfigInfo = {}

local configMap = {};

local function str_split(str,split_char)
    str = tostring(str);
    local sub_str_tab = {};
    while (true) do
        local pos = nil
        for i = 1, #str do
            if string.sub(str, i, i) == split_char then 
               pos = i
               break
            end
        end
        if (not pos) then
            sub_str_tab[#sub_str_tab + 1] = str;
            break;
        end
        local sub_str = string.sub(str, 1, pos - 1);
        sub_str_tab[#sub_str_tab + 1] = sub_str;
        str = string.sub(str, pos + 1, #str);
    end

    return sub_str_tab;
end

--配置表信息
local function init()
    local itemStr = cc.FileUtils:getInstance():getStringFromFile("config.ini");
    local function readLineFun(lineStr)
        local contents = str_split(lineStr,'=');
        if #contents > 1 then
            configMap[contents[1]] = contents[2];
        end
    end

    local index = 1;
    local function readLine(str)
        local a,b = string.find(str,"\r\n");
        if a then
            local lineStr = string.sub(str,1,a-1);
            readLineFun(lineStr);

            index = index + 1;
            str = string.sub(str,b+1);
            readLine(str);
        else
            readLineFun(str);
        end
    end
    readLine(itemStr);
end
init();

--通过ID获取信息
function ConfigInfo.getConfigInfo(id)
    return configMap[tostring(id)];
end

--读取txt配置
function ConfigInfo.readTxtLines(linesStr)
    local lineMap = {};
    local function readLineFun(lineStr)
        local contents = str_split(lineStr,'=');
        lineMap[contents[1]] = contents[2];
    end

    local index = 1;
    local function readLine(str)
        local a,b = string.find(str,"\r\n");
        if a then
            local lineStr = string.sub(str,1,a-1);
            readLineFun(lineStr);

            index = index + 1;
            str = string.sub(str,b+1);
            readLine(str);
        else
            readLineFun(str);
        end
    end
    readLine(linesStr);
    return lineMap;
end

return ConfigInfo