-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunity.lua
More file actions
109 lines (97 loc) · 3.14 KB
/
unity.lua
File metadata and controls
109 lines (97 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---@diagnostic disable: undefined-field
--[[DAP configuration that allows Neovim DAP client to attach to a running
Unity Player instance.]]
local dap = require("dap")
local function try_get_unity_editor_ip_port()
local port = ""
local ip = ""
---@type string?
local editor_log_fp = nil
if vim.loop.os_uname().sysname == "Linux" then
editor_log_fp = vim.fn.expand("~/.config/unity3d/Editor.log")
elseif vim.uv.os_uname().version:match("Windows") then
local LOCALAPPDATA = os.getenv("LOCALAPPDATA")
if LOCALAPPDATA ~= nil then
editor_log_fp = vim.fn.resolve(LOCALAPPDATA .. "/Unity/Editor/Editor.log")
end
else
editor_log_fp = vim.fn.expand("~/Library/Logs/Unity/Editor.log")
end
-- this means that currently no Unity Editor instance is running
if editor_log_fp == nil or not vim.fn.filereadable(editor_log_fp) then
return ip, port
end
-- we are looking for a line like this:
-- Using monoOptions --debugger-agent=transport=dt_socket,embedding=1,server=y,suspend=n,address=127.0.0.1:56183
for l in io.lines(editor_log_fp) do
local _ip, _port = string.match(
l,
"^Using monoOptions.*address=(%d+%.%d+%.%d+%.%d+):(%d%d%d%d%d)"
)
if _ip ~= nil and _port ~= nil then
ip = _ip
port = _port
break
end
end
return ip, port
end
dap.adapters.unity = function(cb, config)
local cb_arg = {
type = "executable",
-- adjust unity-debug-adapter.exe path (don't forget to `chmod +x` it)
-- get Unity debug adapter from: https://github.com/walcht/unity-dap
command = "unity-debug-adapter.exe",
args = {
-- optional log level argument: trace | debug | info | warn | error | critical | none
"--log-level=warn",
-- optional path to log file (logs to stderr in case this is not provided)
-- "--log-file=<path_to_log_file_txt>",
},
}
if config.port ~= nil and config.address ~= nil then
cb(cb_arg)
return
end
-- when connecting to a running Unity Editor, the TCP address of the listening connection is
-- usually localhost (i.e., 127.0.0.1)
vim.ui.input(
{ prompt = "address [127.0.0.1]: ", default = "127.0.0.1" },
function(result)
config.address = result
end
)
vim.ui.input({ prompt = "port [56---]: " }, function(result)
config.port = tonumber(result)
end)
cb(cb_arg)
end
-- make sure not to override other C# DAP configurations
if dap.configurations.cs == nil then
dap.configurations.cs = {}
end
dap.providers.configs["unity_editor_config"] = function()
local configs = {}
local ip, port = try_get_unity_editor_ip_port()
if ip and port then
table.insert(configs, {
name = string.format(
"Connect to Unity Editor instance at %s:%s",
ip,
port
),
type = "unity",
-- the debuggee is Unity which is not our responsibility to launch
-- (i.e., it should already be running and we should just attach to it)
request = "attach",
address = ip,
port = port,
})
end
table.insert(configs, {
name = "Manually enter IP:PORT for Unity Editor/Player instance [Mono]",
type = "unity",
request = "attach",
})
return configs
end