Exemple #1
0
        public async Task <bool> LoadPlugin(string pluginUrl)
        {
            try
            {
                string filepath = null;

                if (File.Exists(pluginUrl))
                {
                    filepath = pluginUrl;
                }
                else
                {
                    Uri uri = new Uri(pluginUrl);
                    filepath = FindLocalisedPlugin(uri);

                    if (filepath is null)
                    {
                        using (HttpClient client = new HttpClient())
                        {
                            var    streamTask = client.GetStreamAsync(uri);
                            string localDir   = LocalDirectory(uri);
                            if (!Directory.Exists(localDir))
                            {
                                Directory.CreateDirectory(localDir);
                            }

                            var    stream        = await streamTask;
                            string localisedFile = Path.Combine(localDir, Path.GetFileName(pluginUrl));
                            using (FileStream fstream = new FileStream(localisedFile, FileMode.CreateNew))
                            {
                                await stream.CopyToAsync(fstream);
                            }

                            if (Path.GetExtension(pluginUrl).Equals(".zip", StringComparison.OrdinalIgnoreCase))
                            {
                                ZipFile.ExtractToDirectory(localisedFile, localDir);
                            }
                        }
                    }

                    filepath = FindLocalisedPlugin(uri);
                }

                if (filepath != null && File.Exists(filepath))
                {
                    Assembly.LoadFrom(filepath);
                    XMeeplangDeserialiser.InvalidateCache();
                    DataSelector.InvalidateCache();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, "{0} thrown trying to load plugin {1}: {2}", ex.GetType().Name, pluginUrl, ex.Message);
            }
            return(false);
        }
Exemple #2
0
        public override bool Read()
        {
            bool read = base.Read();

            if (read && _Reader.NodeType == XmlNodeType.Element)
            {
                if (_Reader.NamespaceURI == ANamable.DefaultNamespace && _Reader.LocalName == "Plugin")
                {
                    try
                    {
                        string filename = GetAttribute("File");
                        if (filename != null)
                        {
                            string filepath = null;
                            if (File.Exists(filename))
                            {
                                filepath = filename;
                            }
                            else
                            {
                                filepath = Path.Combine(pluginDir, filename);
                            }

                            if (File.Exists(filepath))
                            {
                                Assembly.LoadFrom(filepath);
                                XMeeplangDeserialiser.InvalidateCache();
                            }
                            else
                            {
                                logger.Warn($"Plugin {filename} not found");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex, $"{ex.GetType().Name} thrown when loading plugin: {ex.Message}");
                    }
                }
            }

            return(read);
        }