Пример #1
0
        public void PublishPlugin(byte[] plugin, string accessToken, DateTime publishDate)
        {
            Log.DebugFormat("Adding plugin ({0} bytes) to repository...", plugin.Length);

            IPluginPackageIndex pluginIndex;
            DateTime            builtTime;
            IReadOnlyList <SerializableChange> changes;

            byte[] icon;
            using (var stream = new MemoryStream(plugin))
                using (var archive = OpenPlugin(stream))
                {
                    pluginIndex = archive.Index;
                    changes     = archive.LoadChanges();
                    builtTime   = GetBuildTime(archive.ReadAssembly());
                    icon        = archive.ReadIcon()?.ReadToEnd();
                }
            var id = new PluginIdentifier(pluginIndex.Id, pluginIndex.Version);

            if (!Guid.TryParse(accessToken, out var token))
            {
                throw new InvalidUserTokenException($"'{accessToken}' is not a valid access token.");
            }

            using (var transaction = _database.BeginTransaction())
            {
                if (!_usernamesByAccessToken.TryGet(token, out var userName))
                {
                    throw new InvalidUserTokenException($"'{accessToken}' is not a valid access token.");
                }

                // TODO: Only throw a temper tantrum in case the plugin to be added differs from the plugin stored in this repository
                if (_pluginDescriptions.ContainsKey(id) || _plugins.ContainsKey(id))
                {
                    throw new PluginAlreadyPublishedException($"The plugin '{id}' already exists and cannot be modified.");
                }

                var publishedPlugin = new PublishedPlugin(pluginIndex)
                {
                    Publisher          = userName,
                    Identifier         = id,
                    BuildDate          = builtTime,
                    SizeInBytes        = plugin.Length,
                    PublishDate        = publishDate,
                    RequiredInterfaces = pluginIndex.ImplementedPluginInterfaces
                                         .Select(x => new PluginInterface(x.InterfaceTypename, x.InterfaceVersion)).ToList(),
                };
                _pluginDescriptions.Put(id, publishedPlugin);
                _plugins.Put(id, plugin);
                _pluginIcons.Put(id, icon);

                transaction.Commit();
                Log.InfoFormat("Added plugin '{0}' to repository!", id);
            }
        }
Пример #2
0
 private static PublishedPluginDescription CreateDescription(PublishedPlugin plugin)
 {
     return(new PublishedPluginDescription
     {
         Identifier = plugin.Identifier,
         Name = plugin.Name,
         Author = plugin.Author,
         Website = plugin.Website,
         Description = plugin.Description,
         Publisher = plugin.Publisher,
         PublishTimestamp = plugin.PublishDate
     });
 }
Пример #3
0
        private static bool IsSupported(PublishedPlugin pluginRequirements,
                                        System.Collections.Generic.IReadOnlyDictionary <string, int> interfaces)
        {
            var requirements = pluginRequirements.RequiredInterfaces;

            if (requirements == null)
            {
                return(true);
            }

            foreach (var requirement in requirements)
            {
                if (interfaces.TryGetValue(requirement.FullName, out var actualVersion))
                {
                    if (requirement.Version != actualVersion)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }