From 3dc85900d3b6f2bdd269229683452702c3d37cae Mon Sep 17 00:00:00 2001
From: whiteshark0 <whiteshark0@b956fd51-792f-4845-bead-9b4dfca2ff2c>
Date: Mon, 4 May 2009 12:50:21 +0000
Subject: automaticaly remove hooks from unloaded plugins

git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/branches/experimental@1712 b956fd51-792f-4845-bead-9b4dfca2ff2c
---
 Lua/src/lua/ULuaCore.pas | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

(limited to 'Lua/src/lua')

diff --git a/Lua/src/lua/ULuaCore.pas b/Lua/src/lua/ULuaCore.pas
index c5b3a590..919f54b9 100644
--- a/Lua/src/lua/ULuaCore.pas
+++ b/Lua/src/lua/ULuaCore.pas
@@ -147,6 +147,8 @@ type
       function GetEventbyName(Name: String): THookableEvent;       //< tries to find the event w/ the given name in the list
       function GetEventbyHandle(hEvent: Integer): THookableEvent;     //< tries to find the event w/ the given handle
 
+      procedure UnHookByParent(Parent: Integer); //< remove all hooks by given parent id from all events
+
       procedure PrepareState(L: Plua_State);
 
       procedure DumpPlugins; //< prints plugin runtime information w/ Log.LogStatus
@@ -399,7 +401,7 @@ begin
 end;
 
 { tries to find the event w/ the given handle }
-function TLuaCore.GetEventbyHandle(hEvent: Integer): THookableEvent;   
+function TLuaCore.GetEventbyHandle(hEvent: Integer): THookableEvent;
 begin
   If (hEvent >= 0) AND (hEvent <= High(EventHandles)) AND (Length(EventHandles[hEvent]) > 0) then
   begin //hEvent in bounds and not already deleted
@@ -409,6 +411,24 @@ begin
     Result := nil;
 end;
 
+{ remove all hooks by given parent id from all events }
+procedure TLuaCore.UnHookByParent(Parent: Integer);
+  var
+    Cur: PEventListItem;
+begin
+  if (Parent >= 0) and (Parent <= High(Plugins)) then
+  begin
+    // go through event list
+    Cur := EventList;
+
+    While (Cur <> nil) do
+    begin
+      Cur.Event.UnHookByParent(Parent);
+      Cur := Cur.Next;
+    end;
+  end;
+end;
+
 { prepares the given already opened Lua state with the
   basic usdx environment, e.g.: base and package Modules,
   usdx moduleloader and usdx table }
@@ -813,6 +833,8 @@ begin
     lua_close(State);
     State := nil; //don't forget to nil it ;)
 
+    LuaCore.UnHookByParent(iId);
+
     if (sStatus = psRunning) then
       sStatus := psClosed;
   end;
-- 
cgit v1.2.3