-
Notifications
You must be signed in to change notification settings - Fork 10
/
commands.lua
991 lines (823 loc) · 30.4 KB
/
commands.lua
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
local kConfigFileName = "UnrealNvim.json"
local kCurrentVersion = "0.0.2"
local kLogLevel_Error = 1
local kLogLevel_Warning = 2
local kLogLevel_Log = 3
local kLogLevel_Verbose = 4
local kLogLevel_VeryVerbose = 5
local TaskState =
{
scheduled = "scheduled",
inprogress = "inprogress",
completed = "completed"
}
-- fix false diagnostic about vim
if not vim then
vim = {}
end
local logFilePath = vim.fn.stdpath("data") .. '/unrealnvim.log'
local function logWithVerbosity(verbosity, message)
if not vim.g.unrealnvim_debug then return end
local cfgVerbosity = kLogLevel_Log
if vim.g.unrealnvim_loglevel then
cfgVerbosity = vim.g.unrealnvim_loglevel
end
if verbosity > cfgVerbosity then return end
local file = nil
if Commands.logFile then
file = Commands.logFile
else
file = io.open(logFilePath, "a")
end
if file then
local time = os.date('%m/%d/%y %H:%M:%S');
file:write("["..time .. "]["..verbosity.."]: " .. message .. '\n')
end
end
local function log(message)
if not message then
logWithVerbosity(kLogLevel_Error, "message was nill")
return
end
logWithVerbosity(kLogLevel_Log, message)
end
local function logError(message)
logWithVerbosity(kLogLevel_Error, message)
end
local function PrintAndLogMessage(a,b)
if a and b then
log(tostring(a)..tostring(b))
elseif a then
log(tostring(a))
end
end
local function PrintAndLogError(a,b)
if a and b then
local msg = "Error: "..tostring(a)..tostring(b)
print(msg)
log(msg)
elseif a then
local msg = "Error: ".. tostring(a)
print(msg)
log(msg)
end
end
local function MakeUnixPath(win_path)
if not win_path then
logError("MakeUnixPath received a nil argument")
return;
end
-- Convert backslashes to forward slashes
local unix_path = win_path:gsub("\\", "/")
-- Remove duplicate slashes
unix_path = unix_path:gsub("//+", "/")
return unix_path
end
local function FuncBind(func, data)
return function()
func(data)
end
end
if not vim.g.unrealnvim_loaded then
Commands = {}
CurrentGenData =
{
config = {},
target = nil,
prjName = nil,
targetNameSuffix = nil,
prjDir = nil,
tasks = {},
currentTask = "",
ubtPath = "",
ueBuildBat = "",
projectPath = "",
logFile = nil
}
-- clear the log
CurrentGenData.logFile = io.open(logFilePath, "w")
if CurrentGenData.logFile then
CurrentGenData.logFile:write("")
CurrentGenData.logFile:close()
CurrentGenData.logFile = io.open(logFilePath, "a")
end
vim.g.unrealnvim_loaded = true
end
Commands.LogLevel_Error = kLogLevel_Error
Commands.LogLevel_Warning = kLogLevel_Warning
Commands.LogLevel_Log = kLogLevel_Log
Commands.LogLevel_Verbose = kLogLevel_Verbose
Commands.LogLevel_VeryVerbose = kLogLevel_VeryVerbose
function Commands.Log(msg)
PrintAndLogError(msg)
end
Commands.onStatusUpdate = function()
end
function Commands:Inspect(objToInspect)
if not vim.g.unrealnvim_debug then return end
if not objToInspect then
log(objToInspect)
return
end
if not self._inspect then
local inspect_path = vim.fn.stdpath("data") .. "/site/pack/packer/start/inspect.lua/inspect.lua"
self._inspect = loadfile(inspect_path)(Commands._inspect)
if self._inspect then
log("Inspect loaded.")
else
logError("Inspect failed to load from path" .. inspect_path)
end
if self._inspect.inspect then
log("inspect method exists")
else
logError("inspect method doesn't exist")
end
end
return self._inspect.inspect(objToInspect)
end
function SplitString(str)
-- Split a string into lines
local lines = {}
for line in string.gmatch(str, "[^\r\n]+") do
table.insert(lines, line)
end
return lines
end
function Commands._CreateConfigFile(configFilePath, projectName)
local configContents = [[
{
"version" : "0.0.2",
"_comment": "dont forget to escape backslashes in EnginePath",
"EngineDir": "",
"Targets": [
{
"TargetName" : "]] .. projectName .. [[-Editor",
"Configuration" : "DebugGame",
"withEditor" : true,
"UbtExtraFlags" : "",
"PlatformName" : "Win64"
},
{
"TargetName" : "]] .. projectName .. [[",
"Configuration" : "DebugGame",
"withEditor" : false,
"UbtExtraFlags" : "",
"PlatformName" : "Win64"
},
{
"TargetName" : "]] .. projectName .. [[-Editor",
"Configuration" : "Development",
"withEditor" : true,
"UbtExtraFlags" : "",
"PlatformName" : "Win64"
},
{
"TargetName" : "]] .. projectName .. [[",
"Configuration" : "Development",
"withEditor" : false,
"UbtExtraFlags" : "",
"PlatformName" : "Win64"
},
{
"TargetName" : "]] .. projectName .. [[-Editor",
"Configuration" : "Shipping",
"withEditor" : true,
"UbtExtraFlags" : "",
"PlatformName" : "Win64"
},
{
"TargetName" : "]] .. projectName .. [[",
"Configuration" : "Shipping",
"withEditor" : false,
"UbtExtraFlags" : "",
"PlatformName" : "Win64"
}
]
}
]]
-- local file = io.open(configFilePath, "w")
-- file:write(configContents)
-- file:close()
PrintAndLogMessage("Please populate the configuration for the Unreal project, especially EnginePath, the path to the Unreal Engine")
-- local buf = vim.api.nvim_create_buf(false, true)
vim.cmd('new ' .. configFilePath)
vim.cmd('setlocal buftype=')
-- vim.api.nvim_buf_set_name(0, configFilePath)
vim.api.nvim_buf_set_lines(0, 0, -1, false, SplitString(configContents))
-- vim.api.nvim_open_win(buf, true, {relative="win", height=20, width=80, row=1, col=0})
end
function Commands._EnsureConfigFile(projectRootDir, projectName)
local configFilePath = projectRootDir.."/".. kConfigFileName
local configFile = io.open(configFilePath, "r")
if (not configFile) then
Commands._CreateConfigFile(configFilePath, projectName)
PrintAndLogMessage("created config file")
return nil
end
local content = configFile:read("*all")
configFile:close()
local data = vim.fn.json_decode(content)
Commands:Inspect(data)
if data and (data.version ~= kCurrentVersion) then
PrintAndLogError("Your " .. configFilePath .. " format is incompatible. Please back up this file somewhere and then delete this one, you will be asked to create a new one")
data = nil
end
if data then
data.EngineDir = MakeUnixPath(data.EngineDir)
end
return data
end
function Commands._GetDefaultProjectNameAndDir(filepath)
local uprojectPath, projectDir
projectDir, uprojectPath = Commands._find_file_with_extension(filepath, "uproject")
if not uprojectPath then
PrintAndLogMessage("Failed to determine project name, could not find the root of the project that contains the .uproject")
return nil, nil
end
local projectName = vim.fn.fnamemodify(uprojectPath, ":t:r")
return projectName, projectDir
end
local CurrentCompileCommandsTargetFilePath = ""
function CurrentGenData:GetTaskAndStatus()
if not self or not self.currentTask or self.currentTask == "" then
return "[No Task]"
end
local status = self:GetTaskStatus(self.currentTask)
return self.currentTask.."->".. status
end
function CurrentGenData:GetTaskStatus(taskName)
local status = self.tasks[taskName]
if not status then
status = "none"
end
return status
end
function CurrentGenData:SetTaskStatus(taskName, newStatus)
if (self.currentTask ~= "" and self.currentTask ~= taskName) and (self:GetTaskStatus(self.currentTask) ~= TaskState.completed) then
PrintAndLogMessage("Cannot start a new task. Current task still in progress " .. self.currentTask)
PrintAndLogError("Cannot start a new task. Current task still in progress " .. self.currentTask)
return
end
PrintAndLogMessage("SetTaskStatus: " .. taskName .. "->" .. newStatus)
self.currentTask = taskName
self.tasks[taskName] = newStatus
end
function CurrentGenData:ClearTasks()
self.tasks = {}
self.currentTask = ""
end
function ExtractRSP(rsppath)
local extraFlags = "-std=c++20 -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -ferror-limit=0 -Wno-inconsistent-missing-override"
local extraIncludes = {
"Engine/Source/Runtime/CoreUObject/Public/UObject/ObjectMacros.h",
"Engine/Source/Runtime/Core/Public/Misc/EnumRange.h"
}
rsppath = rsppath:gsub("\\\\","/")
PrintAndLogMessage(rsppath)
local lines = {}
local isFirstLine = true
local lineNb = 0;
for line in io.lines(rsppath) do
local discardLine = true
-- ignored lines
if line:find("^/FI") then discardLine = false end
if line:find("^/I") then discardLine = false end
if line:find("^-W") then discardLine = false end
line = line:gsub("^/FI", "-include ")
line = line:gsub("^(/I )(.*)", "-I \"%2\"")
if isFirstLine then
discardLine = false
end
if not discardLine then
lines[lineNb] = line .. "\n"
lineNb = lineNb + 1
end
isFirstLine = false
end
for _, incl in ipairs(extraIncludes) do
lines[lineNb] ="\n" .. "-include \"" .. CurrentGenData.config.EngineDir .. "/" .. incl .. "\""
lineNb = lineNb + 1
end
lines[lineNb] = "\n" .. extraFlags
lineNb = lineNb + 1
--table.insert(lines, "\n\"" .. currentFilename .. "\"")
return table.concat(lines)
end
function CreateCommandLine()
end
function EscapePath(path)
-- path = path:gsub("\\", "\\\\")
path = path:gsub("\\\\", "/")
path = path:gsub("\\", "/")
path = path:gsub("\"", "\\\"")
return path
end
function EnsureDirPath(path)
PrintAndLogMessage("Ensuring path exists: "..path)
-- os.execute("mkdir -p " .. path)
local handle = io.popen("cmd.exe /c mkdir \"" .. path.. "\"")
handle:flush()
local result = handle:read("*a")
handle:close()
end
local function IsEngineFile(path, start)
local unixPath = MakeUnixPath(path)
local unixStart = MakeUnixPath(start)
local startIndex, _ = string.find(unixPath, unixStart, 1, true)
return startIndex ~= nil
end
local function IsQuickfixWin(winid)
if not vim.api.nvim_win_is_valid(winid) then return false end
local bufnr = vim.api.nvim_win_get_buf(winid)
local buftype = vim.api.nvim_buf_get_option(bufnr, 'buftype')
return buftype == 'quickfix'
end
local function GetQuickfixWinId()
local quickfix_winid = nil
for _, winid in ipairs(vim.api.nvim_list_wins()) do
if IsQuickfixWin(winid) then
quickfix_winid = winid
break
end
end
return quickfix_winid
end
Commands.QuickfixWinId = 0
local function ScrollQF()
if not IsQuickfixWin(Commands.QuickfixWinId) then
Commands.QuickfixWinId = GetQuickfixWinId()
end
local qf_list = vim.fn.getqflist()
local last_line = #qf_list
if last_line > 0 then
vim.api.nvim_win_set_cursor(Commands.QuickfixWinId, {last_line, 0})
end
end
local function AppendToQF(entry)
vim.fn.setqflist({}, 'a', { items = { entry } })
ScrollQF()
end
local function DeleteAutocmd(AutocmdId)
local success, _ = pcall(function()
vim.api.nvim_del_autocmd(AutocmdId)
end)
end
function Stage_UbtGenCmd()
coroutine.yield()
Commands.BeginTask("gencmd")
PrintAndLogMessage("callback called!")
local outputJsonPath = CurrentGenData.config.EngineDir .. "/compile_commands.json"
local rspdir = CurrentGenData.prjDir .. "/Intermediate/clangRsp/" ..
CurrentGenData.target.PlatformName .. "/"..
CurrentGenData.target.Configuration .. "/"
-- all these replaces are slow, could be rewritten as a parser
EnsureDirPath(rspdir)
-- replace bad compiler
local file_path = outputJsonPath
local old_text = "Llvm\\\\x64\\\\bin\\\\clang%-cl%.exe"
local new_text = "Llvm/x64/bin/clang++.exe"
local contentLines = {}
PrintAndLogMessage("processing compile_commands.json and writing response files")
PrintAndLogMessage(file_path)
local skipEngineFiles = true
if CurrentGenData.WithEngine then
skipEngineFiles = false
end
local qflistentry = {text = "Preparing files for parsing." }
if not skipEngineFiles then
qflistentry.text = qflistentry.text .. " Engine source files included, process will take longer"
end
AppendToQF(qflistentry)
local currentFilename = ""
for line in io.lines(file_path) do
local i,j = line:find("\"command")
if i then
coroutine.yield()
-- show progress
logWithVerbosity(kLogLevel_Verbose, "Preparing for LSP symbol parsing: " .. currentFilename)
local isEngineFile = IsEngineFile(currentFilename, CurrentGenData.config.EngineDir)
local shouldSkipFile = isEngineFile and skipEngineFiles
local qflistentry = {filename = "", lnum = 0, col = 0,
text = currentFilename}
if not shouldSkipFile then
AppendToQF(qflistentry)
end
line = line:gsub(old_text, new_text)
-- content = content .. "matched:\n"
i,j = line:find("%@")
if i then
local _,endpos = line:find("\"", j)
local rsppath = line:sub(j+1, endpos-1)
if rsppath then
local newrsppath = rsppath .. ".clang.rsp"
-- rewrite rsp contents
if not shouldSkipFile then
local rspfile = io.open(newrsppath, "w")
local rspcontent = ExtractRSP(rsppath)
rspfile:write(rspcontent)
rspfile:close()
end
coroutine.yield()
table.insert(contentLines, "\t\t\"command\": \"clang++.exe @\\\"" ..newrsppath .."\\\"\",\n")
end
else
-- it's not an rsp command, the flags will be clang compatible
-- for some reason they're only incompatible flags inside
-- rsps. keep line as is
local _, endArgsPos = line:find("%.exe\\\"")
local args = line:sub(endArgsPos+1, -1)
local rspfilename = currentFilename:gsub("\\\\","/")
rspfilename = rspfilename:gsub(":","")
rspfilename = rspfilename:gsub("\"","")
rspfilename = rspfilename:gsub(",","")
rspfilename = rspfilename:gsub("\\","/")
rspfilename = rspfilename:gsub("/","_")
rspfilename = rspfilename .. ".rsp"
local rspfilepath = rspdir .. rspfilename
if not shouldSkipFile then
PrintAndLogMessage("Writing rsp: " .. rspfilepath)
args = args:gsub("-D\\\"", "-D\"")
args = args:gsub("-I\\\"", "-I\"")
args = args:gsub("\\\"\\\"\\\"", "__3Q_PLACEHOLDER__")
args = args:gsub("\\\"\\\"", "\\\"\"")
args = args:gsub("\\\" ", "\" ")
args = args:gsub("\\\\", "/")
args = args:gsub(",%s*$", "") -- remove trailing comma and spaces
args = args:gsub("\" ", "\"\n") -- one arg per line
args = args:gsub("__3Q_PLACEHOLDER__", "\\\"\\\"\"")
args = args:gsub("\n[^\n]*$", "")
local rspfile = io.open(rspfilepath, "w")
rspfile:write(args)
rspfile:close()
end
coroutine.yield()
table.insert(contentLines, "\t\t\"command\": \"clang++.exe @\\\"" .. EscapePath(rspfilepath) .."\\\""
.. " ".. EscapePath(currentFilename) .."\",\n")
end
else
local fbegin, fend = line:find("\"file\": ")
if fbegin then
currentFilename = line:sub(fend+1, -2)
logWithVerbosity(kLogLevel_Verbose, "currentfile: " .. currentFilename)
end
table.insert(contentLines, line .. "\n")
end
::continue::
end
local file = io.open(CurrentCompileCommandsTargetFilePath, "w")
file:write(table.concat(contentLines))
file:flush()
file:close()
PrintAndLogMessage("finished processing compile_commands.json")
PrintAndLogMessage("generating header files with Unreal Header Tool...")
Commands.EndTask("gencmd")
DeleteAutocmd(Commands.gencmdAutocmdid)
Commands.ScheduleTask("headers")
Commands.BeginTask("headers")
Commands.headersAutocmdid = vim.api.nvim_create_autocmd("ShellCmdPost",{
pattern = "*",
callback = FuncBind(DispatchUnrealnvimCb, "headers")
})
local cmd = CurrentGenData.ubtPath .. " -project=" ..
CurrentGenData.projectPath .. " " .. CurrentGenData.target.UbtExtraFlags .. " " ..
CurrentGenData.prjName .. CurrentGenData.targetNameSuffix .. " " .. CurrentGenData.target.Configuration .. " " ..
CurrentGenData.target.PlatformName .. " -headers"
vim.cmd("compiler msvc")
vim.cmd("Dispatch " .. cmd)
end
function Stage_GenHeadersCompleted()
PrintAndLogMessage("Finished generating header files with Unreal Header Tool...")
vim.api.nvim_command('autocmd! ShellCmdPost * lua DispatchUnrealnvimCb()')
vim.api.nvim_command('LspRestart')
Commands.EndTask("headers")
Commands.EndTask("final")
Commands:SetCurrentAnimation("kirbyIdle")
DeleteAutocmd(Commands.headersAutocmdid)
end
Commands.renderedAnim = ""
function Commands.GetStatusBar()
local status = "unset"
if CurrentGenData:GetTaskStatus("final") == TaskState.completed then
status = Commands.renderedAnim .. " Build completed!"
elseif CurrentGenData.currentTask ~= "" then
status = Commands.renderedAnim .. " Building... Step: " .. CurrentGenData.currentTask .. "->".. CurrentGenData:GetTaskStatus(CurrentGenData.currentTask)
else
status = Commands.renderedAnim .. " Idle"
end
return status
end
function DispatchUnrealnvimCb(data)
log("DispatchUnrealnvimCb()")
Commands.taskCoroutine = coroutine.create(FuncBind(DispatchCallbackCoroutine, data))
end
function DispatchCallbackCoroutine(data)
coroutine.yield()
if not data then
log("data was nil")
end
PrintAndLogMessage("DispatchCallbackCoroutine()")
PrintAndLogMessage("DispatchCallbackCoroutine() task="..CurrentGenData:GetTaskAndStatus())
if data == "gencmd" and CurrentGenData:GetTaskStatus("gencmd") == TaskState.scheduled then
CurrentGenData:SetTaskStatus("gencmd", TaskState.inprogress)
Commands.taskCoroutine = coroutine.create(Stage_UbtGenCmd)
elseif data == "headers" and CurrentGenData:GetTaskStatus("headers") == TaskState.inprogress then
Commands.taskCoroutine = coroutine.create(Stage_GenHeadersCompleted)
end
end
function PromptBuildTargetIndex()
print("target to build:")
for i, x in ipairs(CurrentGenData.config.Targets) do
local configName = x.Configuration
if x.withEditor then
configName = configName .. "-Editor"
end
print(tostring(i) .. ". " .. configName)
end
return tonumber(vim.fn.input "<number> : ")
end
function Commands.GetProjectName()
local current_file_path = vim.api.nvim_buf_get_name(0)
local prjName, _ = Commands._GetDefaultProjectNameAndDir(current_file_path)
if not prjName then
return "" --"<Unknown.uproject>"
end
return CurrentGenData.prjName .. ".uproject"
end
function InitializeCurrentGenData()
PrintAndLogMessage("initializing")
local current_file_path = vim.api.nvim_buf_get_name(0)
CurrentGenData.prjName, CurrentGenData.prjDir = Commands._GetDefaultProjectNameAndDir(current_file_path)
if not CurrentGenData.prjName then
PrintAndLogMessage("could not find project. aborting")
return false
end
CurrentGenData.config = Commands._EnsureConfigFile(CurrentGenData.prjDir,
CurrentGenData.prjName)
if not CurrentGenData.config then
PrintAndLogMessage("no config file. aborting")
return false
end
CurrentGenData.ubtPath = "\"" .. CurrentGenData.config.EngineDir .."/Engine/Binaries/DotNET/UnrealBuildTool/UnrealBuildTool.exe\""
CurrentGenData.ueBuildBat = "\"" .. CurrentGenData.config.EngineDir .."/Engine/Build/BatchFiles/Build.bat\""
CurrentGenData.projectPath = "\"" .. CurrentGenData.prjDir .. "/" ..
CurrentGenData.prjName .. ".uproject\""
local desiredTargetIndex = PromptBuildTargetIndex()
CurrentGenData.target = CurrentGenData.config.Targets[desiredTargetIndex]
CurrentGenData.targetNameSuffix = ""
if CurrentGenData.target.withEditor then
CurrentGenData.targetNameSuffix = "Editor"
end
PrintAndLogMessage("Using engine at:"..CurrentGenData.config.EngineDir)
return true
end
function Commands.ScheduleTask(taskName)
PrintAndLogMessage("ScheduleTask: " .. taskName)
CurrentGenData:SetTaskStatus(taskName, TaskState.scheduled)
end
function Commands.ClearTasks()
CurrentGenData:ClearTasks()
end
function Commands.BeginTask(taskName)
PrintAndLogMessage("BeginTask: " .. taskName)
CurrentGenData:SetTaskStatus(taskName, TaskState.inprogress)
end
function Commands.EndTask(taskName)
PrintAndLogMessage("EndTask: " .. taskName)
CurrentGenData:SetTaskStatus(taskName, TaskState.completed)
Commands.taskCoroutine = nil
end
function BuildComplete()
Commands.EndTask("build")
Commands.EndTask("final")
Commands:SetCurrentAnimation("kirbyIdle")
DeleteAutocmd(Commands.buildAutocmdid)
end
function Commands.BuildCoroutine()
Commands.buildAutocmdid = vim.api.nvim_create_autocmd("ShellCmdPost",
{
pattern = "*",
callback = BuildComplete
})
local cmd = CurrentGenData.ueBuildBat .. " " .. CurrentGenData.prjName ..
CurrentGenData.targetNameSuffix .. " " ..
CurrentGenData.target.PlatformName .. " " ..
CurrentGenData.target.Configuration .. " " ..
CurrentGenData.projectPath .. " -waitmutex"
vim.cmd("compiler msvc")
vim.cmd("Dispatch " .. cmd)
end
function Commands.build(opts)
CurrentGenData:ClearTasks()
PrintAndLogMessage("Building uproject")
if not InitializeCurrentGenData() then
return
end
Commands.EnsureUpdateStarted();
Commands.ScheduleTask("build")
Commands:SetCurrentAnimation("kirbyFlip")
Commands.taskCoroutine = coroutine.create(Commands.BuildCoroutine)
end
function Commands.run(opts)
CurrentGenData:ClearTasks()
PrintAndLogMessage("Running uproject")
if not InitializeCurrentGenData() then
return
end
Commands.ScheduleTask("run")
local cmd = ""
if CurrentGenData.target.withEditor then
local editorSuffix = ""
if CurrentGenData.target.Configuration ~= "Development" then
editorSuffix = "-" .. CurrentGenData.target.PlatformName .. "-" ..
CurrentGenData.target.Configuration
end
local executablePath = "\"".. CurrentGenData.config.EngineDir .. "/Engine/Binaries/" ..
CurrentGenData.target.PlatformName .. "/UnrealEditor" .. editorSuffix .. ".exe\""
cmd = executablePath .. " " ..
CurrentGenData.projectPath .. " -skipcompile"
else
local exeSuffix = ""
if CurrentGenData.target.Configuration ~= "Development" then
exeSuffix = "-" .. CurrentGenData.target.PlatformName .. "-" ..
CurrentGenData.target.Configuration
end
local executablePath = "\"".. CurrentGenData.prjDir .. "/Binaries/" ..
CurrentGenData.target.PlatformName .. "/" .. CurrentGenData.prjName .. exeSuffix .. ".exe\""
cmd = executablePath
end
PrintAndLogMessage(cmd)
vim.cmd("compiler msvc")
vim.cmd("Dispatch " .. cmd)
Commands.EndTask("run")
Commands.EndTask("final")
end
function Commands.EnsureUpdateStarted()
if Commands.cbTimer then return end
Commands.lastUpdateTime = vim.loop.now()
Commands.updateTimer = 0
-- UI update loop
Commands.cbTimer = vim.loop.new_timer()
Commands.cbTimer:start(1,30, vim.schedule_wrap(Commands.safeUpdateLoop))
-- coroutine update loop
vim.schedule(Commands.safeLogicUpdate)
end
function Commands.generateCommands(opts)
log(Commands.Inspect(opts))
if not InitializeCurrentGenData() then
PrintAndLogMessage("init failed")
return
end
if opts.WithEngine then
CurrentGenData.WithEngine = true
end
-- vim.api.nvim_command('autocmd ShellCmdPost * lua DispatchUnrealnvimCb()')
Commands.gencmdAutocmdid = vim.api.nvim_create_autocmd("ShellCmdPost",
{
pattern = "*",
callback = FuncBind(DispatchUnrealnvimCb, "gencmd")
})
PrintAndLogMessage("listening to ShellCmdPost")
--vim.cmd("compiler msvc")
PrintAndLogMessage("compiler set to msvc")
Commands.taskCoroutine = coroutine.create(Commands.generateCommandsCoroutine)
Commands.EnsureUpdateStarted()
end
function Commands.updateLoop()
local elapsedTime = vim.loop.now() - Commands.lastUpdateTime
Commands:uiUpdate(elapsedTime)
Commands.lastUpdateTime = vim.loop.now()
end
function Commands.safeUpdateLoop()
local success, errmsg = pcall(Commands.updateLoop)
if not success then
vim.api.nvim_err_writeln("Error in update:".. errmsg)
end
end
local gtimer = 0
local resetCount = 0
function Commands:uiUpdate(delta)
local animFrameCount = 4
local animFrameDuration = 200
local animDuration = animFrameCount * animFrameDuration
local anim = {
"▌",
"▀",
"▐",
"▄"
}
local anim1 = {
"1",
"2",
"3",
"4"
}
if Commands.animData then
anim = Commands.animData.frames
animFrameDuration = Commands.animData.interval
animFrameCount = #anim
animDuration = animFrameCount * animFrameDuration
end
local index = 1 + (math.floor(math.fmod(vim.loop.now(), animDuration) / animFrameDuration))
Commands.renderedAnim = (anim[index] or "")
end
function Commands.safeLogicUpdate()
local success, errmsg = pcall(function() Commands:LogicUpdate() end)
if not success then
vim.api.nvim_err_writeln("Error in update:".. errmsg)
end
vim.defer_fn(Commands.safeLogicUpdate, 1)
end
function Commands:LogicUpdate()
if self.taskCoroutine then
if coroutine.status(self.taskCoroutine) ~= "dead" then
local ok, errmsg = coroutine.resume(self.taskCoroutine)
if not ok then
self.taskCoroutine = nil
error(errmsg)
end
else
self.taskCoroutine = nil
end
end
vim.defer_fn(Commands.onStatusUpdate, 1)
end
local function GetInstallDir()
local packer_install_dir = vim.fn.stdpath('data') .. '/site/pack/packer/start/'
return packer_install_dir .. "Unreal.nvim//"
end
local mydbg = true
function Commands:SetCurrentAnimation(animationName)
local jsonPath = GetInstallDir() .. "lua/spinners.json"
local file = io.open(jsonPath, "r")
if file then
local content = file:read("*all")
local json = vim.fn.json_decode(content)
Commands.animData = json[animationName]
end
end
function Commands.generateCommandsCoroutine()
PrintAndLogMessage("Generating clang-compatible compile_commands.json")
Commands:SetCurrentAnimation("kirbyFlip")
coroutine.yield()
Commands.ClearTasks()
local editorFlag = ""
if CurrentGenData.config.withEditor then
PrintAndLogMessage("Building editor")
editorFlag = "-Editor"
end
Commands.ScheduleTask("gencmd")
-- local cmd = CurrentGenData.ubtPath .. " -mode=GenerateClangDatabase -StaticAnalyzer=Clang -project=" ..
local cmd = CurrentGenData.ubtPath .. " -mode=GenerateClangDatabase -project=" ..
CurrentGenData.projectPath .. " -game -engine " .. CurrentGenData.target.UbtExtraFlags .. " " ..
editorFlag .. " " ..
CurrentGenData.prjName .. CurrentGenData.targetNameSuffix .. " " .. CurrentGenData.target.Configuration .. " " ..
CurrentGenData.target.PlatformName
PrintAndLogMessage("Dispatching command:")
PrintAndLogMessage(cmd)
CurrentCompileCommandsTargetFilePath = CurrentGenData.prjDir .. "/compile_commands.json"
vim.api.nvim_command("Dispatch " .. cmd)
PrintAndLogMessage("Dispatched")
end
function Commands.SetUnrealCD()
local current_file_path = vim.api.nvim_buf_get_name(0)
local prjName, prjDir = Commands._GetDefaultProjectNameAndDir(current_file_path)
if prjDir then
vim.cmd("cd " .. prjDir)
else
PrintAndLogMessage("Could not find unreal project root directory, make sure you have the correct buffer selected")
end
end
function Commands._check_extension_in_directory(directory, extension)
local dir = vim.loop.fs_opendir(directory)
if not dir then
return nil
end
handle = vim.loop.fs_scandir(directory)
local name, typ
while handle do
name, typ = vim.loop.fs_scandir_next(handle)
if not name then break end
local ext = vim.fn.fnamemodify(name, ":e")
if ( ext == "uproject" ) then
return directory.."/"..name
end
end
return nil
end
function Commands._find_file_with_extension(filepath, extension)
local current_dir = vim.fn.fnamemodify(filepath, ":h")
local parent_dir = vim.fn.fnamemodify(current_dir, ":h")
-- Check if the file exists in the current directory
local filename = vim.fn.fnamemodify(filepath, ":t")
local full_path = Commands._check_extension_in_directory(current_dir, extension)
if full_path then
return current_dir, full_path
end
-- Recursively check parent directories until we find the file or reach the root directory
if current_dir ~= parent_dir then
return Commands._find_file_with_extension(parent_dir .. "/" .. filename, extension)
end
-- File not found
return nil
end
return Commands