예제 #1
0
        public static TcPlugin GetTcPlugin(string wrapperAssembly, PluginType pluginType)
        {
            pluginSettings = GetPluginSettings(wrapperAssembly);
#if TRACE
            writeTrace = Convert.ToBoolean(pluginSettings["writeTrace"]);
            TraceOut(String.Format(TraceMsg1, wrapperAssembly), TraceMsg2);
#endif
            TcPlugin tcPlugin;
            try {
                string pluginAssembly = pluginSettings["pluginAssembly"];
                if (String.IsNullOrEmpty(pluginAssembly))
                {
                    pluginAssembly = Path.ChangeExtension(wrapperAssembly, ".dll");
                }
                string wrapperFolder = Path.GetDirectoryName(wrapperAssembly);
                if (!Path.IsPathRooted(pluginAssembly))
                {
                    pluginAssembly = Path.Combine(wrapperFolder, pluginAssembly);
                }
#if TRACE
                TraceOut(pluginAssembly, TraceMsg3);
#endif
                string pluginFolder = Path.GetDirectoryName(pluginAssembly);
                pluginSettings["pluginFolder"] = pluginFolder;
                string    pluginAssemblyName = Path.GetFileNameWithoutExtension(pluginAssembly);
                string    title = pluginSettings["pluginTitle"] ?? pluginAssemblyName;
                AppDomain pluginDomain;
                bool      useSeparateDomain = !Convert.ToBoolean(pluginSettings["startInDefaultDomain"]);
                if (useSeparateDomain)
                {
                    pluginDomain = CreatePluginAppDomain(pluginAssembly, title);
                }
                else
                {
                    pluginDomain = MainDomain;
                    MainDomain.AssemblyResolve += MainDomainResolveEventHandler;
#if TRACE
                    TraceOut(null, "Load into Default AppDomain.");
#endif
                }

                string pluginClassName = pluginSettings["pluginClass"];
                // Create object implemented required TC plugin interface.
                tcPlugin = CreateTcPluginStub(pluginDomain, pluginAssembly, pluginType, null, ref pluginClassName);
                if (tcPlugin == null)
                {
                    if (String.IsNullOrEmpty(pluginClassName))
                    {
                        throw new InvalidOperationException(ErrorMsg2);
                    }
                }
                else
                {
                    tcPlugin.MainDomain = MainDomain;
#if TRACE
                    TraceOut(String.Format(TraceMsg6, tcPlugin.Title, pluginClassName),
                             String.Format(TraceMsg7, TcUtils.PluginNames[pluginType]));
#endif
                }
                // File System plugins only - try to load content plugin associated with current FS plugin
                if (pluginType == PluginType.FileSystem)
                {
                    string contentAssembly = pluginSettings.ContainsKey("contentAssembly") ?
                                             pluginSettings["contentAssembly"] : pluginAssembly;
                    if (!String.IsNullOrEmpty(contentAssembly))
                    {
                        if (!Path.IsPathRooted(contentAssembly))
                        {
                            contentAssembly = Path.Combine(wrapperFolder, contentAssembly);
                        }
                        string contentClassName = pluginSettings["contentClass"];
#if TRACE
                        TraceOut(contentAssembly, TraceMsg8);
#endif

                        // Create object implemented Content plugin interface.
                        // Full FS plugin class name (with assembly name)
                        // is passed as "masterClassName" parameter.
                        string fullPluginClassName = pluginClassName + ", " + pluginAssembly;
                        try {
                            ContentPlugin cntPlugin = (ContentPlugin)CreateTcPluginStub(pluginDomain,
                                                                                        contentAssembly, PluginType.Content, fullPluginClassName, ref contentClassName);
                            FsPlugin fsPlugin = (FsPlugin)tcPlugin;
                            if (fsPlugin != null)
                            {
                                if (cntPlugin == null)
                                {
                                    if (String.IsNullOrEmpty(contentClassName))
                                    {
                                        // No implementation for Content plugin interface
#if TRACE
                                        TraceOut(null, TraceMsg9);
#endif
                                    }
                                    else if (pluginClassName.Equals(contentClassName))
                                    {
                                        // Content plugin interface is implemented in found FS plugin class
                                        try {
                                            cntPlugin = (ContentPlugin)tcPlugin;
                                            cntPlugin.PluginNumber = fsPlugin.PluginNumber;
                                            fsPlugin.ContentPlgn   = cntPlugin;
#if TRACE
                                            TraceOut(null, TraceMsg10);
#endif
                                        } catch (InvalidCastException) { }
                                    }
                                }
                                else
                                {
                                    cntPlugin.PluginNumber = fsPlugin.PluginNumber;
                                    fsPlugin.ContentPlgn   = cntPlugin;
#if TRACE
                                    TraceOut(String.Format(TraceMsg11, contentClassName), TraceMsg12);
#endif
                                }
                            }
                        } catch (Exception ex) {
#if TRACE
                            TraceOut(String.Format(TraceMsg13, contentClassName, ex.Message), String.Empty);
#endif
                        }
                    }
                }
                if (useSeparateDomain)
                {
                    tcPlugin.TcPluginEventHandler += HandleTcPluginEvent;
                }
                else
                {
                    tcPlugin.TcPluginEventHandler += TcCallback.HandleTcPluginEvent;
                }
                tcPlugin.WrapperFileName = wrapperAssembly;
                TcPluginLoadingInfo loadingInfo = FindPluginLoadingInfoByWrapperFileName(wrapperAssembly);
                if (loadingInfo == null)
                {
                    loadedPlugins.Add(new TcPluginLoadingInfo(wrapperAssembly, tcPlugin, pluginDomain));
                }
                else
                {
                    if (loadingInfo.LifetimeStatus == PluginLifetimeStatus.PluginUnloaded)
                    {
                        tcPlugin.PluginNumber = loadingInfo.PluginNumber;
                        tcPlugin.CreatePassword(loadingInfo.CryptoNumber, loadingInfo.CryptoFlags);

                        loadingInfo.LifetimeStatus = PluginLifetimeStatus.Active;
                        loadingInfo.Plugin         = tcPlugin;
                        loadingInfo.Domain         = pluginDomain;
#if TRACE
                        TraceOut(String.Format(TraceMsg14, tcPlugin.Title), String.Empty);
#endif
                    }
                }
            } finally {
#if TRACE
                writeTrace = false;
#endif
            }
            return(tcPlugin);
        }
예제 #2
0
파일: FsPrompt.cs 프로젝트: r-Larch/TcBuild
 public FsPrompt(FsPlugin plugin)
 {
     _plugin = plugin;
 }