コード例 #1
0
        public int ComputeTvClientConfigurationHashCode(dynamic settings)
        {
            HashCode hash = new HashCode();

            if (settings.TvShows.Client == DownloadClient.Sonarr)
            {
                var clientSettings = new SonarrSettingsProvider().Provide();

                hash.Add(clientSettings.Categories.Select(x => x.Name).GetSequenceHashCode());
                hash.Add(clientSettings.Hostname);
                hash.Add(clientSettings.Port);
                hash.Add(clientSettings.ApiKey);
                hash.Add(clientSettings.UseSSL);
                hash.Add(clientSettings.Version);
            }
            else if (settings.TvShows.Client == DownloadClient.Ombi)
            {
                var clientSettings = new OmbiSettingsProvider().Provide();

                hash.Add(clientSettings.ApiKey);
                hash.Add(clientSettings.Hostname);
                hash.Add(clientSettings.Port);
                hash.Add(clientSettings.UseSSL);
                hash.Add(clientSettings.Version);
            }
            else if (settings.TvShows.Client == DownloadClient.Overseerr)
            {
                var clientSettings = new OverseerrSettingsProvider().Provide();

                hash.Add(clientSettings.TvShows.Categories.Select(x => x.Name).GetSequenceHashCode());
                hash.Add(clientSettings.ApiKey);
                hash.Add(clientSettings.Hostname);
                hash.Add(clientSettings.Port);
                hash.Add(clientSettings.UseSSL);
                hash.Add(clientSettings.Version);
            }
            else
            {
                hash.Add(DownloadClient.Disabled);
            }

            return(hash.ToHashCode());
        }
コード例 #2
0
        public static Type Build(ILogger logger, DiscordSettings settings, RadarrSettingsProvider radarrSettingsProvider, SonarrSettingsProvider sonarrSettingsProvider, OverseerrSettingsProvider overseerrSettingsProvider)
        {
            string code     = GetCode(settings, radarrSettingsProvider.Provide(), sonarrSettingsProvider.Provide(), overseerrSettingsProvider.Provide());
            var    tree     = SyntaxFactory.ParseSyntaxTree(code);
            string fileName = $"{DLLFileName}-{Guid.NewGuid()}.dll";

            var references = new List <PortableExecutableReference>()
            {
                MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(System.Linq.Enumerable).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(ApplicationCommandModule).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(SlashCommandBuilder).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Attribute).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Task).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(DiscordUser).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(IServiceProvider).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(ILogger).GetTypeInfo().Assembly.Location),
            };

            references.Add(MetadataReference.CreateFromFile(AppDomain.CurrentDomain.GetAssemblies().Single(a => a.GetName().Name == "netstandard").Location));

            Assembly.GetEntryAssembly().GetReferencedAssemblies().ToList()
            .ForEach(a => references.Add(MetadataReference.CreateFromFile(Assembly.Load(a).Location)));

            var compilation = CSharpCompilation.Create(fileName)
                              .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
                              .AddReferences(references)
                              .AddSyntaxTrees(tree);

            var dirInfo      = new DirectoryInfo(Directory.GetCurrentDirectory());
            var tmpDirectory = dirInfo.EnumerateDirectories().Where(x => x.Name == "tmp").Single().FullName;

            string path = Path.Combine(tmpDirectory, fileName);

            var compilationResult = compilation.Emit(path);

            if (compilationResult.Success)
            {
                var asm = AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
                return(asm.GetType("Requestrr.WebApi.RequestrrBot.SlashCommands"));
            }
            else
            {
                foreach (Diagnostic codeIssue in compilationResult.Diagnostics)
                {
                    string issue = $"ID: {codeIssue.Id}, Message: {codeIssue.GetMessage()}, Location: { codeIssue.Location.GetLineSpan()},Severity: { codeIssue.Severity}";
                    logger.LogError("Failed to build SlashCommands assembly: " + issue);
                }
            }

            throw new Exception("Failed to build SlashCommands assembly.");
        }