- using System;
- using System.Collections.ObjectModel;
- using System.Collections.Generic;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using OpenMetaverse;
- namespace LinkBot
- {
- public class Linkset : InternalDict<uint, Primitive>
- {
- public uint LocalID
- {
- get { return this.First().Value.ParentID; }
- }
- }
- public class LinksetManager
- {
- private List<Linkset> _linksets;
- private GridClient _client;
- private bool _active = false;
- private bool _clientSettingsObjectTracking;
- public LinksetManager(GridClient client) :
- this(client, true){}
- public LinksetManager(GridClient client, bool active)
- {
- _client = client;
- Active = active;
- _linksets = new List<Linkset>();
- }
- public ReadOnlyCollection<Linkset> Linksets
- {
- get { return(_linksets.AsReadOnly()); }
- }
- public bool Active
- {
- get { return(_active); }
- set
- {
- if (value)
- {
- _clientSettingsObjectTracking = _client.Settings.OBJECT_TRACKING;
- _client.Settings.OBJECT_TRACKING = true;
- InitCallbacks(true);
- }
- else
- {
- _client.Settings.OBJECT_TRACKING = _clientSettingsObjectTracking;
- InitCallbacks(false);
- }
- }
- }
- protected void InitCallbacks(bool handle)
- {
- if (handle)
- {
- _client.Objects.ObjectUpdate += new EventHandler<PrimEventArgs>(Objects_ObjectUpdate);
- _client.Objects.KillObject += new EventHandler<KillObjectEventArgs>(Objects_KillObject);
- }
- else
- {
- _client.Objects.ObjectUpdate -= Objects_ObjectUpdate;
- _client.Objects.KillObject -= Objects_KillObject;
- }
- }
- void Objects_KillObject(object sender, KillObjectEventArgs e)
- {
- lock (_linksets)
- {
- foreach (Linkset linkset in _linksets)
- {
- if (linkset.LocalID == e.ObjectLocalID)
- _linksets.Remove(linkset);
- }
- }
- }
- void Objects_ObjectUpdate(object sender, PrimEventArgs e)
- {
- lock (_linksets)
- {
- bool found = false;
- foreach (Linkset linkset in _linksets)
- {
- if (linkset.ContainsKey(e.Prim.LocalID))
- {
- found = true;
- break;
- }
- else if (linkset.LocalID == e.Prim.ParentID)
- {
- found = true;
- linkset.Add(e.Prim.LocalID, e.Prim);
- break;
- }
- }
- if (!found)
- {
- Linkset linkset = new Linkset();
- linkset.Add(e.Prim.LocalID, e.Prim);
- _linksets.Add(linkset);
- }
- }
- }
- }
- }