<commit_before>﻿using Mono.Cecil;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Text;

namespace MonoMod.InlineRT {
    public static class MMILExec {

        public static void ExecuteRules(this MonoModder self, TypeDefinition orig) {
            ModuleDefinition wrapper = ModuleDefinition.CreateModule(
                $"{orig.Module.Name.Substring(0, orig.Module.Name.Length - 4)}.MonoModRules -ID:{MMILProxyManager.GetId(self)} -MMILRT",
                new ModuleParameters() {
                    Architecture = orig.Module.Architecture,
                    AssemblyResolver = self.AssemblyResolver,
                    Kind = ModuleKind.Dll,
                    MetadataResolver = orig.Module.MetadataResolver,
                    Runtime = orig.Module.Runtime
                }
            );
            MonoModder wrapperMod = new MonoModder() {
                Module = wrapper,

                Logger = msg => self.Log("[MonoModRule] " + msg),

                DependencyCache = self.DependencyCache,
                DependencyDirs = self.DependencyDirs,
                OnMissingDependency = self.OnMissingDependency,

                Relinker = (mtp, context) =>
                    mtp is MethodReference && ((MethodReference) mtp).DeclaringType.FullName.Contains("MMIL") ?
                    wrapper.ImportReference(MMILProxyManager.Relink(self, (MethodReference) mtp)) :
                    self.Relinker(mtp, context)
            };


            wrapperMod.PrePatchType(orig, forceAdd: true);
            wrapperMod.PatchType(orig);
            TypeDefinition rulesCecil = wrapper.GetType(orig.FullName);
            wrapperMod.PatchRefsInType(rulesCecil);

            Assembly asm;
            using (MemoryStream asmStream = new MemoryStream()) {
                wrapperMod.Write(asmStream);
                asm = Assembly.Load(asmStream.GetBuffer());
            }

            using (FileStream debugStream = File.OpenWrite(Path.Combine(self.DependencyDirs[0], $"{orig.Module.Name.Substring(0, orig.Module.Name.Length - 4)}.MonoModRules-MMILRT.dll"))) {
                wrapperMod.Write(debugStream);
            }

            Type rules = asm.GetType(orig.FullName);
            RuntimeHelpers.RunClassConstructor(rules.TypeHandle);

        }
<commit_msg>MMILRT: Take over DependencyMap<commit_after>﻿using Mono.Cecil;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Text;

namespace MonoMod.InlineRT {
    public static class MMILExec {

        public static void ExecuteRules(this MonoModder self, TypeDefinition orig) {
            ModuleDefinition wrapper = ModuleDefinition.CreateModule(
                $"{orig.Module.Name.Substring(0, orig.Module.Name.Length - 4)}.MonoModRules -ID:{MMILProxyManager.GetId(self)} -MMILRT",
                new ModuleParameters() {
                    Architecture = orig.Module.Architecture,
                    AssemblyResolver = self.AssemblyResolver,
                    Kind = ModuleKind.Dll,
                    MetadataResolver = orig.Module.MetadataResolver,
                    Runtime = orig.Module.Runtime
                }
            );
            MonoModder wrapperMod = new MonoModder() {
                Module = wrapper,

                Logger = msg => self.Log("[MonoModRule] " + msg),

                DependencyCache = self.DependencyCache,
                DependencyDirs = self.DependencyDirs,
                DependencyMap = self.DependencyMap,
                OnMissingDependency = self.OnMissingDependency,

                Relinker = (mtp, context) =>
                    mtp is MethodReference && ((MethodReference) mtp).DeclaringType.FullName.Contains("MMIL") ?
                    wrapper.ImportReference(MMILProxyManager.Relink(self, (MethodReference) mtp)) :
                    self.Relinker(mtp, context)
            };


            wrapperMod.PrePatchType(orig, forceAdd: true);
            wrapperMod.PatchType(orig);
            TypeDefinition rulesCecil = wrapper.GetType(orig.FullName);
            wrapperMod.PatchRefsInType(rulesCecil);

            Assembly asm;
            using (MemoryStream asmStream = new MemoryStream()) {
                wrapperMod.Write(asmStream);
                asm = Assembly.Load(asmStream.GetBuffer());
            }

            using (FileStream debugStream = File.OpenWrite(Path.Combine(self.DependencyDirs[0], $"{orig.Module.Name.Substring(0, orig.Module.Name.Length - 4)}.MonoModRules-MMILRT.dll"))) {
                wrapperMod.Write(debugStream);
            }

            Type rules = asm.GetType(orig.FullName);
            RuntimeHelpers.RunClassConstructor(rules.TypeHandle);

        }

{'fraction_non_alphanumeric': 0.09570154095701541, 'fraction_numerical': 0.00202757502027575, 'mean_word_length': 2.1064231738035266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4304443', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1246, 'n_words': 227}