From 27d5e29487b25aac8ce6a532b0d4ae4157cb2d23 Mon Sep 17 00:00:00 2001 From: Madpeter <57852827+Madpeterz@users.noreply.github.com> Date: Thu, 5 Feb 2026 22:18:20 +0000 Subject: [PATCH] Refactor KnownAvatar into helper methods Extract avatar lookup logic from KnownAvatar into three methods: SyncAvatarsHashMap (sync local avatar map), KnownAvatarUUID (check by UUID via UUIDNameReply), and KnownAvatarName (check by display name via AvatarPickerReply). Reworked KnownAvatar to call SyncAvatarsHashMap and then validate using the new helpers, returning false for UUID.Zero and avoiding the previous always-true outcome. Preserves event subscription/teardown and timeout waits while clarifying visibility (protected helpers, public KnownAvatar) and cleaning up callback logic. --- SecondBotEvents/Services/DataStoreService.cs | 114 ++++++++++++------- 1 file changed, 70 insertions(+), 44 deletions(-) diff --git a/SecondBotEvents/Services/DataStoreService.cs b/SecondBotEvents/Services/DataStoreService.cs index 99a46c4..f0a7046 100644 --- a/SecondBotEvents/Services/DataStoreService.cs +++ b/SecondBotEvents/Services/DataStoreService.cs @@ -607,7 +607,7 @@ namespace SecondBotEvents.Services * lookup the avatar */ - public bool KnownAvatar(string name, UUID avatarId) + protected void SyncAvatarsHashMap() { int currentHash = GetClient().Network.CurrentSim.ObjectsAvatars.GetHashCode(); if (currentHash != ObjectsAvatarsHash) @@ -619,65 +619,91 @@ namespace SecondBotEvents.Services AddAvatar(A.ID, A.Name); } } - if (avatarId != UUID.Zero) - { - if(avatarsName2Key.ContainsValue(avatarId) == false) - { - bool hasReply = false; - AutoResetEvent fetchEvent = new(false); - void callback(object sender, UUIDNameReplyEventArgs e) - { - foreach (KeyValuePair av in e.Names) - { - if (av.Key == avatarId) - { - hasReply = true; - fetchEvent.Set(); - } - } - } - GetClient().Avatars.UUIDNameReply += callback; - GetClient().Avatars.RequestAvatarName(avatarId); + } - fetchEvent.WaitOne(1000, false); - GetClient().Avatars.UUIDNameReply -= callback; - return hasReply; - } + protected bool KnownAvatarUUID(UUID avatarId) + { + if (avatarsName2Key.ContainsValue(avatarId) == true) + { return true; } + bool hasReply = false; + AutoResetEvent fetchEvent = new(false); + void callback(object sender, UUIDNameReplyEventArgs e) + { + foreach (KeyValuePair av in e.Names) + { + if (av.Key == avatarId) + { + hasReply = true; + fetchEvent.Set(); + } + } + } + GetClient().Avatars.UUIDNameReply += callback; + GetClient().Avatars.RequestAvatarName(avatarId); + + fetchEvent.WaitOne(1000, false); + GetClient().Avatars.UUIDNameReply -= callback; + return hasReply; + } + protected bool KnownAvatarName(string name) + { + if (name == null) + { + return false; + } string[] bits = name.Split(' '); if (bits.Length == 1) { name += " Resident"; } - if (avatarsName2Key.ContainsKey(name) == false) + if (avatarsName2Key.ContainsKey(name) == true) { - UUID wantedReplyID = UUID.SecureRandom(); - - bool hasReply = false; - AutoResetEvent fetchEvent = new(false); - void callback(object sender, AvatarPickerReplyEventArgs e) + return true; + } + UUID wantedReplyID = UUID.SecureRandom(); + + bool hasReply = false; + AutoResetEvent fetchEvent = new(false); + void callback(object sender, AvatarPickerReplyEventArgs e) + { + if (e.QueryID == wantedReplyID) { - if (e.QueryID == wantedReplyID) + foreach (KeyValuePair av in e.Avatars) { - foreach (KeyValuePair av in e.Avatars) + if (av.Value == name) { - if (av.Value == name) - { - hasReply = true; - fetchEvent.Set(); - } + hasReply = true; + fetchEvent.Set(); } } } - GetClient().Avatars.AvatarPickerReply += callback; - GetClient().Avatars.RequestAvatarNameSearch(name, wantedReplyID); - - fetchEvent.WaitOne(1000, false); - GetClient().Avatars.AvatarPickerReply -= callback; - return hasReply; } - return true; + GetClient().Avatars.AvatarPickerReply += callback; + GetClient().Avatars.RequestAvatarNameSearch(name, wantedReplyID); + + fetchEvent.WaitOne(1000, false); + GetClient().Avatars.AvatarPickerReply -= callback; + return hasReply; + } + + public bool KnownAvatar(string name, UUID avatarId) + { + SyncAvatarsHashMap(); + if (avatarId == UUID.Zero) + { + return false; + } + if(KnownAvatarUUID(avatarId) == true) + { + return true; + } + if(KnownAvatarName(name) == true) + { + return true; + } + return false; } public void GetAvatarNames(List avatars)