<commit_before>﻿using System;
using System.Collections.Generic;
using Mono.Cecil;

namespace ILspect
{
    public class DefaultAssemblyResolver : IAssemblyResolver
    {
        private readonly Dictionary<string, AssemblyDefinition> _cache = new Dictionary<string, AssemblyDefinition>();

        public AssemblyDefinition Resolve(AssemblyNameReference name)
        {
            if (_cache.TryGetValue(name.FullName, out var asmDef))
            {
                return asmDef;
            }
            return LoadAssembly(name, new ReaderParameters(ReadingMode.Deferred));
        }

        public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {
            if (_cache.TryGetValue(name.FullName, out var asmDef))
            {
                return asmDef;
            }
            return LoadAssembly(name, parameters);
        }

        public void Register(AssemblyDefinition asmDef)
        {
            _cache[asmDef.FullName] = asmDef;
        }

        public void Dispose()
        {
        }

        private AssemblyDefinition LoadAssembly(AssemblyNameReference name, ReaderParameters readerParameters)
        {
            var path = FindAssembly(name);

            if (string.IsNullOrEmpty(path))
            {
                return null;
            }

            var asm = AssemblyDefinition.ReadAssembly(path, readerParameters);
            Register(asm);
            return asm;
        }

        private string FindAssembly(AssemblyNameReference name)
        {
            if (name.FullName.Equals("System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=<KEY>"))
            {
                return @"C:\Users\anurse\.nuget\packages\system.runtime\4.3.0\ref\netstandard1.3\System.Runtime.dll";
            }
            return null;
        }
    }
}<commit_msg>add marginally less crappy resolution<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using Mono.Cecil;

namespace ILspect
{
    public class DefaultAssemblyResolver : IAssemblyResolver
    {
        private readonly Dictionary<string, AssemblyDefinition> _cache = new Dictionary<string, AssemblyDefinition>();

        public AssemblyDefinition Resolve(AssemblyNameReference name)
        {
            if (_cache.TryGetValue(name.FullName, out var asmDef))
            {
                return asmDef;
            }
            return LoadAssembly(name, new ReaderParameters(ReadingMode.Deferred));
        }

        public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {
            if (_cache.TryGetValue(name.FullName, out var asmDef))
            {
                return asmDef;
            }
            return LoadAssembly(name, parameters);
        }

        public void Register(AssemblyDefinition asmDef)
        {
            _cache[asmDef.FullName] = asmDef;
        }

        public void Dispose()
        {
        }

        private AssemblyDefinition LoadAssembly(AssemblyNameReference name, ReaderParameters readerParameters)
        {
            var path = FindAssembly(name);

            if (string.IsNullOrEmpty(path))
            {
                return null;
            }

            var asm = AssemblyDefinition.ReadAssembly(path, readerParameters);
            Register(asm);
            return asm;
        }

        private string FindAssembly(AssemblyNameReference name)
        {
            if (name.FullName.Equals("System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=<KEY>"))
            {
                return Path.Combine(GetNuGetPackageDir(), "system.runtime", "4.3.0", "ref", "netstandard1.3", "System.Runtime.dll");
            }

            throw new NotSupportedException($"TODO: Find: {name.FullName}");
        }

        private string GetNuGetPackageDir()
        {
            var home = Environment.GetEnvironmentVariable("USERPROFILE");

            if (string.IsNullOrEmpty(home))
            {
                home = Environment.GetEnvironmentVariable("HOME");
            }

            if (string.IsNullOrEmpty(home))
            {
                throw new NotImplementedException("TODO: Find NuGet Packages!");
            }

            return Path.Combine(home, ".nuget", "packages");
        }
    }
}
{'fraction_non_alphanumeric': 0.08665430954587582, 'fraction_numerical': 0.004633920296570899, 'mean_word_length': 1.9507860560492138, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6805158', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1053, 'n_words': 222}