Default event Handler Implementation

A place to submit .patch fixes for the DOL SVN

Moderator: Developer Team

Default event Handler Implementation

Postby Leodagan » Fri Sep 13, 2013 5:08 am

I created a IDOLEventHandler Interface.

Once it's implemented in a class it provide a "void Notify(DOLEvent e, object sender, EventArgs args);" method to any object who would need event notification.

I can just add this Interface to some objects like Brain and GameObject without any modification to test them for Event Handling compatibility ! (they already implement the Notify() Handler)

useful for
Code: Select all
if(object is IDOLEventHandler) { object.Notify(dolevent, this, new EventArgs()); }
Is it a good idea to use this kind of interface in objects needing notifications ?
Attachments
IDOLEventHandler.cs
(1.04 KiB) Downloaded 12 times
User avatar
Leodagan
Developer
 
Posts: 1350
Joined: Tue May 01, 2012 9:30 am
Website: https://daoc.freyad.net
Location: Lyon

Re: Default event Handler Implementation

Postby Graveen » Sun Sep 15, 2013 4:15 pm

Can you provide some (quick) examples in this post of in the SVN commit ? Thank you !
Image
* pm me to contribute in Dawn of Light: code, database *
User avatar
Graveen
Project Leader
 
Posts: 12660
Joined: Fri Oct 19, 2007 9:22 pm
Location: France

Re: Default event Handler Implementation

Postby Leodagan » Tue Sep 17, 2013 7:35 am

I'll use my LosCheck Manager as example :
Code: Select all
/// <summary> /// Dictionary holding a list of object needing Notify /// </summary> private Dictionary<int, List<IDOLEventHandler>> m_registeredLosEvents = new Dictionary<int, List<IDOLEventHandler>>(); private void AddRegisteredEvent(int key, object notifier) { if(notifier is IDOLEventHandler) RegisteredLosEvents[key].Add(notifier); } private static void NotifyObjects(IEnumerable<IDOLEventHandler> notifiers) { foreach(IDOLEventHandler notif in notifiers) notif.Notify(DOLEvent e, this, new EventArgs()); }
This allow to identify clearly which object are supporting "Notify()" ;)
User avatar
Leodagan
Developer
 
Posts: 1350
Joined: Tue May 01, 2012 9:30 am
Website: https://daoc.freyad.net
Location: Lyon

Re: Default event Handler Implementation

Postby Ephemeral » Sun Sep 29, 2013 8:54 pm

For reference the reason that there wasn't an interface for this in the first place is because there was no point for it, that isn't how delegates are supposed to be specified, all they have to do is match the function signature, which in the case of things like static event handlers is the only way it can be done because interfaces do not allow you to have static functions in them.
"The swarm is always smarter, faster and more adept than any actor which seeks to thwart it."
C++, Java, C#, VB.NET, ASM.
Current Project:
Engine Developer for The Red Solstice
User avatar
Ephemeral
DOL Freak
 
Posts: 554
Joined: Wed Mar 30, 2011 12:23 am

Re: Default event Handler Implementation

Postby Leodagan » Mon Sep 30, 2013 5:01 am

From what I remember "Notify" in GameObject class and subclass, aren't static method. (maybe in other class I haven't checked)

Nor they are Delegate, DOL Event Mgr seems to check class type for specific one that can receive Notify message or not...

Looks totally like a case where to use an interface :)

Or I get something wrong ?
User avatar
Leodagan
Developer
 
Posts: 1350
Joined: Tue May 01, 2012 9:30 am
Website: https://daoc.freyad.net
Location: Lyon

Re: Default event Handler Implementation

Postby Ephemeral » Wed Oct 02, 2013 6:25 am

Code: Select all
/// <summary> /// Caches the DBPrivilegeGroup from the database to PrivilegeGroups within /// the server's memory for quick lookup and management. /// </summary> [GameServerStartedEvent] public static void LoadGroupCache(DOLEvent e, object sender, EventArgs arguments) { if (ServerProperties.Properties.USE_NEW_PRIVILEGE_SYSTEM) { Log.Info("[Privilege Manager] Loading Groups Cache."); UpdateDefaults(); m_groupCache = new Dictionary<int, PrivilegeGroup>(); m_parameterizedTypeCache = LoadParameterizedCache(); Log.Info("[Privilege Manager] Loaded " + m_parameterizedTypeCache.Count + " Parameterized Type Bindings."); IList<PrivilegeGroup> tmpGroups = GameServer.Database.SelectAllObjects<DBPrivilegeGroup>().Select(dbGrp => new PrivilegeGroup(dbGrp)).ToList(); foreach (PrivilegeGroup privilegeGroup in tmpGroups) { privilegeGroup.Initialize(); m_groupCache.Add(privilegeGroup.DBEntry.GroupIndex, privilegeGroup); } foreach (PrivilegeGroup privilegeGroup in tmpGroups) { privilegeGroup.ResolveGroups(); } foreach (PrivilegeGroup privGrp in tmpGroups.Where(privGrp => privGrp.HasCircularInheritanceChain())) { Log.Error("[Privilege Manager] Group has Circular Inheritance Chain -> " + privGrp.DBEntry.Name); m_groupCache.Remove(privGrp.DBEntry.GroupIndex); } WhoBinding whoBinding = (WhoBinding) GetParameterizedPrivilege("who", new[]{"derp", "herp"}); Log.Error(String.Format("[Privilege Manager] Has Indexed under derp? {0}{1}", whoBinding.IsIndexedAs("derp"), whoBinding.IsIndexedAs("derp") ? " yes it is as -> " + whoBinding.GetAliasFor("derp") : " is not indexed")); } }
Example of a static event handler, and an example of why an interface isn't needed, if you looking around the core you're going to find more instances of static event handlers that are just made by matching the function signature and being manually registered or loaded with reflection.
"The swarm is always smarter, faster and more adept than any actor which seeks to thwart it."
C++, Java, C#, VB.NET, ASM.
Current Project:
Engine Developer for The Red Solstice
User avatar
Ephemeral
DOL Freak
 
Posts: 554
Joined: Wed Mar 30, 2011 12:23 am


Return to “%s” DOL Code Contributions

Who is online

Users browsing this forum: No registered users and 1 guest