From 1b2346740c8b23f56a29bcbb646266667a911f6b Mon Sep 17 00:00:00 2001 From: whiteshark0 Date: Sat, 16 May 2009 13:06:57 +0000 Subject: add Lua_GetOwner to ULuaUtils (function returns plugin by lua state) use new function in ULuaParty and ULuaUsdx new lua function: Usdx.ShutMeDown to give plugins the ability to unload git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/branches/experimental@1732 b956fd51-792f-4845-bead-9b4dfca2ff2c --- Lua/src/lua/ULuaUtils.pas | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'Lua/src/lua/ULuaUtils.pas') diff --git a/Lua/src/lua/ULuaUtils.pas b/Lua/src/lua/ULuaUtils.pas index 2bcfea68..de3bd15f 100644 --- a/Lua/src/lua/ULuaUtils.pas +++ b/Lua/src/lua/ULuaUtils.pas @@ -33,7 +33,7 @@ interface {$I switches.inc} -uses ULua; +uses ULua, ULuaCore; { converts a lua table with a structure like: * = 1 , * = 4 , * = 5 @@ -42,6 +42,13 @@ uses ULua; does not pop anything } function Lua_ToBinInt(L: PLua_State; idx: Integer): Integer; +{ returns plugin that is the owner of the given state + may raise a lua error if the parent id is not found + in states registry, if state owner does not exists + or is not loaded. So a check for a nil value is not + necessary } +function Lua_GetOwner(L: PLua_State): TLuaPlugin; + { this is a helper in case an evenet owner don't has no use for the results returns number of popped elements } function Lua_ClearStack(L: Plua_State): Integer; @@ -61,7 +68,7 @@ begin // default: no bits set Result := 0; - lua_checkstack(L, 3); + lua_checkstack(L, 2); if (idx < 0) then dec(idx); // we will push one value before using this @@ -81,10 +88,32 @@ begin end; end; +{ returns plugin that is the owner of the given state + may raise a lua error if the parent id is not found + in states registry, if state owner does not exists + or is not loaded. So a check for a nil value is not + necessary } +function Lua_GetOwner(L: PLua_State): TLuaPlugin; +begin + lua_checkstack(L, 1); + + lua_getfield (L, LUA_REGISTRYINDEX, '_USDX_STATE_ID'); + if (not lua_isNumber(L, -1)) then + luaL_error(L, 'unable to get _USDX_STATE_ID'); + + Result := LuaCore.GetPluginById(lua_toInteger(L, -1)); + + lua_pop(L, 1); //< remove state id from stack + + if (Result = nil) then + luaL_error(L, '_USDX_STATE_ID has invalid value') + else if (Result.Status > psRunning) then + luaL_error(L, 'owning plugin is not loaded or already unloaded in Lua_GetOwner'); +end; + { this is a helper in case an evenet owner don't has no use for the results returns number of popped elements } function Lua_ClearStack(L: Plua_State): Integer; - var I: Integer; begin Result := lua_gettop(L); lua_pop(L, Result); -- cgit v1.2.3