Beispiel #1
0
        private static void EnsureAppDomainInitialized(PluginFileInfo pluginFileInfo)
        {
            var pluginInfoName = pluginFileInfo.Name;

            if (Instances.ContainsKey(pluginInfoName))
            {
                return;
            }

            lock (PluginInitLocker)
            {
                if (Instances.ContainsKey(pluginInfoName))
                {
                    return;
                }
                Logger.Info($"当前机器人平台为:{MahuaGlobal.CurrentPlatform:G}");
                Logger.Info("开始加载插件");
                Logger.Debug(pluginFileInfo.ToString());
                Logger.Debug($"当前插件名称为{pluginInfoName}");
                Logger.Debug($"开始复制插件Asset文件 : {pluginInfoName}");
                var pluginAssetDir   = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AssetDirName, pluginInfoName);
                var pluginRuntimeDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, pluginInfoName);
                if (Directory.Exists(pluginRuntimeDir))
                {
                    Directory.Delete(pluginRuntimeDir, true);
                }

                Directory.CreateDirectory(pluginRuntimeDir);
                foreach (var fileFullname in Directory.GetFiles(pluginAssetDir))
                {
                    var filename = Path.GetFileName(fileFullname);
                    File.Copy(fileFullname, Path.Combine(pluginRuntimeDir, filename));
                }
                Logger.Debug($"复制Asset文件完毕 : {pluginInfoName}");
                var domainLoader = new DomainLoader(
                    pluginInfoName,
                    pluginFileInfo.PluginEntyPointDirectory,
                    pluginFileInfo.PluginEntryPointConfigFullFilename,
                    true);
                Logger.Debug($"创建AppDomain进行加载插件:{pluginInfoName}");
                domainLoader.Load();
                Logger.Debug("开始创建透明代理");
                var loader = domainLoader.Create <IPluginLoader>(typeof(CrossAppDomainPluginLoader).FullName);
                Logger.Debug(
                    $"透明代理创建完毕,类型为{loader.GetType().FullName},将开始调用{nameof(CrossAppDomainPluginLoader.LoadPlugin)}方法");
                if (!loader.LoadPlugin(pluginFileInfo.PluginEntryPointDllFullFilename))
                {
                    throw new PluginLoadException(pluginInfoName, loader.Message);
                }

                var pluginInstance = new PluginInstance
                {
                    DomainLoader   = domainLoader,
                    PluginLoader   = loader,
                    PluginFileInfo = pluginFileInfo,
                };
                var watcher = new FileSystemWatcher
                {
                    Path         = Path.Combine(pluginAssetDir),
                    NotifyFilter = NotifyFilters.LastWrite,
                    Filter       = "hash.txt"
                };
                watcher.Changed                 += Watcher_Changed;
                watcher.EnableRaisingEvents      = true;
                pluginInstance.FileSystemWatcher = watcher;
                Instances.Add(pluginInfoName, pluginInstance);
            }
        }