Example #1
0
        private static void UnblockModule(ModuleInfo moduleInfo)
        {
            var binDir = new Uri(Path.Combine(
                                     Environment.CurrentDirectory,
                                     Path.GetDirectoryName(ModuleInfo.GetPath(moduleInfo.Alias)),
                                     "bin", Common.ConfigName
                                     )).LocalPath;

            if (!Directory.Exists(binDir))
            {
                return;
            }

            foreach (var file in Directory.EnumerateFiles(binDir))
            {
                var ext = Path.GetExtension(file);
                if (ext == ".dll")
                {
                    if (SecurityHelpers.UnblockFile(file))
                    {
                        Console.WriteLine("Unblocked: " + file);
                    }
                }
            }
        }
        private static Assembly OnResolve(object sender, ResolveEventArgs args)
        {
            try {
                var name = new AssemblyName(args.Name);

                // handle ambiguous resolve requests by checking all mod bin dirs
                // TODO: recursive scan module dirs
                if (args.RequestingAssembly == null)
                {
                    foreach (var modInfo in ModuleInfo.GetModules())
                    {
                        if (!modInfo.IsSelected)
                        {
                            continue;
                        }

                        var modDir = Path.GetDirectoryName(ModuleInfo.GetPath(modInfo.Id));
                        if (modDir == null)
                        {
                            continue;
                        }

                        var modPath = Path.Combine(modDir, "bin", Common.ConfigName, name.Name + ".dll");
                        if (!File.Exists(modPath))
                        {
                            continue;
                        }

                        var absPath = new Uri(Path.Combine(Environment.CurrentDirectory, modPath)).LocalPath;

                        if (SecurityHelpers.UnblockFile(absPath))
                        {
                            Console.WriteLine("Unblocked: " + absPath);
                        }

                        Console.WriteLine("Resolved: " + absPath);

                        return(Assembly.LoadFrom(absPath));
                    }

                    return(null);
                }

                MBSubModuleBase reqSm = null;
                foreach (var sm in Module.CurrentModule.SubModules)
                {
                    var smAsm = sm.GetType().Assembly;
                    if (smAsm == args.RequestingAssembly)
                    {
                        reqSm = sm;
                    }
                }

                if (reqSm == null)
                {
                    return(null);
                }

                var           resolvable = new LinkedList <(ModuleInfo Mod, SubModuleInfo SubMod)>();
                ModuleInfo    reqMi      = null;
                SubModuleInfo reqSmi     = null;
                var           modules    = ModuleInfo.GetModules();
                foreach (var mi in modules)
                {
                    if (!mi.IsSelected)
                    {
                        continue;
                    }

                    foreach (var smi in mi.SubModules)
                    {
                        if (smi.Assemblies.Contains(args.Name))
                        {
                            resolvable.AddLast((mi, smi));
                        }

                        if (smi.SubModuleClassType != reqSm.GetType().FullName)
                        {
                            continue;
                        }

                        reqMi  = mi;
                        reqSmi = smi;
                    }
                }

                if (reqSmi == null)
                {
                    return(null);
                }

                foreach (var modId in reqMi.DependedModuleIds)
                {
                    foreach (var resolution in resolvable)
                    {
                        if (modId != resolution.Mod.Id)
                        {
                            continue;
                        }

                        var modDir = Path.GetDirectoryName(ModuleInfo.GetPath(modId));
                        if (modDir == null)
                        {
                            continue;
                        }

                        var modPath = Path.Combine(modDir, "bin", Common.ConfigName, name.Name + ".dll");

                        if (!File.Exists(modPath))
                        {
                            continue;
                        }

                        var absPath = new Uri(Path.Combine(Environment.CurrentDirectory, modPath)).LocalPath;

                        if (SecurityHelpers.UnblockFile(absPath))
                        {
                            Console.WriteLine("Unblocked: " + absPath);
                        }

                        Console.WriteLine("Resolved: " + absPath);

                        return(Assembly.LoadFrom(absPath));
                    }
                }
            }
            catch {
                // TODO: log?
            }

            return(null);
        }