Skip to content

Commit 771a7c4

Browse files
Synchronize changes from 1.6 master branch [ci skip]
6ee97e7 Use export redirection to load winmm exports
2 parents 561dacb + 6ee97e7 commit 771a7c4

File tree

2 files changed

+4
-112
lines changed

2 files changed

+4
-112
lines changed

Client/loader-proxy/exports.def

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
EXPORTS
2-
timeBeginPeriod=noreturn
3-
timeEndPeriod=noreturn
4-
timeGetDevCaps=noreturn
5-
timeGetTime=noreturn
2+
timeBeginPeriod=winmm.timeBeginPeriod
3+
timeEndPeriod=winmm.timeEndPeriod
4+
timeGetDevCaps=winmm.timeGetDevCaps
5+
timeGetTime=winmm.timeGetTime

Client/loader-proxy/main.cpp

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ BOOL OnLibraryAttach();
4747
auto SetImportProcAddress(const char* moduleName, const char* procedureName, FARPROC replacement) -> FARPROC;
4848
void DisplayErrorMessageBox(const std::wstring& message, const std::wstring& errorCode);
4949
bool DisplayWarningMessageBox(const std::wstring& message, const std::wstring& errorCode);
50-
auto PatchWinmmImports() -> int;
5150
auto GetSystemErrorMessage(DWORD errorCode) -> std::wstring;
5251
auto GetCurrentProcessPath() -> fs::path;
5352
auto GetParentProcessPath() -> fs::path;
@@ -232,29 +231,6 @@ VOID OnGameLaunch()
232231
return;
233232
}
234233

235-
// Patch the winmm.dll imports we've taken over with our mtasa.dll library back to the functions from the winmm.dll library.
236-
if (int error = PatchWinmmImports())
237-
{
238-
std::wstring message;
239-
240-
switch (error)
241-
{
242-
case 1:
243-
message = L"Loading system-provided winmm.dll failed.";
244-
break;
245-
case 4:
246-
message = L"Unable to find winmm.dll import entry.";
247-
break;
248-
default:
249-
message = L"Patching winmm.dll imports failed.";
250-
break;
251-
}
252-
253-
AddLaunchLog("Patching imports has failed (%d)", error);
254-
DisplayErrorMessageBox(MakeMissingFilesError(message), L"CL58");
255-
return;
256-
}
257-
258234
// For dll searches, this call replaces the current directory entry and turns off 'SafeDllSearchMode'.
259235
// Meaning it will search the supplied path before the system and windows directory.
260236
// http://msdn.microsoft.com/en-us/library/ms682586%28VS.85%29.aspx
@@ -357,15 +333,6 @@ BOOL WINAPI MyGetVersionExA(LPOSVERSIONINFOA versionInfo)
357333
return result;
358334
}
359335

360-
/**
361-
* @brief A placeholder function for fake winmm.dll exports.
362-
*/
363-
EXTERN_C void noreturn()
364-
{
365-
// We should never enter this function.
366-
assert(false);
367-
}
368-
369336
/**
370337
* @brief Replaces an import library procedure with the given replacement function.
371338
* @param moduleName Name of the import library (case-insensitive)
@@ -596,81 +563,6 @@ auto GetParentProcessPath() -> fs::path
596563
return GetProcessPath(GetParentProcessId());
597564
}
598565

599-
/**
600-
* @brief Loads the winmm.dll library.
601-
* @return A handle to the library
602-
*/
603-
auto LoadWinmmLibrary() -> HMODULE
604-
{
605-
if (HMODULE winmm = LoadLibraryExW(L"winmm.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32))
606-
return winmm;
607-
608-
return LoadLibraryW(L"winmm.dll");
609-
}
610-
611-
/**
612-
* @brief Replaces fake mtasa.dll imports with functions retrieved from the winmm.dll library.
613-
*/
614-
auto PatchWinmmImports() -> int
615-
{
616-
auto base = reinterpret_cast<std::byte*>(g_exe);
617-
auto dos = reinterpret_cast<IMAGE_DOS_HEADER*>(base);
618-
auto nt = reinterpret_cast<IMAGE_NT_HEADERS32*>(base + dos->e_lfanew);
619-
auto descriptor = reinterpret_cast<IMAGE_IMPORT_DESCRIPTOR*>(base + nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
620-
621-
// Iterate through the import descriptors and find the winmm.dll entry, which we renamed to mtasa.dll.
622-
for (; descriptor->Name; ++descriptor)
623-
{
624-
auto name = reinterpret_cast<const char*>(base + descriptor->Name);
625-
626-
if (stricmp("mtasa.dll", name))
627-
continue;
628-
629-
HMODULE winmm = LoadWinmmLibrary();
630-
631-
if (!winmm)
632-
return 1;
633-
634-
auto nameTableEntry = reinterpret_cast<DWORD*>(base + descriptor->FirstThunk);
635-
auto addressTableEntry = reinterpret_cast<FARPROC*>(nameTableEntry);
636-
637-
if (descriptor->OriginalFirstThunk)
638-
nameTableEntry = reinterpret_cast<DWORD*>(base + descriptor->OriginalFirstThunk);
639-
640-
// Replace every import with the correct function from the winmm.dll library.
641-
for (; *nameTableEntry; ++nameTableEntry, ++addressTableEntry)
642-
{
643-
const char* functionName;
644-
645-
if (IMAGE_SNAP_BY_ORDINAL(*nameTableEntry))
646-
{
647-
functionName = reinterpret_cast<char const*>(IMAGE_ORDINAL(*nameTableEntry));
648-
}
649-
else
650-
{
651-
functionName = reinterpret_cast<IMAGE_IMPORT_BY_NAME*>(base + *nameTableEntry)->Name;
652-
}
653-
654-
if (!functionName)
655-
return 2;
656-
657-
FARPROC function = GetProcAddress(winmm, functionName);
658-
659-
if (!function)
660-
return 3;
661-
662-
DWORD protection;
663-
VirtualProtect(addressTableEntry, sizeof(FARPROC), PAGE_READWRITE, &protection);
664-
*addressTableEntry = function;
665-
VirtualProtect(addressTableEntry, sizeof(FARPROC), protection, &protection);
666-
}
667-
668-
return 0;
669-
}
670-
671-
return 4;
672-
}
673-
674566
/**
675567
* @brief Transforms the numeric error code into a system-generated error string.
676568
*/

0 commit comments

Comments
 (0)