cocos2dx 4.x lua环境配置与代码调试

本文介绍了如何在 Cocos2d-x 4.x 版本中配置调试环境,包括使用 VSCode 集成 LocalLuaDebugger 插件。详细步骤涵盖了安装插件、创建启动配置、设置断点等,确保开发者能够顺利进行 Lua 代码的调试。同时,文章还讨论了调试过程中的注意事项,如 lldebugger.lua 文件的创建和调试配置的设置。

版本:
cococ2dx 4.x
调试环境
vscode
插件:
Local Lua Debugger

安装好插件后 如下图,增加一个启动配置,按F5启动调试
里面的路径根据你项目的exe实际位置进行修改
在这里插入图片描述

注意点

在这里插入图片描述
1.如上图,根目录下新建lldebugger.lua
内容如下

local ____modules = {
   
   }
local ____moduleCache = {
   
   }
local ____originalRequire = require
local function include(file)
    if ____moduleCache[file] then
        return ____moduleCache[file]
    end
    if ____modules[file] then
        ____moduleCache[file] = ____modules[file]()
        return ____moduleCache[file]
    else
        if ____originalRequire then
            return ____originalRequire(file)
        else
            error("module '" .. file .. "' not found")
        end
    end
end
____modules = {
   
   
["luafuncs"] = function() local ____exports = {
   
   }
____exports.luaAssert = _G.assert
____exports.luaError = _G.error
____exports.luaCoroutineWrap = coroutine.wrap
____exports.luaDebugTraceback = debug.traceback
____exports.luaCoroutineCreate = coroutine.create
____exports.luaRawLen = rawlen or (function(v)
    local mt = getmetatable(v)
    if (not mt) or (not rawget(mt, "__len")) then
        return #v
    else
        local len = 1
        while rawget(v, len) do
            len = len + 1
        end
        return len - 1
    end
end)
function ____exports.loadLuaString(str, env)
    if setfenv then
        local f, e = loadstring(str, str)
        if f and env then
            setfenv(f, env)
        end
        return unpack({
   
   f, e})
    else
        return load(str, str, "t", env)
    end
end
function ____exports.loadLuaFile(filename, env)
    if setfenv then
        local f, e = loadfile(filename)
        if f and env then
            setfenv(f, env)
        end
        return unpack({
   
   f, e})
    else
        return loadfile(filename, "t", env)
    end
end
function ____exports.luaGetEnv(level, thread)
    local info = (thread and debug.getinfo(thread, level + 1, "f")) or debug.getinfo(level + 1, "f")
    local func = assert(info.func)
    if getfenv then
        return getfenv(func)
    else
        local i = 1
        while true do
            local name, value = debug.getupvalue(func, i)
            if not name then
                break
            end
            if name == "_ENV" then
                return value
            end
            i = i + 1
        end
    end
end
return ____exports
end,
["path"] = function() local ____exports = {
   
   }
local ____luafuncs = include("luafuncs")
local luaAssert = ____luafuncs.luaAssert
____exports.Path = {
   
   }
local Path = ____exports.Path
do
    Path.separator = (function()
        local config = _G.package.config
        if config then
            local sep = config:match("^[^\n]+")
            if sep then
                return sep
            end
        end
        return "/"
    end)()
    local cwd
    function Path.getCwd()
        if not cwd then
            local p = io.popen(((Path.separator == "\\") and "cd") or "pwd")
            if p then
                cwd = p:read("*a"):match("^%s*(.-)%s*$")
            end
            cwd = cwd or ""
        end
        return cwd
    end
    function Path.dirName(path)
        local dir = path:match(
            ((("^(.-)" .. tostring(Path.separator)) .. "+[^") .. tostring(Path.separator)) .. "]+$"
        )
        return dir or "."
    end
    function Path.splitDrive(path)
        local drive, pathPart = path:match("^[@=]?([a-zA-Z]:)[\\/](.*)")
        if drive then
            drive = tostring(
                drive:upper()
            ) .. tostring(Path.separator)
        else
            drive = ''
            pathPart = path:match("^[@=]?[\\/]*(.*)")
        end
        return luaAssert(drive), luaAssert(pathPart)
    end
    local formattedPathCache = {
   
   }
    function Path.format(path)
        local formattedPath = formattedPathCache[path]
        if not formattedPath then
            local drive, pathOnly = Path.splitDrive(path)
            local pathParts = {
   
   }
            for part in luaAssert(pathOnly):gmatch("[^\\/]+") do
                if part ~= "." then
                    if ((part == "..") and (#pathParts > 0)) and (pathParts[#pathParts] ~= "..") then
                        table.remove(pathParts)
                    else
                        table.insert(pathParts, part)
                    end
                end
            end
            formattedPath = tostring(drive) .. tostring(
                table.concat(pathParts, Path.separator)
            )
            formattedPathCache[path] = formattedPath
        end
        return formattedPath
    end
    function Path.isAbsolute(path)
        local drive = Path.splitDrive(path)
        return #drive > 0
    end
    function Path.getAbsolute(path)
        if Path.isAbsolute(path) then
            return Path.format(path)
        end
        return Path.format(
            (tostring(
                Path.getCwd()
            ) .. tostring(Path.separator)) .. tostring(path)
        )
    end
end
return ____exports
end,
["breakpoint"] = function() local ____exports = {
   
   }
local ____path = include("path")
local Path = ____path.Path
____exports.Breakpoint = {
   
   }
local Breakpoint = ____exports.Breakpoint
do
    local current = {
   
   }
    function Breakpoint.get(file, line)
        file = Path.format(file)
        for _, breakpoint in ipairs(current) do
            if (breakpoint.file == file) and (breakpoint.line == line) then
                return breakpoint
            end
        end
        return nil
    end
    function Breakpoint.getAll()
        return current
    end
    function Breakpoint.add(file, line, condition)
        table.insert(
            current,
            {
   
   
                file = Path.format(file),
                line = line,
                enabled = true,
                condition = condition
            }
        )
    end
    function Breakpoint.remove(file, line)
        file = Path.format(file)
        for i, breakpoint in ipairs(current) do
            if (breakpoint.file == file) and (breakpoint.line == line) then
                table.remove(current, i)
                break
            end
        end
    end
    function Breakpoint.clear()
        current = {
   
   }
    end
end
return ____exports
end,
["sourcemap"] = function() local ____exports = {
   
   }
local ____luafuncs = include("luafuncs")
local luaAssert = ____luafuncs.luaAssert
local ____path = include("path")
local Path = ____path.Path
____exports.SourceMap = {
   
   }
local SourceMap = ____exports.SourceMap
do
    local cache = {
   
   }
    local base64Lookup = {
   
   A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7, I = 8, J = 9, K = 10, L = 11, M = 12, N = 13, O = 14, P = 15, Q = 16, R = 17, S = 18, T = 19, U = 20, V = 21, W = 22, X = 23, Y = 24, Z = 25, a = 26, b = 27, c = 28, d = 29, e = 30, f = 31, g = 32, h = 33, i = 34, j = 35, k = 36, l = 37, m = 38, n = 39, o = 40, p = 41, q = 42, r = 43, s = 44, t = 45, u = 46, v = 47, w = 48, x = 49, y = 50, z = 51, ["0"] = 52, ["1"] = 53, ["2"] = 54, ["3"] = 55, ["4"] = 56, ["5"] = 57, ["6"] = 58, ["7"] = 59, ["8"] = 60, ["9"] = 61, ["+"] = 62, ["/"] = 63, ["="] = 0}
    local function base64Decode(input)
        local results = {
   
   }
        local bits = {
   
   }
        for c in input:gmatch(".") do
            local sextet = luaAssert(base64Lookup[c])
            for i = 1, 6 do
                local bit = (sextet % 2) ~= 0
                table.insert(bits, i, bit)
                sextet = math.floor(sextet / 2)
            end
            if #bits >= 8 then
                local value = 0
                for i = 7, 0, -1 do
                    local bit = table.remove(bits)
                    if bit then
                        value = value + (2 ^ i)
                    end
                end
                table.insert(
                    results,
                    string.char(value)
                )
            end
        end
        return table.concat(results)
    end
    local function decodeBase64VLQ(input)
        local values = {
   
   }
        local bits = {
   
   }
        for c in input:gmatch(".") do
            local sextet = luaAssert(base64Lookup[c])
            for _ = 1, 5 do
                local bit = (sextet % 2) ~= 0
                table.insert(bits, bit)
                sextet = math.floor(sextet / 2)
            end
            local continueBit = (sextet % 2) ~= 0
            if not continueBit then
                local value = 0
                for i = 1, #bits - 1 do
                    if bits[i + 1] then
                        value = value + (2 ^ (i - 1))
                    end
                end
                if bits[1] then
                    value = -value
                end
                table.insert(values, value)
                bits = {
   
   }
            end
        end
        return values
    end
    local function build(data, mapDir, luaScript)
        local sources = data:match("\"sources\"%s*:%s*(%b[])")
        local mappings = data:match("\"mappings\"%s*:%s*\"([^\"]+)\"")
        if (not mappings) or (not sources) then
            return nil
        end
        local sourceMap = {
   
   sources = {
   
   }, sourceNames = {
   
   }, luaNames = {
   
   }, hasMappedNames = false}
        local sourceRoot = data:match("\"sourceRoot\"%s*:%s*\"([^\"]+)\"")
        if (sourceRoot == nil) or (#sourceRoot == 0) then
            sourceRoot = "."
        end
        for source in sources:gmatch("\"([^\"]+)\"") do
            local sourcePath = (((tostring(mapDir) .. tostring(Path.separator)) .. tostring(sourceRoot)) .. tostring(Path.separator)) .. tostring(source)
            table.insert(
                sourceMap.sources,
                Path.getAbsolute(sourcePath)
            )
        end
        local names = data:match("\"names\"%s*:%s*(%b[])")
        local nameList
        if names then
            nameList = {
   
   }
            for name in names:gmatch("\"([^\"]+)\"") do
                table.insert(nameList, name)
            end
        end
        local luaLines
        local line = 1
        local column = 1
        local sourceIndex = 0
        local sourceLine = 1
        local sourceColumn = 1
        local nameIndex = 0
        for mapping, separator in mappings
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值