-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLuaManager.cpp
More file actions
660 lines (511 loc) · 22.8 KB
/
LuaManager.cpp
File metadata and controls
660 lines (511 loc) · 22.8 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
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
#include "LuaManager.h"
#include <Base/CVarSystem/CVarSystem.h>
#include <Base/Memory/Bytebuffer.h>
#include <Base/Memory/FileReader.h>
#include <Base/Util/DebugHandler.h>
#include <Luau/Compiler.h>
#include <Luau/CodeGen.h>
#include <lua.h>
#include <lualib.h>
#include <vector>
#include <filesystem>
namespace fs = std::filesystem;
AutoCVar_String CVAR_ScriptDir(CVarCategory::Client, "scriptingDirectory", "defines the directory from where scripts are loaded", "Data/Scripts");
AutoCVar_String CVAR_ScriptExtension(CVarCategory::Client, "scriptingExtension", "defines the file extension to recognized as a script file", ".luau");
AutoCVar_String CVAR_ScriptMotd(CVarCategory::Client, "scriptingMotd", "defines the message of the day passed in the GameLoaded Event", "Welcome to Novuscore");
AutoCVar_Int CVAR_ScriptNativeCompilation(CVarCategory::Client, "scriptingNativeCompilation", "enables native compilation of luau scripts, may improve performance but increases load times", 0, CVarFlags::EditCheckbox);
namespace Scripting
{
static i32 lua_require(::lua_State* state)
{
lua_getglobal(state, "Zenith");
if (lua_islightuserdata(state, -1) == 0)
{
luaL_error(state, "error requiring module, global 'Zenith' missing");
return 0;
}
void* zenithPtr = lua_touserdata(state, -1);
lua_pop(state, 1);
LuaManager* luaManager = reinterpret_cast<LuaManager*>(zenithPtr);
ZenithStateManager& zenithStateManager = luaManager->GetZenithStateManager();
ZenithBytecodeCache& bytecodeCache = zenithStateManager.GetBytecodeCache();
Zenith* zenith = zenithStateManager.Get(state);
if (zenith->GetTop() != 1)
{
luaL_error(state, "error requiring module, invalid number of arguments");
return 0;
}
std::string name = luaL_checkstring(state, 1);
const char* scriptDirPath = zenith->GetGlobalField<const char*>("path");
if (!scriptDirPath)
{
luaL_error(state, "error requiring module, global 'path' missing");
return 0;
}
if (StringUtils::BeginsWith(name, "@src/"))
{
name = name.substr(5);
}
std::string scriptPath = std::string(scriptDirPath) + "/" + name + ".luau";
std::replace(scriptPath.begin(), scriptPath.end(), '\\', '/');
zenith->Pop();
if (!fs::exists(scriptPath))
{
luaL_error(state, "error requiring module, script not found");
return 0;
}
u32 moduleHash = StringUtils::fnv1a_32(scriptPath.c_str(), scriptPath.size());
if (!bytecodeCache.apiHashToBytecodeIndex.contains(moduleHash))
{
luaL_error(state, "error requiring module, bytecode not found");
return 0;
}
// Stack: Name, ModuleProxy
zenith->CreateTable();
// Stack: Name, ModuleProxy, __ImportedModules__
zenith->GetGlobalKey("__ImportedModules__");
// Stack: Name, ModuleProxy, __ImportedModules__, Module
if (!zenith->GetTableField(scriptPath.c_str()))
{
bool enableNativeCompilation = CVAR_ScriptNativeCompilation.Get() != 0;
u32 bytecodeIndex = bytecodeCache.apiHashToBytecodeIndex[moduleHash];
ZenithBytecodeEntry& bytecodeEntry = bytecodeCache.apiBytecodeList[bytecodeIndex];
i32 result = zenith->LoadBytecode(bytecodeEntry.filePath, bytecodeEntry.bytecode, 0);
if (result != LUA_OK)
{
lua_error(zenith->state);
return false;
}
if (enableNativeCompilation)
{
Luau::CodeGen::CompilationResult codeGenResult = Luau::CodeGen::compile(state, -1, 0);
if (codeGenResult.hasErrors() && codeGenResult.result != Luau::CodeGen::CodeGenCompilationResult::NothingToCompile)
return false;
}
if (!zenith->PCall(0, 1))
return false;
// Stack: Name, ModuleProxy, __ImportedModules__, Module
if (zenith->GetTop() == 0)
{
luaL_error(state, "error requiring module, module does not return a value");
return 0;
}
else if (!lua_istable(state, -1))
{
luaL_error(state, "error requiring module, module must return a table");
return 0;
}
// Stack: Name, ModuleProxy, __ImportedModules__, Module, Module
zenith->PushValue();
// Stack: Name, ModuleProxy, __ImportedModules__, Module
zenith->SetTableKey("__index");
// Set the ModuleTable as readonly
lua_setreadonly(state, -1, true);
// Add Table to __ImportedModules__
// Stack: Name, ModuleProxy, __ImportedModules__
zenith->SetTableKey(scriptPath.c_str());
// Push Table to the stack
zenith->GetTableField(scriptPath.c_str());
zenith->loadedModuleHashes.insert(moduleHash);
}
// Stack: Name, ModuleProxy, __ImportedModules__
lua_setmetatable(state, -3);
// Stack: Name, ModuleProxy
zenith->Pop();
// Stack: ModuleProxy
lua_replace(state, -2);
return 1;
}
LuaManager::LuaManager()
{
_luaHandlers.reserve(32);
}
void LuaManager::Init()
{
_currentTick = 0;
}
void LuaManager::Update(f32 deltaTime, u64 tick)
{
_currentTick = tick;
bool isLuaManagerDirty = IsDirty();
if (isLuaManagerDirty)
ClearDirty();
u32 numHandlers = static_cast<u32>(_luaHandlers.size());
bool failedToCompile = false;
for (auto& [key, zenith] : _zenithStateManager)
{
bool result = true;
bool isStateDirty = zenith->IsDirty();
bool isDirty = !failedToCompile && (isLuaManagerDirty || zenith->IsDirty());
// If we previously failed to compile, we want to keep the state as it is
if (failedToCompile && isStateDirty)
zenith->ClearDirty();
if (isDirty)
{
bool loadedScripts = true;
bool reloadedZenith = false;
// Recompile scripts if needed
if (NeedsRecompilation())
loadedScripts = LoadScripts();
// Reload Zenith
if (loadedScripts)
reloadedZenith = ReloadZenith(key, zenith.get());
// If we failed to load scripts or reload Zenith, we want to mark that we failed to compile
failedToCompile = !(loadedScripts && reloadedZenith);
}
for (u32 i = 0; i < numHandlers; i++)
{
LuaHandlerBase* luaHandler = _luaHandlers[i];
if (isDirty)
luaHandler->PostLoad(zenith.get());
luaHandler->Update(zenith.get(), deltaTime);
}
}
}
bool LuaManager::ReloadZenith(const ZenithInfoKey& key, Zenith* zenith)
{
u32 numHandlers = static_cast<u32>(_luaHandlers.size());
for (u32 i = 0; i < numHandlers; i++)
{
LuaHandlerBase* luaHandler = _luaHandlers[i];
luaHandler->Clear(zenith);
}
bool result = LoadBytecode(key, zenith);
zenith->ClearDirty();
return result;
}
bool LuaManager::DoString(Zenith* zenith, const std::string& code)
{
Luau::CompileOptions compileOptions;
{
compileOptions.optimizationLevel = 1;
compileOptions.debugLevel = 2;
compileOptions.typeInfoLevel = 1;
compileOptions.coverageLevel = 2;
}
Luau::ParseOptions parseOptions;
std::string bytecode = Luau::compile(code, compileOptions, parseOptions);
i32 result = zenith->LoadBytecode("", bytecode, 0);
if (result != LUA_OK)
{
zenith->ReportError();
return false;
}
bool success = zenith->PCall(0, 0);
return success;
}
void LuaManager::PrepareToAddLuaHandlers(u16 numHandlers)
{
_luaHandlers.resize(numHandlers, nullptr);
}
void LuaManager::SetLuaHandler(LuaHandlerID handlerID, LuaHandlerBase* luaHandler)
{
u16 index = static_cast<u16>(handlerID);
_luaHandlers[index] = luaHandler;
}
u32 LuaManager::GetPathDepth(const std::string& rootPath, const std::string& filePath)
{
std::string relativePath = filePath.substr(rootPath.length());
u32 depth = 0;
for (char c : relativePath)
{
// Handle both forward and backslashes
if (c == '/' || c == '\\')
{
depth++;
}
}
return depth;
}
bool LuaManager::LoadScripts()
{
const char* scriptDir = CVAR_ScriptDir.Get();
const char* scriptExtension = CVAR_ScriptExtension.Get();
bool enableNativeCompilation = CVAR_ScriptNativeCompilation.Get() != 0;
fs::path scriptDirectory = fs::absolute(scriptDir);
fs::path scriptAPIDirectory = scriptDirectory / "API";
fs::path scriptBootstrapDirectory = scriptDirectory / "Bootstrap";
std::string scriptDirectoryAsString = scriptDirectory.string();
std::string scriptAPIDirectoryAsString = scriptAPIDirectory.string();
std::string scriptBootstrapDirectoryAsString = scriptBootstrapDirectory.string();
if (!fs::exists(scriptDirectory))
fs::create_directories(scriptDirectory);
if (!fs::exists(scriptAPIDirectory))
fs::create_directories(scriptAPIDirectory);
if (!fs::exists(scriptBootstrapDirectory))
fs::create_directories(scriptBootstrapDirectory);
ZenithBytecodeCache bytecodeCache;
Zenith zenith;
zenith.SetState(luaL_newstate());
if (enableNativeCompilation)
Luau::CodeGen::create(zenith.state);
zenith.RegisterDefaultLibraries();
zenith.AddGlobalField("path", scriptDirectoryAsString.c_str());
zenith.AddGlobalField("require", lua_require);
for (u32 i = 0; i < _luaHandlers.size(); i++)
{
LuaHandlerBase* base = _luaHandlers[i];
base->Register(&zenith);
}
luaL_sandbox(zenith.state);
luaL_sandboxthread(zenith.state);
zenith.CreateTable("__ImportedModules__");
zenith.Pop();
// TODO : Figure out if this catches hidden folders, and if so exclude them
// TODO : Should we use a custom file extension for "include" files? Force load any files that for example use ".ext"
std::vector<std::pair<u32, fs::path>> paths;
std::vector<std::pair<u32, fs::path>> apiPaths;
std::vector<std::pair<u32, fs::path>> bootstrapPaths;
paths.reserve(1024);
apiPaths.reserve(1024);
bootstrapPaths.reserve(1024);
fs::recursive_directory_iterator fsScriptAPIDir { scriptAPIDirectory };
for (const auto& dirEntry : fsScriptAPIDir)
{
if (!dirEntry.is_regular_file())
continue;
fs::path path = dirEntry.path();
if (path.extension() != scriptExtension)
continue;
std::string pathStr = path.string();
u32 depth = GetPathDepth(scriptAPIDirectoryAsString, pathStr);
apiPaths.push_back({ depth, path });
}
std::sort(apiPaths.begin(), apiPaths.end());
fs::recursive_directory_iterator fsScriptBootstrapDir { scriptBootstrapDirectory };
for (const auto& dirEntry : fsScriptBootstrapDir)
{
if (!dirEntry.is_regular_file())
continue;
fs::path path = dirEntry.path();
if (path.extension() != scriptExtension)
continue;
std::string pathStr = path.string();
u32 depth = GetPathDepth(scriptBootstrapDirectoryAsString, pathStr);
bootstrapPaths.push_back({ depth, path });
}
std::sort(bootstrapPaths.begin(), bootstrapPaths.end());
fs::recursive_directory_iterator fsScriptDir { scriptDirectory };
for (const auto& dirEntry : fsScriptDir)
{
if (!dirEntry.is_regular_file())
continue;
fs::path path = dirEntry.path();
if (path.extension() != scriptExtension)
continue;
fs::path relativePath = fs::relative(path, scriptDirectory);
std::string relativePathAsString = relativePath.string();
std::replace(relativePathAsString.begin(), relativePathAsString.end(), '\\', '/');
if (StringUtils::BeginsWith(relativePathAsString, "API/") || StringUtils::BeginsWith(relativePathAsString, "Bootstrap/"))
continue;
if (!_developerMode && StringUtils::BeginsWith(relativePathAsString, "Editor/"))
continue;
std::string pathStr = path.string();
u32 depth = GetPathDepth(scriptBootstrapDirectoryAsString, pathStr);
paths.push_back({ depth, path });
}
std::sort(paths.begin(), paths.end());
Luau::CompileOptions compileOptions;
{
compileOptions.optimizationLevel = 1;
compileOptions.debugLevel = 2;
compileOptions.typeInfoLevel = 1;
compileOptions.coverageLevel = 2;
}
Luau::ParseOptions parseOptions;
bool didFail = false;
for (auto& path : apiPaths)
{
const std::string pathAsStr = path.second.string();
FileReader reader(pathAsStr);
if (!reader.Open())
continue;
u32 bufferSize = static_cast<u32>(reader.Length());
std::shared_ptr<Bytebuffer> buffer = Bytebuffer::BorrowRuntime(bufferSize);
reader.Read(buffer.get(), bufferSize);
std::string luaCode;
luaCode.resize(bufferSize);
if (!buffer->GetString(luaCode, bufferSize))
continue;
fs::path relPath = fs::relative(path.second, scriptDirectory);
std::string scriptPath = scriptDirectoryAsString + "/" + relPath.string();
std::replace(scriptPath.begin(), scriptPath.end(), '\\', '/');
StringUtils::StringHash pathHash = StringUtils::fnv1a_32(scriptPath.c_str(), scriptPath.size());
ZenithBytecodeEntry bytecodeEntry
{
pathHash,
path.second.filename().string(),
pathAsStr,
Luau::compile(luaCode, compileOptions, parseOptions)
};
u32 index = static_cast<u32>(bytecodeCache.apiBytecodeList.size());
bytecodeCache.apiBytecodeList.push_back(bytecodeEntry);
bytecodeCache.apiHashToBytecodeIndex[pathHash] = index;
}
for (auto& path : bootstrapPaths)
{
const std::string pathAsStr = path.second.string();
FileReader reader(pathAsStr);
if (!reader.Open())
continue;
u32 bufferSize = static_cast<u32>(reader.Length());
std::shared_ptr<Bytebuffer> buffer = Bytebuffer::BorrowRuntime(bufferSize);
reader.Read(buffer.get(), bufferSize);
std::string luaCode;
luaCode.resize(bufferSize);
if (!buffer->GetString(luaCode, bufferSize))
continue;
fs::path relPath = fs::relative(path.second, scriptDirectory);
std::string scriptPath = scriptDirectoryAsString + "/" + relPath.string();
std::replace(scriptPath.begin(), scriptPath.end(), '\\', '/');
StringUtils::StringHash pathHash = StringUtils::fnv1a_32(scriptPath.c_str(), scriptPath.size());
ZenithBytecodeEntry bytecodeEntry
{
pathHash,
path.second.filename().string(),
pathAsStr,
Luau::compile(luaCode, compileOptions, parseOptions)
};
u32 index = static_cast<u32>(bytecodeCache.bytecodeList.size());
bytecodeCache.bytecodeList.push_back(bytecodeEntry);
}
for (auto& path : paths)
{
const std::string pathAsStr = path.second.string();
FileReader reader(pathAsStr);
if (!reader.Open())
continue;
u32 bufferSize = static_cast<u32>(reader.Length());
std::shared_ptr<Bytebuffer> buffer = Bytebuffer::BorrowRuntime(bufferSize);
reader.Read(buffer.get(), bufferSize);
std::string luaCode;
luaCode.resize(bufferSize);
if (!buffer->GetString(luaCode, bufferSize))
continue;
fs::path relPath = fs::relative(path.second, scriptDirectory);
std::string scriptPath = scriptDirectoryAsString + "/" + relPath.string();
std::replace(scriptPath.begin(), scriptPath.end(), '\\', '/');
StringUtils::StringHash pathHash = StringUtils::fnv1a_32(scriptPath.c_str(), scriptPath.size());
ZenithBytecodeEntry bytecodeEntry
{
pathHash,
path.second.filename().string(),
pathAsStr,
Luau::compile(luaCode, compileOptions, parseOptions)
};
u32 index = static_cast<u32>(bytecodeCache.bytecodeList.size());
bytecodeCache.bytecodeList.push_back(bytecodeEntry);
}
if (!didFail)
{
u32 numScriptsToLoad = static_cast<u32>(bytecodeCache.bytecodeList.size());
for (u32 i = 0; i < numScriptsToLoad; i++)
{
ZenithBytecodeEntry& bytecodeEntry = bytecodeCache.bytecodeList[i];
bool isLoaded = zenith.loadedModuleHashes.contains(bytecodeEntry.hash);
if (isLoaded)
continue;
i32 result = zenith.LoadBytecode(bytecodeEntry.filePath, bytecodeEntry.bytecode, 0);
if (result != LUA_OK)
{
std::string error = lua_tostring(zenith.state, -1);
NC_LOG_ERROR("Failed to Compile script : {0}\n{1}", bytecodeEntry.filePath, error);
didFail = true;
break;
}
if (enableNativeCompilation)
{
Luau::CodeGen::CompilationResult codeGenResult = Luau::CodeGen::compile(zenith.state, -1, 0);
if (codeGenResult.hasErrors() && codeGenResult.result != Luau::CodeGen::CodeGenCompilationResult::NothingToCompile)
{
didFail = true;
break;
}
}
}
}
_lastRecompiledTick = _currentTick;
if (!didFail)
_zenithStateManager.GetBytecodeCache() = std::move(bytecodeCache);
zenith.Clear();
return !didFail;
}
bool LuaManager::LoadBytecode(const ZenithInfoKey& key, Zenith* zenith)
{
const char* scriptDir = CVAR_ScriptDir.Get();
bool enableNativeCompilation = CVAR_ScriptNativeCompilation.Get() != 0;
fs::path scriptDirectory = fs::absolute(scriptDir);
std::string scriptDirectoryAsString = scriptDirectory.string();
const ZenithBytecodeCache& bytecodeCache = _zenithStateManager.GetBytecodeCache();
// Special Key used for loading bytecode without overwriting the current state
ZenithInfoKey tmpKey = ZenithInfoKey::Make(key.GetMapID(), key.GetInstanceID(), std::numeric_limits<u16>().max());
lua_State* state = luaL_newstate();
_zenithStateManager.Add(tmpKey);
_zenithStateManager.Add(tmpKey, state);
Zenith* tmpZenith = _zenithStateManager.Get(tmpKey);
tmpZenith->SetState(state);
if (enableNativeCompilation)
Luau::CodeGen::create(tmpZenith->state);
tmpZenith->RegisterDefaultLibraries();
tmpZenith->AddGlobalField("path", scriptDirectoryAsString.c_str());
tmpZenith->AddGlobalField("require", lua_require);
tmpZenith->PushLightUserData(this);
tmpZenith->SetGlobalKey("Zenith");
for (u32 i = 0; i < _luaHandlers.size(); i++)
{
LuaHandlerBase* base = _luaHandlers[i];
base->Register(tmpZenith);
}
luaL_sandbox(tmpZenith->state);
luaL_sandboxthread(tmpZenith->state);
tmpZenith->CreateTable("__ImportedModules__");
tmpZenith->Pop();
bool failed = false;
u32 numScriptsToLoad = static_cast<u32>(bytecodeCache.bytecodeList.size());
for (u32 i = 0; i < numScriptsToLoad; i++)
{
const ZenithBytecodeEntry& bytecodeEntry = bytecodeCache.bytecodeList[i];
bool isLoaded = tmpZenith->loadedModuleHashes.contains(bytecodeEntry.hash);
if (isLoaded)
continue;
tmpZenith->LoadBytecode(bytecodeEntry.filePath, bytecodeEntry.bytecode, 0);
if (enableNativeCompilation)
Luau::CodeGen::CompilationResult codeGenResult = Luau::CodeGen::compile(tmpZenith->state, -1, 0);
i32 status = tmpZenith->Resume();
if (status != LUA_OK)
{
std::string error = (status == LUA_YIELD) ? "thread yielded unexpectedly" : lua_tostring(tmpZenith->state, -1);
error += "\nstacktrace:\n";
error += lua_debugtrace(tmpZenith->state);
NC_LOG_ERROR("Failed to load script : {0}\n{1}", bytecodeEntry.filePath, error);
failed = true;
break;
}
tmpZenith->loadedModuleHashes.insert(bytecodeEntry.hash);
}
// Remove the temporary state -> key mapping
_zenithStateManager.Remove(tmpZenith->state);
if (!failed)
{
// If the current zenith has a state, we need to remove the state -> key mapping
if (zenith->state != nullptr)
_zenithStateManager.Remove(zenith->state);
// Prepare to move the temporary zenith state to the current zenith
tmpZenith->ClearState();
zenith->Clear();
// Move the temporary zenith state to the current zenith
zenith->SetState(state);
zenith->eventState = std::move(tmpZenith->eventState);
zenith->loadedModuleHashes = std::move(tmpZenith->loadedModuleHashes);
// Add the new state -> key mapping
_zenithStateManager.Add(key, zenith->state);
}
// Remove the temporary zenith state (Will be cleaned up by the state manager)
_zenithStateManager.Remove(tmpKey);
return !failed;
}
bool LuaManager::NeedsRecompilation() const
{
return _currentTick > _lastRecompiledTick;
}
}