美文网首页
Lua语法 dump()函数

Lua语法 dump()函数

作者: 白小白152 | 来源:发表于2020-12-16 17:54 被阅读0次

    cocos2d-x 3.4版本lua新增了方法dump(),
    dump()为输出堆栈方法,可以方便的打印表table结构和对象内容,
    如果使用低于3.4的版本,可以载入导入以下代码,自定义dump()函数

    代码

    function dump(value, description, nesting)
        if type(nesting) ~= "number" then nesting = 3 end
    
        local lookupTable = {}
        local result = {}
    
        local traceback = string.split(debug.traceback("", 2), "\n")
        print("dump from: " .. string.trim(traceback[3]))
    
        local function dump_(value, description, indent, nest, keylen)
            description = description or "<var>"
            local spc = ""
            if type(keylen) == "number" then
                spc = string.rep(" ", keylen - string.len(dump_value_(description)))
            end
            if type(value) ~= "table" then
                result[#result +1 ] = string.format("%s%s%s = %s", indent, dump_value_(description), spc, dump_value_(value))
            elseif lookupTable[tostring(value)] then
                result[#result +1 ] = string.format("%s%s%s = *REF*", indent, dump_value_(description), spc)
            else
                lookupTable[tostring(value)] = true
                if nest > nesting then
                    result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, dump_value_(description))
                else
                    result[#result +1 ] = string.format("%s%s = {", indent, dump_value_(description))
                    local indent2 = indent.."    "
                    local keys = {}
                    local keylen = 0
                    local values = {}
                    for k, v in pairs(value) do
                        keys[#keys + 1] = k
                        local vk = dump_value_(k)
                        local vkl = string.len(vk)
                        if vkl > keylen then keylen = vkl end
                        values[k] = v
                    end
                    table.sort(keys, function(a, b)
                        if type(a) == "number" and type(b) == "number" then
                            return a < b
                        else
                            return tostring(a) < tostring(b)
                        end
                    end)
                    for i, k in ipairs(keys) do
                        dump_(values[k], k, indent2, nest + 1, keylen)
                    end
                    result[#result +1] = string.format("%s}", indent)
                end
            end
        end
        dump_(value, description, "- ", 1)
    
        for i, line in ipairs(result) do
            print(line)
        end
    end
    
    -- input:要分割的字符串
    -- delimiter:分隔符
    function string.split(input, delimiter)
        input = tostring(input)
        delimiter = tostring(delimiter)
        if (delimiter=='') then return false end
        local pos,arr = 0, {}
        -- for each divider found
        for st,sp in function() return string.find(input, delimiter, pos, true) end do
            table.insert(arr, string.sub(input, pos, st - 1))
            pos = sp + 1
        end
        table.insert(arr, string.sub(input, pos))
        return arr
    end
    
    -- 删除字符串两端的空白字符
    function string.trim(input)
        input = string.gsub(input, "^[ \t\n\r]+", "")
        return string.gsub(input, "[ \t\n\r]+$", "")
    end
    

    示例:

    dump(self.keywords_info,"keywords_info=====")
    输出结果为:
    "keywords_info=====" = {
    [LUA-print] -     1 = {
    [LUA-print] -         "audio_cn"             = "scenes/story/2014/content/audio/word/chs/word_01.mp3"
    [LUA-print] -         "audio_en"             = "scenes/story/2014/content/audio/word/eng/word_01.mp3"
    [LUA-print] -         "audio_us"             = "scenes/story/2014/content/audio/word/us/word_01.mp3"
    [LUA-print] -         "image_csb"            = "scenes/story/2014/content/game/option_huaban.csb"
    [LUA-print] -         "image_parent_node"    = userdata: 0x011cd0f318
    [LUA-print] -         "target_area"          = userdata: 0x01206e7c00
    [LUA-print] -         "target_area_is_empty" = true
    [LUA-print] -         "word"                 = "skateboard"
    [LUA-print] -         "word_id"              = 1
    [LUA-print] -     }
    [LUA-print] -     2 = {
    [LUA-print] -         "audio_cn"             = "scenes/story/2014/content/audio/word/chs/word_02.mp3"
    [LUA-print] -         "audio_en"             = "scenes/story/2014/content/audio/word/eng/word_02.mp3"
    [LUA-print] -         "audio_us"             = "scenes/story/2014/content/audio/word/us/word_02.mp3"
    [LUA-print] -         "image_csb"            = "scenes/story/2014/content/game/option_qiuqian.csb"
    [LUA-print] -         "image_parent_node"    = userdata: 0x011cd0f3d8
    [LUA-print] -         "target_area"          = userdata: 0x01206e7b10
    [LUA-print] -         "target_area_is_empty" = true
    [LUA-print] -         "word"                 = "swing"
    [LUA-print] -         "word_id"              = 2
    [LUA-print] -     }
    [LUA-print] - }
    

    相关文章

      网友评论

          本文标题:Lua语法 dump()函数

          本文链接:https://www.haomeiwen.com/subject/eplogktx.html