00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../../stdafx.h"
00013 #include "../../debug.h"
00014 #include "../../gfx_func.h"
00015 #include "../../textbuf_gui.h"
00016 #include "../../fileio_func.h"
00017 #include "../../fios.h"
00018 #include <windows.h>
00019 #include <fcntl.h>
00020 #include <shlobj.h>
00021 #include "win32.h"
00022 #include "../../core/alloc_func.hpp"
00023 #include "../../openttd.h"
00024 #include "../../core/random_func.hpp"
00025 #include "../../string_func.h"
00026 #include "../../crashlog.h"
00027 #include <errno.h>
00028 #include <sys/stat.h>
00029
00030 static bool _has_console;
00031
00032 static bool cursor_visible = true;
00033
00034 bool MyShowCursor(bool show)
00035 {
00036 if (cursor_visible == show) return show;
00037
00038 cursor_visible = show;
00039 ShowCursor(show);
00040
00041 return !show;
00042 }
00043
00049 bool LoadLibraryList(Function proc[], const char *dll)
00050 {
00051 while (*dll != '\0') {
00052 HMODULE lib;
00053 lib = LoadLibrary(MB_TO_WIDE(dll));
00054
00055 if (lib == NULL) return false;
00056 for (;;) {
00057 FARPROC p;
00058
00059 while (*dll++ != '\0') { }
00060 if (*dll == '\0') break;
00061 #if defined(WINCE)
00062 p = GetProcAddress(lib, MB_TO_WIDE(dll));
00063 #else
00064 p = GetProcAddress(lib, dll);
00065 #endif
00066 if (p == NULL) return false;
00067 *proc++ = (Function)p;
00068 }
00069 dll++;
00070 }
00071 return true;
00072 }
00073
00074 void ShowOSErrorBox(const char *buf, bool system)
00075 {
00076 MyShowCursor(true);
00077 MessageBox(GetActiveWindow(), MB_TO_WIDE(buf), _T("Error!"), MB_ICONSTOP);
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 static DIR _global_dir;
00090 static LONG _global_dir_is_in_use = false;
00091
00092 static inline DIR *dir_calloc()
00093 {
00094 DIR *d;
00095
00096 if (InterlockedExchange(&_global_dir_is_in_use, true) == (LONG)true) {
00097 d = CallocT<DIR>(1);
00098 } else {
00099 d = &_global_dir;
00100 memset(d, 0, sizeof(*d));
00101 }
00102 return d;
00103 }
00104
00105 static inline void dir_free(DIR *d)
00106 {
00107 if (d == &_global_dir) {
00108 _global_dir_is_in_use = (LONG)false;
00109 } else {
00110 free(d);
00111 }
00112 }
00113
00114 DIR *opendir(const TCHAR *path)
00115 {
00116 DIR *d;
00117 UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS);
00118 DWORD fa = GetFileAttributes(path);
00119
00120 if ((fa != INVALID_FILE_ATTRIBUTES) && (fa & FILE_ATTRIBUTE_DIRECTORY)) {
00121 d = dir_calloc();
00122 if (d != NULL) {
00123 TCHAR search_path[MAX_PATH];
00124 bool slash = path[_tcslen(path) - 1] == '\\';
00125
00126
00127
00128 _sntprintf(search_path, lengthof(search_path), _T("%s%s*"), path, slash ? _T("") : _T("\\"));
00129 *lastof(search_path) = '\0';
00130 d->hFind = FindFirstFile(search_path, &d->fd);
00131
00132 if (d->hFind != INVALID_HANDLE_VALUE ||
00133 GetLastError() == ERROR_NO_MORE_FILES) {
00134 d->ent.dir = d;
00135 d->at_first_entry = true;
00136 } else {
00137 dir_free(d);
00138 d = NULL;
00139 }
00140 } else {
00141 errno = ENOMEM;
00142 }
00143 } else {
00144
00145 d = NULL;
00146 errno = ENOENT;
00147 }
00148
00149 SetErrorMode(sem);
00150 return d;
00151 }
00152
00153 struct dirent *readdir(DIR *d)
00154 {
00155 DWORD prev_err = GetLastError();
00156
00157 if (d->at_first_entry) {
00158
00159 if (d->hFind == INVALID_HANDLE_VALUE) return NULL;
00160 d->at_first_entry = false;
00161 } else if (!FindNextFile(d->hFind, &d->fd)) {
00162 if (GetLastError() == ERROR_NO_MORE_FILES) SetLastError(prev_err);
00163 return NULL;
00164 }
00165
00166
00167
00168 d->ent.d_name = d->fd.cFileName;
00169 return &d->ent;
00170 }
00171
00172 int closedir(DIR *d)
00173 {
00174 FindClose(d->hFind);
00175 dir_free(d);
00176 return 0;
00177 }
00178
00179 bool FiosIsRoot(const char *file)
00180 {
00181 return file[3] == '\0';
00182 }
00183
00184 void FiosGetDrives()
00185 {
00186 #if defined(WINCE)
00187
00188 FiosItem *fios = _fios_items.Append();
00189 fios->type = FIOS_TYPE_DRIVE;
00190 fios->mtime = 0;
00191 snprintf(fios->name, lengthof(fios->name), PATHSEP "");
00192 strecpy(fios->title, fios->name, lastof(fios->title));
00193 #else
00194 TCHAR drives[256];
00195 const TCHAR *s;
00196
00197 GetLogicalDriveStrings(lengthof(drives), drives);
00198 for (s = drives; *s != '\0';) {
00199 FiosItem *fios = _fios_items.Append();
00200 fios->type = FIOS_TYPE_DRIVE;
00201 fios->mtime = 0;
00202 snprintf(fios->name, lengthof(fios->name), "%c:", s[0] & 0xFF);
00203 strecpy(fios->title, fios->name, lastof(fios->title));
00204 while (*s++ != '\0') { }
00205 }
00206 #endif
00207 }
00208
00209 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
00210 {
00211
00212 static const int64 posix_epoch_hns = 0x019DB1DED53E8000LL;
00213 const WIN32_FIND_DATA *fd = &ent->dir->fd;
00214
00215 sb->st_size = ((uint64) fd->nFileSizeHigh << 32) + fd->nFileSizeLow;
00216
00217
00218
00219
00220
00221 sb->st_mtime = (time_t)((*(uint64*)&fd->ftLastWriteTime - posix_epoch_hns) / 1E7);
00222 sb->st_mode = (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)? S_IFDIR : S_IFREG;
00223
00224 return true;
00225 }
00226
00227 bool FiosIsHiddenFile(const struct dirent *ent)
00228 {
00229 return (ent->dir->fd.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
00230 }
00231
00232 bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
00233 {
00234 UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS);
00235 bool retval = false;
00236 TCHAR root[4];
00237 DWORD spc, bps, nfc, tnc;
00238
00239 _sntprintf(root, lengthof(root), _T("%c:") _T(PATHSEP), path[0]);
00240 if (tot != NULL && GetDiskFreeSpace(root, &spc, &bps, &nfc, &tnc)) {
00241 *tot = ((spc * bps) * (uint64)nfc);
00242 retval = true;
00243 }
00244
00245 SetErrorMode(sem);
00246 return retval;
00247 }
00248
00249 static int ParseCommandLine(char *line, char **argv, int max_argc)
00250 {
00251 int n = 0;
00252
00253 do {
00254
00255 while (*line == ' ' || *line == '\t') line++;
00256
00257
00258 if (*line == '\0') break;
00259
00260
00261 if (*line == '"') {
00262 argv[n++] = ++line;
00263 while (*line != '"') {
00264 if (*line == '\0') return n;
00265 line++;
00266 }
00267 } else {
00268 argv[n++] = line;
00269 while (*line != ' ' && *line != '\t') {
00270 if (*line == '\0') return n;
00271 line++;
00272 }
00273 }
00274 *line++ = '\0';
00275 } while (n != max_argc);
00276
00277 return n;
00278 }
00279
00280 void CreateConsole()
00281 {
00282 #if defined(WINCE)
00283
00284 #else
00285 HANDLE hand;
00286 CONSOLE_SCREEN_BUFFER_INFO coninfo;
00287
00288 if (_has_console) return;
00289 _has_console = true;
00290
00291 AllocConsole();
00292
00293 hand = GetStdHandle(STD_OUTPUT_HANDLE);
00294 GetConsoleScreenBufferInfo(hand, &coninfo);
00295 coninfo.dwSize.Y = 500;
00296 SetConsoleScreenBufferSize(hand, coninfo.dwSize);
00297
00298
00299 #if !defined(__CYGWIN__)
00300
00301
00302 int fd = _open_osfhandle((intptr_t)hand, _O_TEXT);
00303 if (fd == -1) {
00304
00305 FreeConsole();
00306 _has_console = false;
00307 _close(fd);
00308 CloseHandle(hand);
00309
00310 ShowInfo("Unable to open an output handle to the console. Check known-bugs.txt for details.");
00311 return;
00312 }
00313
00314 *stdout = *_fdopen(fd, "w");
00315 *stdin = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT), "r" );
00316 *stderr = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT), "w" );
00317 #else
00318
00319 *stdout = *fdopen(1, "w" );
00320 *stdin = *fdopen(0, "r" );
00321 *stderr = *fdopen(2, "w" );
00322 #endif
00323
00324 setvbuf(stdin, NULL, _IONBF, 0);
00325 setvbuf(stdout, NULL, _IONBF, 0);
00326 setvbuf(stderr, NULL, _IONBF, 0);
00327 #endif
00328 }
00329
00331 static const char *_help_msg;
00332
00334 static INT_PTR CALLBACK HelpDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
00335 {
00336 switch (msg) {
00337 case WM_INITDIALOG: {
00338 char help_msg[8192];
00339 const char *p = _help_msg;
00340 char *q = help_msg;
00341 while (q != lastof(help_msg) && *p != '\0') {
00342 if (*p == '\n') {
00343 *q++ = '\r';
00344 if (q == lastof(help_msg)) {
00345 q[-1] = '\0';
00346 break;
00347 }
00348 }
00349 *q++ = *p++;
00350 }
00351 *q = '\0';
00352 #if defined(UNICODE)
00353
00354
00355 wchar_t help_msgW[8192];
00356 #endif
00357 SetDlgItemText(wnd, 11, MB_TO_WIDE_BUFFER(help_msg, help_msgW, lengthof(help_msgW)));
00358 SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
00359 } return TRUE;
00360
00361 case WM_COMMAND:
00362 if (wParam == 12) ExitProcess(0);
00363 return TRUE;
00364 case WM_CLOSE:
00365 ExitProcess(0);
00366 }
00367
00368 return FALSE;
00369 }
00370
00371 void ShowInfo(const char *str)
00372 {
00373 if (_has_console) {
00374 fprintf(stderr, "%s\n", str);
00375 } else {
00376 bool old;
00377 ReleaseCapture();
00378 _left_button_clicked = _left_button_down = false;
00379
00380 old = MyShowCursor(true);
00381 if (strlen(str) > 2048) {
00382
00383
00384
00385 _help_msg = str;
00386 DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(101), NULL, HelpDialogFunc);
00387 } else {
00388 #if defined(UNICODE)
00389
00390
00391 wchar_t help_msgW[8192];
00392 #endif
00393 if (MessageBox(GetActiveWindow(), MB_TO_WIDE_BUFFER(str, help_msgW, lengthof(help_msgW)), _T("OpenTTD"), MB_ICONINFORMATION | MB_OKCANCEL) == IDCANCEL) {
00394 CreateConsole();
00395 }
00396 }
00397 MyShowCursor(old);
00398 }
00399 }
00400
00401 #if defined(WINCE)
00402 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
00403 #else
00404 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
00405 #endif
00406 {
00407 int argc;
00408 char *argv[64];
00409 char *cmdline;
00410
00411 #if !defined(UNICODE)
00412 _codepage = GetACP();
00413 #endif
00414
00415 CrashLog::InitialiseCrashLog();
00416
00417 #if defined(UNICODE)
00418
00419 #if !defined(WINCE)
00420
00421 if (HasBit(GetVersion(), 31)) usererror("This version of OpenTTD doesn't run on windows 95/98/ME.\nPlease download the win9x binary and try again.");
00422 #endif
00423
00424
00425
00426
00427 char cmdlinebuf[MAX_PATH];
00428 #endif
00429
00430 cmdline = WIDE_TO_MB_BUFFER(GetCommandLine(), cmdlinebuf, lengthof(cmdlinebuf));
00431
00432 #if defined(_DEBUG)
00433 CreateConsole();
00434 #endif
00435
00436 #if !defined(WINCE)
00437 _set_error_mode(_OUT_TO_MSGBOX);
00438 #endif
00439
00440
00441 SetRandomSeed(GetTickCount());
00442
00443 argc = ParseCommandLine(cmdline, argv, lengthof(argv));
00444
00445 ttd_main(argc, argv);
00446 return 0;
00447 }
00448
00449 #if defined(WINCE)
00450 void GetCurrentDirectoryW(int length, wchar_t *path)
00451 {
00452
00453 GetModuleFileName(NULL, path, length);
00454
00455
00456 wchar_t *pDest = wcsrchr(path, '\\');
00457 if (pDest != NULL) {
00458 int result = pDest - path + 1;
00459 path[result] = '\0';
00460 }
00461 }
00462 #endif
00463
00464 char *getcwd(char *buf, size_t size)
00465 {
00466 #if defined(WINCE)
00467 TCHAR path[MAX_PATH];
00468 GetModuleFileName(NULL, path, MAX_PATH);
00469 convert_from_fs(path, buf, size);
00470
00471 char *p = strrchr(buf, '\\');
00472 if (p != NULL) *p = '\0';
00473 #elif defined(UNICODE)
00474 TCHAR path[MAX_PATH];
00475 GetCurrentDirectory(MAX_PATH - 1, path);
00476 convert_from_fs(path, buf, size);
00477 #else
00478 GetCurrentDirectory(size, buf);
00479 #endif
00480 return buf;
00481 }
00482
00483
00484 void DetermineBasePaths(const char *exe)
00485 {
00486 char tmp[MAX_PATH];
00487 TCHAR path[MAX_PATH];
00488 #ifdef WITH_PERSONAL_DIR
00489 SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, path);
00490 strecpy(tmp, WIDE_TO_MB_BUFFER(path, tmp, lengthof(tmp)), lastof(tmp));
00491 AppendPathSeparator(tmp, MAX_PATH);
00492 ttd_strlcat(tmp, PERSONAL_DIR, MAX_PATH);
00493 AppendPathSeparator(tmp, MAX_PATH);
00494 _searchpaths[SP_PERSONAL_DIR] = strdup(tmp);
00495
00496 SHGetFolderPath(NULL, CSIDL_COMMON_DOCUMENTS, NULL, SHGFP_TYPE_CURRENT, path);
00497 strecpy(tmp, WIDE_TO_MB_BUFFER(path, tmp, lengthof(tmp)), lastof(tmp));
00498 AppendPathSeparator(tmp, MAX_PATH);
00499 ttd_strlcat(tmp, PERSONAL_DIR, MAX_PATH);
00500 AppendPathSeparator(tmp, MAX_PATH);
00501 _searchpaths[SP_SHARED_DIR] = strdup(tmp);
00502 #else
00503 _searchpaths[SP_PERSONAL_DIR] = NULL;
00504 _searchpaths[SP_SHARED_DIR] = NULL;
00505 #endif
00506
00507
00508 getcwd(tmp, lengthof(tmp));
00509 AppendPathSeparator(tmp, MAX_PATH);
00510 _searchpaths[SP_WORKING_DIR] = strdup(tmp);
00511
00512 if (!GetModuleFileName(NULL, path, lengthof(path))) {
00513 DEBUG(misc, 0, "GetModuleFileName failed (%lu)\n", GetLastError());
00514 _searchpaths[SP_BINARY_DIR] = NULL;
00515 } else {
00516 TCHAR exec_dir[MAX_PATH];
00517 _tcsncpy(path, MB_TO_WIDE_BUFFER(exe, path, lengthof(path)), lengthof(path));
00518 if (!GetFullPathName(path, lengthof(exec_dir), exec_dir, NULL)) {
00519 DEBUG(misc, 0, "GetFullPathName failed (%lu)\n", GetLastError());
00520 _searchpaths[SP_BINARY_DIR] = NULL;
00521 } else {
00522 strecpy(tmp, WIDE_TO_MB_BUFFER(exec_dir, tmp, lengthof(tmp)), lastof(tmp));
00523 char *s = strrchr(tmp, PATHSEPCHAR);
00524 *(s + 1) = '\0';
00525 _searchpaths[SP_BINARY_DIR] = strdup(tmp);
00526 }
00527 }
00528
00529 _searchpaths[SP_INSTALLATION_DIR] = NULL;
00530 _searchpaths[SP_APPLICATION_BUNDLE_DIR] = NULL;
00531 }
00532
00533
00534 bool GetClipboardContents(char *buffer, size_t buff_len)
00535 {
00536 HGLOBAL cbuf;
00537 const char *ptr;
00538
00539 if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
00540 OpenClipboard(NULL);
00541 cbuf = GetClipboardData(CF_UNICODETEXT);
00542
00543 ptr = (const char*)GlobalLock(cbuf);
00544 const char *ret = convert_from_fs((wchar_t*)ptr, buffer, buff_len);
00545 GlobalUnlock(cbuf);
00546 CloseClipboard();
00547
00548 if (*ret == '\0') return false;
00549 #if !defined(UNICODE)
00550 } else if (IsClipboardFormatAvailable(CF_TEXT)) {
00551 OpenClipboard(NULL);
00552 cbuf = GetClipboardData(CF_TEXT);
00553
00554 ptr = (const char*)GlobalLock(cbuf);
00555 ttd_strlcpy(buffer, FS2OTTD(ptr), buff_len);
00556
00557 GlobalUnlock(cbuf);
00558 CloseClipboard();
00559 #endif
00560 } else {
00561 return false;
00562 }
00563
00564 return true;
00565 }
00566
00567
00568 void CSleep(int milliseconds)
00569 {
00570 Sleep(milliseconds);
00571 }
00572
00573
00587 const char *FS2OTTD(const TCHAR *name)
00588 {
00589 static char utf8_buf[512];
00590 #if defined(UNICODE)
00591 return convert_from_fs(name, utf8_buf, lengthof(utf8_buf));
00592 #else
00593 char *s = utf8_buf;
00594
00595 for (; *name != '\0'; name++) {
00596 wchar_t w;
00597 int len = MultiByteToWideChar(_codepage, 0, name, 1, &w, 1);
00598 if (len != 1) {
00599 DEBUG(misc, 0, "[utf8] M2W error converting '%c'. Errno %lu", *name, GetLastError());
00600 continue;
00601 }
00602
00603 if (s + Utf8CharLen(w) >= lastof(utf8_buf)) break;
00604 s += Utf8Encode(s, w);
00605 }
00606
00607 *s = '\0';
00608 return utf8_buf;
00609 #endif
00610 }
00611
00625 const TCHAR *OTTD2FS(const char *name)
00626 {
00627 static TCHAR system_buf[512];
00628 #if defined(UNICODE)
00629 return convert_to_fs(name, system_buf, lengthof(system_buf));
00630 #else
00631 char *s = system_buf;
00632
00633 for (WChar c; (c = Utf8Consume(&name)) != '\0';) {
00634 if (s >= lastof(system_buf)) break;
00635
00636 char mb;
00637 int len = WideCharToMultiByte(_codepage, 0, (wchar_t*)&c, 1, &mb, 1, NULL, NULL);
00638 if (len != 1) {
00639 DEBUG(misc, 0, "[utf8] W2M error converting '0x%X'. Errno %lu", c, GetLastError());
00640 continue;
00641 }
00642
00643 *s++ = mb;
00644 }
00645
00646 *s = '\0';
00647 return system_buf;
00648 #endif
00649 }
00650
00651
00660 char *convert_from_fs(const wchar_t *name, char *utf8_buf, size_t buflen)
00661 {
00662 int len = WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8_buf, (int)buflen, NULL, NULL);
00663 if (len == 0) {
00664 DEBUG(misc, 0, "[utf8] W2M error converting wide-string. Errno %lu", GetLastError());
00665 utf8_buf[0] = '\0';
00666 }
00667
00668 return utf8_buf;
00669 }
00670
00671
00681 wchar_t *convert_to_fs(const char *name, wchar_t *utf16_buf, size_t buflen)
00682 {
00683 int len = MultiByteToWideChar(CP_UTF8, 0, name, -1, utf16_buf, (int)buflen);
00684 if (len == 0) {
00685 DEBUG(misc, 0, "[utf8] M2W error converting '%s'. Errno %lu", name, GetLastError());
00686 utf16_buf[0] = '\0';
00687 }
00688
00689 return utf16_buf;
00690 }
00691
00698 HRESULT OTTDSHGetFolderPath(HWND hwnd, int csidl, HANDLE hToken, DWORD dwFlags, LPTSTR pszPath)
00699 {
00700 static HRESULT (WINAPI *SHGetFolderPath)(HWND, int, HANDLE, DWORD, LPTSTR) = NULL;
00701 static bool first_time = true;
00702
00703
00704 if (first_time) {
00705 #if defined(UNICODE)
00706 # define W(x) x "W"
00707 #else
00708 # define W(x) x "A"
00709 #endif
00710 if (!LoadLibraryList((Function*)&SHGetFolderPath, "SHFolder.dll\0" W("SHGetFolderPath") "\0\0")) {
00711 DEBUG(misc, 0, "Unable to load " W("SHGetFolderPath") "from SHFolder.dll");
00712 }
00713 #undef W
00714 first_time = false;
00715 }
00716
00717 if (SHGetFolderPath != NULL) return SHGetFolderPath(hwnd, csidl, hToken, dwFlags, pszPath);
00718
00719
00720
00721
00722
00723
00724
00725
00726 {
00727 DWORD ret;
00728 switch (csidl) {
00729 case CSIDL_FONTS:
00730 ret = GetEnvironmentVariable(_T("WINDIR"), pszPath, MAX_PATH);
00731 if (ret == 0) break;
00732 _tcsncat(pszPath, _T("\\Fonts"), MAX_PATH);
00733
00734 return (HRESULT)0;
00735
00736
00737 }
00738 }
00739
00740 return E_INVALIDARG;
00741 }
00742
00744 const char *GetCurrentLocale(const char *)
00745 {
00746 char lang[9], country[9];
00747 if (GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME, lang, lengthof(lang)) == 0 ||
00748 GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SISO3166CTRYNAME, country, lengthof(country)) == 0) {
00749
00750 return NULL;
00751 }
00752
00753 static char retbuf[6] = {lang[0], lang[1], '_', country[0], country[1], 0};
00754 return retbuf;
00755 }