Example #1
0
        public void StubType(TypeDefinition type)
        {
            foreach (FieldDefinition field in type.Fields)
            {
                field.Attributes &= ~FieldAttributes.HasFieldRVA;
            }

            foreach (MethodDefinition method in type.Methods)
            {
                if (method.HasPInvokeInfo)
                {
                    method.PInvokeInfo = null;
                }
                method.IsManaged      = true;
                method.IsIL           = true;
                method.IsNative       = false;
                method.PInvokeInfo    = null;
                method.IsPreserveSig  = false;
                method.IsInternalCall = false;
                method.IsPInvokeImpl  = false;

                MethodBody body = method.Body = new MethodBody(method);
                body.InitLocals = true;
                ILProcessor il = body.GetILProcessor();

                for (int i = 0; i < method.Parameters.Count; i++)
                {
                    ParameterDefinition param = method.Parameters[i];
                    if (param.IsOut || param.IsReturnValue)
                    {
                        il.Emit(OpCodes.Ldarg, param);
                        il.EmitDefault(param.ParameterType, true);
                    }
                }

                il.EmitDefault(method.ReturnType ?? method.Module.TypeSystem.Void);
                il.Emit(OpCodes.Ret);
            }

            foreach (TypeDefinition nested in type.NestedTypes)
            {
                StubType(nested);
            }
        }
Example #2
0
        private static void PostProcessMethod(MonoModder modder, MethodDefinition method)
        {
            // Find all FMOD-related extern methods and stub them.
            if (FMODStub &&
                !method.HasBody && method.HasPInvokeInfo && (
                    method.PInvokeInfo.Module.Name == "fmod" ||
                    method.PInvokeInfo.Module.Name == "fmodstudio")
                )
            {
                MonoModRule.Modder.Log($"[Everest] [FMODStub] Stubbing {method.FullName} -> {method.PInvokeInfo.Module.Name}::{method.PInvokeInfo.EntryPoint}");

                if (method.HasPInvokeInfo)
                {
                    method.PInvokeInfo = null;
                }
                method.IsManaged      = true;
                method.IsIL           = true;
                method.IsNative       = false;
                method.PInvokeInfo    = null;
                method.IsPreserveSig  = false;
                method.IsInternalCall = false;
                method.IsPInvokeImpl  = false;

                MethodBody body = method.Body = new MethodBody(method);
                body.InitLocals = true;
                ILProcessor il = body.GetILProcessor();

                for (int i = 0; i < method.Parameters.Count; i++)
                {
                    ParameterDefinition param = method.Parameters[i];
                    if (param.IsOut || param.IsReturnValue)
                    {
                        // il.Emit(OpCodes.Ldarg, i);
                        // il.EmitDefault(param.ParameterType, true);
                    }
                }

                il.EmitDefault(method.ReturnType ?? method.Module.TypeSystem.Void);
                il.Emit(OpCodes.Ret);
                return;
            }
        }