10#include "../../stdafx.h"
11#include "../../debug.h"
12#include "../../gfx_func.h"
13#include "../../textbuf_gui.h"
14#include "../../fileio_func.h"
19#define NO_SHOBJIDL_SORTDIRECTION
24#include "../../fios.h"
25#include "../../core/alloc_func.hpp"
26#include "../../string_func.h"
28#include "../../language.h"
29#include "../../thread.h"
30#include "../../library_loader.h"
32#include "../../safeguards.h"
34static bool _has_console;
35static bool _cursor_disable =
true;
36static bool _cursor_visible =
true;
38bool MyShowCursor(
bool show,
bool toggle)
40 if (toggle) _cursor_disable = !_cursor_disable;
41 if (_cursor_disable)
return show;
42 if (_cursor_visible == show)
return show;
44 _cursor_visible = show;
50void ShowOSErrorBox(
const char *buf,
bool)
53 MessageBox(GetActiveWindow(),
OTTD2FS(buf).c_str(), L
"Error!", MB_ICONSTOP | MB_TASKMODAL);
56void OSOpenBrowser(
const std::string &url)
58 ShellExecute(GetActiveWindow(), L
"open",
OTTD2FS(url).c_str(),
nullptr,
nullptr, SW_SHOWNORMAL);
61bool FiosIsRoot(
const std::string &file)
63 return file.size() == 3;
66void FiosGetDrives(
FileList &file_list)
71 GetLogicalDriveStrings(
static_cast<DWORD
>(std::size(drives)), drives);
72 for (s = drives; *s !=
'\0';) {
73 FiosItem *fios = &file_list.emplace_back();
74 fios->type = FIOS_TYPE_DRIVE;
76 fios->name += (char)(s[0] & 0xFF);
78 fios->title = fios->name;
79 while (*s++ !=
'\0') { }
83bool FiosIsHiddenFile(
const std::filesystem::path &path)
85 UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS);
87 DWORD attributes = GetFileAttributes(path.c_str());
91 return (attributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
94std::optional<uint64_t> FiosGetDiskFreeSpace(
const std::string &path)
96 UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS);
98 ULARGE_INTEGER bytes_free;
99 bool retval = GetDiskFreeSpaceEx(
OTTD2FS(path).c_str(), &bytes_free,
nullptr,
nullptr);
103 if (retval)
return bytes_free.QuadPart;
110 CONSOLE_SCREEN_BUFFER_INFO coninfo;
112 if (_has_console)
return;
115 if (!AllocConsole())
return;
117 hand = GetStdHandle(STD_OUTPUT_HANDLE);
118 GetConsoleScreenBufferInfo(hand, &coninfo);
119 coninfo.dwSize.Y = 500;
120 SetConsoleScreenBufferSize(hand, coninfo.dwSize);
123#if !defined(__CYGWIN__)
126 int fd = _open_osfhandle((intptr_t)hand, _O_TEXT);
130 _has_console =
false;
134 ShowInfo(
"Unable to open an output handle to the console. Check known-bugs.md for details.");
139 freopen(
"CONOUT$",
"a", stdout);
140 freopen(
"CONIN$",
"r", stdin);
141 freopen(
"CONOUT$",
"a", stderr);
143 *stdout = *_fdopen(fd,
"w");
144 *stdin = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT),
"r" );
145 *stderr = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT),
"w" );
150 *stdout = *fdopen(1,
"w" );
151 *stdin = *fdopen(0,
"r" );
152 *stderr = *fdopen(2,
"w" );
155 setvbuf(stdin,
nullptr, _IONBF, 0);
156 setvbuf(stdout,
nullptr, _IONBF, 0);
157 setvbuf(stderr,
nullptr, _IONBF, 0);
171 while ((next = msg.find(
'\n', last)) != std::string_view::npos) {
172 output += msg.substr(last, next - last);
176 output += msg.substr(last);
182static INT_PTR CALLBACK
HelpDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
185 case WM_INITDIALOG: {
186 std::wstring &msg = *
reinterpret_cast<std::wstring *
>(lParam);
187 SetDlgItemText(wnd, 11, msg.c_str());
188 SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
192 if (wParam == 12) ExitProcess(0);
201void ShowInfoI(
const std::string &str)
204 fmt::print(stderr,
"{}\n", str);
210 old = MyShowCursor(
true);
212 if (native_str.size() > 2048) {
216 DialogBoxParam(GetModuleHandle(
nullptr), MAKEINTRESOURCE(101),
nullptr,
HelpDialogFunc,
reinterpret_cast<LPARAM
>(&native_str));
218 MessageBox(GetActiveWindow(), native_str.c_str(), L
"OpenTTD", MB_ICONINFORMATION | MB_OK);
224char *getcwd(
char *buf,
size_t size)
226 wchar_t path[MAX_PATH];
227 GetCurrentDirectory(MAX_PATH - 1, path);
236 extern std::array<std::string, NUM_SEARCHPATHS>
_searchpaths;
238 wchar_t path[MAX_PATH];
239#ifdef WITH_PERSONAL_DIR
240 if (SUCCEEDED(SHGetFolderPath(
nullptr, CSIDL_PERSONAL,
nullptr, SHGFP_TYPE_CURRENT, path))) {
241 std::string tmp(
FS2OTTD(path));
247 tmp +=
"content_download";
254 if (SUCCEEDED(SHGetFolderPath(
nullptr, CSIDL_COMMON_DOCUMENTS,
nullptr, SHGFP_TYPE_CURRENT, path))) {
255 std::string tmp(
FS2OTTD(path));
271 std::string cwd_s(cwd);
276 wchar_t config_dir[MAX_PATH];
278 if (!GetFullPathName(path,
static_cast<DWORD
>(std::size(config_dir)), config_dir,
nullptr)) {
279 Debug(misc, 0,
"GetFullPathName failed ({})", GetLastError());
282 std::string tmp(
FS2OTTD(config_dir));
283 auto pos = tmp.find_last_of(PATHSEPCHAR);
284 if (pos != std::string::npos) tmp.erase(pos + 1);
290 if (!GetModuleFileName(
nullptr, path,
static_cast<DWORD
>(std::size(path)))) {
291 Debug(misc, 0,
"GetModuleFileName failed ({})", GetLastError());
294 wchar_t exec_dir[MAX_PATH];
296 if (!GetFullPathName(path,
static_cast<DWORD
>(std::size(exec_dir)), exec_dir,
nullptr)) {
297 Debug(misc, 0,
"GetFullPathName failed ({})", GetLastError());
300 std::string tmp(
FS2OTTD(exec_dir));
301 auto pos = tmp.find_last_of(PATHSEPCHAR);
302 if (pos != std::string::npos) tmp.erase(pos + 1);
315 if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
return std::nullopt;
317 OpenClipboard(
nullptr);
318 HGLOBAL cbuf = GetClipboardData(CF_UNICODETEXT);
320 std::string result =
FS2OTTD(
static_cast<LPCWSTR
>(GlobalLock(cbuf)));
324 if (result.empty())
return std::nullopt;
339 int name_len = (name.length() >= INT_MAX) ? INT_MAX : (int)name.length();
340 int len = WideCharToMultiByte(CP_UTF8, 0, name.c_str(), name_len,
nullptr, 0,
nullptr,
nullptr);
341 if (len <= 0)
return std::string();
342 std::string utf8_buf(len,
'\0');
343 WideCharToMultiByte(CP_UTF8, 0, name.c_str(), name_len, utf8_buf.data(), len,
nullptr,
nullptr);
356 int name_len = (name.length() >= INT_MAX) ? INT_MAX : (int)name.length();
357 int len = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), name_len,
nullptr, 0);
358 if (len <= 0)
return std::wstring();
359 std::wstring system_buf(len, L
'\0');
360 MultiByteToWideChar(CP_UTF8, 0, name.c_str(), name_len, system_buf.data(), len);
375 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);
378 return dst_buf.data();
389wchar_t *
convert_to_fs(
const std::string_view src, std::span<wchar_t> dst_buf)
391 int len = MultiByteToWideChar(CP_UTF8, 0, src.data(),
static_cast<int>(src.size()), dst_buf.data(),
static_cast<int>(dst_buf.size() - 1U));
394 return dst_buf.data();
400 const LANGID userUiLang = GetUserDefaultUILanguage();
401 const LCID userUiLocale = MAKELCID(userUiLang, SORT_DEFAULT);
403 char lang[9], country[9];
404 if (GetLocaleInfoA(userUiLocale, LOCALE_SISO639LANGNAME, lang,
static_cast<int>(std::size(lang))) == 0 ||
405 GetLocaleInfoA(userUiLocale, LOCALE_SISO3166CTRYNAME, country,
static_cast<int>(std::size(country))) == 0) {
410 static char retbuf[6] = {lang[0], lang[1],
'_', country[0], country[1], 0};
415static WCHAR _cur_iso_locale[16] = L
"";
417void Win32SetCurrentLocaleName(std::string iso_code)
420 if (iso_code ==
"zh_TW") {
421 iso_code =
"zh-Hant";
422 }
else if (iso_code ==
"zh_CN") {
423 iso_code =
"zh-Hans";
426 for (
char &c : iso_code) {
427 if (c ==
'_') c =
'-';
431 MultiByteToWideChar(CP_UTF8, 0, iso_code.c_str(), -1, _cur_iso_locale,
static_cast<int>(std::size(_cur_iso_locale)));
434int OTTDStringCompare(std::string_view s1, std::string_view s2)
436 typedef int (WINAPI *PFNCOMPARESTRINGEX)(LPCWSTR, DWORD, LPCWCH, int, LPCWCH, int, LPVOID, LPVOID, LPARAM);
437 static PFNCOMPARESTRINGEX _CompareStringEx =
nullptr;
438 static bool first_time =
true;
440#ifndef SORT_DIGITSASNUMBERS
441# define SORT_DIGITSASNUMBERS 0x00000008
443#ifndef LINGUISTIC_IGNORECASE
444# define LINGUISTIC_IGNORECASE 0x00000010
449 _CompareStringEx = _kernel32.GetFunction(
"CompareStringEx");
453 int len_s1 = MultiByteToWideChar(CP_UTF8, 0, s1.data(), (
int)s1.size(),
nullptr, 0);
454 int len_s2 = MultiByteToWideChar(CP_UTF8, 0, s2.data(), (
int)s2.size(),
nullptr, 0);
456 std::wstring str_s1(len_s1, L
'\0');
457 std::wstring str_s2(len_s2, L
'\0');
459 if (len_s1 != 0) MultiByteToWideChar(CP_UTF8, 0, s1.data(), (
int)s1.size(), str_s1.data(), len_s1);
460 if (len_s2 != 0) MultiByteToWideChar(CP_UTF8, 0, s2.data(), (
int)s2.size(), str_s2.data(), len_s2);
463 if (_CompareStringEx !=
nullptr) {
464 int result = _CompareStringEx(_cur_iso_locale, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, str_s1.c_str(), len_s1, str_s2.c_str(), len_s2,
nullptr,
nullptr, 0);
465 if (result != 0)
return result;
468 return CompareString(MAKELCID(
_current_language->
winlangid, SORT_DEFAULT), NORM_IGNORECASE, str_s1.c_str(), len_s1, str_s2.c_str(), len_s2);
481 typedef int (WINAPI *PFNFINDNLSSTRINGEX)(LPCWSTR, DWORD, LPCWSTR, int, LPCWSTR, int, LPINT, LPNLSVERSIONINFO, LPVOID, LPARAM);
482 static PFNFINDNLSSTRINGEX _FindNLSStringEx =
nullptr;
483 static bool first_time =
true;
487 _FindNLSStringEx = _kernel32.
GetFunction(
"FindNLSStringEx");
491 if (_FindNLSStringEx !=
nullptr) {
492 int len_str = MultiByteToWideChar(CP_UTF8, 0, str.data(), (
int)str.size(),
nullptr, 0);
493 int len_value = MultiByteToWideChar(CP_UTF8, 0, value.data(), (
int)value.size(),
nullptr, 0);
495 if (len_str != 0 && len_value != 0) {
496 std::wstring str_str(len_str, L
'\0');
497 std::wstring str_value(len_value, L
'\0');
499 MultiByteToWideChar(CP_UTF8, 0, str.data(), (
int)str.size(), str_str.data(), len_str);
500 MultiByteToWideChar(CP_UTF8, 0, value.data(), (
int)value.size(), str_value.data(), len_value);
502 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;
511const DWORD MS_VC_EXCEPTION = 0x406D1388;
513PACK_N(
struct THREADNAME_INFO {
525 THREADNAME_INFO info;
526 info.dwType = 0x1000;
527 info.szName = threadName;
528 info.dwThreadID = -1;
532#pragma warning(disable: 6320 6322)
534 RaiseException(MS_VC_EXCEPTION, 0,
sizeof(info) /
sizeof(ULONG_PTR), (ULONG_PTR*)&info);
535 } __except (EXCEPTION_EXECUTE_HANDLER) {
List of file information.
Function GetFunction(const std::string &symbol_name)
Get a function from a loaded library.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
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.
void DetermineBasePaths(const char *exe)
Determine the base (personal dir and game data dir) paths.
@ 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.
Deals with finding savegames.
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.
char * 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::optional< std::string > GetClipboardContents()
Try to retrieve the current clipboard contents.
static std::string ConvertLfToCrLf(std::string_view msg)
Replace linefeeds with carriage-return and linefeed.
static INT_PTR CALLBACK HelpDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
Callback function to handle the window.
void SetCurrentThreadName(const char *)
Name the thread this function is called on for the debugger.
std::string FS2OTTD(const std::wstring &name)
Convert to OpenTTD's encoding from a wide string.
int Win32StringContains(const std::string_view str, const std::string_view value, bool case_insensitive)
Search if a string is contained in another string using the current locale.
wchar_t * convert_to_fs(const std::string_view src, std::span< wchar_t > dst_buf)
Convert from OpenTTD's encoding to that of the environment in UNICODE.
const char * GetCurrentLocale(const char *)
Determine the current user's locale.
std::string _config_file
Configuration file of OpenTTD.
declarations of functions for MS windows systems