<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;

namespace HATE
{
    struct StringPointer
    {
        public Pointer Base;
        public string Ending;
        public byte[] Str;

        public StringPointer(Pointer ptr, byte[] str)
        {
            Base = ptr;
            Str = str;
            char[] FormatChars = { '%', '/' };
            List<char> Ending = new List<char>();

            for (int i = 1; i < str.Length; i++)
            {
                char C = (char)str[str.Length - i];

                if (FormatChars.Contains(C))
                {
                    Ending.Add(C);
                }
                else
                {
                    break;
                }
            }

            Ending.Reverse();
            this.Ending = new string(Ending.ToArray());
        }
    }

    struct Pointer
    {
        public byte[] Ptr;
        public long PtrLocation;

        public Pointer(byte[] ptr, long loc) { Ptr = ptr; PtrLocation = loc; }
    }

    partial class HATE
    {
        public bool LoadDataAndFind(string header, long loc, float shufflechance, Func<FileStream, float, string, bool> Action)
        {
            byte[] ToFind = header.ToCharArray().Select(x => (byte)x).ToArray();
            byte[] ReadBuffer = new byte[ToFind.Length];
            FileStream Data = Safe.OpenFileStream("./data.win");
            if (Data == null) { return false; }

            Data.Position = loc;

            for (int i = 0; i < 2137420; i++)
            {
                Data.Read(ReadBuffer, 0, ToFind.Length);
                Data.Position -= 3;

                if (ReadBuffer.Select((value, index) => value == ToFind[index]).All(x => x))
                {
                    Console.WriteLine(header + " Memory Region Found at " + Data.Position.ToString("X") + ".");
                    Data.Position += 7;

                    if (!Action(Data, shufflechance, header))
                    {
                        Console.WriteLine("An Error Occured While Attempting To Modify " + header + " Memory Region.");

                        Data.Close();
                        return false;
                    }

                    Data.Close();
                    Console.WriteLine(header + " Memory Region Modified Successfully.");
                    return true;
                }
            }

            Console.WriteLine("Error: " + header + " Memory Region Not Found.");
            return false;
        }

        public bool SimpleShuffle(FileStream Data, float shufflechance, string header)
        {
            byte[] ReadBuffer = new byte[4];
            int PointerNum = 0;
            long PointerArrayBegin = 0;
            List<Pointer> PointerList = new List<Pointer>();

            Data.Read(ReadBuffer, 0, 4);
            PointerNum = BitConverter.ToInt32(ReadBuffer, 0);
            PointerArrayBegin = Data.Position;

            for (int i = 0; i < PointerNum; i++)
            {
                if (RNG.NextDouble() < shufflechance)
                {
                    byte[] _tmp = new byte[4];
                    Data.Read(_tmp, 0, 4);
                    PointerList.Add(new Pointer(_tmp, Data.Position - 4));
                }
            }

            PointerList.Shuffle();
            Data.Position = PointerArrayBegin;

            for (int i = 0; i < PointerNum; i++)
            {
                Data.Position = PointerList[(i + 1) % PointerList.Count].PtrLocation;
                Data.Write(PointerList[i].Ptr, 0, 4);
            }

            return true;
        }

        public bool ShuffleAudio_Func()
        {
            return LoadDataAndFind("AUDO", 23300000, 1.0f, SimpleShuffle) && LoadDataAndFind("SOND", 0, 1.0f, SimpleShuffle);
        }

        public bool ShuffleBG_Func()
        {
            return LoadDataAndFind("BGND", 1900000, 1.0f, SimpleShuffle);
        }

        public bool ShuffleFont_Func()
        {
            return LoadDataAndFind("FONT", 1900000, 1.0f, SimpleShuffle);
        }

