<commit_before>﻿using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;

static class ILTemplateWithTempAssembly
{
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
    public static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, int dwFlags);

    //public const int MOVEFILE_DELAY_UNTIL_REBOOT = 0x4;

    private static string tempBasePath;

    public static void Attach()
    {
        var currentDomain = AppDomain.CurrentDomain;
        currentDomain.AssemblyResolve += ResolveAssembly;

        //Create a unique Temp directory for the application path.
        var md5Hash = CreateMd5Hash(Assembly.GetExecutingAssembly().CodeBase);
        var prefixPath = Path.Combine(Path.GetTempPath(), "Costura");
        tempBasePath = Path.Combine(prefixPath, md5Hash);
        CreateDirectory();

        PreloadUnmanagedLibraries();
    }


    public static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
    {
        var name = new AssemblyName(args.Name).Name.ToLowerInvariant();
        var existingAssembly = ReadExistingAssembly(name);
        if (existingAssembly != null)
        {
            return existingAssembly;
        }

        var prefix = string.Concat("costura.", name);

        var assemblyTempFilePath = Path.Combine(tempBasePath, string.Concat(prefix, ".dll"));
        if (File.Exists(assemblyTempFilePath))
        {
            return Assembly.LoadFile(assemblyTempFilePath);
        }

        var executingAssembly = Assembly.GetExecutingAssembly();

        var libInfo = executingAssembly.GetManifestResourceInfo(String.Concat(prefix, ".dll"));
        if (libInfo == null)
        {
            prefix = string.Concat("costura32.", name);
            libInfo = executingAssembly.GetManifestResourceInfo(String.Concat(prefix, ".dll"));
        }
        if (libInfo == null)
        {
            prefix = string.Concat("costura64.", name);
            libInfo = executingAssembly.GetManifestResourceInfo(String.Concat(prefix, ".dll"));
        }
        if (libInfo == null)
            return null;

        using (var assemblyStream = GetAssemblyStream(executingAssembly, prefix))
        {
            if (assemblyStream == null)
            {
                return null;
            }
            var assemblyData = ReadStream(assemblyStream);
            File.WriteAllBytes(assemblyTempFilePath, assemblyData);
        }

        using (var pdbStream = GetDebugStream(executingAssembly, prefix))
        {
            if (pdbStream != null)
            {
                var pdbData = ReadStream(pdbStream);
                var pdbTempFilePath = Path.Combine(tempBasePath, string.Concat(prefix, ".pdb"));
                var assemblyPdbTempFilePath = Path.Combine(tempBasePath, pdbTempFilePath);
                File.WriteAllBytes(assemblyPdbTempFilePath, pdbData);
            }
        }
        return Assembly.LoadFile(assemblyTempFilePath);
    }

    static void CreateDirectory()
    {
        if (Directory.Exists(tempBasePath))
        {
            try
            {
                Directory.Delete(tempBasePath, true);
            }
            catch
            {}
        }
        Directory.CreateDirectory(tempBasePath);
        MoveFileEx(tempBasePath, null, 0x4);
    }

    static string CreateMd5Hash(string input)
    {
        using (var md5 = MD5.Create())
        {
            var inputBytes = Encoding.ASCII.GetBytes(input);
            var hashBytes = md5.ComputeHash(inputBytes);
<commit_msg>Fixed pdb loading from disk not working.<commit_after>﻿using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;

static class ILTemplateWithTempAssembly
{
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
    public static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, int dwFlags);

    //public const int MOVEFILE_DELAY_UNTIL_REBOOT = 0x4;

    private static string tempBasePath;

    public static void Attach()
    {
        var currentDomain = AppDomain.CurrentDomain;
        currentDomain.AssemblyResolve += ResolveAssembly;

        //Create a unique Temp directory for the application path.
        var md5Hash = CreateMd5Hash(Assembly.GetExecutingAssembly().CodeBase);
        var prefixPath = Path.Combine(Path.GetTempPath(), "Costura");
        tempBasePath = Path.Combine(prefixPath, md5Hash);
        CreateDirectory();

        PreloadUnmanagedLibraries();
    }


    public static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
    {
        var name = new AssemblyName(args.Name).Name.ToLowerInvariant();
        var existingAssembly = ReadExistingAssembly(name);
        if (existingAssembly != null)
        {
            return existingAssembly;
        }

        var prefix = "costura.";

        var assemblyTempFilePath = Path.Combine(tempBasePath, string.Concat(name, ".dll"));
        if (File.Exists(assemblyTempFilePath))
        {
            return Assembly.LoadFile(assemblyTempFilePath);
        }

        var executingAssembly = Assembly.GetExecutingAssembly();

        var libInfo = executingAssembly.GetManifestResourceInfo(String.Concat(prefix, name, ".dll"));
        if (libInfo == null)
        {
            prefix = "costura32.";
            libInfo = executingAssembly.GetManifestResourceInfo(String.Concat(prefix, name, ".dll"));
        }
        if (libInfo == null)
        {
            prefix = "costura64.";
            libInfo = executingAssembly.GetManifestResourceInfo(String.Concat(prefix, name, ".dll"));
        }
        if (libInfo == null)
            return null;

        using (var assemblyStream = GetAssemblyStream(executingAssembly, String.Concat(prefix, name)))
        {
            if (assemblyStream == null)
            {
                return null;
            }
            var assemblyData = ReadStream(assemblyStream);
            File.WriteAllBytes(assemblyTempFilePath, assemblyData);
        }

        using (var pdbStream = GetDebugStream(executingAssembly, String.Concat(prefix, name)))
        {
            if (pdbStream != null)
            {
                var pdbData = ReadStream(pdbStream);
                var pdbTempFilePath = Path.Combine(tempBasePath, string.Concat(name, ".pdb"));
                var assemblyPdbTempFilePath = Path.Combine(tempBasePath, pdbTempFilePath);
                File.WriteAllBytes(assemblyPdbTempFilePath, pdbData);
            }
        }
        return Assembly.LoadFile(assemblyTempFilePath);
    }

    static void CreateDirectory()
    {
        if (Directory.Exists(tempBasePath))
        {
            try
            {
                Directory.Delete(tempBasePath, true);
            }
            catch
            {}
        }
        Directory.CreateDirectory(tempBasePath);
        MoveFileEx(tempBasePath, null, 0x4);
    }

    static string CreateMd5Hash(string input)
    {
        using (var md5 = MD5.Create())
        {
            var inputBytes = Encoding.ASCII.GetBytes(input);
            var hashBytes = md5.ComputeHash(inputBytes);

{'fraction_non_alphanumeric': 0.09324847814056447, 'fraction_numerical': 0.004703929164360819, 'mean_word_length': 2.3328722913785156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5699555', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1877, 'n_words': 398}