Skip to content

Commit

Permalink
Fix even more string array crashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
blueskythlikesclouds committed Jun 24, 2022
1 parent 69df3df commit caf8cd3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
10 changes: 5 additions & 5 deletions Source/DivaModLoader/DatabaseLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,17 @@ HOOK(void, __fastcall, LoadStrArray, sigLoadStrArray())
loadStrArray(dir + "/rom/lang2/mod_str_array.toml");
}

// RDX, R8 and R9 in the argument list are required to prevent memory corruptions.
// I am not sure why this is required, but it is the only way to prevent crashing.
// This function isn't implemented here. See DatabaseLoaderImp.asm for details.
HOOK(const char*, __fastcall, GetStr, sigGetStr(), const int id);

HOOK(const char*, __fastcall, GetStr, sigGetStr(), const int id, void* RDX, void* R8, void* R9)
const char* getStrImp(const int id)
{
const auto str = strArray.find(id);

if (str != strArray.end())
return str->second.c_str();

return originalGetStr(id, RDX, R8, R9);
return originalGetStr(id);
}

void DatabaseLoader::init()
Expand Down
44 changes: 44 additions & 0 deletions Source/DivaModLoader/DatabaseLoaderImp.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.code

; THIS FUNCTION IS CURSED.
; The original function barely utilizes any registers, so functions calling it don't care about their temporary registers getting corrupted.
; The hook changes them, so we run into crashes.

; Push every register known to mankind to overcome this issue.

?getStrImp@@YAPEBDH@Z proto

?implOfGetStr@@YAPEBDH@Z:
push rbx
push rdx
push rsi
push rdi
push r8
push r9
push r10
push r11
push r12
push r13
push r14
push r15

call ?getStrImp@@YAPEBDH@Z

pop r15
pop r14
pop r13
pop r12
pop r11
pop r10
pop r9
pop r8
pop rdi
pop rsi
pop rdx
pop rbx

ret

public ?implOfGetStr@@YAPEBDH@Z

end
7 changes: 7 additions & 0 deletions Source/DivaModLoader/DivaModLoader.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.props" />
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
Expand Down Expand Up @@ -126,7 +127,13 @@
</ClCompile>
<ClCompile Include="SigScan.cpp" />
</ItemGroup>
<ItemGroup>
<MASM Include="DatabaseLoaderImp.asm">
<FileType>Document</FileType>
</MASM>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" />
</ImportGroup>
</Project>
3 changes: 3 additions & 0 deletions Source/DivaModLoader/DivaModLoader.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@
<ClCompile Include="DatabaseLoader.cpp" />
<ClCompile Include="FileLoader.cpp" />
</ItemGroup>
<ItemGroup>
<MASM Include="DatabaseLoaderImp.asm" />
</ItemGroup>
</Project>

0 comments on commit caf8cd3

Please sign in to comment.