        public bool HitboxFix_Func()
        {
            return LoadDataAndFind("SPRT", 15000, 1.0f, delegate (FileStream Data, float shufflechance, string header)
            {
                byte[] ReadBuffer = new byte[4];
                int PointerNum = 0;
                long PointerArrayBegin = 0;
                List<Pointer> PointerList = new List<Pointer>();

                Data.Read(ReadBuffer, 0, 4);
                PointerNum = BitConverter.ToInt32(ReadBuffer, 0);
                PointerArrayBegin = Data.Position;

                for (int i = 0; i < PointerNum; i++)
                {
                    byte[] PointerBuffer = new byte[4];
                    Data.Read(PointerBuffer, 0, 4);
                    PointerList.Add(new Pointer(PointerBuffer, Data.Position - 4));
                }

                for (int i = 0; i < PointerNum - 1; i++)
                {
                    int SpriteSizeX, SpriteSizeY, SpriteHitboxSize, Counter = 8;
                    Data.Position = BitConverter.ToInt32(PointerList[i].Ptr, 0) + 4;
                    Data.Read(ReadBuffer, 0, 4);
                    SpriteSizeX = BitConverter.ToInt32(ReadBuffer, 0);
                    Data.Read(ReadBuffer, 0, 4);
                    SpriteSizeY = BitConverter.ToInt32(ReadBuffer, 0);
                    SpriteHitboxSize = SpriteSizeX * SpriteSizeY / 8;

                    if (SpriteHitboxSize < 8 || SpriteHitboxSize > 9001)
                    {
                        continue;
                    }

                    Data.Position = BitConverter.ToInt32(PointerList[i + 1].Ptr, 0) - SpriteHitboxSize;
                    Data.Write(new byte[] { 255, 0, 0, 0, 0, 0, 0, 0 }, 0, 8);

                    while (Counter++ < SpriteHitboxSize)
                    {
                        Data.Write(new byte[] { 0 }, 0, 1);
                    }
                }

                return true;
            });
        }

        public bool ShuffleGFX_Func()
        {
            return LoadDataAndFind("SPRT", 15000, 1.0f, SimpleShuffle);
        }

