Esempio n. 1
0
        public void LoadAssemblyInfo()
        {
            this.eventInfo           = new Dictionary <int, EntityTypeInfo>();
            this.typeToEntityEventId = new Dictionary <Type, int>();

            Type[]        types         = DllHelper.GetMonoTypes();
            List <string> allEntityType = Enum.GetNames(typeof(EntityEventType)).ToList();

            foreach (Type type in types)
            {
                object[] attrs = type.GetCustomAttributes(typeof(EntityEventAttribute), true);
                if (attrs.Length == 0)
                {
                    continue;
                }

                EntityEventAttribute entityEventAttribute = attrs[0] as EntityEventAttribute;

                int entityEventId = entityEventAttribute.ClassType;

                this.typeToEntityEventId[type] = entityEventId;

                if (!this.eventInfo.ContainsKey(entityEventId))
                {
                    this.eventInfo.Add(entityEventId, new EntityTypeInfo());
                }

                MethodInfo[] methodInfos = type.GetMethods(
                    BindingFlags.Instance | BindingFlags.Static | BindingFlags.InvokeMethod |
                    BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly);
                foreach (MethodInfo methodInfo in methodInfos)
                {
                    int n = methodInfo.GetParameters().Length;
                    if (methodInfo.IsStatic)
                    {
                        --n;
                    }

                    string sn = n > 0 ? $"{methodInfo.Name}{n}" : methodInfo.Name;
                    if (!allEntityType.Contains(sn))
                    {
                        continue;
                    }

                    EntityEventType t = EnumHelper.FromString <EntityEventType>(sn);
                    this.eventInfo[entityEventId].Add(t, new MonoStaticMethod(methodInfo));
                }
            }

#if !SERVER
            if (this.appDomain == null)
            {
                return;
            }

            IType[] ilTypes = this.appDomain.LoadedTypes.Values.ToArray();
            foreach (IType itype in ilTypes)
            {
                Type     type  = itype.ReflectionType;
                object[] attrs = type.GetCustomAttributes(typeof(EntityEventAttribute), true);
                if (attrs.Length == 0)
                {
                    continue;
                }

                EntityEventAttribute entityEventAttribute = attrs[0] as EntityEventAttribute;

                int entityEventId = entityEventAttribute.ClassType;

                if (!this.eventInfo.ContainsKey(entityEventId))
                {
                    this.eventInfo.Add(entityEventId, new EntityTypeInfo());
                }

                foreach (IMethod methodInfo in itype.GetMethods())
                {
                    int n = methodInfo.ParameterCount;
                    if (methodInfo.IsStatic)
                    {
                        --n;
                    }

                    string sn = n > 0 ? $"{methodInfo.Name}{n}" : methodInfo.Name;
                    if (!allEntityType.Contains(sn))
                    {
                        continue;
                    }

                    EntityEventType t = EnumHelper.FromString <EntityEventType>(sn);
                    this.eventInfo[entityEventId].Add(t, new ILStaticMethod(methodInfo, n));
                }
            }
#endif
        }
Esempio n. 2
0
        private void Register()
        {
            Type[] types = DllHelper.GetMonoTypes();
            foreach (Type type in types)
            {
                object[] attrs = type.GetCustomAttributes(typeof(ObjectSystemAttribute), false);

                if (attrs.Length == 0)
                {
                    continue;
                }

                object        obj          = Activator.CreateInstance(type);
                IObjectSystem objectSystem = obj as IObjectSystem;
                if (objectSystem == null)
                {
                    Log.Error($"组件事件没有继承IObjectEvent: {type.Name}");
                    continue;
                }
                this.disposerEvents[objectSystem.Type()] = objectSystem;
            }


            this.allEvents.Clear();
            foreach (Type type in types)
            {
                object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);

                foreach (object attr in attrs)
                {
                    EventAttribute aEventAttribute = (EventAttribute)attr;
                    object         obj             = Activator.CreateInstance(type);
                    if (!this.allEvents.ContainsKey((EventIdType)aEventAttribute.Type))
                    {
                        this.allEvents.Add((EventIdType)aEventAttribute.Type, new List <IEventMethod>());
                    }
                    this.allEvents[(EventIdType)aEventAttribute.Type].Add(new IEventMonoMethod(obj));
                }
            }

            // hotfix dll
            Type[] hotfixTypes = DllHelper.GetHotfixTypes();
            foreach (Type type in hotfixTypes)
            {
                object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
                foreach (object attr in attrs)
                {
                    EventAttribute aEventAttribute = (EventAttribute)attr;
#if ILRuntime
                    IEventMethod method = new IEventILMethod(type, "Run");
#else
                    object       obj    = Activator.CreateInstance(type);
                    IEventMethod method = new IEventMonoMethod(obj);
#endif
                    if (!allEvents.ContainsKey((EventIdType)aEventAttribute.Type))
                    {
                        allEvents.Add((EventIdType)aEventAttribute.Type, new List <IEventMethod>());
                    }
                    allEvents[(EventIdType)aEventAttribute.Type].Add(method);
                }
            }
        }