Ejemplo n.º 1
0
		public AssemblyResolver CreateResolver() {
			var resolver = new AssemblyResolver(new ModuleContext(this));
			foreach (var assembly in LoadedAssemblies.Values)
				resolver.AddToCache(assembly);
			return resolver;
		}
Ejemplo n.º 2
0
        static List<AssemblyDef> LoadAssemblies(string target)
        {
            AssemblyResolver asmResolver = new AssemblyResolver();
            ModuleContext modCtx = new ModuleContext(asmResolver);
            asmResolver.DefaultModuleContext = modCtx;
            asmResolver.EnableTypeDefCache = true;

            var directory = Path.GetFullPath(Path.GetDirectoryName(target));
            asmResolver.PreSearchPaths.Add(directory);

            List<AssemblyDef> assemblies = new List<AssemblyDef>();
            
            foreach (var asm in Directory.GetFiles(directory, "*.*")
                .Where(f=>Path.GetExtension(f.ToLower()) == ".exe" || Path.GetExtension(f.ToLower())==".dll"))
            {
                try
                {
                    var def = AssemblyDef.Load(asm);
                    def.Modules[0].Context = modCtx;
                    asmResolver.AddToCache(def);
                    assemblies.Add(def);
                }
                catch
                {
                    //Ignore
                }
            }

            return assemblies;
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Runs the engine.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <param name="token">The cancellation token.</param>
        private static void RunInternal(ConfuserParameters parameters, CancellationToken token)
        {
            // 1. Setup context
            var context = new ConfuserContext();
            context.Logger = parameters.GetLogger();
            context.Project = parameters.Project;
            context.PackerInitiated = parameters.PackerInitiated;
            context.token = token;

            PrintInfo(context);

            bool ok = false;
            try {
                var asmResolver = new AssemblyResolver();
                asmResolver.EnableTypeDefCache = true;
                asmResolver.DefaultModuleContext = new ModuleContext(asmResolver);
                context.Resolver = asmResolver;
                context.BaseDirectory = Path.Combine(Environment.CurrentDirectory, parameters.Project.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar);
                context.OutputDirectory = Path.Combine(parameters.Project.BaseDirectory, parameters.Project.OutputDirectory.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar);
                foreach (string probePath in parameters.Project.ProbePaths)
                    asmResolver.PostSearchPaths.Insert(0, Path.Combine(context.BaseDirectory, probePath));

                context.CheckCancellation();

                Marker marker = parameters.GetMarker();

                // 2. Discover plugins
                context.Logger.Debug("Discovering plugins...");

                IList<Protection> prots;
                IList<Packer> packers;
                IList<ConfuserComponent> components;
                parameters.GetPluginDiscovery().GetPlugins(context, out prots, out packers, out components);

                context.Logger.InfoFormat("Discovered {0} protections, {1} packers.", prots.Count, packers.Count);

                context.CheckCancellation();

                // 3. Resolve dependency
                context.Logger.Debug("Resolving component dependency...");
                try {
                    var resolver = new DependencyResolver(prots);
                    prots = resolver.SortDependency();
                }
                catch (CircularDependencyException ex) {
                    context.Logger.ErrorException("", ex);
                    throw new ConfuserException(ex);
                }

                components.Insert(0, new CoreComponent(parameters, marker));
                foreach (Protection prot in prots)
                    components.Add(prot);
                foreach (Packer packer in packers)
                    components.Add(packer);

                context.CheckCancellation();

                // 4. Load modules
                context.Logger.Info("Loading input modules...");
                marker.Initalize(prots, packers);
                MarkerResult markings = marker.MarkProject(parameters.Project, context);
                context.Modules = markings.Modules.ToList().AsReadOnly();
                context.OutputModules = Enumerable.Repeat<byte[]>(null, markings.Modules.Count).ToArray();
                context.OutputSymbols = Enumerable.Repeat<byte[]>(null, markings.Modules.Count).ToArray();
                context.OutputPaths = Enumerable.Repeat<string>(null, markings.Modules.Count).ToArray();
                foreach (ModuleDefMD module in context.Modules)
                    asmResolver.AddToCache(module);
                context.Packer = markings.Packer;

                context.CheckCancellation();

                // 5. Initialize components
                context.Logger.Info("Initializing...");
                foreach (ConfuserComponent comp in components) {
                    try {
                        comp.Initialize(context);
                    }
                    catch (Exception ex) {
                        context.Logger.ErrorException("Error occured during initialization of '" + comp.Name + "'.", ex);
                        throw new ConfuserException(ex);
                    }
                    context.CheckCancellation();
                }

                context.CheckCancellation();

                // 6. Build pipeline
                context.Logger.Debug("Building pipeline...");
                var pipeline = new ProtectionPipeline();
                context.Pipeline = pipeline;
                foreach (ConfuserComponent comp in components) {
                    comp.PopulatePipeline(pipeline);
                }

                context.CheckCancellation();

                //7. Run pipeline
                RunPipeline(pipeline, context);

                ok = true;
            }
            catch (AssemblyResolveException ex) {
                context.Logger.ErrorException("Failed to resolve a assembly, check if all dependencies are of correct version.", ex);
                PrintEnvironmentInfo(context);
            }
            catch (TypeResolveException ex) {
                context.Logger.ErrorException("Failed to resolve a type, check if all dependencies are of correct version.", ex);
                PrintEnvironmentInfo(context);
            }
            catch (MemberRefResolveException ex) {
                context.Logger.ErrorException("Failed to resolve a member, check if all dependencies are of correct version.", ex);
                PrintEnvironmentInfo(context);
            }
            catch (IOException ex) {
                context.Logger.ErrorException("An IO error occurred, check if all input/output locations are read/writable.", ex);
            }
            catch (OperationCanceledException) {
                context.Logger.Error("Operation is canceled.");
            }
            catch (ConfuserException) {
                // Exception is already handled/logged, so just ignore and report failure
            }
            catch (Exception ex) {
                context.Logger.ErrorException("Unknown error occurred.", ex);
            }
            finally {
                if (context.Resolver != null)
                    context.Resolver.Clear();
                context.Logger.Finish(ok);
            }
        }