示例#1
0
        /// <summary>
        /// Function will test if the game we're trying to inject to
        ///  is distributing a mscorlib.dll file and if so, we're going
        ///  to test whether reflection is enabled.
        /// </summary>
        /// <param name="strAssemblyPath"></param>
        /// <param name="strEntryPoint"></param>
        /// <param name="exception"></param>
        internal static bool IsReflectionEnabled(string strDataPath)
        {
            // This method will most definitely have to be enhanced as we encounter new
            //  situations where a game's mscorlib reflection functionality may have been disabled.
            const string ENTRY = "System.Reflection.Emit.AssemblyBuilder::DefineDynamicAssembly";

            bool bReflectionEnabled = true;

            if (!File.Exists(Path.Combine(strDataPath, Constants.MSCORLIB)))
            {
                // No custom corlib, safe to assume that reflection is enabled.
                return(bReflectionEnabled);
            }

            string tempFile = GetTempFile(Path.Combine(strDataPath, Constants.MSCORLIB));

            string[] entryPoint = ENTRY.Split(new string[] { "::" }, StringSplitOptions.None);
            try
            {
                AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(tempFile);
                TypeDefinition     type     = assembly.MainModule.GetType(entryPoint[0]);
                if (null == type)
                {
                    throw new NullReferenceException("Failed to find entry Type in mod assembly");
                }

                MethodDefinition meth = type.Methods
                                        .Where(method => method.Name.Contains(entryPoint[1]) && method.Parameters.Count == 2)
                                        .FirstOrDefault();

                if (null == meth)
                {
                    throw new NullReferenceException("Failed to find entry Method in mod assembly");
                }

                Instruction instr = meth.Body.Instructions
                                    .Where(instruction => instruction.ToString().Contains(nameof(PlatformNotSupportedException)))
                                    .SingleOrDefault();

                bReflectionEnabled = (instr == null);

                assembly.Dispose();
            }
            catch (Exception exc)
            {
                bReflectionEnabled = false;
            }

            DeleteTemp(tempFile);
            return(bReflectionEnabled);
        }
示例#2
0
        /// <summary>
        /// Function will test if the game we're trying to inject to
        ///  is distributing a mscorlib.dll file and if so, we're going
        ///  to test whether reflection is enabled.
        /// </summary>
        /// <param name="dataPath"></param>
        private bool IsReflectionEnabled(string dataPath)
        {
            // This method will most definitely have to be enhanced as we encounter new
            //  situations where a game's mscorlib reflection functionality may have been disabled.
            const string ENTRY             = "System.Reflection.Emit.AssemblyBuilder::DefineDynamicAssembly";
            string       corLib            = Path.Combine(dataPath, Constants.MSCORLIB);
            bool         reflectionEnabled = true;

            if (!File.Exists(corLib))
            {
                // No custom corlib, safe to assume that reflection is enabled.
                return(reflectionEnabled);
            }

            string tempFile = string.Empty;

            try {
                tempFile = Util.GetTempFile(Path.Combine(dataPath, Constants.MSCORLIB));
            } catch (Exception) {
                tempFile = Util.GetTempFile(Path.Combine(dataPath, Constants.MSCORLIB + VortexInjectorIPC.Constants.VORTEX_BACKUP_TAG));
            }

            string [] entryPoint = ENTRY.Split(new string [] { "::" }, StringSplitOptions.None);
            try {
                AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(tempFile);
                if (assembly.Name.Version.Major <= 3)
                {
                    // There doesn't seem to be a reason to replace the corlib
                    //  for older .NET versions.
                    assembly.Dispose();
                    return(true);
                }
                TypeDefinition type = assembly.MainModule.GetType(entryPoint [0]);
                if (null == type)
                {
                    throw new NullReferenceException("Failed to find entry Type in mod assembly");
                }

                MethodDefinition meth = type.Methods
                                        .Where(method => method.Name.Contains(entryPoint [1]) && method.Parameters.Count == 2)
                                        .FirstOrDefault();

                if (null == meth)
                {
                    throw new NullReferenceException("Failed to find entry Method in mod assembly");
                }

                Instruction instr = meth.Body.Instructions
                                    .Where(instruction => instruction.ToString().Contains(nameof(PlatformNotSupportedException)))
                                    .SingleOrDefault();

                reflectionEnabled = (instr == null);

                assembly.Dispose();
            } catch (Exception) {
                reflectionEnabled = false;
            }

            Util.DeleteTemp(tempFile);
            return(reflectionEnabled);
        }