Unity builds the debug environment of xlua and emmy_lua

configuration steps

1 environment

1.1 vscode install emmy_lua

1.2 Install the corresponding lua version

1.3 Install java8 and configure the environment

1.4 Download the 64-bit version of emmy_lua from the github of emmy_lua, decompress it and put it in the project directory client\Tools\EmmyLua\

Download address: https://github.com/EmmyLua/EmmyLuaDebugger/releases icon-default.png?t=M85Bhttps://github.com/EmmyLua/EmmyLuaDebugger/releases

The structure is as follows

client

——Assets

——Tools\EmmyLua

——EasyHook.dll

——emmy_core.dll

——emmy_hook.dll

2 Execute the following code when the program starts

local function split(line, sep, maxsplit)
    if string.len(line) == 0 then
        return {}
    end
    sep = sep or " "
    maxsplit = maxsplit or 0
    local retval = {}
    local pos = 1
    local step = 0
    while true do
        local from, to = string.find(line, sep, pos, true)
        step = step + 1
        if (maxsplit ~= 0 and step > maxsplit) or not from then
            local item = string.sub(line, pos)
            table.insert(retval, item)
            break
        else
            local item = string.sub(line, pos, from - 1)
            table.insert(retval, item)
            pos = to + 1
        end
    end
    return retval
end

--连接EmmyLua
local function connectEmmyLua()
    local func = function()
        local assets = CS.UnityEngine.Application.dataPath
        local assetDict = split(assets, "/")
        local path = ''
        for i = 1, #assetDict-1 do
            path = path .. assetDict[i] .. '/'
        end
        package.cpath = package.cpath .. ';' .. path .. 'Tools/EmmyLua/emmy_core.dll'
        local dbg = require('emmy_core')
        dbg.tcpConnect('localhost', 9966)
    end
 
    local handle = function(error)
        print('IDE没有开启调试', error)
    end
 
    xpcall(func, handle)
end

3 Configure the launch.json used by debug

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "emmylua_new",
            "request": "launch",
            "name": "EmmyLua New Debug",
            "host": "localhost",
            "port": 9966,
            "ext": [
                ".lua",
                ".lua.txt",
                ".lua.bytes",
                ".txt"
            ],
            "ideConnectDebugger": false
        },
    ]
}

4 If it is xlua, modify the custom LuaLoader

There is a pit here, such as require("game.xxx.XX") in lua

In the custom loader, public byte[] CustomLoader(ref string luaPath)

luaPath is game.xxx.XX

If you don’t do anything to this luaPath, the emmylua breakpoint will not take effect, and no error will be reported, and nothing will happen. Simply check the source code of the emmylua plug-in, but no code related to the lua source code can be found

Later, I used the breakHere method to forcibly enter a lua breakpoint, but the error "could not load the source" was reported, and the source code could not be seen.

local dbg = require('emmy_core')
dbg.tcpConnect('localhost', 8866)
dbg.breakHere()

I guess the path passed to emmylua from xlua is wrong?

If you want emmylua to find the file after a breakpoint, you need to reassign this luaPath to the real file path

luaPath = GetRealPath(luaPath)

Solve the problem

5 vscode starts debug

6 Run the game after setting a breakpoint

References

VSCode uses EmmyLua to debug Lua code_HKW_hankangwen's blog-CSDN blog_emmylua vscode

Guess you like

Origin blog.csdn.net/ak47007tiger/article/details/127869894