<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading;
using Elders.Cronus.Logging;

namespace Elders.Cronus.Discoveries
{
    internal class AssemblyLoader
    {
        static readonly ILog log = LogProvider.GetLogger(nameof(AssemblyLoader));

        static int shouldLoadAssembliesFromDir = 1;
        static string[] excludedAssemblies = new string[] { "apphost.exe", "clrcompression.dll", "clretwrc.dll", "clrjit.dll", "coreclr.dll", "dbgshim.dll", "hostfxr.dll", "hostpolicy.dll", "mscordaccore.dll", "mscordaccore_amd64_amd64_4.6.26814.03.dll", "mscordbi.dll", "mscorrc.debug.dll", "mscorrc.dll", "sos.dll", "sos_amd64_amd64_4.6.26814.03.dll", "ucrtbase.dll" };
        static string[] wildcards = new string[] { "microsoft", "api-ms" };

        internal static IDictionary<string, Assembly> assemblies = new Dictionary<string, Assembly>();

        static AssemblyLoader()
        {
            InitAssemblies();
        }

        static void LoadAssembliesFromDirecotry(string directoryWithAssemblies)
        {
            var files = directoryWithAssemblies.GetFiles(new[] { "*.exe", "*.dll" }).Select(filepath => filepath.ToLower());
            foreach (var assemblyFile in files)
            {
                if (wildcards.Any(x => assemblyFile.Contains(x))) continue;
                if (excludedAssemblies.Any(x => assemblyFile.EndsWith(x))) continue;

                var assembly = AppDomain.CurrentDomain.GetAssemblies()
                    .Where(x => x.IsDynamic == false)
                    .Where(x => x.Location.Equals(assemblyFile, StringComparison.OrdinalIgnoreCase) || x.CodeBase.Equals(assemblyFile, StringComparison.OrdinalIgnoreCase))
                    .SingleOrDefault();

                if (assembly is null)
                {
                    try
                    {
                        assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyFile);
                        //byte[] assemblyRaw = File.ReadAllBytes(assemblyFile);
                        //assembly = Assembly.ReflectionOnlyLoad(assemblyRaw);
                        //assembly = AppDomain.CurrentDomain.Load(assembly.GetName());
                    }
                    catch (BadImageFormatException ex)
                    {
                        log.WarnException($"Unable to load assembly {assemblyFile} which is not a bad thing at all", ex);
                    }
                }

                if (assembly is null == false)
                    assemblies.Add(assembly.FullName, assembly);
            }
        }

        static void InitAssemblies()
        {
            if (1 == Interlocked.Exchange(ref shouldLoadAssembliesFromDir, 0))
            {
                string codeBase = Assembly.GetEntryAssembly().CodeBase;
                UriBuilder uri = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uri.Path);
                var dir = Path.GetDirectoryName(path);
                LoadAssembliesFromDirecotry(dir);
            }
        }
    }
}<commit_msg>Adds log message from which path we load the assemblies<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading;
using Elders.Cronus.Logging;

namespace Elders.Cronus.Discoveries
{
    internal class AssemblyLoader
    {
        static readonly ILog log = LogProvider.GetLogger(nameof(AssemblyLoader));

        static int shouldLoadAssembliesFromDir = 1;
        static string[] excludedAssemblies = new string[] { "apphost.exe", "clrcompression.dll", "clretwrc.dll", "clrjit.dll", "coreclr.dll", "dbgshim.dll", "hostfxr.dll", "hostpolicy.dll", "mscordaccore.dll", "mscordaccore_amd64_amd64_4.6.26814.03.dll", "mscordbi.dll", "mscorrc.debug.dll", "mscorrc.dll", "sos.dll", "sos_amd64_amd64_4.6.26814.03.dll", "ucrtbase.dll" };
        static string[] wildcards = new string[] { "microsoft", "api-ms" };

        internal static IDictionary<string, Assembly> assemblies = new Dictionary<string, Assembly>();

        static AssemblyLoader()
        {
            InitAssemblies();
        }

        static void LoadAssembliesFromDirecotry(string directoryWithAssemblies)
        {
            var files = directoryWithAssemblies.GetFiles(new[] { "*.exe", "*.dll" }).Select(filepath => filepath.ToLower());
            foreach (var assemblyFile in files)
            {
                if (wildcards.Any(x => assemblyFile.Contains(x))) continue;
                if (excludedAssemblies.Any(x => assemblyFile.EndsWith(x))) continue;

                var assembly = AppDomain.CurrentDomain.GetAssemblies()
                    .Where(x => x.IsDynamic == false)
                    .Where(x => x.Location.Equals(assemblyFile, StringComparison.OrdinalIgnoreCase) || x.CodeBase.Equals(assemblyFile, StringComparison.OrdinalIgnoreCase))
                    .SingleOrDefault();

                if (assembly is null)
                {
                    try
                    {
                        assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyFile);
                        //byte[] assemblyRaw = File.ReadAllBytes(assemblyFile);
                        //assembly = Assembly.ReflectionOnlyLoad(assemblyRaw);
                        //assembly = AppDomain.CurrentDomain.Load(assembly.GetName());
                    }
                    catch (BadImageFormatException ex)
                    {
                        log.WarnException($"Unable to load assembly {assemblyFile} which is not a bad thing at all", ex);
                    }
                }

                if (assembly is null == false)
                    assemblies.Add(assembly.FullName, assembly);
            }
        }

        static void InitAssemblies()
        {
            if (1 == Interlocked.Exchange(ref shouldLoadAssembliesFromDir, 0))
            {
                string codeBase = Assembly.GetEntryAssembly().CodeBase;
                log.Info($"Try loading assemblies from directory `{codeBase}`");
                UriBuilder uri = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uri.Path);
                var dir = Path.GetDirectoryName(path);
                LoadAssembliesFromDirecotry(dir);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.10683826949914715, 'fraction_numerical': 0.008993642425182198, 'mean_word_length': 2.0699666825321277, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2394624', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1682, 'n_words': 345}