10#include "../../stdafx.h"
11#include "../../debug.h"
12#include "../../gfx_func.h"
13#include "../../strings_func.h"
14#include "../../textbuf_gui.h"
15#include "../../fileio_func.h"
20#define NO_SHOBJIDL_SORTDIRECTION
26#include "../../fios.h"
27#include "../../string_func.h"
29#include "../../language.h"
30#include "../../thread.h"
31#include "../../library_loader.h"
33#include "table/strings.h"
35#include "../../safeguards.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;
53void ShowOSErrorBox(std::string_view buf,
bool)
56 MessageBox(GetActiveWindow(),
OTTD2FS(buf).c_str(), L
"Error!", MB_ICONSTOP | MB_TASKMODAL);
59void OSOpenBrowser(
const std::string &url)
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);
185static INT_PTR CALLBACK
HelpDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
188 case WM_INITDIALOG: {
189 std::wstring &msg = *
reinterpret_cast<std::wstring *
>(lParam);
190 SetDlgItemText(wnd, 11, msg.c_str());
191 SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
195 if (wParam == 12) ExitProcess(0);
204void ShowInfoI(std::string_view str)
207 fmt::print(stderr,
"{}\n", str);
213 old = MyShowCursor(
true);
215 if (native_str.size() > 2048) {
219 DialogBoxParam(GetModuleHandle(
nullptr), MAKEINTRESOURCE(101),
nullptr,
HelpDialogFunc,
reinterpret_cast<LPARAM
>(&native_str));
221 MessageBox(GetActiveWindow(), native_str.c_str(), L
"OpenTTD", MB_ICONINFORMATION | MB_OK);
227char *getcwd(
char *buf,
size_t size)
229 wchar_t path[MAX_PATH];
230 GetCurrentDirectory(MAX_PATH - 1, path);
239 extern std::array<std::string, NUM_SEARCHPATHS>
_searchpaths;
241 wchar_t path[MAX_PATH];
242#ifdef WITH_PERSONAL_DIR
243 if (SUCCEEDED(SHGetFolderPath(
nullptr, CSIDL_PERSONAL,
nullptr, SHGFP_TYPE_CURRENT, path))) {
244 std::string tmp(
FS2OTTD(path));
250 tmp +=
"content_download";
257 if (SUCCEEDED(SHGetFolderPath(
nullptr, CSIDL_COMMON_DOCUMENTS,
nullptr, SHGFP_TYPE_CURRENT, path))) {
258 std::string tmp(
FS2OTTD(path));
274 std::string cwd_s(cwd);
279 wchar_t config_dir[MAX_PATH];
281 if (!GetFullPathName(path,
static_cast<DWORD
>(std::size(config_dir)), config_dir,
nullptr)) {
282 Debug(misc, 0,
"GetFullPathName failed ({})", GetLastError());
285 std::string tmp(
FS2OTTD(config_dir));
286 auto pos = tmp.find_last_of(PATHSEPCHAR);
287 if (pos != std::string::npos) tmp.erase(pos + 1);
293 if (!GetModuleFileName(
nullptr, path,
static_cast<DWORD
>(std::size(path)))) {
294 Debug(misc, 0,
"GetModuleFileName failed ({})", GetLastError());
297 wchar_t exec_dir[MAX_PATH];
299 if (!GetFullPathName(path,
static_cast<DWORD
>(std::size(exec_dir)), exec_dir,
nullptr)) {
300 Debug(misc, 0,
"GetFullPathName failed ({})", GetLastError());
303 std::string tmp(
FS2OTTD(exec_dir));
304 auto pos = tmp.find_last_of(PATHSEPCHAR);
305 if (pos != std::string::npos) tmp.erase(pos + 1);
318 if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
return std::nullopt;
320 OpenClipboard(
nullptr);
321 HGLOBAL cbuf = GetClipboardData(CF_UNICODETEXT);
323 std::string result =
FS2OTTD(
static_cast<LPCWSTR
>(GlobalLock(cbuf)));
327 if (result.empty())
return std::nullopt;
342 int name_len = (name.length() >= INT_MAX) ? INT_MAX :
static_cast<int>(name.length());
343 int len = WideCharToMultiByte(CP_UTF8, 0, name.data(), name_len,
nullptr, 0,
nullptr,
nullptr);
344 if (len <= 0)
return std::string();
345 std::string utf8_buf(len,
'\0');
346 WideCharToMultiByte(CP_UTF8, 0, name.data(), name_len, utf8_buf.data(), len,
nullptr,
nullptr);
359 int name_len = (name.length() >= INT_MAX) ? INT_MAX :
static_cast<int>(name.length());
360 int len = MultiByteToWideChar(CP_UTF8, 0, name.data(), name_len,
nullptr, 0);
361 if (len <= 0)
return std::wstring();
362 std::wstring system_buf(len, L
'\0');
363 MultiByteToWideChar(CP_UTF8, 0, name.data(), name_len, system_buf.data(), len);
378 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);
381 return std::string_view(dst_buf.data(), len);
394 int len = MultiByteToWideChar(CP_UTF8, 0, src.data(),
static_cast<int>(src.size()), dst_buf.data(),
static_cast<int>(dst_buf.size() - 1U));
397 return dst_buf.data();
403 const LANGID userUiLang = GetUserDefaultUILanguage();
404 const LCID userUiLocale = MAKELCID(userUiLang, SORT_DEFAULT);
406 char lang[9], country[9];
407 if (GetLocaleInfoA(userUiLocale, LOCALE_SISO639LANGNAME, lang,
static_cast<int>(std::size(lang))) == 0 ||
408 GetLocaleInfoA(userUiLocale, LOCALE_SISO3166CTRYNAME, country,
static_cast<int>(std::size(country))) == 0) {
413 return fmt::format(
"{}_{}", std::string_view{lang, 2}, std::string_view{country, 2});
417static WCHAR _cur_iso_locale[16] = L
"";
419void Win32SetCurrentLocaleName(std::string iso_code)
422 if (iso_code ==
"zh_TW") {
423 iso_code =
"zh-Hant";
424 }
else if (iso_code ==
"zh_CN") {
425 iso_code =
"zh-Hans";
428 for (
char &c : iso_code) {
429 if (c ==
'_') c =
'-';
433 MultiByteToWideChar(CP_UTF8, 0, iso_code.data(),
static_cast<int>(iso_code.size()), _cur_iso_locale,
static_cast<int>(std::size(_cur_iso_locale)));
439 return _kernel32.GetFunction(symbol_name);
442int OTTDStringCompare(std::string_view s1, std::string_view s2)
444 typedef int (WINAPI *PFNCOMPARESTRINGEX)(LPCWSTR, DWORD, LPCWCH, int, LPCWCH, int, LPVOID, LPVOID, LPARAM);
445 static const PFNCOMPARESTRINGEX _CompareStringEx = GetKernel32Function(
"CompareStringEx");
447#ifndef SORT_DIGITSASNUMBERS
448# define SORT_DIGITSASNUMBERS 0x00000008
450#ifndef LINGUISTIC_IGNORECASE
451# define LINGUISTIC_IGNORECASE 0x00000010
454 int len_s1 = MultiByteToWideChar(CP_UTF8, 0, s1.data(), (
int)s1.size(),
nullptr, 0);
455 int len_s2 = MultiByteToWideChar(CP_UTF8, 0, s2.data(), (
int)s2.size(),
nullptr, 0);
457 std::wstring str_s1(len_s1, L
'\0');
458 std::wstring str_s2(len_s2, L
'\0');
460 if (len_s1 != 0) MultiByteToWideChar(CP_UTF8, 0, s1.data(), (
int)s1.size(), str_s1.data(), len_s1);
461 if (len_s2 != 0) MultiByteToWideChar(CP_UTF8, 0, s2.data(), (
int)s2.size(), str_s2.data(), len_s2);
464 if (_CompareStringEx !=
nullptr) {
465 int result = _CompareStringEx(_cur_iso_locale, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, str_s1.data(), len_s1, str_s2.data(), len_s2,
nullptr,
nullptr, 0);
466 if (result != 0)
return result;
469 return CompareString(MAKELCID(
_current_language->
winlangid, SORT_DEFAULT), NORM_IGNORECASE, str_s1.data(), len_s1, str_s2.data(), len_s2);
482 typedef int (WINAPI *PFNFINDNLSSTRINGEX)(LPCWSTR, DWORD, LPCWSTR, int, LPCWSTR, int, LPINT, LPNLSVERSIONINFO, LPVOID, LPARAM);
483 static const PFNFINDNLSSTRINGEX _FindNLSStringEx = GetKernel32Function(
"FindNLSStringEx");
485 if (_FindNLSStringEx !=
nullptr) {
486 int len_str = MultiByteToWideChar(CP_UTF8, 0, str.data(), (
int)str.size(),
nullptr, 0);
487 int len_value = MultiByteToWideChar(CP_UTF8, 0, value.data(), (
int)value.size(),
nullptr, 0);
489 if (len_str != 0 && len_value != 0) {
490 std::wstring str_str(len_str, L
'\0');
491 std::wstring str_value(len_value, L
'\0');
493 MultiByteToWideChar(CP_UTF8, 0, str.data(), (
int)str.size(), str_str.data(), len_str);
494 MultiByteToWideChar(CP_UTF8, 0, value.data(), (
int)value.size(), str_value.data(), len_value);
496 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;
505const DWORD MS_VC_EXCEPTION = 0x406D1388;
507PACK_N(
struct THREADNAME_INFO {
519 THREADNAME_INFO info;
520 info.dwType = 0x1000;
521 info.szName = thread_name.c_str();
522 info.dwThreadID = -1;
526#pragma warning(disable: 6320 6322)
528 RaiseException(MS_VC_EXCEPTION, 0,
sizeof(info) /
sizeof(ULONG_PTR), (ULONG_PTR*)&info);
529 } __except (EXCEPTION_EXECUTE_HANDLER) {
List of file information.
A function loaded from a library.
#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.
@ 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.
bool _left_button_down
Is left mouse button pressed?
bool _left_button_clicked
Is left mouse button clicked?
const LanguageMetadata * _current_language
The currently loaded language.
#define lengthof(array)
Return the length of an fixed size array.
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Deals with finding savegames.
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 CALLBACK 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.
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.
std::string _config_file
Configuration file of OpenTTD.
std::optional< std::string > GetCurrentLocale(const char *)
Determine the current user's locale.
declarations of functions for MS windows systems