1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
8 /** @file win32.cpp Implementation of MS Windows system calls */
9 
10 #include "../../stdafx.h"
11 #include "../../debug.h"
12 #include "../../gfx_func.h"
13 #include "../../textbuf_gui.h"
14 #include "../../fileio_func.h"
15 #include <windows.h>
16 #include <fcntl.h>
17 #include <mmsystem.h>
18 #include <regstr.h>
19 #define NO_SHOBJIDL_SORTDIRECTION // Avoid multiple definition of SORT_ASCENDING
20 #include <shlobj.h> /* SHGetFolderPath */
21 #include <shellapi.h>
22 #include "win32.h"
23 #include "../../fios.h"
24 #include "../../core/alloc_func.hpp"
25 #include "../../openttd.h"
26 #include "../../core/random_func.hpp"
27 #include "../../string_func.h"
28 #include "../../crashlog.h"
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include "../../language.h"
32 #include "../../thread.h"
33 #include <array>
34 
35 #include "../../safeguards.h"
36 
37 static bool _has_console;
38 static bool _cursor_disable = true;
39 static bool _cursor_visible = true;
40 
MyShowCursor(bool show,bool toggle)41 bool MyShowCursor(bool show, bool toggle)
42 {
43 	if (toggle) _cursor_disable = !_cursor_disable;
44 	if (_cursor_disable) return show;
45 	if (_cursor_visible == show) return show;
46 
47 	_cursor_visible = show;
48 	ShowCursor(show);
49 
50 	return !show;
51 }
52 
ShowOSErrorBox(const char * buf,bool system)53 void ShowOSErrorBox(const char *buf, bool system)
54 {
55 	MyShowCursor(true);
56 	MessageBox(GetActiveWindow(), OTTD2FS(buf).c_str(), L"Error!", MB_ICONSTOP | MB_TASKMODAL);
57 }
58 
OSOpenBrowser(const char * url)59 void OSOpenBrowser(const char *url)
60 {
61 	ShellExecute(GetActiveWindow(), L"open", OTTD2FS(url).c_str(), nullptr, nullptr, SW_SHOWNORMAL);
62 }
63 
64 /* Code below for windows version of opendir/readdir/closedir copied and
65  * modified from Jan Wassenberg's GPL implementation posted over at
66  * http://www.gamedev.net/community/forums/topic.asp?topic_id=364584&whichpage=1&#2398903 */
67 
68 struct DIR {
69 	HANDLE hFind;
70 	/* the dirent returned by readdir.
71 	 * note: having only one global instance is not possible because
72 	 * multiple independent opendir/readdir sequences must be supported. */
73 	dirent ent;
74 	WIN32_FIND_DATA fd;
75 	/* since opendir calls FindFirstFile, we need a means of telling the
76 	 * first call to readdir that we already have a file.
77 	 * that's the case iff this is true */
78 	bool at_first_entry;
79 };
80 
81 /* suballocator - satisfies most requests with a reusable static instance.
82  * this avoids hundreds of alloc/free which would fragment the heap.
83  * To guarantee concurrency, we fall back to malloc if the instance is
84  * already in use (it's important to avoid surprises since this is such a
85  * low-level routine). */
86 static DIR _global_dir;
87 static LONG _global_dir_is_in_use = false;
88 
dir_calloc()89 static inline DIR *dir_calloc()
90 {
91 	DIR *d;
92 
93 	if (InterlockedExchange(&_global_dir_is_in_use, true) == (LONG)true) {
94 		d = CallocT<DIR>(1);
95 	} else {
96 		d = &_global_dir;
97 		memset(d, 0, sizeof(*d));
98 	}
99 	return d;
100 }
101 
dir_free(DIR * d)102 static inline void dir_free(DIR *d)
103 {
104 	if (d == &_global_dir) {
105 		_global_dir_is_in_use = (LONG)false;
106 	} else {
107 		free(d);
108 	}
109 }
110 
opendir(const wchar_t * path)111 DIR *opendir(const wchar_t *path)
112 {
113 	DIR *d;
114 	UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box
115 	DWORD fa = GetFileAttributes(path);
116 
117 	if ((fa != INVALID_FILE_ATTRIBUTES) && (fa & FILE_ATTRIBUTE_DIRECTORY)) {
118 		d = dir_calloc();
119 		if (d != nullptr) {
120 			std::wstring search_path = path;
121 			bool slash = path[wcslen(path) - 1] == '\\';
122 
123 			/* build search path for FindFirstFile, try not to append additional slashes
124 			 * as it throws Win9x off its groove for root directories */
125 			if (!slash) search_path += L"\\";
126 			search_path += L"*";
127 			d->hFind = FindFirstFile(search_path.c_str(), &d->fd);
128 
129 			if (d->hFind != INVALID_HANDLE_VALUE ||
130 					GetLastError() == ERROR_NO_MORE_FILES) { // the directory is empty
131 				d->ent.dir = d;
132 				d->at_first_entry = true;
133 			} else {
134 				dir_free(d);
135 				d = nullptr;
136 			}
137 		} else {
138 			errno = ENOMEM;
139 		}
140 	} else {
141 		/* path not found or not a directory */
142 		d = nullptr;
143 		errno = ENOENT;
144 	}
145 
146 	SetErrorMode(sem); // restore previous setting
147 	return d;
148 }
149 
readdir(DIR * d)150 struct dirent *readdir(DIR *d)
151 {
152 	DWORD prev_err = GetLastError(); // avoid polluting last error
153 
154 	if (d->at_first_entry) {
155 		/* the directory was empty when opened */
156 		if (d->hFind == INVALID_HANDLE_VALUE) return nullptr;
157 		d->at_first_entry = false;
158 	} else if (!FindNextFile(d->hFind, &d->fd)) { // determine cause and bail
159 		if (GetLastError() == ERROR_NO_MORE_FILES) SetLastError(prev_err);
160 		return nullptr;
161 	}
162 
163 	/* This entry has passed all checks; return information about it.
164 	 * (note: d_name is a pointer; see struct dirent definition) */
165 	d->ent.d_name = d->fd.cFileName;
166 	return &d->ent;
167 }
168 
closedir(DIR * d)169 int closedir(DIR *d)
170 {
171 	FindClose(d->hFind);
172 	dir_free(d);
173 	return 0;
174 }
175 
FiosIsRoot(const char * file)176 bool FiosIsRoot(const char *file)
177 {
178 	return file[3] == '\0'; // C:\...
179 }
180 
FiosGetDrives(FileList & file_list)181 void FiosGetDrives(FileList &file_list)
182 {
183 	wchar_t drives[256];
184 	const wchar_t *s;
185 
186 	GetLogicalDriveStrings(lengthof(drives), drives);
187 	for (s = drives; *s != '\0';) {
188 		FiosItem *fios = &file_list.emplace_back();
189 		fios->type = FIOS_TYPE_DRIVE;
190 		fios->mtime = 0;
191 		seprintf(fios->name, lastof(fios->name),  "%c:", s[0] & 0xFF);
192 		strecpy(fios->title, fios->name, lastof(fios->title));
193 		while (*s++ != '\0') { /* Nothing */ }
194 	}
195 }
196 
FiosIsValidFile(const char * path,const struct dirent * ent,struct stat * sb)197 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
198 {
199 	/* hectonanoseconds between Windows and POSIX epoch */
200 	static const int64 posix_epoch_hns = 0x019DB1DED53E8000LL;
201 	const WIN32_FIND_DATA *fd = &ent->dir->fd;
202 
203 	sb->st_size  = ((uint64) fd->nFileSizeHigh << 32) + fd->nFileSizeLow;
204 	/* UTC FILETIME to seconds-since-1970 UTC
205 	 * we just have to subtract POSIX epoch and scale down to units of seconds.
206 	 * http://www.gamedev.net/community/forums/topic.asp?topic_id=294070&whichpage=1&#1860504
207 	 * XXX - not entirely correct, since filetimes on FAT aren't UTC but local,
208 	 * this won't entirely be correct, but we use the time only for comparison. */
209 	sb->st_mtime = (time_t)((*(const uint64*)&fd->ftLastWriteTime - posix_epoch_hns) / 1E7);
210 	sb->st_mode  = (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)? S_IFDIR : S_IFREG;
211 
212 	return true;
213 }
214 
FiosIsHiddenFile(const struct dirent * ent)215 bool FiosIsHiddenFile(const struct dirent *ent)
216 {
217 	return (ent->dir->fd.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
218 }
219 
FiosGetDiskFreeSpace(const char * path,uint64 * tot)220 bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
221 {
222 	UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS);  // disable 'no-disk' message box
223 
224 	ULARGE_INTEGER bytes_free;
225 	bool retval = GetDiskFreeSpaceEx(OTTD2FS(path).c_str(), &bytes_free, nullptr, nullptr);
226 	if (retval && tot != nullptr) *tot = bytes_free.QuadPart;
227 
228 	SetErrorMode(sem); // reset previous setting
229 	return retval;
230 }
231 
ParseCommandLine(char * line,char ** argv,int max_argc)232 static int ParseCommandLine(char *line, char **argv, int max_argc)
233 {
234 	int n = 0;
235 
236 	do {
237 		/* skip whitespace */
238 		while (*line == ' ' || *line == '\t') line++;
239 
240 		/* end? */
241 		if (*line == '\0') break;
242 
243 		/* special handling when quoted */
244 		if (*line == '"') {
245 			argv[n++] = ++line;
246 			while (*line != '"') {
247 				if (*line == '\0') return n;
248 				line++;
249 			}
250 		} else {
251 			argv[n++] = line;
252 			while (*line != ' ' && *line != '\t') {
253 				if (*line == '\0') return n;
254 				line++;
255 			}
256 		}
257 		*line++ = '\0';
258 	} while (n != max_argc);
259 
260 	return n;
261 }
262 
CreateConsole()263 void CreateConsole()
264 {
265 	HANDLE hand;
266 	CONSOLE_SCREEN_BUFFER_INFO coninfo;
267 
268 	if (_has_console) return;
269 	_has_console = true;
270 
271 	if (!AllocConsole()) return;
272 
273 	hand = GetStdHandle(STD_OUTPUT_HANDLE);
274 	GetConsoleScreenBufferInfo(hand, &coninfo);
275 	coninfo.dwSize.Y = 500;
276 	SetConsoleScreenBufferSize(hand, coninfo.dwSize);
277 
278 	/* redirect unbuffered STDIN, STDOUT, STDERR to the console */
279 #if !defined(__CYGWIN__)
280 
281 	/* Check if we can open a handle to STDOUT. */
282 	int fd = _open_osfhandle((intptr_t)hand, _O_TEXT);
283 	if (fd == -1) {
284 		/* Free everything related to the console. */
285 		FreeConsole();
286 		_has_console = false;
287 		_close(fd);
288 		CloseHandle(hand);
289 
290 		ShowInfo("Unable to open an output handle to the console. Check known-bugs.txt for details.");
291 		return;
292 	}
293 
294 #if defined(_MSC_VER) && _MSC_VER >= 1900
295 	freopen("CONOUT$", "a", stdout);
296 	freopen("CONIN$", "r", stdin);
297 	freopen("CONOUT$", "a", stderr);
298 #else
299 	*stdout = *_fdopen(fd, "w");
300 	*stdin = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT), "r" );
301 	*stderr = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT), "w" );
302 #endif
303 
304 #else
305 	/* open_osfhandle is not in cygwin */
306 	*stdout = *fdopen(1, "w" );
307 	*stdin = *fdopen(0, "r" );
308 	*stderr = *fdopen(2, "w" );
309 #endif
310 
311 	setvbuf(stdin, nullptr, _IONBF, 0);
312 	setvbuf(stdout, nullptr, _IONBF, 0);
313 	setvbuf(stderr, nullptr, _IONBF, 0);
314 }
315 
316 /** Temporary pointer to get the help message to the window */
317 static const char *_help_msg;
318 
319 /** Callback function to handle the window */
HelpDialogFunc(HWND wnd,UINT msg,WPARAM wParam,LPARAM lParam)320 static INT_PTR CALLBACK HelpDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
321 {
322 	switch (msg) {
323 		case WM_INITDIALOG: {
324 			char help_msg[8192];
325 			const char *p = _help_msg;
326 			char *q = help_msg;
327 			while (q != lastof(help_msg) && *p != '\0') {
328 				if (*p == '\n') {
329 					*q++ = '\r';
330 					if (q == lastof(help_msg)) {
331 						q[-1] = '\0';
332 						break;
333 					}
334 				}
335 				*q++ = *p++;
336 			}
337 			*q = '\0';
338 			/* We need to put the text in a separate buffer because the default
339 			 * buffer in OTTD2FS might not be large enough (512 chars). */
340 			wchar_t help_msg_buf[8192];
341 			SetDlgItemText(wnd, 11, convert_to_fs(help_msg, help_msg_buf, lengthof(help_msg_buf)));
342 			SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
343 		} return TRUE;
344 
345 		case WM_COMMAND:
346 			if (wParam == 12) ExitProcess(0);
347 			return TRUE;
348 		case WM_CLOSE:
349 			ExitProcess(0);
350 	}
351 
352 	return FALSE;
353 }
354 
ShowInfo(const char * str)355 void ShowInfo(const char *str)
356 {
357 	if (_has_console) {
358 		fprintf(stderr, "%s\n", str);
359 	} else {
360 		bool old;
361 		ReleaseCapture();
362 		_left_button_clicked = _left_button_down = false;
363 
364 		old = MyShowCursor(true);
365 		if (strlen(str) > 2048) {
366 			/* The minimum length of the help message is 2048. Other messages sent via
367 			 * ShowInfo are much shorter, or so long they need this way of displaying
368 			 * them anyway. */
369 			_help_msg = str;
370 			DialogBox(GetModuleHandle(nullptr), MAKEINTRESOURCE(101), nullptr, HelpDialogFunc);
371 		} else {
372 			/* We need to put the text in a separate buffer because the default
373 			 * buffer in OTTD2FS might not be large enough (512 chars). */
374 			wchar_t help_msg_buf[8192];
375 			MessageBox(GetActiveWindow(), convert_to_fs(str, help_msg_buf, lengthof(help_msg_buf)), L"OpenTTD", MB_ICONINFORMATION | MB_OK);
376 		}
377 		MyShowCursor(old);
378 	}
379 }
380 
WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)381 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
382 {
383 	int argc;
384 	char *argv[64]; // max 64 command line arguments
385 
386 	/* Set system timer resolution to 1ms. */
387 	timeBeginPeriod(1);
388 
389 	CrashLog::InitialiseCrashLog();
390 
391 	/* Convert the command line to UTF-8. We need a dedicated buffer
392 	 * for this because argv[] points into this buffer and this needs to
393 	 * be available between subsequent calls to FS2OTTD(). */
394 	char *cmdline = stredup(FS2OTTD(GetCommandLine()).c_str());
395 
396 	/* Set the console codepage to UTF-8. */
397 	SetConsoleOutputCP(CP_UTF8);
398 
399 #if defined(_DEBUG)
400 	CreateConsole();
401 #endif
402 
403 	_set_error_mode(_OUT_TO_MSGBOX); // force assertion output to messagebox
404 
405 	/* setup random seed to something quite random */
406 	SetRandomSeed(GetTickCount());
407 
408 	argc = ParseCommandLine(cmdline, argv, lengthof(argv));
409 
410 	/* Make sure our arguments contain only valid UTF-8 characters. */
411 	for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]);
412 
413 	openttd_main(argc, argv);
414 
415 	/* Restore system timer resolution. */
416 	timeEndPeriod(1);
417 
418 	free(cmdline);
419 	return 0;
420 }
421 
getcwd(char * buf,size_t size)422 char *getcwd(char *buf, size_t size)
423 {
424 	wchar_t path[MAX_PATH];
425 	GetCurrentDirectory(MAX_PATH - 1, path);
426 	convert_from_fs(path, buf, size);
427 	return buf;
428 }
429 
430 extern std::string _config_file;
431 
DetermineBasePaths(const char * exe)432 void DetermineBasePaths(const char *exe)
433 {
434 	extern std::array<std::string, NUM_SEARCHPATHS> _searchpaths;
435 
436 	wchar_t path[MAX_PATH];
437 #ifdef WITH_PERSONAL_DIR
438 	if (SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_PERSONAL, nullptr, SHGFP_TYPE_CURRENT, path))) {
439 		std::string tmp(FS2OTTD(path));
440 		AppendPathSeparator(tmp);
441 		tmp += PERSONAL_DIR;
442 		AppendPathSeparator(tmp);
443 		_searchpaths[SP_PERSONAL_DIR] = tmp;
444 
445 		tmp += "content_download";
446 		AppendPathSeparator(tmp);
447 		_searchpaths[SP_AUTODOWNLOAD_PERSONAL_DIR] = tmp;
448 	} else {
449 		_searchpaths[SP_PERSONAL_DIR].clear();
450 	}
451 
452 	if (SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_COMMON_DOCUMENTS, nullptr, SHGFP_TYPE_CURRENT, path))) {
453 		std::string tmp(FS2OTTD(path));
454 		AppendPathSeparator(tmp);
455 		tmp += PERSONAL_DIR;
456 		AppendPathSeparator(tmp);
457 		_searchpaths[SP_SHARED_DIR] = tmp;
458 	} else {
459 		_searchpaths[SP_SHARED_DIR].clear();
460 	}
461 #else
462 	_searchpaths[SP_PERSONAL_DIR].clear();
463 	_searchpaths[SP_SHARED_DIR].clear();
464 #endif
465 
466 	if (_config_file.empty()) {
467 		char cwd[MAX_PATH];
468 		getcwd(cwd, lengthof(cwd));
469 		std::string cwd_s(cwd);
470 		AppendPathSeparator(cwd_s);
471 		_searchpaths[SP_WORKING_DIR] = cwd_s;
472 	} else {
473 		/* Use the folder of the config file as working directory. */
474 		wchar_t config_dir[MAX_PATH];
475 		wcsncpy(path, convert_to_fs(_config_file.c_str(), path, lengthof(path)), lengthof(path));
476 		if (!GetFullPathName(path, lengthof(config_dir), config_dir, nullptr)) {
477 			Debug(misc, 0, "GetFullPathName failed ({})", GetLastError());
478 			_searchpaths[SP_WORKING_DIR].clear();
479 		} else {
480 			std::string tmp(FS2OTTD(config_dir));
481 			auto pos = tmp.find_last_of(PATHSEPCHAR);
482 			if (pos != std::string::npos) tmp.erase(pos + 1);
483 
484 			_searchpaths[SP_WORKING_DIR] = tmp;
485 		}
486 	}
487 
488 	if (!GetModuleFileName(nullptr, path, lengthof(path))) {
489 		Debug(misc, 0, "GetModuleFileName failed ({})", GetLastError());
490 		_searchpaths[SP_BINARY_DIR].clear();
491 	} else {
492 		wchar_t exec_dir[MAX_PATH];
493 		wcsncpy(path, convert_to_fs(exe, path, lengthof(path)), lengthof(path));
494 		if (!GetFullPathName(path, lengthof(exec_dir), exec_dir, nullptr)) {
495 			Debug(misc, 0, "GetFullPathName failed ({})", GetLastError());
496 			_searchpaths[SP_BINARY_DIR].clear();
497 		} else {
498 			std::string tmp(FS2OTTD(exec_dir));
499 			auto pos = tmp.find_last_of(PATHSEPCHAR);
500 			if (pos != std::string::npos) tmp.erase(pos + 1);
501 
502 			_searchpaths[SP_BINARY_DIR] = tmp;
503 		}
504 	}
505 
506 	_searchpaths[SP_INSTALLATION_DIR].clear();
507 	_searchpaths[SP_APPLICATION_BUNDLE_DIR].clear();
508 }
509 
510 
GetClipboardContents(char * buffer,const char * last)511 bool GetClipboardContents(char *buffer, const char *last)
512 {
513 	HGLOBAL cbuf;
514 	const char *ptr;
515 
516 	if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
517 		OpenClipboard(nullptr);
518 		cbuf = GetClipboardData(CF_UNICODETEXT);
519 
520 		ptr = (const char*)GlobalLock(cbuf);
521 		int out_len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)ptr, -1, buffer, (last - buffer) + 1, nullptr, nullptr);
522 		GlobalUnlock(cbuf);
523 		CloseClipboard();
524 
525 		if (out_len == 0) return false;
526 	} else {
527 		return false;
528 	}
529 
530 	return true;
531 }
532 
533 
534 /**
535  * Convert to OpenTTD's encoding from a wide string.
536  * OpenTTD internal encoding is UTF8.
537  * @param name valid string that will be converted (local, or wide)
538  * @return converted string; if failed string is of zero-length
539  * @see the current code-page comes from video\win32_v.cpp, event-notification
540  * WM_INPUTLANGCHANGE
541  */
FS2OTTD(const std::wstring & name)542 std::string FS2OTTD(const std::wstring &name)
543 {
544 	int name_len = (name.length() >= INT_MAX) ? INT_MAX : (int)name.length();
545 	int len = WideCharToMultiByte(CP_UTF8, 0, name.c_str(), name_len, nullptr, 0, nullptr, nullptr);
546 	if (len <= 0) return std::string();
547 	char *utf8_buf = AllocaM(char, len + 1);
548 	utf8_buf[len] = '\0';
549 	WideCharToMultiByte(CP_UTF8, 0, name.c_str(), name_len, utf8_buf, len, nullptr, nullptr);
550 	return std::string(utf8_buf, static_cast<size_t>(len));
551 }
552 
553 /**
554  * Convert from OpenTTD's encoding to a wide string.
555  * OpenTTD internal encoding is UTF8.
556  * @param name valid string that will be converted (UTF8)
557  * @param console_cp convert to the console encoding instead of the normal system encoding.
558  * @return converted string; if failed string is of zero-length
559  */
OTTD2FS(const std::string & name)560 std::wstring OTTD2FS(const std::string &name)
561 {
562 	int name_len = (name.length() >= INT_MAX) ? INT_MAX : (int)name.length();
563 	int len = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), name_len, nullptr, 0);
564 	if (len <= 0) return std::wstring();
565 	wchar_t *system_buf = AllocaM(wchar_t, len + 1);
566 	system_buf[len] = L'\0';
567 	MultiByteToWideChar(CP_UTF8, 0, name.c_str(), name_len, system_buf, len);
568 	return std::wstring(system_buf, static_cast<size_t>(len));
569 }
570 
571 
572 /**
573  * Convert to OpenTTD's encoding from that of the environment in
574  * UNICODE. OpenTTD encoding is UTF8, local is wide.
575  * @param name pointer to a valid string that will be converted
576  * @param utf8_buf pointer to a valid buffer that will receive the converted string
577  * @param buflen length in characters of the receiving buffer
578  * @return pointer to utf8_buf. If conversion fails the string is of zero-length
579  */
convert_from_fs(const wchar_t * name,char * utf8_buf,size_t buflen)580 char *convert_from_fs(const wchar_t *name, char *utf8_buf, size_t buflen)
581 {
582 	/* Convert UTF-16 string to UTF-8. */
583 	int len = WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8_buf, (int)buflen, nullptr, nullptr);
584 	if (len == 0) utf8_buf[0] = '\0';
585 
586 	return utf8_buf;
587 }
588 
589 
590 /**
591  * Convert from OpenTTD's encoding to that of the environment in
592  * UNICODE. OpenTTD encoding is UTF8, local is wide.
593  * @param name pointer to a valid string that will be converted
594  * @param system_buf pointer to a valid wide-char buffer that will receive the
595  * converted string
596  * @param buflen length in wide characters of the receiving buffer
597  * @param console_cp convert to the console encoding instead of the normal system encoding.
598  * @return pointer to system_buf. If conversion fails the string is of zero-length
599  */
convert_to_fs(const char * name,wchar_t * system_buf,size_t buflen)600 wchar_t *convert_to_fs(const char *name, wchar_t *system_buf, size_t buflen)
601 {
602 	int len = MultiByteToWideChar(CP_UTF8, 0, name, -1, system_buf, (int)buflen);
603 	if (len == 0) system_buf[0] = '\0';
604 
605 	return system_buf;
606 }
607 
608 /** Determine the current user's locale. */
GetCurrentLocale(const char *)609 const char *GetCurrentLocale(const char *)
610 {
611 	const LANGID userUiLang = GetUserDefaultUILanguage();
612 	const LCID userUiLocale = MAKELCID(userUiLang, SORT_DEFAULT);
613 
614 	char lang[9], country[9];
615 	if (GetLocaleInfoA(userUiLocale, LOCALE_SISO639LANGNAME, lang, lengthof(lang)) == 0 ||
616 	    GetLocaleInfoA(userUiLocale, LOCALE_SISO3166CTRYNAME, country, lengthof(country)) == 0) {
617 		/* Unable to retrieve the locale. */
618 		return nullptr;
619 	}
620 	/* Format it as 'en_us'. */
621 	static char retbuf[6] = {lang[0], lang[1], '_', country[0], country[1], 0};
622 	return retbuf;
623 }
624 
625 
626 static WCHAR _cur_iso_locale[16] = L"";
627 
Win32SetCurrentLocaleName(const char * iso_code)628 void Win32SetCurrentLocaleName(const char *iso_code)
629 {
630 	/* Convert the iso code into the format that windows expects. */
631 	char iso[16];
632 	if (strcmp(iso_code, "zh_TW") == 0) {
633 		strecpy(iso, "zh-Hant", lastof(iso));
634 	} else if (strcmp(iso_code, "zh_CN") == 0) {
635 		strecpy(iso, "zh-Hans", lastof(iso));
636 	} else {
637 		/* Windows expects a '-' between language and country code, but we use a '_'. */
638 		strecpy(iso, iso_code, lastof(iso));
639 		for (char *c = iso; *c != '\0'; c++) {
640 			if (*c == '_') *c = '-';
641 		}
642 	}
643 
644 	MultiByteToWideChar(CP_UTF8, 0, iso, -1, _cur_iso_locale, lengthof(_cur_iso_locale));
645 }
646 
OTTDStringCompare(const char * s1,const char * s2)647 int OTTDStringCompare(const char *s1, const char *s2)
648 {
649 	typedef int (WINAPI *PFNCOMPARESTRINGEX)(LPCWSTR, DWORD, LPCWCH, int, LPCWCH, int, LPVOID, LPVOID, LPARAM);
650 	static PFNCOMPARESTRINGEX _CompareStringEx = nullptr;
651 	static bool first_time = true;
652 
653 #ifndef SORT_DIGITSASNUMBERS
654 #	define SORT_DIGITSASNUMBERS 0x00000008  // use digits as numbers sort method
655 #endif
656 #ifndef LINGUISTIC_IGNORECASE
657 #	define LINGUISTIC_IGNORECASE 0x00000010 // linguistically appropriate 'ignore case'
658 #endif
659 
660 	if (first_time) {
661 		static DllLoader _kernel32(L"Kernel32.dll");
662 		_CompareStringEx = _kernel32.GetProcAddress("CompareStringEx");
663 		first_time = false;
664 	}
665 
666 	if (_CompareStringEx != nullptr) {
667 		/* CompareStringEx takes UTF-16 strings, even in ANSI-builds. */
668 		int len_s1 = MultiByteToWideChar(CP_UTF8, 0, s1, -1, nullptr, 0);
669 		int len_s2 = MultiByteToWideChar(CP_UTF8, 0, s2, -1, nullptr, 0);
670 
671 		if (len_s1 != 0 && len_s2 != 0) {
672 			LPWSTR str_s1 = AllocaM(WCHAR, len_s1);
673 			LPWSTR str_s2 = AllocaM(WCHAR, len_s2);
674 
675 			MultiByteToWideChar(CP_UTF8, 0, s1, -1, str_s1, len_s1);
676 			MultiByteToWideChar(CP_UTF8, 0, s2, -1, str_s2, len_s2);
677 
678 			int result = _CompareStringEx(_cur_iso_locale, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, str_s1, -1, str_s2, -1, nullptr, nullptr, 0);
679 			if (result != 0) return result;
680 		}
681 	}
682 
683 	wchar_t s1_buf[512], s2_buf[512];
684 	convert_to_fs(s1, s1_buf, lengthof(s1_buf));
685 	convert_to_fs(s2, s2_buf, lengthof(s2_buf));
686 
687 	return CompareString(MAKELCID(_current_language->winlangid, SORT_DEFAULT), NORM_IGNORECASE, s1_buf, -1, s2_buf, -1);
688 }
689 
690 #ifdef _MSC_VER
691 /* Based on code from MSDN: https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx */
692 const DWORD MS_VC_EXCEPTION = 0x406D1388;
693 
694 PACK_N(struct THREADNAME_INFO {
695 	DWORD dwType;     ///< Must be 0x1000.
696 	LPCSTR szName;    ///< Pointer to name (in user addr space).
697 	DWORD dwThreadID; ///< Thread ID (-1=caller thread).
698 	DWORD dwFlags;    ///< Reserved for future use, must be zero.
699 }, 8);
700 
701 /**
702  * Signal thread name to any attached debuggers.
703  */
SetCurrentThreadName(const char * threadName)704 void SetCurrentThreadName(const char *threadName)
705 {
706 	THREADNAME_INFO info;
707 	info.dwType = 0x1000;
708 	info.szName = threadName;
709 	info.dwThreadID = -1;
710 	info.dwFlags = 0;
711 
712 #pragma warning(push)
713 #pragma warning(disable: 6320 6322)
714 	__try {
715 		RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
716 	} __except (EXCEPTION_EXECUTE_HANDLER) {
717 	}
718 #pragma warning(pop)
719 }
720 #else
SetCurrentThreadName(const char *)721 void SetCurrentThreadName(const char *) {}
722 #endif
723