<commit_before>#include "stdafx.h"
#include "..\includes\CPatch.h"

#define _USE_MATH_DEFINES
#include "math.h"
HWND hWnd;

int hud_patch;
int res_x;
int res_y;
float fres_x, fres_y;
float fDynamicScreenFieldOfViewScale;
float fAspectRatio;
float hud_multiplier_x;

#define DEGREE_TO_RADIAN(fAngle) \
	((fAngle)* (float)M_PI / 180.0f)
#define RADIAN_TO_DEGREE(fAngle) \
	((fAngle)* 180.0f / (float)M_PI)
#define SCREEN_FOV_HORIZONTAL		124.59155f
#define SCREEN_FOV_VERTICAL			(2.0f * RADIAN_TO_DEGREE(atan(tan(DEGREE_TO_RADIAN(SCREEN_FOV_HORIZONTAL * 0.5f)) / (4.0f / 3.0f))))

void Init()
{
	CIniReader iniReader("");
	res_x = iniReader.ReadInteger("MAIN", "X", 0);
	res_y = iniReader.ReadInteger("MAIN", "Y", 0);
	hud_patch = iniReader.ReadInteger("MAIN", "HUD_PATCH", 0);

	if (!res_x || !res_y) {
		HMONITOR monitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
		MONITORINFO info;
		info.cbSize = sizeof(MONITORINFO);
		GetMonitorInfo(monitor, &info);
		res_x = info.rcMonitor.right - info.rcMonitor.left;
		res_y = info.rcMonitor.bottom - info.rcMonitor.top;
	}

	if ((*(DWORD*)0x4E2932 == 640))
	{
		CPatch::SetUInt(0x67FBCC, res_x);
		CPatch::SetUInt(0x67FBD0, res_y);

		fres_x = static_cast<float>(res_x);
		fres_y = static_cast<float>(res_y);

		/*CPatch::SetPointer(0x4370CB + 0x4, &fres_x);
		CPatch::SetPointer(0x4370F7 + 0x4, &fres_y);

		CPatch::SetPointer(0x4370CB + 0x4, &fres_x);
		CPatch::SetPointer(0x4370F7 + 0x4, &fres_y);*/

		CPatch::SetUInt(0x4D88AC + 0x1, res_x);
		CPatch::SetUInt(0x4D88CE + 0x4, res_x);
		CPatch::SetUInt(0x4D88DA + 0x4, res_x);
		CPatch::SetUInt(0x4D88E2 + 0x4, res_x);

		CPatch::SetUInt(0x4D88EE + 0x4, res_y);
		CPatch::SetUInt(0x4D88F6 + 0x4, res_y);
		CPatch::SetUInt(0x4D88FE + 0x4, res_y);

		fAspectRatio = fres_x / fres_y;
		fDynamicScreenFieldOfViewScale = 2.0f * RADIAN_TO_DEGREE(atan(tan(DEGREE_TO_RADIAN(SCREEN_FOV_VERTICAL * 0.5f)) * fAspectRatio));

		CPatch::SetFloat(0x649804, fDynamicScreenFieldOfViewScale);
		CPatch::SetPointer(0x4A0860 + 0x2, &fAspectRatio);

		hud_multiplier_x = 1.0f / res_x * (res_y / 480.0f);
		if (hud_patch)
		{
			CPatch::SetPointer(0x4D8A30 + 0x2, &hud_multiplier_x);
			CPatch::SetPointer(0x4B6CF8 + 0x2, &hud_multiplier_x);

			CPatch::Nop(0x4D87F0, 6);
			CPatch::Nop(0x4D8A58, 6);
			DWORD nHudAlignment = static_cast<DWORD>((fres_x - (fres_y * (4.0f / 3.0f))) / 2);
			CPatch::SetUInt(0x787D88, nHudAlignment);
		}
	}
	else
	{
		CPatch::SetUInt(0x67EBCC, res_x);
		CPatch::SetUInt(0x67EBD0, res_y);

		fres_x = static_cast<float>(res_x);
		fres_y = static_cast<float>(res_y);

		CPatch::SetUInt(0x4D873C + 0x1, res_x);
		CPatch::SetUInt(0x4D875E + 0x4, res_x);
		CPatch::SetUInt(0x4D876A + 0x4, res_x);
		CPatch::SetUInt(0x4D8772 + 0x4, res_x);

		CPatch::SetUInt(0x4D877E + 0x4, res_y);
		CPatch::SetUInt(0x4D8786 + 0x4, res_y);
		CPatch::SetUInt(0x4D878E + 0x4, res_y);

		fAspectRatio = fres_x / fres_y;
		fDynamicScreenFieldOfViewScale = 2.0f * RADIAN_TO_DEGREE(atan(tan(DEGREE_TO_RADIAN(SCREEN_FOV_VERTICAL * 0.5f)) * fAspectRatio));

		CPatch::SetFloat(0x648804, fDynamicScreenFieldOfViewScale);
		CPatch::SetPointer(0x4A0790 + 0x2, &fAspectRatio);

		hud_multiplier_x = 1.0f / res_x * (res_y / 480.0f);
		if (hud_patch)
		{
			CPatch::SetPointer(0x4D88C0 + 0x2, &hud_multiplier_x);
			CPatch::SetPointer(0x4B6BA8 + 0x2, &hud_multiplier_x);

			CPatch::Nop(0x4D8680, 6);
			CPatch::Nop(0x4D88E8, 6);
			DWORD nHudAlignment = static_cast<DWORD>((fres_x - (fres_y * (4.0f / 3.0f))) / 2);
			CPatch::SetUInt(0x786D88, nHudAlignment);
		}
	}
}

BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD reason, LPVOID /*lpReserved*/)
{
	if (reason == DLL_PROCESS_ATTACH)
	{
		Init();
	}
	return TRUE;
}<commit_msg>thug - proper fix<commit_after>#include "stdafx.h"

struct Screen
{
    int32_t Width;
    int32_t Height;
    float fWidth;
    float fHeight;
    int32_t Width43;
    float fAspectRatio;
    float fAspectRatioDiff;
    float fFieldOfView;
    float fHUDScaleX;
    float fHudOffset;
    float fHudOffsetReal;
} Screen;

DWORD WINAPI Init(LPVOID bDelay)
{
    auto pattern = hook::pattern("83 C2 01 83 C6 04 83 FA 04");

    if (pattern.empty() && !bDelay)
    {
        LoadLibrary("TonyHawksUnderground.SafeDiscCheck.dll");
        CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&Init, (LPVOID)true, 0, NULL);
        return 0;
    }

    if (bDelay)
        while (pattern.clear().count_hint(1).empty()) { Sleep(0); };

    CIniReader iniReader("");
    Screen.Width = iniReader.ReadInteger("MAIN", "ResX", 0);
    Screen.Height = iniReader.ReadInteger("MAIN", "ResY", 0);
    bool bFixHUD = iniReader.ReadInteger("MAIN", "FixHUD", 1) != 0;

    if (!Screen.Width || !Screen.Height)
        std::tie(Screen.Width, Screen.Height) = GetDesktopRes();

    Screen.fWidth = static_cast<float>(Screen.Width);
    Screen.fHeight = static_cast<float>(Screen.Height);
    Screen.Width43 = static_cast<int32_t>(Screen.fHeight * (4.0f / 3.0f));
    Screen.fAspectRatio = (Screen.fWidth / Screen.fHeight);
    Screen.fAspectRatioDiff = 1.0f / (Screen.fAspectRatio / (4.0f / 3.0f));
    Screen.fHUDScaleX = 1.0f / Screen.fWidth * (Screen.fHeight / 480.0f);
    Screen.fHudOffset = ((480.0f * Screen.fAspectRatio) - 640.0f) / 2.0f;
    Screen.fHudOffsetReal = (Screen.fWidth - Screen.fHeight * (4.0f / 3.0f)) / 2.0f;

    //addresses is not based on drm free exe
    //Resolution
    pattern = hook::pattern("6A 01 6A 01 ? 8B 15");
    static int32_t* dword_6D3608 = *pattern.get_first<int32_t*>(7);
    static int32_t* dword_6D360C = *pattern.get_first<int32_t*>(-4);
    struct SetResHook
    {
        void operator()(injector::reg_pack& regs)
        {
            *dword_6D3608 = Screen.Width;
            *dword_6D360C = Screen.Height;
        }
    };
    pattern = hook::pattern(pattern_str(0xA3, to_bytes(dword_6D3608)));
    for (size_t i = 0; i < pattern.size(); ++i)
        injector::MakeInline<SetResHook>(pattern.get(i).get<uint32_t>(0));

    pattern = hook::pattern(pattern_str(0x89, '?', to_bytes(dword_6D360C)));
    for (size_t i = 0; i < pattern.size(); ++i)
        injector::MakeInline<SetResHook>(pattern.get(i).get<uint32_t>(0), pattern.get(i).get<uint32_t>(6));
    
    pattern = hook::pattern(pattern_str(0x89, '?', to_bytes(dword_6D3608)));
    injector::MakeInline<SetResHook>(pattern.get_first(0), pattern.get_first(6));

    pattern = hook::pattern(pattern_str(0xA3, to_bytes(dword_6D360C)));
    if (!pattern.empty())
        injector::MakeInline<SetResHook>(pattern.get_first(0));

    //Aspect Ratio
    pattern = hook::pattern("68 ? ? ? ? E8 ? ? ? ? 6A 00 68 ? ? ? ? E8 ? ? ? ? D9 5C 24 08");
    if (!pattern.empty())
    {
        injector::WriteMemory<float>(pattern.get_first(1), Screen.fAspectRatio, true);
    }
    else
    {
        pattern = hook::pattern("D9 05 ? ? ? ? 51 D9 1C 24 E8 ? ? ? ? 6A 00");
        injector::WriteMemory<float>(*pattern.get_first<float*>(2), Screen.fAspectRatio, true);
    }

    //FOV
    pattern = hook::pattern("? 0D ? ? ? ? D9 9E ? ? ? ? 5E 59 C3");
    if (*pattern.get_first<uint8_t>(0) == (uint8_t)0xD8) //fmul flt
    {
        #undef SCREEN_FOV_HORIZONTAL
        #undef SCREEN_FOV_VERTICAL
        #define SCREEN_FOV_HORIZONTAL 114.59155f
        #define SCREEN_FOV_VERTICAL (2.0f * RADIAN_TO_DEGREE(atan(tan(DEGREE_TO_RADIAN(SCREEN_FOV_HORIZONTAL * 0.5f)) / SCREEN_AR_NARROW)))
        float fDynamicScreenFieldOfViewScale = 2.0f * RADIAN_TO_DEGREE(atan(tan(DEGREE_TO_RADIAN(SCREEN_FOV_VERTICAL * 0.5f)) * Screen.fAspectRatio)) * (1.0f / SCREEN_FOV_HORIZONTAL);
        injector::WriteMemory<float>(*pattern.get_first<float*>(2), fDynamicScreenFieldOfViewScale, true);
    }
    else //fmul dbl
    {
        #undef SCREEN_FOV_HORIZONTAL
        #undef SCREEN_FOV_VERTICAL
        #define SCREEN_FOV_HORIZONTAL 57.29577791868205f
        #define SCREEN_FOV_VERTICAL (2.0f * RADIAN_TO_DEGREE(atan(tan(DEGREE_TO_RADIAN(SCREEN_FOV_HORIZONTAL * 0.5f)) / SCREEN_AR_NARROW)))
        float fDynamicScreenFieldOfViewScale = 2.0f * RADIAN_TO_DEGREE(atan(tan(DEGREE_TO_RADIAN(SCREEN_FOV_VERTICAL * 0.5f)) * Screen.fAspectRatio)) * (1.0f / SCREEN_FOV_HORIZONTAL);
        static double dbl_62D630 = 57.29577791868205 * fDynamicScreenFieldOfViewScale;
        injector::WriteMemory(pattern.get_first(2), &dbl_62D630, true);
    }

    //HUD
    if (bFixHUD)
    {
        Screen.fHUDScaleX = 1.0f / Screen.fHUDScaleX;
        static int32_t nHudOffset = static_cast<int32_t>(Screen.fHudOffsetReal);
        static int32_t* dword_72DFC4;
        struct SetOffsetHook
        {
            void operator()(injector::reg_pack& regs)
            {
                *dword_72DFC4 = nHudOffset;
            }
        };

        pattern = hook::pattern("D8 0D ? ? ? ? 03 C2 C1 F8 02");
        if (!pattern.empty())
        {
            injector::WriteMemory<float>(*pattern.get_first<float*>(2), Screen.fHUDScaleX, true);
            nHudOffset = static_cast<int32_t>((Screen.fWidth - (Screen.fHeight * (4.0f / 3.0f))) / 4.0f);
            dword_72DFC4 = *hook::get_pattern<int32_t*>("DA 05 ? ? ? ? 0F BF 88", 2);
        }
        else
        {
            
            pattern = hook::pattern("DC 0D ? ? ? ? DE FB D9");
            injector::WriteMemory<double>(*pattern.get_first<double*>(2), Screen.fHUDScaleX, true);

            dword_72DFC4 = *hook::get_pattern<int32_t*>("DA 05 ? ? ? ? 89 4C 24 20 89 54 24 18", 2);
        }

        pattern = hook::pattern(pattern_str(0x89, '?', to_bytes(dword_72DFC4)));
        for (size_t i = 0; i < pattern.size(); ++i)
            injector::MakeInline<SetOffsetHook>(pattern.get(i).get<uint32_t>(0), pattern.get(i).get<uint32_t>(6));

        //4BE172 - filter fullscreen drawings maybe?
    }

    return 0;
}

BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD reason, LPVOID /*lpReserved*/)
{
    if (reason == DLL_PROCESS_ATTACH)
    {
        Init(NULL);
    }
    return TRUE;
}
{'fraction_non_alphanumeric': 0.1494949494949495, 'fraction_numerical': 0.06525252525252526, 'mean_word_length': 3.674693106704438, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 38, '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': '3134556', 'n_tokens_mistral': 4577, 'n_tokens_neox': 4121, 'n_words': 631}