예제 #1
0
            public _Cache_(Type targetType)
            {
                if (targetType == null)
                {
                    return;
                }

                foreach (FieldInfo field in targetType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
                {
                    string name = field.Name;
                    Getters[name] = (obj) => field.GetValue(obj);
                    Setters[name] = (obj, value) => field.SetValue(obj, value);
                }

                foreach (PropertyInfo prop in targetType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
                {
                    string name = prop.Name;

                    MethodInfo get = prop.GetGetMethod(true);
                    if (get != null)
                    {
                        Getters[name] = (obj) => get.Invoke(obj, _NoArgs);
                    }

                    MethodInfo set = prop.GetSetMethod(true);
                    if (set != null)
                    {
                        Setters[name] = (obj, value) => set.Invoke(obj, new object[] { value });
                    }
                }

                Dictionary <string, MethodInfo> methods = new Dictionary <string, MethodInfo>();

                foreach (MethodInfo method in targetType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
                {
                    string name = method.Name;
                    if (methods.ContainsKey(name))
                    {
                        methods[name] = null;
                    }
                    else
                    {
                        methods[name] = method;
                    }
                }

                foreach (KeyValuePair <string, MethodInfo> kvp in methods)
                {
                    if (kvp.Value == null)
                    {
                        continue;
                    }

                    FastReflectionDelegate cb = kvp.Value.GetFastDelegate();
                    Methods[kvp.Key] = (target, args) => cb(target, args);
                }
            }
        public RandomizerCallStaticMethod(Type t, string methodName, params object[] parameters)
        {
            MethodInfo info = t.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

            if (info == null)
            {
                throw new ArgumentException($"Class {t} has no static method {methodName}");
            }

            _method = info.CreateFastDelegate();

            _parameters = parameters;
        }
        internal CachedProperty(PropertyInfo propertyInfo)
        {
            if (propertyInfo.CanRead)
            {
                _get = CustomFastReflectionHelper.CreateFastDelegate(propertyInfo.GetGetMethod(true));
            }

            if (propertyInfo.CanWrite)
            {
                _set = CustomFastReflectionHelper.CreateFastDelegate(propertyInfo.GetSetMethod(true));
            }

            PropertyType = propertyInfo.PropertyType;
        }
예제 #4
0
        static DynData()
        {
            _DataHelper_.Collected += () => {
                HashSet <WeakReference> dead = new HashSet <WeakReference>();

                foreach (WeakReference weak in _DataMap.Keys)
                {
                    if (!weak.IsAlive)
                    {
                        dead.Add(weak);
                    }
                }

                foreach (WeakReference weak in dead)
                {
                    _DataMap.Remove(weak);
                }
            };

            // TODO: Use DynamicMethod to generate more performant getters and setters.
            foreach (FieldInfo field in typeof(TTarget).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
            {
                string name = field.Name;
                _SpecialGetters[name] = (obj) => field.GetValue(obj);
                _SpecialSetters[name] = (obj, value) => field.SetValue(obj, value);
            }

            foreach (PropertyInfo prop in typeof(TTarget).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
            {
                string name = prop.Name;

                MethodInfo get = prop.GetGetMethod(true);
                if (get != null)
                {
                    FastReflectionDelegate getFast = get.CreateFastDelegate(true);
                    _SpecialGetters[name] = (obj) => getFast(obj);
                }

                MethodInfo set = prop.GetSetMethod(true);
                if (set != null)
                {
                    FastReflectionDelegate setFast = set.CreateFastDelegate(true);
                    _SpecialSetters[name] = (obj, value) => setFast(obj, value);
                }
            }
        }
        public BepInExLogger(string source)
            : base(source)
        {
            var staticFlags   = BindingFlags.Static | BindingFlags.Public;
            var instanceFlags = BindingFlags.Instance | BindingFlags.Public;

            var logLevelType = Type.GetType("BepInEx.Logging.LogLevel, BepInEx", false);

            if (logLevelType == null)
            {
                throw new Exception("BepInEx is not loaded!");
            }

            var manualLogSourceType = Type.GetType("BepInEx.Logging.ManualLogSource, BepInEx", false);

            if (manualLogSourceType != null)
            {
                var loggerType            = Type.GetType("BepInEx.Logging.Logger, BepInEx", false);
                var createLogSourceMethod = loggerType.GetMethod("CreateLogSource", staticFlags, null, new[] { typeof(string) }, null);
                _logObject = createLogSourceMethod.Invoke(null, new object[] { Source });
                var logMethod = _logObject.GetType().GetMethod("Log", instanceFlags, null, new[] { logLevelType, typeof(object) }, null);
                _logMethod = CustomFastReflectionHelper.CreateFastDelegate(logMethod);
            }
            else
            {
                var loggerType = Type.GetType("BepInEx.Logger, BepInEx", false);
                _logObject = loggerType.GetProperty("CurrentLogger", staticFlags).GetValue(null, null);
                var logMethod = _logObject.GetType().GetMethod("Log", instanceFlags, null, new[] { logLevelType, typeof(object) }, null);
                _logMethod = CustomFastReflectionHelper.CreateFastDelegate(logMethod);
            }

            if (_logMethod == null)
            {
                throw new Exception("BepInEx is not loaded!");
            }
        }
        /// <inheritdoc />
        /// <summary>
        ///     Constructs the mod, assigns the instance and sets the name.
        /// </summary>
        protected Mod(string name = null)
        {
            if (string.IsNullOrEmpty(name))
            {
                name = GetType().Name;
            }

            if
            (
                this.GetType()
                .GetInterfaces()
                .FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IGlobalSettings <>))
                is Type globalType
            )
            {
                this.globalSettingsType = globalType.GetGenericArguments()[0];
                foreach (var mi in globalType.GetMethods())
                {
                    switch (mi.Name)
                    {
                    case nameof(IGlobalSettings <object> .OnLoadGlobal):
                        this.onLoadGlobalSettings = mi.GetFastDelegate();
                        break;

                    case nameof(IGlobalSettings <object> .OnSaveGlobal):
                        this.onSaveGlobalSettings = mi.GetFastDelegate();
                        break;
                    }
                }
            }

            if
            (
                this.GetType()
                .GetInterfaces()
                .FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ILocalSettings <>))
                is Type saveType
            )
            {
                this.saveSettingsType = saveType.GetGenericArguments()[0];

                foreach (var mi in saveType.GetMethods())
                {
                    switch (mi.Name)
                    {
                    case nameof(ILocalSettings <object> .OnLoadLocal):
                        this.onLoadSaveSettings = mi.GetFastDelegate();
                        break;

                    case nameof(ILocalSettings <object> .OnSaveLocal):
                        this.onSaveSaveSettings = mi.GetFastDelegate();
                        break;
                    }
                }
            }

            Name = name;

            Log("Initializing");

            _globalSettingsPath ??= Path.Combine(Application.persistentDataPath, $"{GetType().Name}.GlobalSettings.json");

            LoadGlobalSettings();
            HookSaveMethods();
        }
 internal CachedMethod(MethodInfo method)
 {
     _invoke = CustomFastReflectionHelper.CreateFastDelegate(method);
 }