Codechange: refactor the Windows-only DllLoader in a cross-platform LibraryLoader (#11751)
This commit is contained in:
@@ -5,6 +5,7 @@ add_files(
|
||||
)
|
||||
|
||||
add_files(
|
||||
library_loader_unix.cpp
|
||||
unix.cpp
|
||||
CONDITION UNIX
|
||||
)
|
||||
|
64
src/os/unix/library_loader_unix.cpp
Normal file
64
src/os/unix/library_loader_unix.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
||||
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file library_loader_unix.cpp Implementation of the LibraryLoader for Linux / MacOS */
|
||||
|
||||
#include "../../stdafx.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include "../../library_loader.h"
|
||||
|
||||
#include "../../safeguards.h"
|
||||
|
||||
/* Emscripten cannot dynamically load other files. */
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
|
||||
void *LibraryLoader::OpenLibrary(const std::string &)
|
||||
{
|
||||
this->error = "Dynamic loading is not supported on this platform.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void LibraryLoader::CloseLibrary()
|
||||
{
|
||||
}
|
||||
|
||||
void *LibraryLoader::GetSymbol(const std::string &)
|
||||
{
|
||||
this->error = "Dynamic loading is not supported on this platform.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void *LibraryLoader::OpenLibrary(const std::string &filename)
|
||||
{
|
||||
void *h = dlopen(filename.c_str(), RTLD_NOW | RTLD_LOCAL);
|
||||
if (h == nullptr) {
|
||||
this->error = dlerror();
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
void LibraryLoader::CloseLibrary()
|
||||
{
|
||||
dlclose(this->handle);
|
||||
}
|
||||
|
||||
void *LibraryLoader::GetSymbol(const std::string &symbol_name)
|
||||
{
|
||||
void *p = dlsym(this->handle, symbol_name.c_str());
|
||||
if (p == nullptr) {
|
||||
this->error = dlerror();
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
#endif /* __EMSCRIPTEN__ */
|
@@ -2,6 +2,7 @@ add_files(
|
||||
crashlog_win.cpp
|
||||
font_win32.cpp
|
||||
font_win32.h
|
||||
library_loader_win.cpp
|
||||
string_uniscribe.cpp
|
||||
string_uniscribe.h
|
||||
survey_win.cpp
|
||||
|
@@ -17,6 +17,7 @@
|
||||
#include "../../gamelog.h"
|
||||
#include "../../saveload/saveload.h"
|
||||
#include "../../video/video_driver.hpp"
|
||||
#include "../../library_loader.h"
|
||||
|
||||
#include <windows.h>
|
||||
#include <mmsystem.h>
|
||||
@@ -177,7 +178,7 @@ static const uint MAX_FRAMES = 64;
|
||||
|
||||
/* virtual */ void CrashLogWindows::SurveyStacktrace(nlohmann::json &survey) const
|
||||
{
|
||||
DllLoader dbghelp(L"dbghelp.dll");
|
||||
LibraryLoader dbghelp("dbghelp.dll");
|
||||
struct ProcPtrs {
|
||||
BOOL (WINAPI * pSymInitialize)(HANDLE, PCSTR, BOOL);
|
||||
BOOL (WINAPI * pSymSetOptions)(DWORD);
|
||||
@@ -189,21 +190,21 @@ static const uint MAX_FRAMES = 64;
|
||||
BOOL (WINAPI * pSymGetSymFromAddr64)(HANDLE, DWORD64, PDWORD64, PIMAGEHLP_SYMBOL64);
|
||||
BOOL (WINAPI * pSymGetLineFromAddr64)(HANDLE, DWORD64, PDWORD, PIMAGEHLP_LINE64);
|
||||
} proc = {
|
||||
dbghelp.GetProcAddress("SymInitialize"),
|
||||
dbghelp.GetProcAddress("SymSetOptions"),
|
||||
dbghelp.GetProcAddress("SymCleanup"),
|
||||
dbghelp.GetProcAddress("StackWalk64"),
|
||||
dbghelp.GetProcAddress("SymFunctionTableAccess64"),
|
||||
dbghelp.GetProcAddress("SymGetModuleBase64"),
|
||||
dbghelp.GetProcAddress("SymGetModuleInfo64"),
|
||||
dbghelp.GetProcAddress("SymGetSymFromAddr64"),
|
||||
dbghelp.GetProcAddress("SymGetLineFromAddr64"),
|
||||
dbghelp.GetFunction("SymInitialize"),
|
||||
dbghelp.GetFunction("SymSetOptions"),
|
||||
dbghelp.GetFunction("SymCleanup"),
|
||||
dbghelp.GetFunction("StackWalk64"),
|
||||
dbghelp.GetFunction("SymFunctionTableAccess64"),
|
||||
dbghelp.GetFunction("SymGetModuleBase64"),
|
||||
dbghelp.GetFunction("SymGetModuleInfo64"),
|
||||
dbghelp.GetFunction("SymGetSymFromAddr64"),
|
||||
dbghelp.GetFunction("SymGetLineFromAddr64"),
|
||||
};
|
||||
|
||||
survey = nlohmann::json::array();
|
||||
|
||||
/* Try to load the functions from the DLL, if that fails because of a too old dbghelp.dll, just skip it. */
|
||||
if (dbghelp.Success()) {
|
||||
if (!dbghelp.HasError()) {
|
||||
/* Initialize symbol handler. */
|
||||
HANDLE hCur = GetCurrentProcess();
|
||||
proc.pSymInitialize(hCur, nullptr, TRUE);
|
||||
|
@@ -15,9 +15,10 @@
|
||||
#include "../../core/mem_func.hpp"
|
||||
#include "../../error_func.h"
|
||||
#include "../../fileio_func.h"
|
||||
#include "../../fontdetection.h"
|
||||
#include "../../fontcache.h"
|
||||
#include "../../fontcache/truetypefontcache.h"
|
||||
#include "../../fontdetection.h"
|
||||
#include "../../library_loader.h"
|
||||
#include "../../string_func.h"
|
||||
#include "../../strings_func.h"
|
||||
#include "../../zoom_func.h"
|
||||
@@ -361,9 +362,9 @@ void LoadWin32Font(FontSize fs)
|
||||
if (AddFontResourceEx(fontPath, FR_PRIVATE, 0) != 0) {
|
||||
/* Try a nice little undocumented function first for getting the internal font name.
|
||||
* Some documentation is found at: http://www.undocprint.org/winspool/getfontresourceinfo */
|
||||
static DllLoader _gdi32(L"gdi32.dll");
|
||||
static LibraryLoader _gdi32("gdi32.dll");
|
||||
typedef BOOL(WINAPI *PFNGETFONTRESOURCEINFO)(LPCTSTR, LPDWORD, LPVOID, DWORD);
|
||||
static PFNGETFONTRESOURCEINFO GetFontResourceInfo = _gdi32.GetProcAddress("GetFontResourceInfoW");
|
||||
static PFNGETFONTRESOURCEINFO GetFontResourceInfo = _gdi32.GetFunction("GetFontResourceInfoW");
|
||||
|
||||
if (GetFontResourceInfo != nullptr) {
|
||||
/* Try to query an array of LOGFONTs that describe the file. */
|
||||
|
@@ -13,6 +13,8 @@
|
||||
#include "../../fontcache/truetypefontcache.h"
|
||||
#include "win32.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
/** Font cache for fonts that are based on a Win32 font. */
|
||||
class Win32FontCache : public TrueTypeFontCache {
|
||||
private:
|
||||
|
58
src/os/windows/library_loader_win.cpp
Normal file
58
src/os/windows/library_loader_win.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
||||
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file library_loader_win.cpp Implementation of the LibraryLoader for Windows */
|
||||
|
||||
#include "../../stdafx.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include "../../library_loader.h"
|
||||
|
||||
#include "../../safeguards.h"
|
||||
|
||||
static std::string GetLoadError()
|
||||
{
|
||||
auto error_code = GetLastError();
|
||||
|
||||
char buffer[512];
|
||||
if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, error_code,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, sizeof(buffer), nullptr) == 0) {
|
||||
return fmt::format("Unknown error {}", error_code);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void *LibraryLoader::OpenLibrary(const std::string &filename)
|
||||
{
|
||||
void *h = ::LoadLibraryW(OTTD2FS(filename).c_str());
|
||||
if (h == nullptr) {
|
||||
this->error = GetLoadError();
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
void LibraryLoader::CloseLibrary()
|
||||
{
|
||||
HMODULE handle = static_cast<HMODULE>(this->handle);
|
||||
|
||||
::FreeLibrary(handle);
|
||||
}
|
||||
|
||||
void *LibraryLoader::GetSymbol(const std::string &symbol_name)
|
||||
{
|
||||
HMODULE handle = static_cast<HMODULE>(this->handle);
|
||||
|
||||
void *p = reinterpret_cast<void *>(::GetProcAddress(handle, symbol_name.c_str()));
|
||||
if (p == nullptr) {
|
||||
this->error = GetLoadError();
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
@@ -27,6 +27,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include "../../language.h"
|
||||
#include "../../thread.h"
|
||||
#include "../../library_loader.h"
|
||||
|
||||
#include "../../safeguards.h"
|
||||
|
||||
@@ -573,8 +574,8 @@ int OTTDStringCompare(std::string_view s1, std::string_view s2)
|
||||
#endif
|
||||
|
||||
if (first_time) {
|
||||
static DllLoader _kernel32(L"Kernel32.dll");
|
||||
_CompareStringEx = _kernel32.GetProcAddress("CompareStringEx");
|
||||
static LibraryLoader _kernel32("Kernel32.dll");
|
||||
_CompareStringEx = _kernel32.GetFunction("CompareStringEx");
|
||||
first_time = false;
|
||||
}
|
||||
|
||||
@@ -617,8 +618,8 @@ int Win32StringContains(const std::string_view str, const std::string_view value
|
||||
static bool first_time = true;
|
||||
|
||||
if (first_time) {
|
||||
static DllLoader _kernel32(L"Kernel32.dll");
|
||||
_FindNLSStringEx = _kernel32.GetProcAddress("FindNLSStringEx");
|
||||
static LibraryLoader _kernel32("Kernel32.dll");
|
||||
_FindNLSStringEx = _kernel32.GetFunction("FindNLSStringEx");
|
||||
first_time = false;
|
||||
}
|
||||
|
||||
|
@@ -10,51 +10,8 @@
|
||||
#ifndef WIN32_H
|
||||
#define WIN32_H
|
||||
|
||||
#include <windows.h>
|
||||
bool MyShowCursor(bool show, bool toggle = false);
|
||||
|
||||
class DllLoader {
|
||||
public:
|
||||
explicit DllLoader(LPCTSTR filename)
|
||||
{
|
||||
this->hmodule = ::LoadLibrary(filename);
|
||||
if (this->hmodule == nullptr) this->success = false;
|
||||
}
|
||||
|
||||
|
||||
~DllLoader()
|
||||
{
|
||||
::FreeLibrary(this->hmodule);
|
||||
}
|
||||
|
||||
bool Success() { return this->success; }
|
||||
|
||||
class ProcAddress {
|
||||
public:
|
||||
explicit ProcAddress(void *p) : p(p) {}
|
||||
|
||||
template <typename T, typename = std::enable_if_t<std::is_function_v<T>>>
|
||||
operator T *() const
|
||||
{
|
||||
return reinterpret_cast<T *>(this->p);
|
||||
}
|
||||
|
||||
private:
|
||||
void *p;
|
||||
};
|
||||
|
||||
ProcAddress GetProcAddress(const char *proc_name)
|
||||
{
|
||||
void *p = reinterpret_cast<void *>(::GetProcAddress(this->hmodule, proc_name));
|
||||
if (p == nullptr) this->success = false;
|
||||
return ProcAddress(p);
|
||||
}
|
||||
|
||||
private:
|
||||
HMODULE hmodule = nullptr;
|
||||
bool success = true;
|
||||
};
|
||||
|
||||
char *convert_from_fs(const wchar_t *name, char *utf8_buf, size_t buflen);
|
||||
wchar_t *convert_to_fs(const std::string_view name, wchar_t *utf16_buf, size_t buflen);
|
||||
|
||||
|
Reference in New Issue
Block a user