* 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.h Functions/types related to loading libraries dynamically. */
#ifndef LIBRARY_LOADER_H
#define LIBRARY_LOADER_H
class LibraryLoader {
public:
/**
* A function loaded from a library.
*
* Will automatically cast to the correct function pointer type on retrieval.
* Check whether an error occurred while loading the library or a function.
*
* @return Whether an error occurred.
*/
bool HasError()
{
return this->error.has_value();
}
/**
* Get the last error that occurred while loading the library or a function.
*
* @return The error message.
*/
std::string GetLastError()
{
return this->error.value_or("No error");
}
/**
* Get a function from a loaded library.
*
* @param symbol_name The name of the function to get.
* @return The function. Check HasError() before using.
*/
Function GetFunction(const std::string &symbol_name)
{
if (this->error.has_value()) return Function(nullptr);
return Function(this->GetSymbol(symbol_name));
}
private:
/**
* Open the library with the given filename.
*
* Should set error if any error occurred.
*
* @param filename The filename of the library to open.
*/
void *OpenLibrary(const std::string &filename);
/**
* Close the library.
*/
void CloseLibrary();
/**
* Get a symbol from the library.
*
* Should set error if any error occurred.
*
* @param symbol_name The name of the symbol to get.
*/
void *GetSymbol(const std::string &symbol_name);
std::optional<std::string> error = {}; ///< The last error that occurred, if set.
void *handle = nullptr; ///< Handle to the library.
};
#endif /* LIBRARY_LOADER_H */
src/os/unix/CMakeLists.txt
➞
Show inline comments
@@ -2,12 +2,13 @@ add_files(
crashlog_unix.cpp
survey_unix.cpp
CONDITION UNIX AND NOT APPLE
)
add_files(
library_loader_unix.cpp
unix.cpp
CONDITION UNIX
)
add_files(
font_unix.cpp
src/os/unix/library_loader_unix.cpp
➞
Show inline comments
new file 100644
/*
* 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. */
/* Try to query an array of LOGFONTs that describe the file. */
DWORD len = 0;
if (GetFontResourceInfo(fontPath, &len, nullptr, 2) && len >= sizeof(LOGFONT)) {
LOGFONT *buf = (LOGFONT *)new byte[len];
src/os/windows/font_win32.h
➞
Show inline comments
@@ -10,12 +10,14 @@
#ifndef FONT_WIN32_H
#define FONT_WIN32_H
#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:
LOGFONT logfont; ///< Logical font information for selecting the font face.
HFONT font = nullptr; ///< The font face associated with this font.
HDC dc = nullptr; ///< Cached GDI device context.
src/os/windows/library_loader_win.cpp
➞
Show inline comments
new file 100644
/*
* 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,