<commit_before>﻿using System;
using System.Collections.Generic;
using System.Xml;
using Hacknet;
using Hacknet.Mission;
using HarmonyLib;
using Mono.Cecil.Cil;
using MonoMod.Cil;

namespace Pathfinder.Event.Loading.Content
{
    [HarmonyPatch]
    public class LoadGoalEvent : PathfinderEvent
    {
        public XmlReader Reader { get; }
        public List<MisisonGoal> GoalList { get; }
        internal static string wtfGoalName = "";
        public string GoalName { get; }

        private bool goalFound = false;
        public bool GoalFound
        {
            get => goalFound;
            set => goalFound |= value;
        }

        public LoadGoalEvent(XmlReader reader, List<MisisonGoal> goalList, string goalName)
        {
            Reader = reader;
            GoalList = goalList;
            GoalName = goalName;
        }

        [HarmonyILManipulator]
        [HarmonyPatch(typeof(ComputerLoader), nameof(ComputerLoader.readMission))]
        internal static void GoalLoadIL(ILContext il)
        {
            ILCursor c = new ILCursor(il);

            c.GotoNext(MoveType.After,
                x => x.MatchLdstr("UNKNOWN")
            );
            c.GotoNext(MoveType.Before,
                x => x.MatchNop(),
                x => x.MatchNop(),
                x => x.MatchLdloc(1),
                x => x.MatchCallvirt(AccessTools.Method(typeof(XmlReader), nameof(XmlReader.Read)))
            );

            c.Emit(OpCodes.Ldloc_1);

            foreach (var label in c.IncomingLabels)
                label.Target = c.Prev;
            
            c.Emit(OpCodes.Ldloc_3);
            c.Emit(OpCodes.Ldloc, 14);
            c.Emit(OpCodes.Stsfld, AccessTools.Field(typeof(LoadGoalEvent), nameof(wtfGoalName)));
            c.EmitDelegate<Action<XmlReader, List<MisisonGoal>>>((reader, goalList) =>
            {
                var loadGoalEvent = new LoadGoalEvent(reader, goalList, wtfGoalName);
                EventManager<LoadGoalEvent>.InvokeAll(loadGoalEvent);
            });
        }
    }
}<commit_msg>Fix goal load event<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Hacknet;
using Hacknet.Mission;
using HarmonyLib;
using Mono.Cecil.Cil;
using MonoMod.Cil;

namespace Pathfinder.Event.Loading.Content
{
    [HarmonyPatch]
    public class LoadGoalEvent : PathfinderEvent
    {
        public XmlReader Reader { get; }
        public List<MisisonGoal> GoalList { get; }
        internal static string wtfGoalName = "";
        public string GoalName { get; }

        private bool goalFound = false;
        public bool GoalFound
        {
            get => goalFound;
            set => goalFound |= value;
        }

        public LoadGoalEvent(XmlReader reader, List<MisisonGoal> goalList, string goalName)
        {
            Reader = reader;
            GoalList = goalList;
            GoalName = goalName;
        }

        [HarmonyILManipulator]
        [HarmonyPatch(typeof(ComputerLoader), nameof(ComputerLoader.readMission))]
        internal static void GoalLoadIL(ILContext il)
        {
            ILCursor c = new ILCursor(il);

            c.GotoNext(MoveType.After,
                x => x.MatchLdstr("UNKNOWN")
            );
            c.GotoNext(MoveType.Before,
                x => x.MatchNop(),
                x => x.MatchLdloc(1),
                x => x.MatchCallvirt(AccessTools.Method(typeof(XmlReader), nameof(XmlReader.Read)))
            );

            var labels = il.Labels.Where(x => x.Target == c.Prev).ToList();
            
            c.Emit(OpCodes.Ldloc_1);

            foreach (var label in labels)
                label.Target = c.Prev;
            
            c.Emit(OpCodes.Ldloc_3);
            c.Emit(OpCodes.Ldloc, 14);
            c.Emit(OpCodes.Stsfld, AccessTools.Field(typeof(LoadGoalEvent), nameof(wtfGoalName)));
            c.EmitDelegate<Action<XmlReader, List<MisisonGoal>>>((reader, goalList) =>
            {
                var loadGoalEvent = new LoadGoalEvent(reader, goalList, wtfGoalName);
                EventManager<LoadGoalEvent>.InvokeAll(loadGoalEvent);
            });
        }
    }
}
{'fraction_non_alphanumeric': 0.10203592814371258, 'fraction_numerical': 0.0023952095808383233, 'mean_word_length': 1.9533239038189534, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 31, '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': '6434446', 'n_tokens_mistral': 1279, 'n_tokens_neox': 1249, 'n_words': 234}