Skip to content

Commit

Permalink
Crysis 3 Benchmark Patch
Browse files Browse the repository at this point in the history
  • Loading branch information
illusion0001 committed Jul 2, 2024
1 parent 1cd93c2 commit 25f763e
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Windows-Game-Patches-x64.sln
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RDR3.Patches", "source\RDR3
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "KittyHawk.Patches", "source\KittyHawk.Patches\KittyHawk.Patches.vcxproj", "{0E5D915F-4905-4496-8024-648E215FC947}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Crysis3Remastered.Patches", "source\Crysis3Remastered.Patches\Crysis3Remastered.Patches.vcxproj", "{426FD79A-652A-4ECA-8146-FA8DD65EDA57}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Expand Down Expand Up @@ -321,6 +323,18 @@ Global
{0E5D915F-4905-4496-8024-648E215FC947}.Release|x64.Build.0 = Release|x64
{0E5D915F-4905-4496-8024-648E215FC947}.Release|x86.ActiveCfg = Release|x64
{0E5D915F-4905-4496-8024-648E215FC947}.Release|x86.Build.0 = Release|x64
{426FD79A-652A-4ECA-8146-FA8DD65EDA57}.Debug|Win32.ActiveCfg = Debug|x64
{426FD79A-652A-4ECA-8146-FA8DD65EDA57}.Debug|Win32.Build.0 = Debug|x64
{426FD79A-652A-4ECA-8146-FA8DD65EDA57}.Debug|x64.ActiveCfg = Debug|x64
{426FD79A-652A-4ECA-8146-FA8DD65EDA57}.Debug|x64.Build.0 = Debug|x64
{426FD79A-652A-4ECA-8146-FA8DD65EDA57}.Debug|x86.ActiveCfg = Debug|x64
{426FD79A-652A-4ECA-8146-FA8DD65EDA57}.Debug|x86.Build.0 = Debug|x64
{426FD79A-652A-4ECA-8146-FA8DD65EDA57}.Release|Win32.ActiveCfg = Release|x64
{426FD79A-652A-4ECA-8146-FA8DD65EDA57}.Release|Win32.Build.0 = Release|x64
{426FD79A-652A-4ECA-8146-FA8DD65EDA57}.Release|x64.ActiveCfg = Release|x64
{426FD79A-652A-4ECA-8146-FA8DD65EDA57}.Release|x64.Build.0 = Release|x64
{426FD79A-652A-4ECA-8146-FA8DD65EDA57}.Release|x86.ActiveCfg = Release|x64
{426FD79A-652A-4ECA-8146-FA8DD65EDA57}.Release|x86.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
122 changes: 122 additions & 0 deletions source/Crysis3Remastered.Patches/Crysis3Remastered.Patches.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{426FD79A-652A-4ECA-8146-FA8DD65EDA57}</ProjectGuid>
<RootNamespace>Crysis3RemasteredPatches</RootNamespace>
<WindowsTargetPlatformVersion>10.0.22621.0</WindowsTargetPlatformVersion>
<ProjectName>Crysis3Remastered.Patches</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>ClangCL</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>ClangCL</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>\..\include\;$(IncludePath)</IncludePath>
<TargetExt>.asi</TargetExt>
<OutDir>$(SolutionDir)$(Configuration)\$(Platform)\$(ProjectName)\</OutDir>
<IntDir>$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IncludePath>\..\include\;$(IncludePath)</IncludePath>
<TargetExt>.asi</TargetExt>
<OutDir>$(SolutionDir)$(Configuration)\$(Platform)\$(ProjectName)\</OutDir>
<IntDir>$(Configuration)\</IntDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>UNICODE;_UNICODE;_USRDLL</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>..\..\include\;..\..\external\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard_C>Default</LanguageStandard_C>
<UndefinePreprocessorDefinitions>
</UndefinePreprocessorDefinitions>
<EnableEnhancedInstructionSet>NotSet</EnableEnhancedInstructionSet>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>false</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>UNICODE;_UNICODE;_USRDLL</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>..\..\include\;..\..\external\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard_C>Default</LanguageStandard_C>
<UndefinePreprocessorDefinitions>
</UndefinePreprocessorDefinitions>
<Optimization>Disabled</Optimization>
<EnableEnhancedInstructionSet>NotSet</EnableEnhancedInstructionSet>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
<PreBuildEvent>
<Command>call $(MSBuildStartupDirectory)\set_git_ver.cmd</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\Shared\helper.cpp" />
<ClCompile Include="..\Shared\memory.cpp" />
<ClCompile Include="..\Shared\stdafx.cpp" />
<ClCompile Include="dllmain.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\helper.hpp" />
<ClInclude Include="..\..\include\memory.hpp" />
<ClInclude Include="..\..\include\stdafx.h" />
<ClInclude Include="IConsole.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Shared\stdafx.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Shared\helper.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Shared\memory.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\helper.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\stdafx.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\memory.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="IConsole.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
15 changes: 15 additions & 0 deletions source/Crysis3Remastered.Patches/IConsole.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <stdbool.h>

