Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Water Surface Rendering Fix #71

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MGSHDFix.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>lib\libMinHook-x64-v141-mt.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down
59 changes: 59 additions & 0 deletions src/dllmain.cpp
Copy link
Owner

@Lyall Lyall Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. My only suggestions would be to use less unique patterns so there's some resilience against game patches, also to add some logging upon successful hooking. Maybe something like this?

  if (eGameType == MgsGame::MGS3)
  {
      MH_STATUS status;

      uint8_t* MGS3_RenderWaterSurfaceScanResult = Memory::PatternScan(baseModule, "0F 57 ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? E8 ?? ?? ?? ?? 48 8B ?? ?? ?? 48 89 ?? ?? ?? ?? ??");
      uintptr_t MGS3_RenderWaterSurfaceScanAddress = Memory::GetAbsolute((uintptr_t)MGS3_RenderWaterSurfaceScanResult + 0x10);
      if (MGS3_RenderWaterSurfaceScanResult && MGS3_RenderWaterSurfaceScanAddress)
      {
          status = Memory::HookFunction((uint8_t*)MGS3_RenderWaterSurfaceScanAddress, MGS3_RenderWaterSurface_Hook, (LPVOID*)&MGS3_RenderWaterSurface);

          if (status != MH_OK)
          {
              LOG_F(INFO, "MGS 3: Render Water Surface: Hook failed.");
          }
          else if (status == MH_OK)
          {
              LOG_F(INFO, "MGS 3: Render Water Surface: Hook successful. Hook address is 0x%" PRIxPTR, MGS3_RenderWaterSurfaceScanAddress);
          }
      }
      else
      {
          LOG_F(INFO, "MGS 3:  Render Water Surface: Pattern scan failed.");
      }

      uint8_t* MGS3_GetViewportCameraOffsetYScanResult = Memory::PatternScan(baseModule, "E8 ?? ?? ?? ?? F3 44 ?? ?? ?? E8 ?? ?? ?? ?? F3 44 ?? ?? ?? ?? ?? ?? 00 00");
      uintptr_t MGS3_GetViewportCameraOffsetYScanAddress = Memory::GetAbsolute((uintptr_t)MGS3_GetViewportCameraOffsetYScanResult + 0xB);
      if (MGS3_GetViewportCameraOffsetYScanResult && MGS3_GetViewportCameraOffsetYScanAddress)
      {
          status = Memory::HookFunction((uint8_t*)MGS3_GetViewportCameraOffsetYScanAddress, MGS3_GetViewportCameraOffsetY_Hook, (LPVOID*)&MGS3_GetViewportCameraOffsetY);

          if (status != MH_OK)
          {
              LOG_F(INFO, "MGS 3: Get Viewport Camera Offset: Hook failed.");
          }
          else if (status == MH_OK)
          {
              LOG_F(INFO, "MGS 3: Get Viewport Camera Offset: Hook successful. Hook address is 0x%" PRIxPTR, MGS3_GetViewportCameraOffsetYScanAddress);
          }
      }
      else
      {
          LOG_F(INFO, "MGS 3: Get Viewport Camera Offset: Pattern scan failed.");
      }
  }

Copy link
Contributor Author

@cipherxof cipherxof Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be more useful to report the address relative to baseModule? At the very least the base module address should be logged, because currently logging the absolute addresses doesn't allow us to actually check what is being patched in a disassembler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated with the requested changes as well as logging the module address.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be more useful to report the address relative to baseModule? At the very least the base module address should be logged, because currently logging the absolute addresses doesn't allow us to actually check what is being patched in a disassembler.

That's true, so should we add a log of the baseModule address during DetectGame? Then for the relative addresses on hooks have something like this?

  uintptr_t MGS3_RenderWaterSurfaceScanRelAddress = MGS3_RenderWaterSurfaceScanAddress - baseModuleAddr;
  LOG_F(INFO, "MGS 3: Render Water Surface: Address is %s+%x", sExeName.c_str(), MGS3_RenderWaterSurfaceScanRelAddress);

