Ejemplo n.º 1
0
        public static TypeDefinition ImportFlagHelper(ModuleDefinition module, VMConstants constants)
        {
            // Clone flag helper class.
            var cloner = new MemberCloner(module);

            cloner.Include(VmHelperType);
            var result         = cloner.Clone();
            var flagHelperType = result.ClonedMembers.OfType <TypeDefinition>().First();

            module.Assembly.Modules[0].TopLevelTypes.Add(flagHelperType);

            // Obtain static cctor.
            var constructor  = flagHelperType.Methods.First(x => x.IsConstructor && x.IsStatic);
            var instructions = constructor.CilMethodBody.Instructions;

            instructions.Clear();

            // Assign values of flags to the fields.
            foreach (var entry in constants.Flags.OrderBy(x => x.Value))
            {
                instructions.Add(CilInstruction.CreateLdcI4(entry.Key));
                instructions.Add(new CilInstruction(CilOpCodes.Stsfld,
                                                    flagHelperType.Fields.First(x => x.Name == "FL_" + entry.Value.ToString())));
            }

            instructions.Add(new CilInstruction(CilOpCodes.Ret));

            return(flagHelperType);
        }
Ejemplo n.º 2
0
        protected static void InjectLoader(ModuleDefinition targetModule, Type loaderClass)
        {
            var sourceModule = ModuleDefinition.FromFile(typeof(Packer).Assembly.Location);
            var cloner       = new MemberCloner(targetModule);
            var loader       = (TypeDefinition)sourceModule.LookupMember(loaderClass.MetadataToken);

            cloner.Include(loader, true);
            var result = cloner.Clone();

            foreach (var clonedType in result.ClonedTopLevelTypes)
            {
                targetModule.TopLevelTypes.Add(clonedType);
            }

            result.GetClonedMember(loader).Namespace = "";

            var entryPoint = (MethodDefinition)result.ClonedMembers.First(m => m.Name == "Main");

            entryPoint.Name = ".origami";
            entryPoint.DeclaringType.Name = "<Origami>";

            targetModule.ManagedEntrypoint = entryPoint;
        }