示例#1
0
        MethodBase GetOriginalMethod()
        {
            var attr = containerAttributes;

            if (attr.declaringType == null)
            {
                return(null);
            }

            switch (attr.methodType)
            {
            case MethodType.Normal:
                if (attr.methodName == null)
                {
                    return(null);
                }
                return(AccessTools.DeclaredMethod(attr.declaringType, attr.methodName, attr.argumentTypes));

            case MethodType.Getter:
                if (attr.methodName == null)
                {
                    return(null);
                }
                return(AccessTools.DeclaredProperty(attr.declaringType, attr.methodName).GetGetMethod());

            case MethodType.Setter:
                if (attr.methodName == null)
                {
                    return(null);
                }
                return(AccessTools.DeclaredProperty(attr.declaringType, attr.methodName).GetSetMethod());

            case MethodType.Constructor:
                return(AccessTools.DeclaredConstructor(attr.declaringType, attr.argumentTypes));

            case MethodType.StaticConstructor:
                return(AccessTools.GetDeclaredConstructors(attr.declaringType)
                       .Where(c => c.IsStatic)
                       .FirstOrDefault());
            }

            return(null);
        }
示例#2
0
        internal PropertyInfo GetPropertyInfo(Type type, string name)
        {
            Dictionary <string, PropertyInfo> propertiesByType = null;

            if (properties.TryGetValue(type, out propertiesByType) == false)
            {
                propertiesByType = new Dictionary <string, PropertyInfo>();
                properties.Add(type, propertiesByType);
            }

            PropertyInfo property = null;

            if (propertiesByType.TryGetValue(name, out property) == false)
            {
                property = AccessTools.DeclaredProperty(type, name);
                propertiesByType.Add(name, property);
            }
            return(property);
        }
        private static DynamicMethod CreateIl2CppShim(DynamicMethod original, Type owner)
        {
            var patchName = original.Name + "_il2cpp";

            var parameters     = original.GetParameters();
            var result         = parameters.Types().ToList();
            var origParamTypes = result.ToArray();
            var paramTypes     = new Type[origParamTypes.Length];

            for (int i = 0; i < paramTypes.Length; ++i)
            {
                paramTypes[i] = UnhollowerSupport.IsGeneratedAssemblyType(origParamTypes[i]) ? typeof(IntPtr) : origParamTypes[i];
            }

            var origReturnType = AccessTools.GetReturnedType(original);
            var returnType     = UnhollowerSupport.IsGeneratedAssemblyType(origReturnType) ? typeof(IntPtr) : origReturnType;

            DynamicMethod method;

            method = new DynamicMethod(
                patchName,
                MethodAttributes.Public | MethodAttributes.Static,
                CallingConventions.Standard,
                returnType,
                paramTypes,
                owner,
                true
                );

            for (var i = 0; i < parameters.Length; i++)
            {
                method.DefineParameter(i + 1, parameters[i].Attributes, parameters[i].Name);
            }

            var il = method.GetILGenerator();

            // Load arguments, invoking the IntPrt -> Il2CppObject constructor for IL2CPP types
            for (int i = 0; i < origParamTypes.Length; ++i)
            {
                Emitter.Emit(il, OpCodes.Ldarg, i);
                if (UnhollowerSupport.IsGeneratedAssemblyType(origParamTypes[i]))
                {
                    Emitter.Emit(il, OpCodes.Newobj, Il2CppConstuctor(origParamTypes[i]));
                }
            }

            // Call the original patch with the now-correct types
            Emitter.Emit(il, OpCodes.Call, original);

            // If needed, unwrap the return value; then return
            if (UnhollowerSupport.IsGeneratedAssemblyType(origReturnType))
            {
                var pointerGetter = AccessTools.DeclaredProperty(UnhollowerSupport.Il2CppObjectBaseType, "Pointer").GetGetMethod();
                Emitter.Emit(il, OpCodes.Call, pointerGetter);
            }

            Emitter.Emit(il, OpCodes.Ret);

            DynamicTools.PrepareDynamicMethod(method);
            return(method);
        }