diff --git a/src/ui/CSimpleFrameScript.cpp b/src/ui/CSimpleFrameScript.cpp index 48ec762..c1d0c1e 100644 --- a/src/ui/CSimpleFrameScript.cpp +++ b/src/ui/CSimpleFrameScript.cpp @@ -129,7 +129,10 @@ int32_t CSimpleFrame_GetScript(lua_State* L) { } int32_t CSimpleFrame_SetScript(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + auto type = CSimpleFrame::GetObjectType(); + auto frame = static_cast(FrameScript_GetObjectThis(L, type)); + + return frame->SetScript(L); } int32_t CSimpleFrame_HookScript(lua_State* L) { diff --git a/src/ui/FrameScript_Object.cpp b/src/ui/FrameScript_Object.cpp index 36e9541..9f909ce 100644 --- a/src/ui/FrameScript_Object.cpp +++ b/src/ui/FrameScript_Object.cpp @@ -142,6 +142,30 @@ void FrameScript_Object::RunScript(ScriptIx const& script, int32_t argCount, con FrameScript_Execute(script.luaRef, this, argCount, a4 ? a4 : script.unk, nullptr); } +int32_t FrameScript_Object::SetScript(lua_State* L) { + if (!lua_isstring(L, 2) || lua_type(L, 3) != LUA_TFUNCTION && lua_type(L, 3)) { + return luaL_error(L, "Usage: %s:SetScript(\"type\", function)", this->GetDisplayName()); + } + + ScriptData data; + auto script = this->GetScriptByName(lua_tostring(L, 2), data); + + if (!script) { + return luaL_error(L, "%s doesn't have a \"%s\" script", this->GetDisplayName(), lua_tostring(L, 2)); + } + + if (script->luaRef) { + luaL_unref(L, LUA_REGISTRYINDEX, script->luaRef); + } + + auto luaRef = luaL_ref(L, LUA_REGISTRYINDEX); + script->luaRef = luaRef <= 0 ? 0 : luaRef; + + // TODO taint tracking + + return 0; +} + void FrameScript_Object::UnregisterScriptObject(const char* name) { auto L = FrameScript_GetContext(); diff --git a/src/ui/FrameScript_Object.hpp b/src/ui/FrameScript_Object.hpp index a69bf55..c8edad2 100644 --- a/src/ui/FrameScript_Object.hpp +++ b/src/ui/FrameScript_Object.hpp @@ -45,6 +45,7 @@ class FrameScript_Object { int32_t RegisterScriptEvent(const char* name); void RegisterScriptObject(const char* name); void RunScript(ScriptIx const& script, int32_t argCount, const char* a4); + int32_t SetScript(lua_State* L); void UnregisterScriptObject(const char* name); };