struct IConsole
{
void* filler[0x22];
//! Execute a string in the console.
//! \param command Console command e.g. "map testy" - no leading slash
//! \param bSilentMode true=suppresses log in error case and logging the command to the console
//! \param bDeferExecution true=the command is stored in special fifo that allows delayed execution by using wait_seconds and wait_frames commands
//! \par Example
//! \include CrySystem/Examples/ConsoleCommand.cpp
void (*ExecuteString)(void* _this, const char* command, const bool bSilentMode, const bool bDeferExecution);
};
173 changes: 173 additions & 0 deletions source/Crysis3Remastered.Patches/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#include "stdafx.h"
#include "helper.hpp"
#include "memory.hpp"
#include "git_ver.h"
#include "IConsole.h"

HMODULE baseModule{};

#define wstr(s) L#s
#define wxstr(s) wstr(s)
#define PROJECT_NAME "Crysis3Remastered.Patches"

enum eBenchmarkTypes
{
eCPUBenchmark = 1,
eGPUBenchmark,
};

// INI Variables
bool bBenchmarkingOnly{};
bool bVerboseConsoleOutput{};
int iBenchmarkType{};

void ReadConfig(void)
{
inipp::Ini<wchar_t> ini;
// Initialize config
std::wstring config_path = L"" PROJECT_NAME ".ini";
std::wifstream iniFile(config_path);
if (!iniFile)
{
// no ini, lets generate one.
std::wstring ini_defaults = L"[Settings]\n"
wstr(bBenchmarkingOnly)" false\n"
wstr(bVerboseConsoleOutput)" = false\n"
"; Benchmark types:\n"
"; 1 = CPU Benchmark\n"
"; 2 = GPU Benchmark\n"
"; Savefiles from `/data/` must be installed to `%userprofile%\\Saved Games\\Crysis3Remastered\\SaveGames`\n"
wstr(iBenchmarkType)" = 1\n";
std::wofstream iniFile(config_path);
iniFile << ini_defaults;
bBenchmarkingOnly = false;
bVerboseConsoleOutput = false;
iBenchmarkType = 1;
}
else
{
ini.parse(iniFile);
inipp::get_value(ini.sections[L"Settings"], wstr(bBenchmarkingOnly), bBenchmarkingOnly);
inipp::get_value(ini.sections[L"Settings"], wstr(bVerboseConsoleOutput), bVerboseConsoleOutput);
inipp::get_value(ini.sections[L"Settings"], wstr(iBenchmarkType), iBenchmarkType);
}
}