This would look like MGS 3: Render Water Surface: Address is METAL GEAR SOLID3.exe+5ac7c0 in the log.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that works. Although it may be worth doing a separate pass on all of the logs in another PR as to not muddy this one. For now, logging the base module address at least allows us to manually find the relative addresses.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that works. Although it may be worth doing a separate pass on all of the logs in another PR as to not muddy this one. For now, logging the base module address at least allows us to manually find the relative addresses.

Sounds good to me. I'll merge this and put up a new release.

Copy link
Contributor

@emoose emoose Dec 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI in case it helps at all, you can disable the relocating with an EXE flag, steamstub would need to be removed first though: nipkownix/re4_tweaks#201 (comment)

Of course that doesn't help with user logs though, but can make debugging stuff a bit easier.

Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,25 @@ void __declspec(naked) MGS2_MGS3_SetSamplerStateAniso_CC()
}
}

bool MGS3_UseAdjustedOffsetY = true;

typedef int64_t MGS3_RenderWaterSurface_Fn(int64_t a1);
MGS3_RenderWaterSurface_Fn* MGS3_RenderWaterSurface = nullptr;
int64_t __fastcall MGS3_RenderWaterSurface_Hook(int64_t work)
{
MGS3_UseAdjustedOffsetY = false;
auto result = MGS3_RenderWaterSurface(work);
MGS3_UseAdjustedOffsetY = true;
return result;
}

typedef float MGS3_GetViewportCameraOffsetY_Fn(void);
MGS3_GetViewportCameraOffsetY_Fn* MGS3_GetViewportCameraOffsetY = nullptr;
float MGS3_GetViewportCameraOffsetY_Hook()
{
return MGS3_UseAdjustedOffsetY ? MGS3_GetViewportCameraOffsetY() : 0.00f;
}

void Logging()
{
loguru::add_file("MGSHDFix.log", loguru::Truncate, loguru::Verbosity_MAX);
Expand Down Expand Up @@ -1214,6 +1233,45 @@ void Miscellaneous()
}
}

void ViewportFix()
{
if (eGameType == MgsGame::MGS3)
{
MH_STATUS status;
uint8_t* MGS3_RenderWaterSurfaceScanResult = Memory::PatternScan(baseModule, "48 8B C4 48 89 58 10 55 48 8D 68 A1 48 81 EC 00");

if (MGS3_RenderWaterSurfaceScanResult)
{
status = Memory::HookFunction(MGS3_RenderWaterSurfaceScanResult, MGS3_RenderWaterSurface_Hook, (LPVOID*)&MGS3_RenderWaterSurface);

if (status != MH_OK)
{
LOG_F(INFO, "MGS 3: Render water surface hook failed.");
}
}
else
{
LOG_F(INFO, "MGS 3: Render water surface pattern scan failed.");
}

uint8_t* MGS3_GetViewportCameraOffsetYScanResult = Memory::PatternScan(baseModule, "01 48 0F 44 C1 F3 0F 10 40 10 C3") - 0x2B;

if (MGS3_GetViewportCameraOffsetYScanResult)
{
status = Memory::HookFunction(MGS3_GetViewportCameraOffsetYScanResult, MGS3_GetViewportCameraOffsetY_Hook, (LPVOID*)&MGS3_GetViewportCameraOffsetY);

if (status != MH_OK)
{
LOG_F(INFO, "MGS 3: Get viewport camera offset hook failed.");
}
}
else
{
LOG_F(INFO, "MGS 3: Get viewport camera offset pattern scan failed.");
}
}
}

using NHT_COsContext_SetControllerID_Fn = void (*)(int controllerType);
NHT_COsContext_SetControllerID_Fn NHT_COsContext_SetControllerID = nullptr;
void NHT_COsContext_SetControllerID_Hook(int controllerType)
Expand Down Expand Up @@ -1430,6 +1488,7 @@ DWORD __stdcall Main(void*)
AspectFOVFix();
HUDFix();
Miscellaneous();
ViewportFix();
}

// Signal any threads which might be waiting for us before continuing
Expand Down