protected override void Pack(ConfuserContext context, ProtectionParameters parameters)
        {
            var ctx = context.Annotations.Get <CompressorContext>(context, ContextKey);

            if (ctx == null)
            {
                context.Logger.Error("No executable module!");
                throw new ConfuserException(null);
            }

            ModuleDefMD originModule = context.Modules[ctx.ModuleIndex];

            ctx.OriginModuleDef = originModule;

            var stubModule = new ModuleDefUser(ctx.ModuleName, originModule.Mvid, originModule.CorLibTypes.AssemblyRef);

            if (ctx.CompatMode)
            {
                var assembly = new AssemblyDefUser(originModule.Assembly);
                assembly.Name += ".cr";
                assembly.Modules.Add(stubModule);
            }
            else
            {
                ctx.Assembly.Modules.Insert(0, stubModule);
                ImportAssemblyTypeReferences(originModule, stubModule);
            }
            stubModule.Characteristics           = originModule.Characteristics;
            stubModule.Cor20HeaderFlags          = originModule.Cor20HeaderFlags;
            stubModule.Cor20HeaderRuntimeVersion = originModule.Cor20HeaderRuntimeVersion;
            stubModule.DllCharacteristics        = originModule.DllCharacteristics;
            stubModule.EncBaseId           = originModule.EncBaseId;
            stubModule.EncId               = originModule.EncId;
            stubModule.Generation          = originModule.Generation;
            stubModule.Kind                = ctx.Kind;
            stubModule.Machine             = originModule.Machine;
            stubModule.RuntimeVersion      = originModule.RuntimeVersion;
            stubModule.TablesHeaderVersion = originModule.TablesHeaderVersion;
            stubModule.Win32Resources      = originModule.Win32Resources;

            InjectStub(context, ctx, parameters, stubModule);

            var snKey = context.Annotations.Get <StrongNameKey>(originModule, Marker.SNKey);

            using (var ms = new MemoryStream()) {
                var options = new ModuleWriterOptions(stubModule)
                {
                    StrongNameKey = snKey
                };
                var injector = new KeyInjector(ctx);
                options.WriterEvent += injector.WriterEvent;

                stubModule.Write(ms, options);
                context.CheckCancellation();
                ProtectStub(context, context.OutputPaths[ctx.ModuleIndex], ms.ToArray(), snKey, new StubProtection(ctx, originModule));
            }
        }
Example #2
0
        protected override void Pack(ConfuserContext context, ProtectionParameters parameters)
        {
            var ctx = context.Annotations.Get <CompressorContext>(context, ContextKey);

            if (ctx == null)
            {
                context.Logger.Error("No executable module!");
                throw new ConfuserException(null);
            }

            ModuleDefMD originModule = context.Modules[ctx.ModuleIndex];

            ctx.OriginModuleDef = originModule;

            var stubModule = new ModuleDefUser(ctx.ModuleName, originModule.Mvid, originModule.CorLibTypes.AssemblyRef);

            if (ctx.CompatMode)
            {
                var assembly = new AssemblyDefUser(originModule.Assembly);
                assembly.Name += ".cr";
                assembly.Modules.Add(stubModule);
                var targetFramework = originModule.Assembly.CustomAttributes.FirstOrDefault(ca => ca.TypeFullName == "System.Runtime.Versioning.TargetFrameworkAttribute");
                if (targetFramework != null)
                {
                    var attrType = stubModule.CorLibTypes.GetTypeRef("System.Runtime.Versioning", "TargetFrameworkAttribute");
                    var ctorSig  = MethodSig.CreateInstance(stubModule.CorLibTypes.Void, stubModule.CorLibTypes.String);
                    assembly.CustomAttributes.Add(new CustomAttribute(
                                                      new MemberRefUser(stubModule, ".ctor", ctorSig, attrType), new CAArgument[] { new CAArgument(stubModule.CorLibTypes.String, targetFramework.ConstructorArguments[0].Value) }));
                }
            }
            else
            {
                ctx.Assembly.Modules.Insert(0, stubModule);
                ImportAssemblyTypeReferences(originModule, stubModule);
            }
            stubModule.Characteristics           = originModule.Characteristics;
            stubModule.Context                   = originModule.Context;
            stubModule.Cor20HeaderFlags          = originModule.Cor20HeaderFlags;
            stubModule.Cor20HeaderRuntimeVersion = originModule.Cor20HeaderRuntimeVersion;
            stubModule.DllCharacteristics        = originModule.DllCharacteristics;
            stubModule.EncBaseId                 = originModule.EncBaseId;
            stubModule.EncId               = originModule.EncId;
            stubModule.Generation          = originModule.Generation;
            stubModule.Kind                = ctx.Kind;
            stubModule.Machine             = originModule.Machine;
            stubModule.RuntimeVersion      = originModule.RuntimeVersion;
            stubModule.TablesHeaderVersion = originModule.TablesHeaderVersion;
            stubModule.Win32Resources      = originModule.Win32Resources;

            InjectStub(context, ctx, parameters, stubModule);

            var snKey       = context.Annotations.Get <StrongNameKey>(originModule, Marker.SNKey);
            var snPubKey    = context.Annotations.Get <StrongNamePublicKey>(originModule, Marker.SNPubKey);
            var snDelaySig  = context.Annotations.Get <bool>(originModule, Marker.SNDelaySig, false);
            var snSigKey    = context.Annotations.Get <StrongNameKey>(originModule, Marker.SNSigKey);
            var snPubSigKey = context.Annotations.Get <StrongNamePublicKey>(originModule, Marker.SNSigPubKey);

            using (var ms = new MemoryStream()) {
                var options = new ModuleWriterOptions(stubModule)
                {
                    StrongNameKey       = snKey,
                    StrongNamePublicKey = snPubKey,
                    DelaySign           = snDelaySig
                };
                var injector = new KeyInjector(ctx);
                options.WriterEvent += injector.WriterEvent;

                stubModule.Write(ms, options);
                context.CheckCancellation();
                ProtectStub(context, context.OutputPaths[ctx.ModuleIndex], ms.ToArray(), snKey, snPubKey, snSigKey, snPubKey, snDelaySig, new StubProtection(ctx, originModule));
            }
        }