public void RegisterHandler(MessageProcessUnitBase module, int noticeID, NotificationHandlerDelegate handler) { if (module != null && handler != null) { NotificationHandler notificationHandler = new NotificationHandler(noticeID, handler); CollectionUtil.AddIntoTable(noticeID, notificationHandler, notificationHandlerTable); CollectionUtil.AddIntoTable(module, noticeID, notificationHandler, moduleNotificationRegisterTable); #if UNITY_EDITOR if (LogUtil.ShowDebug != null) { LogUtil.ShowDebug(string.Format("[本地通知中心]1.注册通知处理器:来自[{0}] 通知ID[{1}] 处理回调[{2}]", module.GetType().Name, noticeID, StringUtility.ToString(handler))); } #endif } #if UNITY_EDITOR else { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("注册通知处理器时传递的参数有[null]值:通知ID[{0}] 模块[{1}] 处理回调[{2}]", noticeID, StringUtility.ToString(module), StringUtility.ToString(handler))); } } #endif }
public void RegisterHandler <T>(int classID, int functionID, NetworkMessageProcessUnitBase module, NetworkMessageHandlerDelegate <T> handler) where T : INetworkMessage { Type msgType = typeof(T); #if UNITY_EDITOR if (!msgType.IsValueType) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("网络消息结构体[{0}]必须定义为值类型(struct)", msgType.Name)); } return; } object[] fieldsDefined = msgType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (fieldsDefined.Length <= 0) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("网络消息结构体[{0}]中没有定义任何成员,如果不需要向外通知任何参数的话,使用Notification", msgType.Name)); } return; } #endif if (module != null && handler != null) { long identity = ((long)classID << 32) | (uint)functionID; #if UNITY_EDITOR if (messageHandlerTable.ContainsKey(identity) && messageHandlerTable[identity].Count > 0 && messageHandlerTable[identity][0].messageType != msgType) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("网络消息[{0}-{1}]绑定了多个不同的消息结构体!", classID, functionID)); } return; } #endif NetworkMessageHandler messageHandler = new NetworkMessageHandler(identity, msgType, handler); CollectionUtil.AddIntoTable(identity, messageHandler, messageHandlerTable); CollectionUtil.AddIntoTable(module, identity, messageHandler, moduleMessageRegisterTable); #if UNITY_EDITOR if (LogUtil.ShowDebug != null) { LogUtil.ShowDebug(string.Format("[网络消息中心]1.注册消息处理器:来自[{0}] 消息类型[{1}-{2}][{3}] 处理回调[{4}]", module.GetType().Name, classID, functionID, msgType.Name, StringUtility.ToString(handler))); } #endif } #if UNITY_EDITOR else { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("注册网络消息处理器时传递的参数有[null]值:消息标识[{0}-{1}] 参数类型[{2}] 模块[{3}] 处理回调[{4}]", classID, functionID, msgType.Name, module.GetType().Name, StringUtility.ToString(handler))); } } #endif }
public T GetData <T>(string alias = "") where T : DataStructBase { T res = null; Type dataType = typeof(T); if (dataType != null) { object cachedRef = null; if (dataCacheTable != null) { cachedRef = CollectionUtil.GetFromTable(dataType, alias, dataCacheTable); } if (cachedRef != null) { #if UNITY_EDITOR if (LogUtil.ShowDebug != null) { LogUtil.ShowDebug(string.Format("[数据中心]从缓存中读取数据引用: 数据集合[{0}] 类型[{1}](别名[{2}])", GetType().Name, dataType.Name, alias)); } #endif res = cachedRef as T; } else { FieldInfo field = CollectionUtil.GetFromTable(dataType, alias, dataTypeAliasTable); if (field != null) { res = field.GetValue(this) as T; if (dataCacheTable == null) { dataCacheTable = new Dictionary <Type, Dictionary <string, object> >(); } CollectionUtil.AddIntoTable(dataType, alias, res, dataCacheTable); } #if UNITY_EDITOR else { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("数据集合[{0}]中没有注册类型为[{1}](别名[{2}])的成员!", GetType().Name, dataType.Name, alias)); } } #endif } } #if UNITY_EDITOR else { if (LogUtil.ShowError != null) { LogUtil.ShowError("获取数据时提供的参数有null值!"); } } #endif return(res); }
protected Queue <AsyncResourceRequester> requestPool = null; // 回收释放掉的AsyncResourceRequester,避免每次申请资源时都new public virtual AsyncResourceRequester LoadResource(string resourceName, Action <AsyncResourceRequester, object> onResourceLoadedCallback, object callbackExtraParam) { ResourceHolder resourceHolder = null; AsyncResourceRequester res = null; if (resourceTable != null && resourceTable.TryGetValue(resourceName, out resourceHolder)) { // 请求的资源已经加载上来了,直接增加资源引用计数,生成请求器,并立刻调用资源加载成功的回调 IncreaseResourceReferenceCount(resourceName); if (requestPool != null && requestPool.Count > 0) { res = requestPool.Dequeue(); } else { res = new AsyncResourceRequester(); } res.Init(resourceName, resourceHolder, onResourceLoadedCallback, callbackExtraParam); res.progress = Constants.oneFloat; res.done = true; if (onResourceLoadedCallback != null) { onResourceLoadedCallback(res, callbackExtraParam); } //// 最好延迟调用,否则会导致先调用了回调,然后函数才返回的问题 //if (onResourceLoadedCallback != null) // DelayedFunctionCaller.DelayedCall(0, onResourceLoadedCallback, res, callbackExtraParam); } else { // 请求的资源还没有加载上来,调用异步加载资源的逻辑,生成请求器,将请求器加入缓存 LoadResourceAsset(resourceName); if (requestPool != null && requestPool.Count > 0) { res = requestPool.Dequeue(); } else { res = new AsyncResourceRequester(); } res.Init(resourceName, null, onResourceLoadedCallback, callbackExtraParam); if (requestCacheTable == null) { requestCacheTable = new Dictionary <string, List <AsyncResourceRequester> >(); } CollectionUtil.AddIntoTable(resourceName, res, requestCacheTable); } return(res); }
public void RegisterProvider(Type dataType, string alias, DataProvider dataProvider) { if (dataType != null && dataProvider != null) { if (dataProviderTable == null) { dataProviderTable = new Dictionary <Type, Dictionary <string, DataProvider> >(); } #if UNITY_EDITOR if (CollectionUtil.GetFromTable(dataType, alias, dataProviderTable) != null) { if (LogUtil.ShowError != null) { if (string.IsNullOrEmpty(alias)) { LogUtil.ShowError(string.Format("数据中心已经注册有[{0}]类型的成员,请为来自[{1}]的该类型数据添加别名", dataType.Name, dataProvider.GetType().Name)); } else { LogUtil.ShowError(string.Format("数据中心已经注册有[{0}]类型且别名为[{1}]的成员,来自[{2}]的数据需要修改别名", dataType.Name, alias, dataProvider.GetType().Name)); } } } #endif CollectionUtil.AddIntoTable(dataType, alias, dataProvider, dataProviderTable); #if UNITY_EDITOR if (LogUtil.ShowDebug != null) { LogUtil.ShowDebug(string.Format("[数据中心]1.注册数据:来自[{0}] 数据类型[{1}] 别名[{2}]", dataProvider.GetType().Name, dataType.Name, alias)); } #endif } #if UNITY_EDITOR else { if (LogUtil.ShowError != null) { LogUtil.ShowError("注册DataProvider时提供的参数有null值!"); } } #endif }
public void RegisterRawInputEventHandler(IRawInputEventHandler handler) { ERawInputMetaType metaType = InputUtil.GetMetaTypeOfRawEventHandler(handler); if (metaType != ERawInputMetaType.Unknown) { if ((metaType & ERawInputMetaType.Touch) != 0) { CollectionUtil.AddIntoTable(ERawInputMetaType.Touch, handler, handlerTable, true); } if ((metaType & ERawInputMetaType.Axis) != 0) { CollectionUtil.AddIntoTable(ERawInputMetaType.Axis, handler, handlerTable, true); } if ((metaType & ERawInputMetaType.Keyboard) != 0) { CollectionUtil.AddIntoTable(ERawInputMetaType.Keyboard, handler, handlerTable, true); } if ((metaType & ERawInputMetaType.Mouse) != 0) { CollectionUtil.AddIntoTable(ERawInputMetaType.Mouse, handler, handlerTable, true); } } }
protected DataProvider() { FieldInfo[] fields = GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); Type dataStructBaseType = typeof(DataStructBase); foreach (FieldInfo field in fields) { Type dataFieldType = field.FieldType; #if UNITY_EDITOR if (dataFieldType.IsPrimitive) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("数据集合[{0:s}]中的成员[{1}]是基础类型,不允许直接对外提供基础数据类型的成员!", GetType().Name, field.Name)); } continue; } else if (!dataFieldType.IsSubclassOf(dataStructBaseType)) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("数据集合[{0:s}]中的成员[{1}]没有从DataStructBase派生,对外提供的数据成员必须从DataStructBase派生!", GetType().Name, field.Name)); } continue; } #endif object[] attributesOnField = field.GetCustomAttributes(typeof(ProvideThisDataAttribute), false); if (attributesOnField == null || attributesOnField.Length <= 0) { continue; // 这个域不提供外部查询,跳过 } #if UNITY_EDITOR else if (attributesOnField.Length > 1) { if (LogUtil.ShowWarning != null) { LogUtil.ShowWarning(string.Format("数据集合[{0:s}]中的成员[{1}]定义了多个ProvideThisData属性,只有第一条会生效", GetType().Name, field.Name)); } } #endif string alias = (attributesOnField[0] as ProvideThisDataAttribute).alias; if (dataTypeAliasTable == null) { dataTypeAliasTable = new Dictionary <Type, Dictionary <string, FieldInfo> >(); } #if UNITY_EDITOR if (CollectionUtil.GetFromTable(field.FieldType, alias, dataTypeAliasTable) != null) { if (LogUtil.ShowError != null) { if (string.IsNullOrEmpty(alias)) { LogUtil.ShowError(string.Format("数据集合[{0:s}]中已经注册有[{1}]类型的成员,请添加别名", GetType().Name, dataFieldType.Name)); } else { LogUtil.ShowError(string.Format("数据集合[{0:s}]中的成员[{1}]定义的别名[{2}]已经被使用,请确保别名的唯一性", GetType().Name, field.Name, alias)); } } } #endif CollectionUtil.AddIntoTable(dataFieldType, alias, field, dataTypeAliasTable); RegisterToDataCenter(dataFieldType, alias); } }
public void RegisterHandler(MessageProcessUnitBase module, MessageHandlerDelegate handler, Type msgType) { if (!typeof(IMessage).IsAssignableFrom(msgType)) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("消息结构体[{0}]没有实现IMessage接口", msgType.Name)); } return; } #endif #if UNITY_EDITOR if (!msgType.IsValueType) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("消息结构体[{0}]必须定义为值类型(struct)", msgType.Name)); } return; } object[] fieldsDefined = msgType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (fieldsDefined.Length <= 0) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("消息结构体[{0}]中没有定义任何成员,如果不需要向外通知任何参数的话,使用Notification", msgType.Name)); } return; } #endif if (module != null && handler != null) { MessageHandler messageHandler = new MessageHandler(msgType, handler); if (!messageHandlerTableLocked) { if (messageHandlerTable == null) { messageHandlerTable = new Dictionary <Type, List <MessageHandler> >(); } CollectionUtil.AddIntoTable(msgType, messageHandler, messageHandlerTable); #if UNITY_EDITOR if (LogUtil.ShowDebug != null) { LogUtil.ShowDebug(string.Format("[本地消息中心]1.注册消息处理器:来自[{0}] 消息类型[{1}] 处理回调[{2}]", module.GetType().Name, msgType.Name, StringUtility.ToString(handler))); } #endif } else { RegisterModifier modifier = pool.Aquire(); modifier.isUnregister = false; modifier.messageType = msgType; modifier.messageHandler = messageHandler; modifier.module = module; if (messageHandlerRegisterChangeCache == null) { messageHandlerRegisterChangeCache = new List <RegisterModifier>(); } messageHandlerRegisterChangeCache.Add(modifier); } if (moduleMessageRegisterTable == null) { moduleMessageRegisterTable = new Dictionary <MessageProcessUnitBase, Dictionary <Type, MessageHandler> >(); } CollectionUtil.AddIntoTable(module, msgType, messageHandler, moduleMessageRegisterTable); } #if UNITY_EDITOR else { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("注册消息处理器时传递的参数有[null]值:消息类型[{0}] 模块[{1}] 处理回调[{2}]", msgType.Name, StringUtility.ToString(module), StringUtility.ToString(handler))); } } #endif }
public void DeliverMessage(IMessage msg) #endif { if (msg != null) { //msg.TimeStamp = DateTimeUtil.NowMillisecond; if (messageHandlerTable != null) { messageHandlerTableLocked = true; List <MessageHandler> handlers; if (messageHandlerTable.TryGetValue(msg.GetType(), out handlers)) { foreach (MessageHandler handler in handlers) { Invoke(handler, msg); } } messageHandlerTableLocked = false; // 如果在消息处理的过程中有生成新的消息处理器或者销毁老的处理器,那么这些操作都被挂起并缓存到了列表里,下面要对这个缓存列表做处理 if (messageHandlerRegisterChangeCache != null && messageHandlerRegisterChangeCache.Count > 0) { for (int i = 0; i < messageHandlerRegisterChangeCache.Count; ++i) { RegisterModifier modifier = messageHandlerRegisterChangeCache[i]; if (modifier.isUnregister) { CollectionUtil.RemoveFromTable(modifier.messageType, modifier.messageHandler, messageHandlerTable); #if UNITY_EDITOR if (LogUtil.ShowDebug != null) { LogUtil.ShowDebug(string.Format("[本地消息中心]2.注销消息处理器:来自[{0}] 消息类型[{1}] 处理回调[{2}]", modifier.module.GetType().Name, modifier.messageType.Name, StringUtility.ToString(modifier.messageHandler.Handler))); } #endif } else { if (messageHandlerTable == null) { messageHandlerTable = new Dictionary <Type, List <MessageHandler> >(); } CollectionUtil.AddIntoTable(modifier.messageType, modifier.messageHandler, messageHandlerTable); #if UNITY_EDITOR if (LogUtil.ShowDebug != null) { LogUtil.ShowDebug(string.Format("[本地消息中心]1.注册消息处理器:来自[{0}] 消息类型[{1}] 处理回调[{2}]", modifier.module.GetType().Name, modifier.messageType.Name, StringUtility.ToString(modifier.messageHandler.Handler))); } #endif } pool.Return(modifier); } messageHandlerRegisterChangeCache.Clear(); } } } #if UNITY_EDITOR else { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("发送消息时传递的参数有[null]值:消息[{0}]", StringUtility.ToString(msg))); } } #endif }