More Compilation stuff lol.

This commit is contained in:
Anemunt
2025-12-12 15:36:40 -05:00
parent ee90559e8c
commit 6ee17f52ee
9 changed files with 358 additions and 29 deletions

View File

@@ -30,13 +30,25 @@ void ScriptContext::SetScale(const glm::vec3& scl) {
if (object) object->scale = scl;
}
void ScriptContext::MarkDirty() {
if (engine) {
engine->markProjectDirty();
}
}
ScriptRuntime::InspectorFn ScriptRuntime::getInspector(const fs::path& binaryPath) {
lastError.clear();
Module* mod = getModule(binaryPath);
return mod ? mod->inspector : nullptr;
}
ScriptRuntime::Module* ScriptRuntime::getModule(const fs::path& binaryPath) {
lastError.clear();
if (binaryPath.empty()) return nullptr;
auto key = binaryPath.string();
auto it = loaded.find(key);
if (it != loaded.end()) {
if (it->second.inspector) return it->second.inspector;
return &it->second;
// Previously loaded but missing inspector; try reloading.
#if defined(_WIN32)
if (it->second.handle) FreeLibrary(static_cast<HMODULE>(it->second.handle));
@@ -54,6 +66,11 @@ ScriptRuntime::InspectorFn ScriptRuntime::getInspector(const fs::path& binaryPat
return nullptr;
}
mod.inspector = reinterpret_cast<InspectorFn>(GetProcAddress(static_cast<HMODULE>(mod.handle), "Script_OnInspector"));
mod.begin = reinterpret_cast<BeginFn>(GetProcAddress(static_cast<HMODULE>(mod.handle), "Script_Begin"));
mod.spec = reinterpret_cast<SpecFn>(GetProcAddress(static_cast<HMODULE>(mod.handle), "Script_Spec"));
mod.testEditor = reinterpret_cast<TestEditorFn>(GetProcAddress(static_cast<HMODULE>(mod.handle), "Script_TestEditor"));
mod.update = reinterpret_cast<UpdateFn>(GetProcAddress(static_cast<HMODULE>(mod.handle), "Script_Update"));
mod.tickUpdate = reinterpret_cast<TickUpdateFn>(GetProcAddress(static_cast<HMODULE>(mod.handle), "Script_TickUpdate"));
#else
mod.handle = dlopen(binaryPath.string().c_str(), RTLD_NOW);
if (!mod.handle) {
@@ -62,26 +79,60 @@ ScriptRuntime::InspectorFn ScriptRuntime::getInspector(const fs::path& binaryPat
return nullptr;
}
mod.inspector = reinterpret_cast<InspectorFn>(dlsym(mod.handle, "Script_OnInspector"));
mod.begin = reinterpret_cast<BeginFn>(dlsym(mod.handle, "Script_Begin"));
mod.spec = reinterpret_cast<SpecFn>(dlsym(mod.handle, "Script_Spec"));
mod.testEditor = reinterpret_cast<TestEditorFn>(dlsym(mod.handle, "Script_TestEditor"));
mod.update = reinterpret_cast<UpdateFn>(dlsym(mod.handle, "Script_Update"));
mod.tickUpdate = reinterpret_cast<TickUpdateFn>(dlsym(mod.handle, "Script_TickUpdate"));
#if !defined(_WIN32)
{
const char* err = dlerror();
if (err && !mod.inspector) lastError = err;
if (err && !mod.inspector && !mod.begin && !mod.spec && !mod.testEditor
&& !mod.update && !mod.tickUpdate) {
lastError = err;
}
}
#endif
#endif
if (!mod.inspector) {
if (!mod.inspector && !mod.begin && !mod.spec && !mod.testEditor
&& !mod.update && !mod.tickUpdate) {
#if defined(_WIN32)
FreeLibrary(static_cast<HMODULE>(mod.handle));
#else
dlclose(mod.handle);
#endif
if (lastError.empty()) lastError = "Script_OnInspector not found";
if (lastError.empty()) lastError = "No script exports found";
return nullptr;
}
loaded[key] = mod;
return mod.inspector;
return &loaded[key];
}
void ScriptRuntime::tickModule(const fs::path& binaryPath, ScriptContext& ctx, float deltaTime,
bool runSpec, bool runTest) {
Module* mod = getModule(binaryPath);
if (!mod) return;
int objId = ctx.object ? ctx.object->id : -1;
if (objId >= 0 && mod->begin && mod->beginCalledObjects.find(objId) == mod->beginCalledObjects.end()) {
mod->begin(ctx, deltaTime);
mod->beginCalledObjects.insert(objId);
}
if (mod->tickUpdate) {
mod->tickUpdate(ctx, deltaTime);
} else if (mod->update) {
mod->update(ctx, deltaTime);
}
if (runSpec && mod->spec) {
mod->spec(ctx, deltaTime);
}
if (runTest && mod->testEditor) {
mod->testEditor(ctx, deltaTime);
}
}
void ScriptRuntime::unloadAll() {