20#define NO_SHOBJIDL_SORTDIRECTION
33#include "table/strings.h"
37static bool _has_console;
38static bool _cursor_disable =
true;
39static bool _cursor_visible =
true;
41bool MyShowCursor(
bool show,
bool toggle)
43 if (toggle) _cursor_disable = !_cursor_disable;
44 if (_cursor_disable)
return show;
45 if (_cursor_visible == show)
return show;
47 _cursor_visible = show;
56 MessageBox(GetActiveWindow(),
OTTD2FS(buf).c_str(), L
"Error!", MB_ICONSTOP | MB_TASKMODAL);
61 ShellExecute(GetActiveWindow(), L
"open",
OTTD2FS(url).c_str(),
nullptr,
nullptr, SW_SHOWNORMAL);
64bool FiosIsRoot(
const std::string &file)
66 return file.size() == 3;
69void FiosGetDrives(
FileList &file_list)
74 GetLogicalDriveStrings(
static_cast<DWORD
>(std::size(drives)), drives);
75 for (s = drives; *s !=
'\0';) {
76 FiosItem *fios = &file_list.emplace_back();
77 fios->type = FIOS_TYPE_DRIVE;
79 fios->name += (char)(s[0] & 0xFF);
82 while (*s++ !=
'\0') { }
86bool FiosIsHiddenFile(
const std::filesystem::path &path)
88 UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS);
90 DWORD attributes = GetFileAttributes(path.c_str());
94 return (attributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
97std::optional<uint64_t> FiosGetDiskFreeSpace(
const std::string &path)
99 UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS);
101 ULARGE_INTEGER bytes_free;
102 bool retval = GetDiskFreeSpaceEx(
OTTD2FS(path).c_str(), &bytes_free,
nullptr,
nullptr);
106 if (retval)
return bytes_free.QuadPart;
113 CONSOLE_SCREEN_BUFFER_INFO coninfo;
115 if (_has_console)
return;
118 if (!AllocConsole())
return;
120 hand = GetStdHandle(STD_OUTPUT_HANDLE);
121 GetConsoleScreenBufferInfo(hand, &coninfo);
122 coninfo.dwSize.Y = 500;
123 SetConsoleScreenBufferSize(hand, coninfo.dwSize);
126#if !defined(__CYGWIN__)
129 int fd = _open_osfhandle((intptr_t)hand, _O_TEXT);
133 _has_console =
false;
137 ShowInfo(
"Unable to open an output handle to the console. Check known-bugs.md for details.");
142 freopen(
"CONOUT$",
"a", stdout);
143 freopen(
"CONIN$",
"r", stdin);
144 freopen(
"CONOUT$",
"a", stderr);
146 *stdout = *_fdopen(fd,
"w");
147 *stdin = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT),
"r" );
148 *stderr = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT),
"w" );
153 *stdout = *fdopen(1,
"w" );
154 *stdin = *fdopen(0,
"r" );
155 *stderr = *fdopen(2,
"w" );
158 setvbuf(stdin,
nullptr, _IONBF, 0);
159 setvbuf(stdout,
nullptr, _IONBF, 0);
160 setvbuf(stderr,
nullptr, _IONBF, 0);
174 while ((next = msg.find(
'\n', last)) != std::string_view::npos) {
175 output += msg.substr(last, next - last);
179 output += msg.substr(last);
194static INT_PTR CALLBACK
HelpDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
196 static constexpr int TEXT_CONTROL = 11;
197 static constexpr int OK_BUTTON = 12;
200 case WM_INITDIALOG: {
201 std::wstring &msg = *
reinterpret_cast<std::wstring *
>(lParam);
202 SetDlgItemText(wnd, TEXT_CONTROL, msg.c_str());
203 SendDlgItemMessage(wnd, TEXT_CONTROL, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
207 if (wParam == OK_BUTTON) ExitProcess(0);
216void ShowInfoI(std::string_view str)
219 fmt::print(stderr,
"{}\n", str);
225 old = MyShowCursor(
true);
227 if (native_str.size() > 2048) {
231 DialogBoxParam(GetModuleHandle(
nullptr), MAKEINTRESOURCE(101),
nullptr,
HelpDialogFunc,
reinterpret_cast<LPARAM
>(&native_str));
233 MessageBox(GetActiveWindow(), native_str.c_str(), L
"OpenTTD", MB_ICONINFORMATION | MB_OK);
239char *getcwd(
char *buf,
size_t size)
241 wchar_t path[MAX_PATH];
242 GetCurrentDirectory(MAX_PATH - 1, path);
251 extern std::array<std::string, NUM_SEARCHPATHS>
_searchpaths;
253 wchar_t path[MAX_PATH];
254#ifdef WITH_PERSONAL_DIR
255 if (SUCCEEDED(SHGetFolderPath(
nullptr, CSIDL_PERSONAL,
nullptr, SHGFP_TYPE_CURRENT, path))) {
256 std::string tmp(
FS2OTTD(path));
262 tmp +=
"content_download";
269 if (SUCCEEDED(SHGetFolderPath(
nullptr, CSIDL_COMMON_DOCUMENTS,
nullptr, SHGFP_TYPE_CURRENT, path))) {
270 std::string tmp(
FS2OTTD(path));
286 std::string cwd_s(cwd);
291 wchar_t config_dir[MAX_PATH];
293 if (!GetFullPathName(path,
static_cast<DWORD
>(std::size(config_dir)), config_dir,
nullptr)) {
294 Debug(misc, 0,
"GetFullPathName failed ({})", GetLastError());
297 std::string tmp(
FS2OTTD(config_dir));
298 auto pos = tmp.find_last_of(PATHSEPCHAR);
299 if (pos != std::string::npos) tmp.erase(pos + 1);
305 if (!GetModuleFileName(
nullptr, path,
static_cast<DWORD
>(std::size(path)))) {
306 Debug(misc, 0,
"GetModuleFileName failed ({})", GetLastError());
309 wchar_t exec_dir[MAX_PATH];
311 if (!GetFullPathName(path,
static_cast<DWORD
>(std::size(exec_dir)), exec_dir,
nullptr)) {
312 Debug(misc, 0,
"GetFullPathName failed ({})", GetLastError());
315 std::string tmp(
FS2OTTD(exec_dir));
316 auto pos = tmp.find_last_of(PATHSEPCHAR);
317 if (pos != std::string::npos) tmp.erase(pos + 1);
330 if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
return std::nullopt;
332 OpenClipboard(
nullptr);
333 HGLOBAL cbuf = GetClipboardData(CF_UNICODETEXT);
335 std::string result =
FS2OTTD(
static_cast<LPCWSTR
>(GlobalLock(cbuf)));
339 if (result.empty())
return std::nullopt;
354 int name_len = (name.length() >= INT_MAX) ? INT_MAX :
static_cast<int>(name.length());
355 int len = WideCharToMultiByte(CP_UTF8, 0, name.data(), name_len,
nullptr, 0,
nullptr,
nullptr);
356 if (len <= 0)
return std::string();
357 std::string utf8_buf(len,
'\0');
358 WideCharToMultiByte(CP_UTF8, 0, name.data(), name_len, utf8_buf.data(), len,
nullptr,
nullptr);
370 int name_len = (name.length() >= INT_MAX) ? INT_MAX :
static_cast<int>(name.length());
371 int len = MultiByteToWideChar(CP_UTF8, 0, name.data(), name_len,
nullptr, 0);
372 if (len <= 0)
return std::wstring();
373 std::wstring system_buf(len, L
'\0');
374 MultiByteToWideChar(CP_UTF8, 0, name.data(), name_len, system_buf.data(), len);
389 int len = WideCharToMultiByte(CP_UTF8, 0, src.data(),
static_cast<int>(src.size()), dst_buf.data(),
static_cast<int>(dst_buf.size() - 1U),
nullptr,
nullptr);
392 return std::string_view(dst_buf.data(), len);
405 int len = MultiByteToWideChar(CP_UTF8, 0, src.data(),
static_cast<int>(src.size()), dst_buf.data(),
static_cast<int>(dst_buf.size() - 1U));
408 return dst_buf.data();
417 const LANGID userUiLang = GetUserDefaultUILanguage();
418 const LCID userUiLocale = MAKELCID(userUiLang, SORT_DEFAULT);
420 char lang[9], country[9];
421 if (GetLocaleInfoA(userUiLocale, LOCALE_SISO639LANGNAME, lang,
static_cast<int>(std::size(lang))) == 0 ||
422 GetLocaleInfoA(userUiLocale, LOCALE_SISO3166CTRYNAME, country,
static_cast<int>(std::size(country))) == 0) {
427 return fmt::format(
"{}_{}", std::string_view{lang, 2}, std::string_view{country, 2});
431static WCHAR _cur_iso_locale[16] = L
"";
433void Win32SetCurrentLocaleName(std::string iso_code)
436 if (iso_code ==
"zh_TW") {
437 iso_code =
"zh-Hant";
438 }
else if (iso_code ==
"zh_CN") {
439 iso_code =
"zh-Hans";
442 for (
char &c : iso_code) {
443 if (c ==
'_') c =
'-';
447 MultiByteToWideChar(CP_UTF8, 0, iso_code.data(),
static_cast<int>(iso_code.size()), _cur_iso_locale,
static_cast<int>(std::size(_cur_iso_locale)));
453 return _kernel32.GetFunction(symbol_name);
456int OTTDStringCompare(std::string_view s1, std::string_view s2)
458 typedef int (WINAPI *PFNCOMPARESTRINGEX)(LPCWSTR, DWORD, LPCWCH, int, LPCWCH, int, LPVOID, LPVOID, LPARAM);
459 static const PFNCOMPARESTRINGEX _CompareStringEx = GetKernel32Function(
"CompareStringEx");
461#ifndef SORT_DIGITSASNUMBERS
462# define SORT_DIGITSASNUMBERS 0x00000008
464#ifndef LINGUISTIC_IGNORECASE
465# define LINGUISTIC_IGNORECASE 0x00000010
468 int len_s1 = MultiByteToWideChar(CP_UTF8, 0, s1.data(), (
int)s1.size(),
nullptr, 0);
469 int len_s2 = MultiByteToWideChar(CP_UTF8, 0, s2.data(), (
int)s2.size(),
nullptr, 0);
471 std::wstring str_s1(len_s1, L
'\0');
472 std::wstring str_s2(len_s2, L
'\0');
474 if (len_s1 != 0) MultiByteToWideChar(CP_UTF8, 0, s1.data(), (
int)s1.size(), str_s1.data(), len_s1);
475 if (len_s2 != 0) MultiByteToWideChar(CP_UTF8, 0, s2.data(), (
int)s2.size(), str_s2.data(), len_s2);
478 if (_CompareStringEx !=
nullptr) {
479 int result = _CompareStringEx(_cur_iso_locale, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, str_s1.data(), len_s1, str_s2.data(), len_s2,
nullptr,
nullptr, 0);
480 if (result != 0)
return result;
483 return CompareString(MAKELCID(
_current_language->winlangid, SORT_DEFAULT), NORM_IGNORECASE, str_s1.data(), len_s1, str_s2.data(), len_s2);
496 typedef int (WINAPI *PFNFINDNLSSTRINGEX)(LPCWSTR, DWORD, LPCWSTR, int, LPCWSTR, int, LPINT, LPNLSVERSIONINFO, LPVOID, LPARAM);
497 static const PFNFINDNLSSTRINGEX _FindNLSStringEx = GetKernel32Function(
"FindNLSStringEx");
499 if (_FindNLSStringEx !=
nullptr) {
500 int len_str = MultiByteToWideChar(CP_UTF8, 0, str.data(), (
int)str.size(),
nullptr, 0);
501 int len_value = MultiByteToWideChar(CP_UTF8, 0, value.data(), (
int)value.size(),
nullptr, 0);
503 if (len_str != 0 && len_value != 0) {
504 std::wstring str_str(len_str, L
'\0');
505 std::wstring str_value(len_value, L
'\0');
507 MultiByteToWideChar(CP_UTF8, 0, str.data(), (
int)str.size(), str_str.data(), len_str);
508 MultiByteToWideChar(CP_UTF8, 0, value.data(), (
int)value.size(), str_value.data(), len_value);
510 return _FindNLSStringEx(_cur_iso_locale, FIND_FROMSTART | (case_insensitive ? LINGUISTIC_IGNORECASE : 0), str_str.data(), len_str, str_value.data(), len_value,
nullptr,
nullptr,
nullptr, 0) >= 0 ? 1 : 0;
519const DWORD MS_VC_EXCEPTION = 0x406D1388;
521PACK_N(
struct THREADNAME_INFO {
533 THREADNAME_INFO info;
534 info.dwType = 0x1000;
535 info.szName = thread_name.c_str();
536 info.dwThreadID = -1;
540#pragma warning(disable: 6320 6322)
542 RaiseException(MS_VC_EXCEPTION, 0,
sizeof(info) /
sizeof(ULONG_PTR), (ULONG_PTR*)&info);
543 } __except (EXCEPTION_EXECUTE_HANDLER) {
List of file information.
A function loaded from a library.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
void DetermineBasePaths(std::string_view exe)
Determine the base (personal dir and game data dir) paths.
void AppendPathSeparator(std::string &buf)
Appends, if necessary, the path separator character to the end of the string.
std::array< std::string, NUM_SEARCHPATHS > _searchpaths
The search paths OpenTTD could search through.
std::string _config_file
Configuration file of OpenTTD.
Functions for standard in/out file operations.
@ SP_SHARED_DIR
Search in the shared directory, like 'Shared Files' under Windows.
@ SP_INSTALLATION_DIR
Search in the installation directory.
@ SP_BINARY_DIR
Search in the directory where the binary resides.
@ SP_AUTODOWNLOAD_PERSONAL_DIR
Search within the autodownload directory located in the personal directory.
@ SP_PERSONAL_DIR
Search in the personal directory.
@ SP_WORKING_DIR
Search in the working directory.
@ SP_APPLICATION_BUNDLE_DIR
Search within the application bundle.
Declarations for savegames operations.
bool _left_button_down
Is left mouse button pressed?
bool _left_button_clicked
Is left mouse button clicked?
Functions related to the gfx engine.
Information about languages and their files.
const LanguageMetadata * _current_language
The currently loaded language.
Functions/types related to loading libraries dynamically.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Functions related to low-level strings.
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Functions related to OTTD's strings.
Deals with finding savegames.
Stuff related to the text buffer GUI.
std::optional< std::string > GetClipboardContents()
Try to retrieve the current clipboard contents.
std::string_view convert_from_fs(const std::wstring_view src, std::span< char > dst_buf)
Convert to OpenTTD's encoding from that of the environment in UNICODE.
std::wstring OTTD2FS(std::string_view name)
Convert from OpenTTD's encoding to a wide string.
int Win32StringContains(std::string_view str, std::string_view value, bool case_insensitive)
Search if a string is contained in another string using the current locale.
static std::string ConvertLfToCrLf(std::string_view msg)
Replace linefeeds with carriage-return and linefeed.
void SetCurrentThreadName(const std::string &)
Name the thread this function is called on for the debugger.
static INT_PTR HelpDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
Callback function to handle the window.
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
void ShowOSErrorBox(std::string_view buf, bool)
Show an error message.
wchar_t * convert_to_fs(std::string_view src, std::span< wchar_t > dst_buf)
Convert from OpenTTD's encoding to that of the environment in UNICODE.
void OSOpenBrowser(const std::string &url)
Opens browser on MacOS.
std::optional< std::string > GetCurrentLocale(const char *)
Determine the current user's locale.
Declarations of functions for MS windows systems.