MCPcopy Create free account
hub / github.com/DavidColson/Polybox / OnFileChanged

Function OnFileChanged

source/cpu.cpp:355–428  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

353// ***********************************************************************
354
355void OnFileChanged(FileChange change, void* pUserData) {
356 // check for luau file changes
357 for (int i=0; i<pState->luaFilesToWatch.count; i++) {
358 if (pState->luaFilesToWatch[i] == change.path) {
359 Log::Info("Luau file %s changed, reloading", change.path.pData);
360
361 // when you reload a module
362 // you will load it up onto the stack, and then look up it's existing
363 // and shallow copy the contents from the new to the old, then just drop the new one
364 pState->frontend.markDirty(change.path.pData);
365 if (!CompileAndLoadModule(change.path, 1)) {
366 return;
367 }
368 lua_State* L = pState->pProgramState;
369
370 // this will be the main.luau file, rather than a module, so no patching required
371 if (lua_isnil(L, -1)) {
372 if (pState->hasStarted == false) {
373 pState->appLoaded = true;
374 Start();
375 }
376 lua_pop(L, 1);
377 return;
378 }
379
380 i32 newModuleIndex = lua_gettop(L);
381 luaL_findtable(L, LUA_REGISTRYINDEX, "_MODULES", 1);
382 lua_getfield(L, -1, change.path.pData);
383
384 // shallow copy the new table into the existing one in the registry (which everyone points to)
385 lua_pushnil(L);
386 while(lua_next(L, newModuleIndex) != 0) {
387 lua_pushvalue(L, -2);
388 lua_insert(L, -2);
389 lua_settable(L, -4);
390 }
391 lua_pop(L, 3);
392 }
393 }
394
395 // check for asset changes
396 for (int i=0; i<pState->assetsToWatch.count; i++) {
397 if (pState->assetsToWatch[i] == change.path) {
398 String extension = TakeAfterLastDot(change.path);
399 if (extension == "gltf") {
400 lua_State* L = pState->pProgramState;
401 AssetImporter::ImportTableAsset& asset = pState->pImportTable->table[change.path];
402
403 // reimport the asset, leaving it loaded into lua (i.e. we don't serialize it)
404 if (AssetImporter::ImportGltf(g_pArenaFrame, L, asset.importFormat, change.path)) {
405 // you want to lookup the asset in the ASSET table
406 i32 newAssetIndex = lua_gettop(L);
407 luaL_findtable(L, LUA_REGISTRYINDEX, "_ASSETS", 1);
408 lua_getfield(L, -1, change.path.pData);
409
410 // check to make sure the asset has not been deleted by gc
411 if (lua_isnil(L, -1)) {
412 lua_pop(L, 3);

Callers

nothing calls this directly

Calls 3

CompileAndLoadModuleFunction · 0.85
StartFunction · 0.85
ImportGltfFunction · 0.85

Tested by

no test coverage detected