#define TYPEDEF_FUNCTION_PTR(ret_type, func_name, ...) \
typedef ret_type (*func_name##_ptr)(__VA_ARGS__); \
extern func_name##_ptr func_name;

#define INIT_FUNCTION_PTR(func_name) \
func_name##_ptr func_name = nullptr

TYPEDEF_FUNCTION_PTR(void, CryLogAlways_, const char* fmt, ...);
INIT_FUNCTION_PTR(CryLogAlways_);

struct ISystem
{
IConsole* pConsole;
};

ISystem** g_System;
bool saveloaded{};

namespace CRichPresence
{
static bool SetRichPresence_Hook(const char* msg)
{
CryLogAlways_(msg);
if (!saveloaded)
{
CryLogAlways_("CRichPresence::SetRichPresence_Hook: Executing benchmark\n");
g_System[0]->pConsole->ExecuteString(g_System[0], iBenchmarkType == eCPUBenchmark ? "load default_CPU.CSF" : "load default_GPU.CSF", true, false);
g_System[0]->pConsole->ExecuteString(g_System[0], "con_restricted 0", true, false);
// why is this considered restricted??
g_System[0]->pConsole->ExecuteString(g_System[0], "g_skipAfterLoadingScreen 1", true, false);
saveloaded = true;
}
return 1;
}
};


static bool CallFunction32(void* src, void* dst, int len)
{
if (len < 5)
{
return false;
}
DWORD curProtection;
VirtualProtect(src, len, PAGE_EXECUTE_READWRITE, &curProtection);

memset(src, 0x90, len);

uintptr_t relativeAddress = ((uintptr_t)dst - (uintptr_t)src) - 5;

*(BYTE*)src = 0xE8;
*(uint32_t*)((uintptr_t)src + 1) = relativeAddress;

DWORD temp;
VirtualProtect(src, len, curProtection, &temp);

return true;
}

static void DoPatches()
{
// kills two bird with one stone
// patches to load our custom save
// and redirect CryLog to CryLogAlways
// https://github.com/ValtoGameEngines/CryEngine/blob/d9d2c9f000836f0676e65a90bed40dcc3b1451eb/Code/GameSDK/GameDll/RichPresence.cpp#L75-L79
// if (!g_pGame->HasExclusiveControllerIndex())
// {
// CryLog("[Rich Presence] not setting rich presence, no player set");
// return true;
// }
uintptr_t AfterLogosHook = FindAndPrintPatternW(L"48 8d 0d ? ? ? ? e8 ? ? ? ? b0 01 48 81 c4 ? ? ? ? 41 5e 5f 5d c3", L"RichPersistence Pattern");
uintptr_t g_SystemAddr = FindAndPrintPatternW(L"48 8b 0d ? ? ? ? 48 8d 15 ? ? ? ? 45 33 c9 45 33 c0 48 8b 01 ff 90 10 01 00 00", L"ISystem Global Ptr");
uintptr_t CryLogAlways_Addr = FindAndPrintPatternW(L"48 8d 0d ? ? ? ? e8 ? ? ? ? 0f b6 83 ? ? ? ?", L"CryLogAlways()");
void* CryLogAlways_Ptr = (void*)ReadLEA32(CryLogAlways_Addr + 7, L"CryLog()", 0, 1, 5);
g_System = (ISystem**)ReadLEA32(g_SystemAddr, L"IConsole**", 0, 3, 7);
CryLogAlways_ = (CryLogAlways__ptr)CryLogAlways_Ptr;
if (bVerboseConsoleOutput)
{
void* CryLogPtr = (void*)ReadLEA32(AfterLogosHook + 7, L"CryLog()", 0, 1, 5);
Memory::DetourFunction32(CryLogPtr, CryLogAlways_Ptr, 5);
}
if (bBenchmarkingOnly)
{
if (AfterLogosHook && g_System)
{
void* JumpPattern = (void*)FindAndPrintPatternW(L"CC CC CC CC CC CC CC CC CC CC CC CC CC", L"Int3 Arrray");
CallFunction32((void*)(AfterLogosHook + 7), JumpPattern, 5);
Memory::DetourFunction64(JumpPattern, (void*)&CRichPresence::SetRichPresence_Hook, 14);
}
}
}

DWORD __stdcall Main(void*)
{
baseModule = GetModuleHandle(NULL);
ReadConfig();
DoPatches();
return true;
}

BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
CreateThread(NULL, 0, Main, 0, NULL, 0);
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

0 comments on commit 25f763e

Please sign in to comment.