| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- namespace Base
- {
- public interface IObjectEvent
- {
- Type ValueType();
- void SetValue(object value);
- }
- public abstract class ObjectEvent<T> : IObjectEvent
- {
- private T value;
- protected T GetValue()
- {
- return value;
- }
- public void SetValue(object v)
- {
- this.value = (T)v;
- }
- public Type ValueType()
- {
- return typeof(T);
- }
- }
- public class ObjectManager : IDisposable
- {
- private readonly Dictionary<string, Assembly> assemblies = new Dictionary<string, Assembly>();
- private Dictionary<Type, IObjectEvent> objectEvents;
- private readonly Dictionary<long, Object> objects = new Dictionary<long, Object>();
- private List<long> starts = new List<long>();
- private List<long> newStarts = new List<long>();
- private List<long> updates = new List<long>(3000);
- private List<long> newUpdates = new List<long>(3000);
- private readonly List<long> loaders = new List<long>();
- public void Dispose()
- {
- foreach (Object o in this.objects.Values.ToArray())
- {
- o.Dispose();
- }
- }
- public void Register(string name, Assembly assembly)
- {
- this.assemblies[name] = assembly;
- objectEvents = new Dictionary<Type, IObjectEvent>();
- foreach (Assembly ass in this.assemblies.Values)
- {
- Type[] types = ass.GetTypes();
- foreach (Type type in types)
- {
- object[] attrs = type.GetCustomAttributes(typeof(ObjectEventAttribute), false);
- if (attrs.Length == 0)
- {
- continue;
- }
- object obj = Activator.CreateInstance(type);
- IObjectEvent objectEvent = obj as IObjectEvent;
- if (objectEvent == null)
- {
- Log.Error($"组件事件没有继承IComponentEvent: {type.Name}");
- }
- objectEvents[objectEvent.ValueType()] = objectEvent;
- }
- }
- this.Load();
- }
- public Assembly GetAssembly(string name)
- {
- return this.assemblies[name];
- }
- public Assembly[] GetAssemblies()
- {
- return this.assemblies.Values.ToArray();
- }
- private void Load()
- {
- foreach (long id in this.loaders)
- {
- Object obj;
- if (!this.objects.TryGetValue(id, out obj))
- {
- continue;
- }
- IObjectEvent objectEvent;
- if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
- {
- continue;
- }
- ILoader iLoader = objectEvent as ILoader;
- if (iLoader == null)
- {
- continue;
- }
- objectEvent.SetValue(obj);
- iLoader.Load();
- }
- }
- public void Add(Object obj)
- {
- if (objectEvents == null)
- {
- return;
- }
- this.objects.Add(obj.Id, obj);
- IObjectEvent objectEvent;
- if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
- {
- return;
- }
- IStart iStart = objectEvent as IStart;
- if (iStart != null)
- {
- this.newStarts.Add(obj.Id);
- }
- IUpdate iUpdate = objectEvent as IUpdate;
- if (iUpdate != null)
- {
- this.newUpdates.Add(obj.Id);
- }
- ILoader iLoader = objectEvent as ILoader;
- if (iLoader != null)
- {
- this.loaders.Add(obj.Id);
- }
- }
- public void Remove(long id)
- {
- this.objects.Remove(id);
- }
- public void Awake(long id)
- {
- Object obj;
- if (!objects.TryGetValue(id, out obj))
- {
- return;
- }
- IObjectEvent objectEvent;
- if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
- {
- return;
- }
- IAwake iAwake = objectEvent as IAwake;
- if (iAwake == null)
- {
- return;
- }
- objectEvent.SetValue(obj);
- iAwake.Awake();
- }
- public void Awake<P1>(long id, P1 p1)
- {
- Object obj;
- if (!objects.TryGetValue(id, out obj))
- {
- return;
- }
- IObjectEvent objectEvent;
- if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
- {
- return;
- }
- IAwake<P1> iAwake = objectEvent as IAwake<P1>;
- if (iAwake == null)
- {
- return;
- }
- objectEvent.SetValue(obj);
- iAwake.Awake(p1);
- }
- public void Awake<P1, P2>(long id, P1 p1, P2 p2)
- {
- Object obj;
- if (!objects.TryGetValue(id, out obj))
- {
- return;
- }
- IObjectEvent objectEvent;
- if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
- {
- return;
- }
- IAwake<P1, P2> iAwake = objectEvent as IAwake<P1, P2>;
- if (iAwake == null)
- {
- return;
- }
- objectEvent.SetValue(obj);
- iAwake.Awake(p1, p2);
- }
- public void Awake<P1, P2, P3>(long id, P1 p1, P2 p2, P3 p3)
- {
- Object obj;
- if (!objects.TryGetValue(id, out obj))
- {
- return;
- }
- IObjectEvent objectEvent;
- if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
- {
- return;
- }
- IAwake<P1, P2, P3> iAwake = objectEvent as IAwake<P1, P2, P3>;
- if (iAwake == null)
- {
- return;
- }
- objectEvent.SetValue(obj);
- iAwake.Awake(p1, p2, p3);
- }
- private void Start()
- {
- starts = newStarts;
- newStarts = new List<long>();
- foreach (long id in starts)
- {
- Object obj;
- if (!this.objects.TryGetValue(id, out obj))
- {
- continue;
- }
- IObjectEvent objectEvent;
- if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
- {
- continue;
- }
- IStart iStart = objectEvent as IStart;
- if (iStart == null)
- {
- continue;
- }
- objectEvent.SetValue(obj);
- iStart.Start();
- }
- }
-
- public void Update()
- {
- this.Start();
- // 交换update
- List<long> tmpUpdate = updates;
- updates = newUpdates;
- newUpdates = tmpUpdate;
- newUpdates.Clear();
- foreach (long id in updates)
- {
- Object obj;
- if (!objects.TryGetValue(id, out obj))
- {
- continue;
- }
- IObjectEvent objectEvent;
- if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent))
- {
- continue;
- }
- IUpdate iUpdate = objectEvent as IUpdate;
- if (iUpdate == null)
- {
- continue;
- }
- newUpdates.Add(id);
- objectEvent.SetValue(obj);
- try
- {
- iUpdate.Update();
- }
- catch (Exception e)
- {
- Log.Error(e.ToString());
- }
- }
- }
- public override string ToString()
- {
- var info = new Dictionary<string, int>();
- foreach (Object obj in objects.Values)
- {
- if (info.ContainsKey(obj.GetType().Name))
- {
- info[obj.GetType().Name] += 1;
- }
- else
- {
- info[obj.GetType().Name] = 1;
- }
- }
- info = info.OrderByDescending(s => s.Value).ToDictionary(p => p.Key, p => p.Value);
- StringBuilder sb = new StringBuilder();
- sb.Append("\r\n");
- foreach (string key in info.Keys)
- {
- sb.Append($"{info[key],10} {key}\r\n");
- }
- sb.Append($"\r\n start: {newStarts.Count}, update: {newUpdates.Count} total: {this.objects.Count}");
- return sb.ToString();
- }
- }
- }
|