<commit_before>﻿using System;
using System.IO;
using System.Linq;

using Mono.Cecil;

namespace OxidePatcher.Patching
{
    /// <summary>
    /// Responsible for performing the actual patch process
    /// </summary>
    public class Patcher
    {
        /// <summary>
        /// Gets the project that this patcher will patch
        /// </summary>
        public Project PatchProject { get; private set; }

        /// <summary>
        /// Called when a log message has been written
        /// </summary>
        public event Action<string> OnLogMessage;

        /// <summary>
        /// Initialises a new instance of the Patcher class
        /// </summary>
        /// <param name="patchproject"></param>
        public Patcher(Project patchproject)
        {
            PatchProject = patchproject;
        }

        private void Log(string format, params object[] args)
        {
            if (OnLogMessage != null) OnLogMessage(string.Format(format, args));
        }

        /// <summary>
        /// Performs the patch process
        /// </summary>
        public void Patch(bool console)
        {
            // Load oxide assembly
            string oxidefilename = Path.Combine(System.Windows.Forms.Application.StartupPath, "Oxide.Core.dll");
            if (!File.Exists(oxidefilename)) throw new FileNotFoundException(string.Format("Failed to locate Oxide.dll assembly"));
            AssemblyDefinition oxideassembly = AssemblyDefinition.ReadAssembly(oxidefilename);

            // CReate reader params
            ReaderParameters readerparams = new ReaderParameters();
            readerparams.AssemblyResolver = new AssemblyResolver { TargetDirectory = PatchProject.TargetDirectory };

            // Loop each manifest
            foreach (var manifest in PatchProject.Manifests)
            {
                // Get the assembly filename
                string filename;
                if (!console)
                {
                    filename = GetAssemblyFilename(manifest.AssemblyName, true);
                    if (!File.Exists(filename)) throw new FileNotFoundException(string.Format("Failed to locate target assembly {0}", manifest.AssemblyName), filename);
                }
                else
                {
                    filename = GetAssemblyFilename(manifest.AssemblyName, true);
                    if (!File.Exists(filename))
                    {
                        filename = GetAssemblyFilename(manifest.AssemblyName, false);
                        if (!File.Exists(filename)) throw new FileNotFoundException(string.Format("Failed to locate target assembly {0}", manifest.AssemblyName), filename);
                    }
                }

                // Load it
                if (console)
                {
                    Console.WriteLine(string.Format("Loading assembly {0}", manifest.AssemblyName));
                }
                else
                {
                    Log("Loading assembly {0}", manifest.AssemblyName);
                }
                AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(filename, readerparams);

                // Loop each hook
                foreach (var hook in manifest.Hooks)
                {
                    // Check if it's flagged
                    if (hook.Flagged)
                    {<commit_msg>Fixes #46 DLL's are now copied before being patched in CLI mode<commit_after>﻿using System;
using System.IO;
using System.Linq;

using Mono.Cecil;

namespace OxidePatcher.Patching
{
    /// <summary>
    /// Responsible for performing the actual patch process
    /// </summary>
    public class Patcher
    {
        /// <summary>
        /// Gets the project that this patcher will patch
        /// </summary>
        public Project PatchProject { get; private set; }

        /// <summary>
        /// Called when a log message has been written
        /// </summary>
        public event Action<string> OnLogMessage;

        /// <summary>
        /// Initialises a new instance of the Patcher class
        /// </summary>
        /// <param name="patchproject"></param>
        public Patcher(Project patchproject)
        {
            PatchProject = patchproject;
        }

        private void Log(string format, params object[] args)
        {
            if (OnLogMessage != null) OnLogMessage(string.Format(format, args));
        }

        /// <summary>
        /// Performs the patch process
        /// </summary>
        public void Patch(bool console)
        {
            // Load oxide assembly
            string oxidefilename = Path.Combine(System.Windows.Forms.Application.StartupPath, "Oxide.Core.dll");
            if (!File.Exists(oxidefilename)) throw new FileNotFoundException(string.Format("Failed to locate Oxide.dll assembly"));
            AssemblyDefinition oxideassembly = AssemblyDefinition.ReadAssembly(oxidefilename);

            // CReate reader params
            ReaderParameters readerparams = new ReaderParameters();
            readerparams.AssemblyResolver = new AssemblyResolver { TargetDirectory = PatchProject.TargetDirectory };

            // Loop each manifest
            foreach (var manifest in PatchProject.Manifests)
            {
                // Get the assembly filename
                string filename;
                if (!console)
                {
                    filename = GetAssemblyFilename(manifest.AssemblyName, true);
                    if (!File.Exists(filename)) throw new FileNotFoundException(string.Format("Failed to locate target assembly {0}", manifest.AssemblyName), filename);
                }
                else
                {
                    filename = GetAssemblyFilename(manifest.AssemblyName, true);
                    if (!File.Exists(filename))
                    {
                        filename = GetAssemblyFilename(manifest.AssemblyName, false);
                        if (!File.Exists(filename))
                        {
                            throw new FileNotFoundException(string.Format("Failed to locate target assembly {0}", manifest.AssemblyName), filename);
                        }
                        else
                        {
                            System.IO.File.Copy(filename, filename.Replace(".dll", "_Original.dll"), true);
                            filename = filename.Replace(".dll", "_Original.dll");
                        }
                    }
                }

                // Load it
                if (console)
                {
                    Console.WriteLine(string.Format("Loading assembly {0}", manifest.AssemblyName));
                }
                else
                {
                    Log("Loading assembly {0}", manifest.AssemblyName);
                }
                AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(filename, readerparams);

                // Loop each hook
                foreach (var hook in manifest.Hooks)
                {
                    // Check if it's flagged
                    if (hook.Flagged)
                    {
{'fraction_non_alphanumeric': 0.09135628952916373, 'fraction_numerical': 0.0014054813773717498, 'mean_word_length': 1.514487632508834, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '3080921', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1503, 'n_words': 429}