Пример #1
0
        T RunMethod <S, T>(T defaultIfNotExisting, params object[] parameters)
        {
            if (container == null)
            {
                return(defaultIfNotExisting);
            }

            var methodName = typeof(S).Name.Replace("Harmony", "");
            var method     = PatchTools.GetPatchMethod <S>(container, methodName);

            if (method != null)
            {
                if (typeof(T).IsAssignableFrom(method.ReturnType))
                {
                    return((T)method.Invoke(null, Type.EmptyTypes));
                }

                var input            = (parameters ?? new object[0]).Union(new object[] { instance }).ToArray();
                var actualParameters = AccessTools.ActualParameters(method, input);
                method.Invoke(null, actualParameters);
                return(defaultIfNotExisting);
            }

            return(defaultIfNotExisting);
        }
Пример #2
0
        /// <summary>Creates an empty patch class processor</summary>
        /// <param name="instance">The Harmony instance</param>
        /// <param name="type">The class to process</param>
        ///
        public PatchClassProcessor(Harmony instance, Type type)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            this.instance = instance;
            containerType = type;

            var harmonyAttributes = HarmonyMethodExtensions.GetFromType(type);

            if (harmonyAttributes == null || harmonyAttributes.Count == 0)
            {
                return;
            }

            containerAttributes = HarmonyMethod.Merge(harmonyAttributes);
            if (containerAttributes.methodType == null)             // MethodType default is Normal
            {
                containerAttributes.methodType = MethodType.Normal;
            }

            auxilaryMethods = new Dictionary <Type, MethodInfo>();
            foreach (var auxType in auxilaryTypes)
            {
                var method = PatchTools.GetPatchMethod(containerType, auxType.FullName);
                if (method != null)
                {
                    auxilaryMethods[auxType] = method;
                }
            }

            patchMethods = PatchTools.GetPatchMethods(containerType);
            foreach (var patchMethod in patchMethods)
            {
                var method = patchMethod.info.method;
                patchMethod.info        = containerAttributes.Merge(patchMethod.info);
                patchMethod.info.method = method;
            }
        }
Пример #3
0
        void RunMethod <S>(params object[] parameters)
        {
            if (container == null)
            {
                return;
            }

            var methodName = typeof(S).Name.Replace("Harmony", "");
            var method     = PatchTools.GetPatchMethod <S>(container, methodName);

            if (method != null)
            {
                var input            = (parameters ?? new object[0]).Union(new object[] { instance }).ToArray();
                var actualParameters = AccessTools.ActualParameters(method, input);
                method.Invoke(null, actualParameters);
            }

            return;
        }