From 51c1da3557d5d0dcefe458c57600db6b37427576 Mon Sep 17 00:00:00 2001 From: FlightControl Date: Tue, 11 Apr 2017 08:19:06 +0200 Subject: [PATCH] Added Event Reset for SPAWN, GROUP and UNIT on SPAWN:ReSpawn() --- Moose Development/Moose/Core/Event.lua | 60 ++++++++++---------- Moose Development/Moose/Functional/Spawn.lua | 2 + Moose Development/Moose/Wrapper/Group.lua | 16 +++++- Moose Development/Moose/Wrapper/Unit.lua | 11 ++++ Moose Mission Setup/Moose.lua | 2 +- 5 files changed, 59 insertions(+), 32 deletions(-) diff --git a/Moose Development/Moose/Core/Event.lua b/Moose Development/Moose/Core/Event.lua index 7a210a29d..e4658dc0f 100644 --- a/Moose Development/Moose/Core/Event.lua +++ b/Moose Development/Moose/Core/Event.lua @@ -424,13 +424,6 @@ function EVENT:New() return self end -function EVENT:EventText( EventID ) - - local EventText = _EVENTMETA[EventID].Text - - return EventText -end - --- Initializes the Events structure for the event -- @param #EVENT self @@ -442,7 +435,7 @@ function EVENT:Init( EventID, EventClass ) if not self.Events[EventID] then -- Create a WEAK table to ensure that the garbage collector is cleaning the event links when the object usage is cleaned. - self.Events[EventID] = setmetatable( {}, { __mode = "k" } ) + self.Events[EventID] = {} end -- Each event has a subtable of EventClasses, ordered by EventPriority. @@ -457,44 +450,51 @@ function EVENT:Init( EventID, EventClass ) return self.Events[EventID][EventPriority][EventClass] end ---- Removes an Events entry +--- Removes a subscription -- @param #EVENT self -- @param Core.Base#BASE EventClass The self instance of the class for which the event is. -- @param Dcs.DCSWorld#world.event EventID -- @return #EVENT.Events function EVENT:Remove( EventClass, EventID ) - self:F3( { EventClass, _EVENTMETA[EventID].Text } ) - local EventClass = EventClass + self:E( { "Removing subscription for class: ", EventClass:GetClassNameAndID() } ) + local EventPriority = EventClass:GetEventPriority() + + self.EventsDead = self.EventsDead or {} + self.EventsDead[EventID] = self.EventsDead[EventID] or {} + self.EventsDead[EventID][EventPriority] = self.EventsDead[EventID][EventPriority] or {} + self.EventsDead[EventID][EventPriority][EventClass] = self.Events[EventID][EventPriority][EventClass] + self.Events[EventID][EventPriority][EventClass] = nil + end ---- Removes an Events entry for a UNIT. +--- Resets subscriptions -- @param #EVENT self -- @param Core.Base#BASE EventClass The self instance of the class for which the event is. -- @param Dcs.DCSWorld#world.event EventID -- @return #EVENT.Events -function EVENT:RemoveForUnit( EventClass, EventID ) - self:F3( { EventClass, _EVENTMETA[EventID].Text } ) +function EVENT:Reset( EventObject ) - local EventClass = EventClass - local EventPriority = EventClass:GetEventPriority() - self.Events[EventID][EventPriority][EventClass] = nil + self:E( { "Resetting subscriptions for class: ", EventObject:GetClassNameAndID() } ) + + local EventPriority = EventObject:GetEventPriority() + for EventID, EventData in pairs( self.Events ) do + if self.EventsDead then + if self.EventsDead[EventID] then + if self.EventsDead[EventID][EventPriority] then + if self.EventsDead[EventID][EventPriority][EventObject] then + self.Events[EventID][EventPriority][EventObject] = self.EventsDead[EventID][EventPriority][EventObject] + end + end + end + end + end end ---- Removes an Events entry for a GROUP. --- @param #EVENT self --- @param Core.Base#BASE EventClass The self instance of the class for which the event is. --- @param Dcs.DCSWorld#world.event EventID --- @return #EVENT.Events -function EVENT:RemoveForGroup( EventClass, EventID ) - self:F3( { EventClass, _EVENTMETA[EventID].Text } ) - local EventClass = EventClass - local EventPriority = EventClass:GetEventPriority() - self.Events[EventID][EventPriority][EventClass] = nil -end + --- Clears all event subscriptions for a @{Base#BASE} derived object. -- @param #EVENT self @@ -869,7 +869,7 @@ function EVENT:onEvent( Event ) end else -- The EventClass is not alive anymore, we remove it from the EventHandlers... - self:RemoveForUnit( EventClass, Event.id ) + self:Remove( EventClass, Event.id ) end else @@ -917,7 +917,7 @@ function EVENT:onEvent( Event ) end else -- The EventClass is not alive anymore, we remove it from the EventHandlers... - self:RemoveForGroup( EventClass, Event.id ) + self:Remove( EventClass, Event.id ) end else diff --git a/Moose Development/Moose/Functional/Spawn.lua b/Moose Development/Moose/Functional/Spawn.lua index 02f757fed..7e612ec5b 100644 --- a/Moose Development/Moose/Functional/Spawn.lua +++ b/Moose Development/Moose/Functional/Spawn.lua @@ -759,6 +759,8 @@ function SPAWN:ReSpawn( SpawnIndex ) SpawnGroup:ReSpawnFunction() end + SpawnGroup:ResetEvents() + return SpawnGroup end diff --git a/Moose Development/Moose/Wrapper/Group.lua b/Moose Development/Moose/Wrapper/Group.lua index f1c70aca6..f99aad2d9 100644 --- a/Moose Development/Moose/Wrapper/Group.lua +++ b/Moose Development/Moose/Wrapper/Group.lua @@ -1077,7 +1077,21 @@ do -- Event Handling -- @return #GROUP function GROUP:UnHandleEvent( Event ) - self:EventDispatcher():RemoveForGroup( self:GetName(), self, Event ) + self:EventDispatcher():Remove( self, Event ) + + return self + end + + --- Reset the subscriptions. + -- @param #GROUP self + -- @return #GROUP + function GROUP:ResetEvents( Event ) + + self:EventDispatcher():Reset( self ) + + for UnitID, UnitData in pairs( self:GetUnits() ) do + UnitData:ResetEvents() + end return self end diff --git a/Moose Development/Moose/Wrapper/Unit.lua b/Moose Development/Moose/Wrapper/Unit.lua index 68be9a981..7e5de76b5 100644 --- a/Moose Development/Moose/Wrapper/Unit.lua +++ b/Moose Development/Moose/Wrapper/Unit.lua @@ -990,5 +990,16 @@ do -- Event Handling return self end + + --- Reset the subscriptions. + -- @param #UNIT self + -- @return #UNIT + function UNIT:ResetEvents( Event ) + + self:EventDispatcher():Reset( self ) + + return self + end + end \ No newline at end of file diff --git a/Moose Mission Setup/Moose.lua b/Moose Mission Setup/Moose.lua index 0f498d764..3720f9694 100644 --- a/Moose Mission Setup/Moose.lua +++ b/Moose Mission Setup/Moose.lua @@ -1,5 +1,5 @@ env.info( '*** MOOSE DYNAMIC INCLUDE START *** ' ) -env.info( 'Moose Generation Timestamp: 20170411_0630' ) +env.info( 'Moose Generation Timestamp: 20170411_0812' ) local base = _G