Esempio n. 1
0
        public static Dictionary <MethodDef, Tuple <int[], int[]> > CreateMethods(ModuleDef loadedMod)
        {
            DynamicCode code = new DynamicCode(3);

            int[] modules = new int[4];
            for (int i = 0; i < modules.Length; i++)
            {
                modules[i] = rand.Next(2, 25);
            }
            Instruction[,] methods = new Instruction[4, 10];
            for (int i = 0; i < 4; i++)
            {
                Instruction[] methodBody = code.Create();
                for (int y = 0; y < methodBody.Length; y++)
                {
                    methods[i, y] = methodBody[y];
                }
            }

            List <Tuple <Instruction[], Tuple <int, Tuple <int[], int[]> > > > InstrToInt =
                new List <Tuple <Instruction[], Tuple <int, Tuple <int[], int[]> > > >();

            for (int i = 0; i < 4; i++)
            {
                List <Instruction> instr = new List <Instruction>();
                int[] numbersTrue        = new int[5];
                int[] numbersFalse       = new int[5];
                for (int y = 0; y < 10; y++)
                {
                    instr.Add(methods[i, y]);
                }
                for (int y = 0; y < 5; y++)
                {
                    numbersTrue[y] = code.RandomNumberInModule(instr.ToArray(), modules[i], true);
                }
                for (int y = 0; y < 5; y++)
                {
                    numbersFalse[y] = code.RandomNumberInModule(instr.ToArray(), modules[i], false);
                }
                InstrToInt.Add(Tuple.Create(instr.ToArray(), Tuple.Create(modules[i], Tuple.Create(numbersTrue, numbersFalse))));
            }
            Dictionary <MethodDef, Tuple <int[], int[]> > final = new Dictionary <MethodDef, Tuple <int[], int[]> >();
            MethodAttributes methFlags = MethodAttributes.Public | MethodAttributes.Static
                                         | MethodAttributes.HideBySig | MethodAttributes.ReuseSlot;
            MethodImplAttributes methImplFlags = MethodImplAttributes.IL | MethodImplAttributes.Managed;

            for (int i = 0; i < 4; i++)
            {
                MethodDef methodDefs1 = new MethodDefUser(
                    "",
                    MethodSig.CreateStatic(loadedMod.CorLibTypes.Boolean, loadedMod.CorLibTypes.Int32),
                    methImplFlags, methFlags);
                RenameTask.Rename(methodDefs1);
                methodDefs1.Body = new CilBody();
                methodDefs1.ParamDefs.Add(new ParamDefUser("lol", 0));
                List <Instruction> preInstr = new List <Instruction>(InstrToInt[i].Item1);
                int module = InstrToInt[i].Item2.Item1;
                //preInstr.RemoveAt(preInstr.Count - 1);
                preInstr.Insert(preInstr.Count - 1, Instruction.CreateLdcI4(module));
                preInstr.Insert(preInstr.Count - 1, OpCodes.Rem.ToInstruction());
                preInstr.Insert(preInstr.Count - 1, Instruction.CreateLdcI4(0));
                preInstr.Insert(preInstr.Count - 1, Instruction.Create(OpCodes.Ceq));
                //preInstr.Insert(preInstr.Count - 1, Instruction.Create(OpCodes.Ret));
                foreach (var item in preInstr)
                {
                    methodDefs1.Body.Instructions.Add(item);
                }
                final.Add(methodDefs1, InstrToInt[i].Item2.Item2);
            }

            TypeDef type1 = new TypeDefUser("", "", loadedMod.CorLibTypes.Object.TypeDefOrRef);

            RenameTask.Rename(type1);
            type1.Attributes = dnlib.DotNet.TypeAttributes.Public | dnlib.DotNet.TypeAttributes.AutoLayout |
                               dnlib.DotNet.TypeAttributes.Class | dnlib.DotNet.TypeAttributes.AnsiClass;
            loadedMod.Types.Add(type1);
            foreach (var item in final)
            {
                type1.Methods.Add(item.Key);
            }
            return(final);
        }