        public bool ShuffleText_Func()
        {
            return LoadDataAndFind("STRG", 11100000, 1.0f, delegate (FileStream Data, float shufflechance, string header)
            {
                byte[] ReadBuffer = new byte[4];
                int PointerNum = 0;
                long PointerArrayBegin = 0;
                List<Pointer> PointerList = new List<Pointer>();
                List<StringPointer> StrPointerList = new List<StringPointer>();
                string[] BannedStrings = { "_" };

                Data.Read(ReadBuffer, 0, 4);
                PointerNum = BitConverter.ToInt32(ReadBuffer, 0);
                PointerArrayBegin = Data.Position;

                for (int i = 0; i < PointerNum; i++)
                {<commit_msg>fixed hitbox<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;

namespace HATE
{
    struct StringPointer
    {
        public Pointer Base;
        public string Ending;
        public byte[] Str;

        public StringPointer(Pointer ptr, byte[] str)
        {
            Base = ptr;
            Str = str;
            char[] FormatChars = { '%', '/' };
            List<char> Ending = new List<char>();

            for (int i = 1; i < str.Length; i++)
            {
                char C = (char)str[str.Length - i];

                if (FormatChars.Contains(C))
                {
                    Ending.Add(C);
                }
                else
                {
                    break;
                }
            }

            Ending.Reverse();
            this.Ending = new string(Ending.ToArray());
        }
    }

    struct Pointer
    {
        public byte[] Ptr;
        public long PtrLocation;

        public Pointer(byte[] ptr, long loc) { Ptr = ptr; PtrLocation = loc; }
    }

    partial class HATE
    {
        public bool LoadDataAndFind(string header, long loc, float shufflechance, Func<FileStream, float, string, bool> Action)
        {
            byte[] ToFind = header.ToCharArray().Select(x => (byte)x).ToArray();
            byte[] ReadBuffer = new byte[ToFind.Length];
            FileStream Data = Safe.OpenFileStream("./data.win");
            if (Data == null) { return false; }

            Data.Position = loc;

            for (int i = 0; i < 2137420; i++)
            {
                Data.Read(ReadBuffer, 0, ToFind.Length);
                Data.Position -= 3;

                if (ReadBuffer.Select((value, index) => value == ToFind[index]).All(x => x))
                {
                    Console.WriteLine(header + " Memory Region Found at " + Data.Position.ToString("X") + ".");
                    Data.Position += 7;

                    if (!Action(Data, shufflechance, header))
                    {
                        Console.WriteLine("An Error Occured While Attempting To Modify " + header + " Memory Region.");

                        Data.Close();
                        return false;
                    }

                    Data.Close();
                    Console.WriteLine(header + " Memory Region Modified Successfully.");
                    return true;
                }
            }

            Console.WriteLine("Error: " + header + " Memory Region Not Found.");
            return false;
        }

        public bool SimpleShuffle(FileStream Data, float shufflechance, string header)
        {
            byte[] ReadBuffer = new byte[4];
            int PointerNum = 0;
            long PointerArrayBegin = 0;
            List<Pointer> PointerList = new List<Pointer>();

            Data.Read(ReadBuffer, 0, 4);
            PointerNum = BitConverter.ToInt32(ReadBuffer, 0);
            PointerArrayBegin = Data.Position;

            for (int i = 0; i < PointerNum; i++)
            {
                if (RNG.NextDouble() < shufflechance)
                {
                    byte[] _tmp = new byte[4];
                    Data.Read(_tmp, 0, 4);
                    PointerList.Add(new Pointer(_tmp, Data.Position - 4));
                }
            }

            PointerList.Shuffle();
            Data.Position = PointerArrayBegin;

            for (int i = 0; i < PointerNum; i++)
            {
                Data.Position = PointerList[(i + 1) % PointerList.Count].PtrLocation;
                Data.Write(PointerList[i].Ptr, 0, 4);
            }

            return true;
        }

        public bool ShuffleAudio_Func()
        {
            return LoadDataAndFind("AUDO", 23300000, 1.0f, SimpleShuffle) && LoadDataAndFind("SOND", 0, 1.0f, SimpleShuffle);
        }

        public bool ShuffleBG_Func()
        {
            return LoadDataAndFind("BGND", 1900000, 1.0f, SimpleShuffle);
        }

        public bool ShuffleFont_Func()
        {
            return LoadDataAndFind("FONT", 1900000, 1.0f, SimpleShuffle);
        }

        public bool HitboxFix_Func()
        {
            return LoadDataAndFind("SPRT", 15000, 1.0f, delegate (FileStream Data, float shufflechance, string header)
            {
                byte[] ReadBuffer = new byte[4];
                int PointerNum = 0;
                long PointerArrayBegin = 0;
                List<Pointer> PointerList = new List<Pointer>();

                Data.Read(ReadBuffer, 0, 4);
                PointerNum = BitConverter.ToInt32(ReadBuffer, 0);
                PointerArrayBegin = Data.Position;

                for (int i = 0; i < PointerNum; i++)
                {
                    byte[] PointerBuffer = new byte[4];
                    Data.Read(PointerBuffer, 0, 4);
                    PointerList.Add(new Pointer(PointerBuffer, Data.Position - 4));
                }

                for (int i = 0; i < PointerNum - 1; i++)
                {
                    int SpriteSizeX, SpriteSizeY, SpriteHitboxSize, Counter = 8;
                    Data.Position = BitConverter.ToInt32(PointerList[i].Ptr, 0) + 4;
                    Data.Read(ReadBuffer, 0, 4);
                    SpriteSizeX = BitConverter.ToInt32(ReadBuffer, 0);
                    Data.Read(ReadBuffer, 0, 4);
                    SpriteSizeY = BitConverter.ToInt32(ReadBuffer, 0);
                    SpriteHitboxSize = SpriteSizeX * SpriteSizeY / 8;

                    if (SpriteHitboxSize < 8 || SpriteHitboxSize > 9001)
                    {
                        continue;
                    }

                    Data.Position = BitConverter.ToInt32(PointerList[i + 1].Ptr, 0) - SpriteHitboxSize;
                    if (SpriteHitboxSize % 2 != 0)
                    {
                        Data.Write(Enumerable.Repeat((byte)0, SpriteHitboxSize / 2).ToArray(), 0, SpriteHitboxSize / 2);
                        Data.Write(new byte[] { 255 }, 0, 1);
                        Data.Write(Enumerable.Repeat((byte)0, SpriteHitboxSize / 2).ToArray(), 0, SpriteHitboxSize / 2);
                    }
                    else
                    {
                        Data.Write(Enumerable.Repeat((byte)0, SpriteHitboxSize / 2 -1).ToArray(), 0, SpriteHitboxSize / 2 -1);
                        Data.Write(new byte[] { 255, 255}, 0, 2);
                        Data.Write(Enumerable.Repeat((byte)0, SpriteHitboxSize / 2 - 1).ToArray(), 0, SpriteHitboxSize / 2 - 1);
                    }
                }

                return true;
            });
        }

        public bool ShuffleGFX_Func()
        {
            return LoadDataAndFind("SPRT", 15000, 1.0f, SimpleShuffle);
        }

        public bool ShuffleText_Func()
        {
            return LoadDataAndFind("STRG", 11100000, 1.0f, delegate (FileStream Data, float shufflechance, string header)
            {
                byte[] ReadBuffer = new byte[4];
                int PointerNum = 0;
                long PointerArrayBegin = 0;
                List<Pointer> PointerList = new List<Pointer>();
                List<StringPointer> StrPointerList = new List<StringPointer>();
                string[] BannedStrings = { "_" };

                Data.Read(ReadBuffer, 0, 4);
                PointerNum = BitConverter.ToInt32(ReadBuffer, 0);
                PointerArrayBegin = Data.Position;

                for (int i = 0; i < PointerNum; i++)
                {
{'fraction_non_alphanumeric': 0.10641519823788546, 'fraction_numerical': 0.021751101321585902, 'mean_word_length': 1.3597531265226572, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 33, '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': '1317411', 'n_tokens_mistral': 4210, 'n_tokens_neox': 3708, 'n_words': 921}