MCPcopy Create free account
hub / github.com/IppClub/Dora-SSR / cliRegisterLua

Function cliRegisterLua

Source/Basic/Application.cpp:392–448  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

390}
391
392void cliRegisterLua(lua_State* L, int argc, char* argv[], const fs::path& scriptPath, int cliIndex, const std::optional<fs::path>& assetPath) {
393 luaL_openlibs(L);
394 luaL_requiref(L, "json", luaopen_colibc_json, 1);
395 lua_pop(L, 1);
396 luaL_requiref(L, "yue", luaopen_yue, 1);
397 lua_pop(L, 1);
398 lua_getglobal(L, "package");
399 lua_getfield(L, -1, "path");
400 auto packagePath = cliGetString(L, -1);
401 lua_pop(L, 1);
402 auto scriptDir = scriptPath.parent_path().string();
403 auto assetsScriptDir = scriptPath.parent_path().parent_path().string();
404 packagePath = scriptDir + "/?.lua;" + assetsScriptDir + "/?.lua;" + assetsScriptDir + "/?/init.lua;" + packagePath;
405 lua_pushlstring(L, packagePath.c_str(), packagePath.size());
406 lua_setfield(L, -2, "path");
407 lua_pop(L, 1);
408
409 if (assetPath) {
410 auto path = assetPath->string();
411 lua_newtable(L); // Content
412 lua_pushlstring(L, path.c_str(), path.size());
413 lua_setfield(L, -2, "assetPath");
414 lua_setglobal(L, "Content");
415 }
416
417 lua_newtable(L); // Dora
418 lua_newtable(L); // Dora.CLI
419 lua_newtable(L); // args
420 int argIndex = 1;
421 for (int i = cliIndex + 1; i < argc; i++) {
422 if (cliIsAssetArgument(argc, argv, i)) continue;
423 lua_pushstring(L, argv[i]);
424 lua_rawseti(L, -2, argIndex++);
425 }
426 lua_setfield(L, -2, "args");
427 auto executablePath = cliAbsolutePath(argv[0]).string();
428 lua_pushlstring(L, executablePath.c_str(), executablePath.size());
429 lua_setfield(L, -2, "executablePath");
430 auto path = scriptPath.string();
431 lua_pushlstring(L, path.c_str(), path.size());
432 lua_setfield(L, -2, "scriptPath");
433 cliSetFunc(L, "cwd", cliLuaCwd);
434 cliSetFunc(L, "env", cliLuaEnv);
435 cliSetFunc(L, "absolute", cliLuaAbsolute);
436 cliSetFunc(L, "exists", cliLuaExists);
437 cliSetFunc(L, "isDir", cliLuaIsDir);
438 cliSetFunc(L, "isFile", cliLuaIsFile);
439 cliSetFunc(L, "mkdirs", cliLuaMkdirs);
440 cliSetFunc(L, "readFile", cliLuaReadFile);
441 cliSetFunc(L, "writeFile", cliLuaWriteFile);
442 cliSetFunc(L, "listDir", cliLuaListDir);
443 cliSetFunc(L, "mtime", cliLuaMTime);
444 cliSetFunc(L, "system", cliLuaSystem);
445 cliSetFunc(L, "http", cliLuaHttp);
446 lua_setfield(L, -2, "CLI");
447 lua_setglobal(L, "Dora");
448}
449

Callers 1

runCliApplicationFunction · 0.85

Calls 15

luaL_openlibsFunction · 0.85
luaL_requirefFunction · 0.85
lua_getglobalFunction · 0.85
cliGetStringFunction · 0.85
lua_setglobalFunction · 0.85
cliIsAssetArgumentFunction · 0.85
cliAbsolutePathFunction · 0.85
cliSetFuncFunction · 0.85
parent_pathMethod · 0.80
lua_getfieldFunction · 0.50
lua_pushlstringFunction · 0.50
lua_setfieldFunction · 0.50

Tested by

no test coverage detected