2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-02-05 21:57:01 +00:00
|
|
|
#include "debug.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2005-07-23 15:16:57 +00:00
|
|
|
#include "macros.h"
|
2005-07-22 06:31:31 +00:00
|
|
|
#include "saveload.h"
|
2005-02-06 13:41:02 +00:00
|
|
|
#include "string.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "gfx.h"
|
|
|
|
#include "window.h"
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winnt.h>
|
|
|
|
#include <wininet.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
2006-12-09 23:55:56 +00:00
|
|
|
#include <shlobj.h> // SHGetFolderPath
|
2005-07-21 22:18:02 +00:00
|
|
|
#include "variables.h"
|
2005-07-24 06:09:54 +00:00
|
|
|
#include "win32.h"
|
2006-08-04 23:41:13 +00:00
|
|
|
#include "fios.h" // opendir/readdir/closedir
|
2006-08-01 19:24:29 +00:00
|
|
|
#include <ctype.h>
|
2006-11-28 19:58:13 +00:00
|
|
|
#include <tchar.h>
|
2006-08-04 23:41:13 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
static bool _has_console;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-03-31 15:34:22 +00:00
|
|
|
#if defined(__MINGW32__)
|
|
|
|
#include <stdint.h>
|
|
|
|
#endif
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-27 19:25:50 +00:00
|
|
|
static bool cursor_visible = true;
|
|
|
|
|
2005-07-27 19:22:38 +00:00
|
|
|
bool MyShowCursor(bool show)
|
|
|
|
{
|
2005-07-27 19:26:53 +00:00
|
|
|
if (cursor_visible == show) return show;
|
2005-07-27 19:22:38 +00:00
|
|
|
|
2005-07-27 19:26:53 +00:00
|
|
|
cursor_visible = show;
|
2005-07-27 19:22:38 +00:00
|
|
|
ShowCursor(show);
|
|
|
|
|
|
|
|
return !show;
|
|
|
|
}
|
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
/** Helper function needed by dynamically loading libraries
|
|
|
|
* XXX: Hurray for MS only having an ANSI GetProcAddress function
|
|
|
|
* on normal windows and no Wide version except for in Windows Mobile/CE */
|
|
|
|
bool LoadLibraryList(Function proc[], const char *dll)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-03-09 16:56:26 +00:00
|
|
|
while (*dll != '\0') {
|
2006-11-28 19:58:13 +00:00
|
|
|
HMODULE lib;
|
|
|
|
lib = LoadLibrary(MB_TO_WIDE(dll));
|
2005-07-24 06:09:54 +00:00
|
|
|
|
|
|
|
if (lib == NULL) return false;
|
2006-06-27 21:25:53 +00:00
|
|
|
for (;;) {
|
|
|
|
FARPROC p;
|
2005-07-24 06:09:54 +00:00
|
|
|
|
|
|
|
while (*dll++ != '\0');
|
|
|
|
if (*dll == '\0') break;
|
2006-11-28 19:58:13 +00:00
|
|
|
#if defined(WINCE)
|
|
|
|
p = GetProcAddress(lib, MB_TO_WIDE(dll);
|
|
|
|
#else
|
2004-08-09 17:04:08 +00:00
|
|
|
p = GetProcAddress(lib, dll);
|
2006-11-28 19:58:13 +00:00
|
|
|
#endif
|
2005-07-24 06:09:54 +00:00
|
|
|
if (p == NULL) return false;
|
|
|
|
*proc++ = (Function)p;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
dll++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-23 22:31:46 +00:00
|
|
|
#ifdef _MSC_VER
|
2006-11-28 19:58:13 +00:00
|
|
|
static const char *_exception_string = NULL;
|
2005-09-15 21:59:07 +00:00
|
|
|
#endif
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
void ShowOSErrorBox(const char *buf)
|
|
|
|
{
|
|
|
|
MyShowCursor(true);
|
2006-11-28 19:58:13 +00:00
|
|
|
MessageBox(GetActiveWindow(), MB_TO_WIDE(buf), _T("Error!"), MB_ICONSTOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// if exception tracker is enabled, we crash here to let the exception handler handle it.
|
|
|
|
#if defined(WIN32_EXCEPTION_TRACKER) && !defined(_DEBUG)
|
|
|
|
if (*buf == '!') {
|
|
|
|
_exception_string = buf;
|
|
|
|
*(byte*)0 = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-09-15 21:59:07 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
|
|
|
static void *_safe_esp;
|
|
|
|
static char *_crash_msg;
|
|
|
|
static bool _expanded;
|
|
|
|
static bool _did_emerg_save;
|
|
|
|
static int _ident;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
typedef struct DebugFileInfo {
|
|
|
|
uint32 size;
|
|
|
|
uint32 crc32;
|
|
|
|
SYSTEMTIME file_time;
|
|
|
|
} DebugFileInfo;
|
|
|
|
|
|
|
|
static uint32 *_crc_table;
|
|
|
|
|
|
|
|
static void MakeCRCTable(uint32 *table) {
|
|
|
|
uint32 crc, poly = 0xEDB88320L;
|
2005-07-08 22:25:24 +00:00
|
|
|
int i;
|
|
|
|
int j;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
_crc_table = table;
|
|
|
|
|
2005-03-09 16:56:26 +00:00
|
|
|
for (i = 0; i != 256; i++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
crc = i;
|
2005-03-09 16:56:26 +00:00
|
|
|
for (j = 8; j != 0; j--) {
|
2006-06-28 03:29:14 +00:00
|
|
|
crc = (crc & 1 ? (crc >> 1) ^ poly : crc >> 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
table[i] = crc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32 CalcCRC(byte *data, uint size, uint32 crc) {
|
2005-03-09 16:56:26 +00:00
|
|
|
for (; size > 0; size--) {
|
|
|
|
crc = ((crc >> 8) & 0x00FFFFFF) ^ _crc_table[(crc ^ *data++) & 0xFF];
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
static void GetFileInfo(DebugFileInfo *dfi, const TCHAR *filename)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-12-10 11:37:22 +00:00
|
|
|
HANDLE file;
|
2004-08-09 17:04:08 +00:00
|
|
|
memset(dfi, 0, sizeof(dfi));
|
|
|
|
|
2006-12-10 11:37:22 +00:00
|
|
|
file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
|
|
|
|
if (file != INVALID_HANDLE_VALUE) {
|
2004-08-09 17:04:08 +00:00
|
|
|
byte buffer[1024];
|
|
|
|
DWORD numread;
|
|
|
|
uint32 filesize = 0;
|
|
|
|
FILETIME write_time;
|
|
|
|
uint32 crc = (uint32)-1;
|
|
|
|
|
2006-12-10 11:37:22 +00:00
|
|
|
for (;;) {
|
|
|
|
if (ReadFile(file, buffer, sizeof(buffer), &numread, NULL) == 0 || numread == 0)
|
|
|
|
break;
|
|
|
|
filesize += numread;
|
|
|
|
crc = CalcCRC(buffer, numread, crc);
|
|
|
|
}
|
|
|
|
dfi->size = filesize;
|
|
|
|
dfi->crc32 = crc ^ (uint32)-1;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-12-10 11:37:22 +00:00
|
|
|
if (GetFileTime(file, NULL, NULL, &write_time)) {
|
|
|
|
FileTimeToSystemTime(&write_time, &dfi->file_time);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-12-10 11:37:22 +00:00
|
|
|
CloseHandle(file);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *PrintModuleInfo(char *output, HMODULE mod)
|
|
|
|
{
|
2006-11-28 19:58:13 +00:00
|
|
|
TCHAR buffer[MAX_PATH];
|
2004-08-09 17:04:08 +00:00
|
|
|
DebugFileInfo dfi;
|
2005-03-09 16:56:26 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
GetModuleFileName(mod, buffer, MAX_PATH);
|
|
|
|
GetFileInfo(&dfi, buffer);
|
2005-09-25 09:04:59 +00:00
|
|
|
output += sprintf(output, " %-20s handle: %p size: %d crc: %.8X date: %d-%.2d-%.2d %.2d:%.2d:%.2d\r\n",
|
2006-11-28 19:58:13 +00:00
|
|
|
WIDE_TO_MB(buffer),
|
2004-08-09 17:04:08 +00:00
|
|
|
mod,
|
|
|
|
dfi.size,
|
|
|
|
dfi.crc32,
|
|
|
|
dfi.file_time.wYear,
|
|
|
|
dfi.file_time.wMonth,
|
|
|
|
dfi.file_time.wDay,
|
|
|
|
dfi.file_time.wHour,
|
|
|
|
dfi.file_time.wMinute,
|
|
|
|
dfi.file_time.wSecond
|
|
|
|
);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *PrintModuleList(char *output)
|
|
|
|
{
|
2006-12-10 11:37:22 +00:00
|
|
|
BOOL (WINAPI *EnumProcessModules)(HANDLE, HMODULE*, DWORD, LPDWORD);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
if (LoadLibraryList((Function*)&EnumProcessModules, "psapi.dll\0EnumProcessModules\0\0")) {
|
2006-12-10 11:37:22 +00:00
|
|
|
HMODULE modules[100];
|
|
|
|
DWORD needed;
|
|
|
|
BOOL res;
|
|
|
|
int count, i;
|
|
|
|
|
|
|
|
HANDLE proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
|
|
|
|
if (proc != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
res = EnumProcessModules(proc, modules, sizeof(modules), &needed);
|
|
|
|
CloseHandle(proc);
|
|
|
|
if (res) {
|
2006-12-10 11:37:22 +00:00
|
|
|
count = min(needed / sizeof(HMODULE), lengthof(modules));
|
|
|
|
|
|
|
|
for (i = 0; i != count; i++) output = PrintModuleInfo(output, modules[i]);
|
2004-08-09 17:04:08 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output = PrintModuleInfo(output, NULL);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
static const TCHAR _crash_desc[] =
|
|
|
|
_T("A serious fault condition occured in the game. The game will shut down.\n")
|
|
|
|
_T("Press \"Submit report\" to send crash information to the developers. ")
|
|
|
|
_T("This will greatly help debugging. ")
|
|
|
|
_T("The information contained in the report is displayed below.\n")
|
|
|
|
_T("Press \"Emergency save\" to attempt saving the game.");
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
static const TCHAR _save_succeeded[] =
|
|
|
|
_T("Emergency save succeeded.\n")
|
|
|
|
_T("Be aware that critical parts of the internal game state may have become ")
|
|
|
|
_T("corrupted. The saved game is not guaranteed to work.");
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-30 22:16:05 +00:00
|
|
|
static bool EmergencySave(void)
|
|
|
|
{
|
|
|
|
SaveOrLoad("crash.sav", SL_SAVE);
|
|
|
|
return true;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-12-10 11:40:31 +00:00
|
|
|
/* Disable the crash-save submit code as it's not used */
|
|
|
|
#if 0
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
typedef struct {
|
2006-11-28 19:58:13 +00:00
|
|
|
HINTERNET (WINAPI *InternetOpen)(LPCTSTR,DWORD, LPCTSTR, LPCTSTR, DWORD);
|
|
|
|
HINTERNET (WINAPI *InternetConnect)(HINTERNET, LPCTSTR, INTERNET_PORT, LPCTSTR, LPCTSTR, DWORD, DWORD, DWORD);
|
|
|
|
HINTERNET (WINAPI *HttpOpenRequest)(HINTERNET, LPCTSTR, LPCTSTR, LPCTSTR, LPCTSTR, LPCTSTR *, DWORD, DWORD);
|
|
|
|
BOOL (WINAPI *HttpSendRequest)(HINTERNET, LPCTSTR, DWORD, LPVOID, DWORD);
|
2004-08-09 17:04:08 +00:00
|
|
|
BOOL (WINAPI *InternetCloseHandle)(HINTERNET);
|
|
|
|
BOOL (WINAPI *HttpQueryInfo)(HINTERNET, DWORD, LPVOID, LPDWORD, LPDWORD);
|
|
|
|
} WinInetProcs;
|
|
|
|
|
|
|
|
#define M(x) x "\0"
|
2006-11-28 19:58:13 +00:00
|
|
|
#if defined(UNICODE)
|
|
|
|
# define W(x) x "W"
|
|
|
|
#else
|
|
|
|
# define W(x) x "A"
|
|
|
|
#endif
|
2004-09-10 19:02:27 +00:00
|
|
|
static const char wininet_files[] =
|
2004-08-09 17:04:08 +00:00
|
|
|
M("wininet.dll")
|
2006-11-28 19:58:13 +00:00
|
|
|
M(W("InternetOpen"))
|
|
|
|
M(W("InternetConnect"))
|
|
|
|
M(W("HttpOpenRequest"))
|
|
|
|
M(W("HttpSendRequest"))
|
2004-08-09 17:04:08 +00:00
|
|
|
M("InternetCloseHandle")
|
2006-11-28 19:58:13 +00:00
|
|
|
M(W("HttpQueryInfo"))
|
2004-08-09 17:04:08 +00:00
|
|
|
M("");
|
2006-11-28 19:58:13 +00:00
|
|
|
#undef W
|
2004-08-09 17:04:08 +00:00
|
|
|
#undef M
|
|
|
|
|
|
|
|
static WinInetProcs _wininet;
|
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
static const TCHAR *SubmitCrashReport(HWND wnd, void *msg, size_t msglen, const TCHAR *arg)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
HINTERNET inet, conn, http;
|
2006-11-28 19:58:13 +00:00
|
|
|
const TCHAR *err = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
DWORD code, len;
|
2006-11-28 19:58:13 +00:00
|
|
|
static TCHAR buf[100];
|
|
|
|
TCHAR buff[100];
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
if (_wininet.InternetOpen == NULL && !LoadLibraryList((Function*)&_wininet, wininet_files)) return _T("can't load wininet.dll");
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
inet = _wininet.InternetOpen(_T("OTTD"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
|
|
|
|
if (inet == NULL) { err = _T("internetopen failed"); goto error1; }
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
conn = _wininet.InternetConnect(inet, _T("www.openttd.org"), INTERNET_DEFAULT_HTTP_PORT, _T(""), _T(""), INTERNET_SERVICE_HTTP, 0, 0);
|
|
|
|
if (conn == NULL) { err = _T("internetconnect failed"); goto error2; }
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
_sntprintf(buff, lengthof(buff), _T("/crash.php?file=%s&ident=%d"), arg, _ident);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
http = _wininet.HttpOpenRequest(conn, _T("POST"), buff, NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE , 0);
|
|
|
|
if (http == NULL) { err = _T("httpopenrequest failed"); goto error3; }
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
if (!_wininet.HttpSendRequest(http, _T("Content-type: application/binary"), -1, msg, (DWORD)msglen)) { err = _T("httpsendrequest failed"); goto error4; }
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
len = sizeof(code);
|
2006-11-28 19:58:13 +00:00
|
|
|
if (!_wininet.HttpQueryInfo(http, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &code, &len, 0)) { err = _T("httpqueryinfo failed"); goto error4; }
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (code != 200) {
|
2006-11-28 19:58:13 +00:00
|
|
|
int l = _sntprintf(buf, lengthof(buf), _T("Server said: %d "), code);
|
2004-08-09 17:04:08 +00:00
|
|
|
len = sizeof(buf) - l;
|
|
|
|
_wininet.HttpQueryInfo(http, HTTP_QUERY_STATUS_TEXT, buf + l, &len, 0);
|
|
|
|
err = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
error4:
|
|
|
|
_wininet.InternetCloseHandle(http);
|
|
|
|
error3:
|
|
|
|
_wininet.InternetCloseHandle(conn);
|
|
|
|
error2:
|
|
|
|
_wininet.InternetCloseHandle(inet);
|
|
|
|
error1:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
static void SubmitFile(HWND wnd, const TCHAR *file)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
HANDLE h;
|
|
|
|
unsigned long size;
|
|
|
|
unsigned long read;
|
|
|
|
void *mem;
|
|
|
|
|
|
|
|
h = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
|
|
|
|
if (h == NULL) return;
|
|
|
|
|
|
|
|
size = GetFileSize(h, NULL);
|
|
|
|
if (size > 500000) goto error1;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
mem = malloc(size);
|
|
|
|
if (mem == NULL) goto error1;
|
|
|
|
|
|
|
|
if (!ReadFile(h, mem, size, &read, NULL) || read != size) goto error2;
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
SubmitCrashReport(wnd, mem, size, file);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
error2:
|
|
|
|
free(mem);
|
|
|
|
error1:
|
|
|
|
CloseHandle(h);
|
|
|
|
}
|
|
|
|
|
2006-12-10 11:40:31 +00:00
|
|
|
#endif /* Disabled crash-submit procedures */
|
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
static const TCHAR * const _expand_texts[] = {_T("S&how report >>"), _T("&Hide report <<") };
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static void SetWndSize(HWND wnd, int mode)
|
|
|
|
{
|
|
|
|
RECT r,r2;
|
|
|
|
int offs;
|
|
|
|
|
|
|
|
GetWindowRect(wnd, &r);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
SetDlgItemText(wnd, 15, _expand_texts[mode == 1]);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (mode >= 0) {
|
|
|
|
GetWindowRect(GetDlgItem(wnd, 11), &r2);
|
|
|
|
offs = r2.bottom - r2.top + 10;
|
2005-03-09 16:56:26 +00:00
|
|
|
if (!mode) offs = -offs;
|
|
|
|
SetWindowPos(wnd, HWND_TOPMOST, 0, 0,
|
|
|
|
r.right - r.left, r.bottom - r.top + offs, SWP_NOMOVE | SWP_NOZORDER);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2004-09-10 19:02:27 +00:00
|
|
|
SetWindowPos(wnd, HWND_TOPMOST,
|
2005-03-09 16:56:26 +00:00
|
|
|
(GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2,
|
|
|
|
(GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2,
|
2004-08-09 17:04:08 +00:00
|
|
|
0, 0, SWP_NOSIZE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool DoEmergencySave(HWND wnd)
|
|
|
|
{
|
|
|
|
bool b = false;
|
|
|
|
|
|
|
|
EnableWindow(GetDlgItem(wnd, 13), FALSE);
|
|
|
|
_did_emerg_save = true;
|
|
|
|
__try {
|
|
|
|
b = EmergencySave();
|
|
|
|
} __except (1) {}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2005-09-25 09:04:59 +00:00
|
|
|
static INT_PTR CALLBACK CrashDialogFunc(HWND wnd,UINT msg,WPARAM wParam,LPARAM lParam)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-02-01 07:36:15 +00:00
|
|
|
switch (msg) {
|
2006-12-10 11:37:22 +00:00
|
|
|
case WM_INITDIALOG: {
|
2006-11-28 19:58:13 +00:00
|
|
|
#if defined(UNICODE)
|
2006-12-10 11:46:43 +00:00
|
|
|
/* We need to put the crash-log in a seperate buffer because the default
|
|
|
|
* buffer in MB_TO_WIDE is not large enough (256 chars) */
|
2006-12-10 11:37:22 +00:00
|
|
|
wchar_t crash_msgW[8096];
|
2006-11-28 19:58:13 +00:00
|
|
|
#endif
|
2006-12-10 11:37:22 +00:00
|
|
|
SetDlgItemText(wnd, 10, _crash_desc);
|
|
|
|
SetDlgItemText(wnd, 11, MB_TO_WIDE_BUFFER(_crash_msg, crash_msgW, lengthof(crash_msgW)));
|
|
|
|
SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
|
|
|
|
SetWndSize(wnd, -1);
|
|
|
|
} return TRUE;
|
|
|
|
case WM_COMMAND:
|
|
|
|
switch (wParam) {
|
|
|
|
case 12: /* Close */
|
|
|
|
ExitProcess(0);
|
|
|
|
case 13: /* Emergency save */
|
|
|
|
if (DoEmergencySave(wnd)) {
|
|
|
|
MessageBox(wnd, _save_succeeded, _T("Save successful"), MB_ICONINFORMATION);
|
|
|
|
} else {
|
|
|
|
MessageBox(wnd, _T("Save failed"), _T("Save failed"), MB_ICONINFORMATION);
|
|
|
|
}
|
|
|
|
break;
|
2006-12-10 11:40:31 +00:00
|
|
|
/* Disable the crash-save submit code as it's not used */
|
|
|
|
#if 0
|
2006-12-10 11:37:22 +00:00
|
|
|
case 14: { /* Submit crash report */
|
|
|
|
const TCHAR *s;
|
|
|
|
|
|
|
|
SetCursor(LoadCursor(NULL, IDC_WAIT));
|
|
|
|
|
|
|
|
s = SubmitCrashReport(wnd, _crash_msg, strlen(_crash_msg), _T(""));
|
|
|
|
if (s != NULL) {
|
|
|
|
MessageBox(wnd, s, _T("Error"), MB_ICONSTOP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to submit emergency savegame
|
|
|
|
if (_did_emerg_save || DoEmergencySave(wnd)) SubmitFile(wnd, _T("crash.sav"));
|
|
|
|
|
|
|
|
// try to submit the autosaved game
|
|
|
|
if (_opt.autosave) {
|
|
|
|
TCHAR buf[40];
|
|
|
|
_sntprintf(buf, lengthof(buf), _T("autosave%d.sav"), (_autosave_ctr - 1) & 3);
|
|
|
|
SubmitFile(wnd, buf);
|
|
|
|
}
|
|
|
|
EnableWindow(GetDlgItem(wnd, 14), FALSE);
|
|
|
|
SetCursor(LoadCursor(NULL, IDC_ARROW));
|
|
|
|
MessageBox(wnd, _T("Crash report submitted. Thank you."), _T("Crash Report"), MB_ICONINFORMATION);
|
|
|
|
} break;
|
2006-12-10 11:40:31 +00:00
|
|
|
#endif /* Disabled crash-submit procedures */
|
2006-12-10 11:37:22 +00:00
|
|
|
case 15: /* Expand window to show crash-message */
|
|
|
|
_expanded ^= 1;
|
|
|
|
SetWndSize(wnd, _expanded);
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-12-10 11:37:22 +00:00
|
|
|
return TRUE;
|
|
|
|
case WM_CLOSE: ExitProcess(0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-01-23 10:34:39 +00:00
|
|
|
static void Handler2(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
ShowCursor(TRUE);
|
|
|
|
ShowWindow(GetActiveWindow(), FALSE);
|
|
|
|
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(100), NULL, CrashDialogFunc);
|
|
|
|
}
|
|
|
|
|
2005-01-23 10:34:39 +00:00
|
|
|
extern bool CloseConsoleLogIfActive(void);
|
2005-01-15 16:38:10 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep)
|
|
|
|
{
|
2006-03-14 21:31:51 +00:00
|
|
|
extern const char _openttd_revision[];
|
2004-08-09 17:04:08 +00:00
|
|
|
char *output;
|
2006-11-28 19:58:13 +00:00
|
|
|
static bool had_exception = false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-09 16:56:26 +00:00
|
|
|
if (had_exception) ExitProcess(0);
|
2004-08-09 17:04:08 +00:00
|
|
|
had_exception = true;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
_ident = GetTickCount(); // something pretty unique
|
|
|
|
|
|
|
|
MakeCRCTable(alloca(256 * sizeof(uint32)));
|
|
|
|
_crash_msg = output = LocalAlloc(LMEM_FIXED, 8192);
|
|
|
|
|
|
|
|
{
|
|
|
|
SYSTEMTIME time;
|
|
|
|
GetLocalTime(&time);
|
2006-11-28 19:58:13 +00:00
|
|
|
output += sprintf(output,
|
2004-08-09 17:04:08 +00:00
|
|
|
"*** OpenTTD Crash Report ***\r\n"
|
|
|
|
"Date: %d-%.2d-%.2d %.2d:%.2d:%.2d\r\n"
|
2006-08-07 11:31:27 +00:00
|
|
|
"Build: %s built on " __DATE__ " " __TIME__ "\r\n",
|
2004-08-09 17:04:08 +00:00
|
|
|
time.wYear,
|
|
|
|
time.wMonth,
|
|
|
|
time.wDay,
|
|
|
|
time.wHour,
|
|
|
|
time.wMinute,
|
|
|
|
time.wSecond,
|
2006-03-14 21:31:51 +00:00
|
|
|
_openttd_revision
|
2004-08-09 17:04:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2005-03-09 16:56:26 +00:00
|
|
|
if (_exception_string)
|
|
|
|
output += sprintf(output, "Reason: %s\r\n", _exception_string);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-10-02 22:39:56 +00:00
|
|
|
#ifdef _M_AMD64
|
|
|
|
output += sprintf(output, "Exception %.8X at %.16IX\r\n"
|
|
|
|
"Registers:\r\n"
|
|
|
|
"RAX: %.16llX RBX: %.16llX RCX: %.16llX RDX: %.16llX\r\n"
|
|
|
|
"RSI: %.16llX RDI: %.16llX RBP: %.16llX RSP: %.16llX\r\n"
|
|
|
|
"R8: %.16llX R9: %.16llX R10: %.16llX R11: %.16llX\r\n"
|
|
|
|
"R12: %.16llX R13: %.16llX R14: %.16llX R15: %.16llX\r\n"
|
|
|
|
"RIP: %.16llX EFLAGS: %.8X\r\n"
|
|
|
|
"\r\nBytes at CS:RIP:\r\n",
|
|
|
|
ep->ExceptionRecord->ExceptionCode,
|
|
|
|
ep->ExceptionRecord->ExceptionAddress,
|
|
|
|
ep->ContextRecord->Rax,
|
|
|
|
ep->ContextRecord->Rbx,
|
|
|
|
ep->ContextRecord->Rcx,
|
|
|
|
ep->ContextRecord->Rdx,
|
|
|
|
ep->ContextRecord->Rsi,
|
|
|
|
ep->ContextRecord->Rdi,
|
|
|
|
ep->ContextRecord->Rbp,
|
|
|
|
ep->ContextRecord->Rsp,
|
|
|
|
ep->ContextRecord->R8,
|
|
|
|
ep->ContextRecord->R9,
|
|
|
|
ep->ContextRecord->R10,
|
|
|
|
ep->ContextRecord->R11,
|
|
|
|
ep->ContextRecord->R12,
|
|
|
|
ep->ContextRecord->R13,
|
|
|
|
ep->ContextRecord->R14,
|
|
|
|
ep->ContextRecord->R15,
|
|
|
|
ep->ContextRecord->Rip,
|
|
|
|
ep->ContextRecord->EFlags
|
|
|
|
);
|
|
|
|
#else
|
2004-08-09 17:04:08 +00:00
|
|
|
output += sprintf(output, "Exception %.8X at %.8X\r\n"
|
|
|
|
"Registers:\r\n"
|
|
|
|
" EAX: %.8X EBX: %.8X ECX: %.8X EDX: %.8X\r\n"
|
|
|
|
" ESI: %.8X EDI: %.8X EBP: %.8X ESP: %.8X\r\n"
|
|
|
|
" EIP: %.8X EFLAGS: %.8X\r\n"
|
|
|
|
"\r\nBytes at CS:EIP:\r\n",
|
|
|
|
ep->ExceptionRecord->ExceptionCode,
|
|
|
|
ep->ExceptionRecord->ExceptionAddress,
|
|
|
|
ep->ContextRecord->Eax,
|
|
|
|
ep->ContextRecord->Ebx,
|
|
|
|
ep->ContextRecord->Ecx,
|
|
|
|
ep->ContextRecord->Edx,
|
|
|
|
ep->ContextRecord->Esi,
|
|
|
|
ep->ContextRecord->Edi,
|
|
|
|
ep->ContextRecord->Ebp,
|
|
|
|
ep->ContextRecord->Esp,
|
|
|
|
ep->ContextRecord->Eip,
|
|
|
|
ep->ContextRecord->EFlags
|
|
|
|
);
|
2005-10-02 22:39:56 +00:00
|
|
|
#endif
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
{
|
2005-10-02 22:39:56 +00:00
|
|
|
#ifdef _M_AMD64
|
|
|
|
byte *b = (byte*)ep->ContextRecord->Rip;
|
|
|
|
#else
|
2004-08-09 17:04:08 +00:00
|
|
|
byte *b = (byte*)ep->ContextRecord->Eip;
|
2005-10-02 22:39:56 +00:00
|
|
|
#endif
|
2004-08-09 17:04:08 +00:00
|
|
|
int i;
|
2005-03-09 16:56:26 +00:00
|
|
|
for (i = 0; i != 24; i++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (IsBadReadPtr(b, 1)) {
|
|
|
|
output += sprintf(output, " ??"); // OCR: WAS: , 0);
|
|
|
|
} else {
|
|
|
|
output += sprintf(output, " %.2X", *b);
|
|
|
|
}
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
output += sprintf(output,
|
|
|
|
"\r\n"
|
|
|
|
"\r\nStack trace: \r\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int i,j;
|
2005-10-02 22:39:56 +00:00
|
|
|
#ifdef _M_AMD64
|
|
|
|
uint32 *b = (uint32*)ep->ContextRecord->Rsp;
|
|
|
|
#else
|
2004-08-09 17:04:08 +00:00
|
|
|
uint32 *b = (uint32*)ep->ContextRecord->Esp;
|
2005-10-02 22:39:56 +00:00
|
|
|
#endif
|
2005-03-09 16:56:26 +00:00
|
|
|
for (j = 0; j != 24; j++) {
|
|
|
|
for (i = 0; i != 8; i++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (IsBadReadPtr(b,sizeof(uint32))) {
|
|
|
|
output += sprintf(output, " ????????"); //OCR: WAS - , 0);
|
|
|
|
} else {
|
|
|
|
output += sprintf(output, " %.8X", *b);
|
|
|
|
}
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
output += sprintf(output, "\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output += sprintf(output, "\r\nModule information:\r\n");
|
|
|
|
output = PrintModuleList(output);
|
|
|
|
|
|
|
|
{
|
|
|
|
OSVERSIONINFO os;
|
|
|
|
os.dwOSVersionInfoSize = sizeof(os);
|
|
|
|
GetVersionEx(&os);
|
|
|
|
output += sprintf(output, "\r\nSystem information:\r\n"
|
2005-03-09 16:56:26 +00:00
|
|
|
" Windows version %d.%d %d %s\r\n",
|
|
|
|
os.dwMajorVersion, os.dwMinorVersion, os.dwBuildNumber, os.szCSDVersion);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2006-11-28 19:58:13 +00:00
|
|
|
HANDLE file = CreateFile(_T("crash.log"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
DWORD num_written;
|
|
|
|
if (file != INVALID_HANDLE_VALUE) {
|
|
|
|
WriteFile(file, _crash_msg, output - _crash_msg, &num_written, NULL);
|
|
|
|
CloseHandle(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-15 16:38:10 +00:00
|
|
|
/* Close any possible log files */
|
|
|
|
CloseConsoleLogIfActive();
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_safe_esp) {
|
2005-10-02 22:39:56 +00:00
|
|
|
#ifdef _M_AMD64
|
|
|
|
ep->ContextRecord->Rip = (DWORD64)Handler2;
|
|
|
|
ep->ContextRecord->Rsp = (DWORD64)_safe_esp;
|
|
|
|
#else
|
2004-08-09 17:04:08 +00:00
|
|
|
ep->ContextRecord->Eip = (DWORD)Handler2;
|
|
|
|
ep->ContextRecord->Esp = (DWORD)_safe_esp;
|
2005-10-02 22:39:56 +00:00
|
|
|
#endif
|
2004-08-09 17:04:08 +00:00
|
|
|
return EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
2005-01-15 16:38:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-23 10:34:39 +00:00
|
|
|
static void Win32InitializeExceptions(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-10-02 22:39:56 +00:00
|
|
|
#ifdef _M_AMD64
|
2006-03-26 21:15:09 +00:00
|
|
|
extern void *_get_save_esp(void);
|
2005-10-02 22:39:56 +00:00
|
|
|
_safe_esp = _get_save_esp();
|
|
|
|
#else
|
2004-08-09 17:04:08 +00:00
|
|
|
_asm {
|
2005-03-09 16:56:26 +00:00
|
|
|
mov _safe_esp, esp
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-10-02 22:39:56 +00:00
|
|
|
#endif
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
SetUnhandledExceptionFilter(ExceptionHandler);
|
|
|
|
}
|
2006-03-26 21:15:09 +00:00
|
|
|
#endif /* _MSC_VER */
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-04 23:41:13 +00:00
|
|
|
/* Code below for windows version of opendir/readdir/closedir copied and
|
|
|
|
* modified from Jan Wassenberg's GPL implementation posted over at
|
|
|
|
* http://www.gamedev.net/community/forums/topic.asp?topic_id=364584&whichpage=1� */
|
|
|
|
|
|
|
|
/* suballocator - satisfies most requests with a reusable static instance.
|
|
|
|
* this avoids hundreds of alloc/free which would fragment the heap.
|
2006-08-05 10:30:43 +00:00
|
|
|
* To guarantee reentrancy, we fall back to malloc if the instance is
|
2006-08-04 23:41:13 +00:00
|
|
|
* already in use (it's important to avoid suprises since this is such a
|
|
|
|
* low-level routine). */
|
|
|
|
static DIR _global_dir;
|
|
|
|
static bool _global_dir_is_in_use = false;
|
|
|
|
|
|
|
|
static inline DIR *dir_calloc(void)
|
|
|
|
{
|
|
|
|
DIR *d;
|
|
|
|
|
|
|
|
if (_global_dir_is_in_use) {
|
2006-08-05 10:30:43 +00:00
|
|
|
d = calloc(1, sizeof(*d));
|
2006-08-04 23:41:13 +00:00
|
|
|
} else {
|
|
|
|
_global_dir_is_in_use = true;
|
|
|
|
d = &_global_dir;
|
2006-08-05 10:30:43 +00:00
|
|
|
memset(d, 0, sizeof(*d));
|
2006-08-04 23:41:13 +00:00
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void dir_free(DIR *d)
|
|
|
|
{
|
|
|
|
if (d == &_global_dir) {
|
|
|
|
_global_dir_is_in_use = false;
|
|
|
|
} else {
|
|
|
|
free(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DIR *opendir(const char *path)
|
|
|
|
{
|
|
|
|
DIR *d;
|
|
|
|
UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box
|
2006-11-28 14:42:31 +00:00
|
|
|
DWORD fa = GetFileAttributesW(OTTD2FS(path));
|
2006-08-04 23:41:13 +00:00
|
|
|
|
2006-08-05 10:30:43 +00:00
|
|
|
if ((fa != INVALID_FILE_ATTRIBUTES) && (fa & FILE_ATTRIBUTE_DIRECTORY)) {
|
|
|
|
d = dir_calloc();
|
|
|
|
if (d != NULL) {
|
2006-11-18 00:14:43 +00:00
|
|
|
char search_path[MAX_PATH];
|
2006-08-05 10:30:43 +00:00
|
|
|
/* build search path for FindFirstFile */
|
|
|
|
snprintf(search_path, lengthof(search_path), "%s" PATHSEP "*", path);
|
2006-11-28 14:42:31 +00:00
|
|
|
d->hFind = FindFirstFileW(OTTD2FS(search_path), &d->fd);
|
2006-08-05 10:30:43 +00:00
|
|
|
|
|
|
|
if (d->hFind != INVALID_HANDLE_VALUE ||
|
|
|
|
GetLastError() == ERROR_NO_MORE_FILES) { // the directory is empty
|
|
|
|
d->ent.dir = d;
|
|
|
|
d->at_first_entry = true;
|
|
|
|
} else {
|
|
|
|
dir_free(d);
|
|
|
|
d = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errno = ENOMEM;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* path not found or not a directory */
|
|
|
|
d = NULL;
|
2006-08-04 23:41:13 +00:00
|
|
|
errno = ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetErrorMode(sem); // restore previous setting
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent *readdir(DIR *d)
|
|
|
|
{
|
|
|
|
DWORD prev_err = GetLastError(); // avoid polluting last error
|
|
|
|
|
|
|
|
if (d->at_first_entry) {
|
|
|
|
/* the directory was empty when opened */
|
|
|
|
if (d->hFind == INVALID_HANDLE_VALUE) return NULL;
|
|
|
|
d->at_first_entry = false;
|
2006-11-28 14:42:31 +00:00
|
|
|
} else if (!FindNextFileW(d->hFind, &d->fd)) { // determine cause and bail
|
2006-08-04 23:41:13 +00:00
|
|
|
if (GetLastError() == ERROR_NO_MORE_FILES) SetLastError(prev_err);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This entry has passed all checks; return information about it.
|
|
|
|
* (note: d_name is a pointer; see struct dirent definition) */
|
|
|
|
d->ent.d_name = d->fd.cFileName;
|
|
|
|
return &d->ent;
|
|
|
|
}
|
|
|
|
|
|
|
|
int closedir(DIR *d)
|
|
|
|
{
|
|
|
|
FindClose(d->hFind);
|
|
|
|
dir_free(d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-05 00:47:32 +00:00
|
|
|
bool FiosIsRoot(const char *file)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-08-05 00:47:32 +00:00
|
|
|
return file[3] == '\0'; // C:\...
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-08-05 00:47:32 +00:00
|
|
|
void FiosGetDrives(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-28 19:58:13 +00:00
|
|
|
TCHAR drives[256];
|
|
|
|
const TCHAR *s;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-05 00:47:32 +00:00
|
|
|
GetLogicalDriveStrings(sizeof(drives), drives);
|
|
|
|
for (s = drives; *s != '\0';) {
|
|
|
|
FiosItem *fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_DRIVE;
|
2005-03-11 14:14:54 +00:00
|
|
|
fios->mtime = 0;
|
2006-11-28 19:58:13 +00:00
|
|
|
snprintf(fios->name, lengthof(fios->name), "%c:", s[0] & 0xFF);
|
2006-08-05 00:47:32 +00:00
|
|
|
ttd_strlcpy(fios->title, fios->name, lengthof(fios->title));
|
|
|
|
while (*s++ != '\0');
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-05 00:47:32 +00:00
|
|
|
bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-08-10 09:10:41 +00:00
|
|
|
// hectonanoseconds between Windows and POSIX epoch
|
2006-08-10 17:28:13 +00:00
|
|
|
static const int64 posix_epoch_hns = 0x019DB1DED53E8000LL;
|
2006-11-28 14:42:31 +00:00
|
|
|
const WIN32_FIND_DATAW *fd = &ent->dir->fd;
|
2006-08-05 10:30:43 +00:00
|
|
|
if (fd->dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-05 10:30:43 +00:00
|
|
|
sb->st_size = ((uint64) fd->nFileSizeHigh << 32) + fd->nFileSizeLow;
|
2006-08-10 09:10:41 +00:00
|
|
|
/* UTC FILETIME to seconds-since-1970 UTC
|
|
|
|
* we just have to subtract POSIX epoch and scale down to units of seconds.
|
|
|
|
* http://www.gamedev.net/community/forums/topic.asp?topic_id=294070&whichpage=1�
|
|
|
|
* XXX - not entirely correct, since filetimes on FAT aren't UTC but local,
|
|
|
|
* this won't entirely be correct, but we use the time only for comparsion. */
|
|
|
|
sb->st_mtime = (time_t)((*(uint64*)&fd->ftLastWriteTime - posix_epoch_hns) / 1E7);
|
2006-08-05 00:47:32 +00:00
|
|
|
sb->st_mode = (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)? S_IFDIR : S_IFREG;
|
2006-08-05 10:30:43 +00:00
|
|
|
|
2006-08-05 00:47:32 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-08-05 00:53:09 +00:00
|
|
|
bool FiosGetDiskFreeSpace(const char *path, uint32 *tot)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-03-28 13:30:51 +00:00
|
|
|
UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box
|
2006-08-05 00:53:09 +00:00
|
|
|
bool retval = false;
|
2006-11-28 19:58:13 +00:00
|
|
|
TCHAR root[4];
|
2004-08-09 17:04:08 +00:00
|
|
|
DWORD spc, bps, nfc, tnc;
|
2005-03-28 13:30:51 +00:00
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
_sntprintf(root, lengthof(root), _T("%c:") _T(PATHSEP), path[0]);
|
2005-03-28 13:30:51 +00:00
|
|
|
if (tot != NULL && GetDiskFreeSpace(root, &spc, &bps, &nfc, &tnc)) {
|
|
|
|
*tot = ((spc * bps) * (uint64)nfc) >> 20;
|
2006-08-05 00:53:09 +00:00
|
|
|
retval = true;
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-03-28 13:30:51 +00:00
|
|
|
|
|
|
|
SetErrorMode(sem); // reset previous setting
|
2006-08-05 00:53:09 +00:00
|
|
|
return retval;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ParseCommandLine(char *line, char **argv, int max_argc)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
// skip whitespace
|
2006-12-10 11:37:22 +00:00
|
|
|
while (*line == ' ' || *line == '\t') line++;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// end?
|
2006-12-10 11:37:22 +00:00
|
|
|
if (*line == '\0') break;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// special handling when quoted
|
|
|
|
if (*line == '"') {
|
|
|
|
argv[n++] = ++line;
|
|
|
|
while (*line != '"') {
|
2005-03-09 16:56:26 +00:00
|
|
|
if (*line == '\0') return n;
|
2004-08-09 17:04:08 +00:00
|
|
|
line++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
argv[n++] = line;
|
|
|
|
while (*line != ' ' && *line != '\t') {
|
2005-03-09 16:56:26 +00:00
|
|
|
if (*line == '\0') return n;
|
2004-08-09 17:04:08 +00:00
|
|
|
line++;
|
|
|
|
}
|
|
|
|
}
|
2005-03-09 16:56:26 +00:00
|
|
|
*line++ = '\0';
|
2004-08-09 17:04:08 +00:00
|
|
|
} while (n != max_argc);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2005-01-23 10:34:39 +00:00
|
|
|
void CreateConsole(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
HANDLE hand;
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO coninfo;
|
|
|
|
|
|
|
|
if (_has_console) return;
|
|
|
|
_has_console = true;
|
|
|
|
|
2006-09-07 22:36:33 +00:00
|
|
|
AllocConsole();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
hand = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
GetConsoleScreenBufferInfo(hand, &coninfo);
|
|
|
|
coninfo.dwSize.Y = 500;
|
|
|
|
SetConsoleScreenBufferSize(hand, coninfo.dwSize);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// redirect unbuffered STDIN, STDOUT, STDERR to the console
|
2005-07-23 15:24:31 +00:00
|
|
|
#if !defined(__CYGWIN__)
|
2005-10-02 22:39:56 +00:00
|
|
|
*stdout = *_fdopen( _open_osfhandle((intptr_t)hand, _O_TEXT), "w" );
|
|
|
|
*stdin = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT), "r" );
|
|
|
|
*stderr = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT), "w" );
|
2004-08-09 17:04:08 +00:00
|
|
|
#else
|
|
|
|
// open_osfhandle is not in cygwin
|
|
|
|
*stdout = *fdopen(1, "w" );
|
2004-12-04 17:54:56 +00:00
|
|
|
*stdin = *fdopen(0, "r" );
|
2004-09-10 19:02:27 +00:00
|
|
|
*stderr = *fdopen(2, "w" );
|
2004-08-09 17:04:08 +00:00
|
|
|
#endif
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-03-09 16:56:26 +00:00
|
|
|
setvbuf(stdin, NULL, _IONBF, 0);
|
|
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
|
|
|
setvbuf(stderr, NULL, _IONBF, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShowInfo(const char *str)
|
|
|
|
{
|
2006-06-27 21:25:53 +00:00
|
|
|
if (_has_console) {
|
2004-08-09 17:04:08 +00:00
|
|
|
puts(str);
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
2004-08-09 17:04:08 +00:00
|
|
|
bool old;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
ReleaseCapture();
|
|
|
|
_left_button_clicked =_left_button_down = false;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
old = MyShowCursor(true);
|
2006-11-28 19:58:13 +00:00
|
|
|
if (MessageBox(GetActiveWindow(), MB_TO_WIDE(str), _T("OpenTTD"), MB_ICONINFORMATION | MB_OKCANCEL) == IDCANCEL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
CreateConsole();
|
|
|
|
}
|
|
|
|
MyShowCursor(old);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-15 21:59:07 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
/* _set_error_mode() constants&function (do not exist in mingw headers) */
|
|
|
|
#define _OUT_TO_DEFAULT 0
|
|
|
|
#define _OUT_TO_STDERR 1
|
|
|
|
#define _OUT_TO_MSGBOX 2
|
|
|
|
#define _REPORT_ERRMODE 3
|
|
|
|
int _set_error_mode(int);
|
|
|
|
#endif
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
2005-03-09 16:56:26 +00:00
|
|
|
LPTSTR lpCmdLine, int nCmdShow)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int argc;
|
|
|
|
char *argv[64]; // max 64 command line arguments
|
2006-12-10 11:46:43 +00:00
|
|
|
char *cmdline;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
#if defined(UNICODE)
|
2006-12-10 11:46:43 +00:00
|
|
|
/* For UNICODE we need to convert the commandline to char* _AND_
|
|
|
|
* save it because argv[] points into this buffer and thus needs to
|
|
|
|
* be available between subsequent calls to FS2OTTD() */
|
|
|
|
char cmdlinebuf[MAX_PATH];
|
2006-11-28 19:58:13 +00:00
|
|
|
#endif
|
|
|
|
|
2006-12-10 11:46:43 +00:00
|
|
|
cmdline = WIDE_TO_MB_BUFFER(GetCommandLine(), cmdlinebuf, lengthof(cmdlinebuf));
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#if defined(_DEBUG)
|
|
|
|
CreateConsole();
|
|
|
|
#endif
|
|
|
|
|
2005-09-15 21:59:07 +00:00
|
|
|
_set_error_mode(_OUT_TO_MSGBOX); // force assertion output to messagebox
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-12-10 11:37:22 +00:00
|
|
|
/* setup random seed to something quite random */
|
2006-08-24 10:19:59 +00:00
|
|
|
_random_seeds[1][0] = _random_seeds[0][0] = GetTickCount();
|
|
|
|
_random_seeds[1][1] = _random_seeds[0][1] = _random_seeds[0][0] * 0x1234567;
|
2005-07-15 20:29:06 +00:00
|
|
|
SeedMT(_random_seeds[0][0]);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-28 19:58:13 +00:00
|
|
|
argc = ParseCommandLine(cmdline, argv, lengthof(argv));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
#if defined(WIN32_EXCEPTION_TRACKER)
|
2006-11-28 19:58:13 +00:00
|
|
|
Win32InitializeExceptions();
|
2004-08-09 17:04:08 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WIN32_EXCEPTION_TRACKER_DEBUG)
|
|
|
|
_try {
|
2006-11-28 19:58:13 +00:00
|
|
|
LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep);
|
2004-08-09 17:04:08 +00:00
|
|
|
#endif
|
|
|
|
ttd_main(argc, argv);
|
|
|
|
|
|
|
|
#if defined(WIN32_EXCEPTION_TRACKER_DEBUG)
|
|
|
|
} _except (ExceptionHandler(_exception_info())) {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-02-21 18:59:54 +00:00
|
|
|
void DeterminePaths(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-28 14:42:31 +00:00
|
|
|
char *s, *cfg;
|
|
|
|
wchar_t path[MAX_PATH];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-12-09 10:56:12 +00:00
|
|
|
_paths.personal_dir = _paths.game_data_dir = cfg = malloc(MAX_PATH);
|
2006-11-28 14:42:31 +00:00
|
|
|
GetCurrentDirectoryW(MAX_PATH - 1, path);
|
2006-12-03 17:47:53 +00:00
|
|
|
convert_from_fs(path, cfg, MAX_PATH);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-10-28 00:09:59 +00:00
|
|
|
cfg[0] = toupper(cfg[0]);
|
2006-11-18 00:14:43 +00:00
|
|
|
s = strchr(cfg, '\0');
|
2005-03-09 16:56:26 +00:00
|
|
|
if (s[-1] != '\\') strcpy(s, "\\");
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-12-09 10:56:12 +00:00
|
|
|
_paths.save_dir = str_fmt("%ssave", cfg);
|
|
|
|
_paths.autosave_dir = str_fmt("%s\\autosave", _paths.save_dir);
|
|
|
|
_paths.scenario_dir = str_fmt("%sscenario", cfg);
|
|
|
|
_paths.heightmap_dir = str_fmt("%sscenario\\heightmap", cfg);
|
|
|
|
_paths.gm_dir = str_fmt("%sgm\\", cfg);
|
|
|
|
_paths.data_dir = str_fmt("%sdata\\", cfg);
|
|
|
|
_paths.lang_dir = str_fmt("%slang\\", cfg);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-10 21:44:17 +00:00
|
|
|
if (_config_file == NULL)
|
2006-12-09 10:56:12 +00:00
|
|
|
_config_file = str_fmt("%sopenttd.cfg", _paths.personal_dir);
|
2005-03-10 21:44:17 +00:00
|
|
|
|
2006-12-09 10:56:12 +00:00
|
|
|
_highscore_file = str_fmt("%shs.dat", _paths.personal_dir);
|
|
|
|
_log_file = str_fmt("%sopenttd.log", _paths.personal_dir);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// make (auto)save and scenario folder
|
2006-12-09 10:56:12 +00:00
|
|
|
CreateDirectoryW(OTTD2FS(_paths.save_dir), NULL);
|
|
|
|
CreateDirectoryW(OTTD2FS(_paths.autosave_dir), NULL);
|
|
|
|
CreateDirectoryW(OTTD2FS(_paths.scenario_dir), NULL);
|
|
|
|
CreateDirectoryW(OTTD2FS(_paths.heightmap_dir), NULL);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-02-21 18:59:54 +00:00
|
|
|
/**
|
|
|
|
* Insert a chunk of text from the clipboard onto the textbuffer. Get TEXT clipboard
|
|
|
|
* and append this up to the maximum length (either absolute or screenlength). If maxlength
|
|
|
|
* is zero, we don't care about the screenlength but only about the physical length of the string
|
|
|
|
* @param tb @Textbuf type to be changed
|
|
|
|
* @return Return true on successfull change of Textbuf, or false otherwise
|
|
|
|
*/
|
|
|
|
bool InsertTextBufferClipboard(Textbuf *tb)
|
|
|
|
{
|
2006-11-16 22:05:33 +00:00
|
|
|
HGLOBAL cbuf;
|
|
|
|
char utf8_buf[512];
|
|
|
|
const char *ptr;
|
2005-02-21 18:59:54 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
WChar c;
|
|
|
|
uint16 width, length;
|
2005-02-21 18:59:54 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
|
2006-12-10 11:46:43 +00:00
|
|
|
const char *ret;
|
2005-02-21 18:59:54 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
OpenClipboard(NULL);
|
|
|
|
cbuf = GetClipboardData(CF_UNICODETEXT);
|
2005-02-21 18:59:54 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
ptr = GlobalLock(cbuf);
|
2006-12-10 11:46:43 +00:00
|
|
|
ret = convert_from_fs((wchar_t*)ptr, utf8_buf, lengthof(utf8_buf));
|
2006-11-16 22:05:33 +00:00
|
|
|
GlobalUnlock(cbuf);
|
|
|
|
CloseClipboard();
|
2005-02-21 18:59:54 +00:00
|
|
|
|
2006-12-10 11:46:43 +00:00
|
|
|
if (*ret == '\0') return false;
|
2006-11-16 22:05:33 +00:00
|
|
|
} else if (IsClipboardFormatAvailable(CF_TEXT)) {
|
|
|
|
OpenClipboard(NULL);
|
|
|
|
cbuf = GetClipboardData(CF_TEXT);
|
2005-02-21 18:59:54 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
ptr = GlobalLock(cbuf);
|
|
|
|
ttd_strlcpy(utf8_buf, ptr, lengthof(utf8_buf));
|
2005-02-21 18:59:54 +00:00
|
|
|
GlobalUnlock(cbuf);
|
|
|
|
CloseClipboard();
|
2006-11-16 22:05:33 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
2005-02-21 18:59:54 +00:00
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
width = length = 0;
|
|
|
|
|
|
|
|
for (ptr = utf8_buf; (c = Utf8Consume(&ptr)) != '\0';) {
|
|
|
|
byte charwidth;
|
|
|
|
|
|
|
|
if (!IsPrintable(c)) break;
|
|
|
|
if (tb->length + length >= tb->maxlength - 1) break;
|
|
|
|
charwidth = GetCharacterWidth(FS_NORMAL, c);
|
|
|
|
|
|
|
|
if (tb->maxwidth != 0 && width + tb->width + charwidth > tb->maxwidth) break;
|
|
|
|
|
|
|
|
width += charwidth;
|
|
|
|
length += Utf8CharLen(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length == 0) return false;
|
|
|
|
|
|
|
|
memmove(tb->buf + tb->caretpos + length, tb->buf + tb->caretpos, tb->length - tb->caretpos);
|
|
|
|
memcpy(tb->buf + tb->caretpos, utf8_buf, length);
|
|
|
|
tb->width += width;
|
|
|
|
tb->caretxoffs += width;
|
|
|
|
|
|
|
|
tb->length += length;
|
|
|
|
tb->caretpos += length;
|
|
|
|
tb->buf[tb->length] = '\0'; // terminating zero
|
|
|
|
|
|
|
|
return true;
|
2005-02-21 18:59:54 +00:00
|
|
|
}
|
2005-06-01 23:08:33 +00:00
|
|
|
|
2005-07-15 20:29:06 +00:00
|
|
|
|
|
|
|
void CSleep(int milliseconds)
|
|
|
|
{
|
|
|
|
Sleep(milliseconds);
|
2005-07-17 15:58:47 +00:00
|
|
|
}
|
2005-07-19 11:42:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Utility function to get the current timestamp in milliseconds
|
|
|
|
// Useful for profiling
|
2005-07-20 09:49:04 +00:00
|
|
|
int64 GetTS(void)
|
2005-07-19 11:42:40 +00:00
|
|
|
{
|
|
|
|
static double freq;
|
|
|
|
__int64 value;
|
|
|
|
if (!freq) {
|
|
|
|
QueryPerformanceFrequency((LARGE_INTEGER*)&value);
|
|
|
|
freq = (double)1000000 / value;
|
|
|
|
}
|
|
|
|
QueryPerformanceCounter((LARGE_INTEGER*)&value);
|
|
|
|
return (__int64)(value * freq);
|
2005-07-20 09:49:04 +00:00
|
|
|
}
|
2006-11-28 14:42:31 +00:00
|
|
|
|
2006-12-03 17:47:53 +00:00
|
|
|
/** Convert from OpenTTD's encoding to that of the local environment in
|
|
|
|
* UNICODE. OpenTTD encoding is UTF8, local is wide-char
|
2006-11-28 14:42:31 +00:00
|
|
|
* @param name pointer to a valid string that will be converted
|
2006-12-03 17:47:53 +00:00
|
|
|
* @param utf16_buf pointer to a valid wide-char buffer that will receive the
|
|
|
|
* converted string
|
|
|
|
* @param buflen length in wide characters of the receiving buffer
|
|
|
|
* @return pointer to utf16_buf. If conversion fails the string is of zero-length */
|
|
|
|
wchar_t *convert_to_fs(const char *name, wchar_t *utf16_buf, size_t buflen)
|
2006-11-28 14:42:31 +00:00
|
|
|
{
|
2006-12-03 17:47:53 +00:00
|
|
|
int len = MultiByteToWideChar(CP_UTF8, 0, name, -1, utf16_buf, buflen);
|
2006-11-28 14:42:31 +00:00
|
|
|
if (len == 0) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(misc, 0, "[utf8] error converting '%s'. Errno %d", name, GetLastError());
|
2006-12-03 17:47:53 +00:00
|
|
|
utf16_buf[0] = '\0';
|
2006-11-28 14:42:31 +00:00
|
|
|
}
|
|
|
|
|
2006-12-03 17:47:53 +00:00
|
|
|
return utf16_buf;
|
2006-11-28 14:42:31 +00:00
|
|
|
}
|
|
|
|
|
2006-12-03 17:47:53 +00:00
|
|
|
/** Convert from OpenTTD's encoding to that of the local environment in
|
|
|
|
* UNICODE. OpenTTD encoding is UTF8, local is wide-char.
|
|
|
|
* The returned value's contents can only be guaranteed until the next call to
|
|
|
|
* this function. So if the value is needed for anything else, use convert_from_fs
|
2006-11-28 14:42:31 +00:00
|
|
|
* @param name pointer to a valid string that will be converted
|
2006-12-03 17:47:53 +00:00
|
|
|
* @return pointer to the converted string; if failed string is of zero-length */
|
|
|
|
const wchar_t *OTTD2FS(const char *name)
|
2006-11-28 14:42:31 +00:00
|
|
|
{
|
2006-12-10 11:46:43 +00:00
|
|
|
static wchar_t utf16_buf[512];
|
2006-12-03 17:47:53 +00:00
|
|
|
return convert_to_fs(name, utf16_buf, lengthof(utf16_buf));
|
|
|
|
}
|
|
|
|
|
2006-11-28 14:42:31 +00:00
|
|
|
|
2006-12-03 17:47:53 +00:00
|
|
|
/** Convert to OpenTTD's encoding from that of the local environment in
|
|
|
|
* UNICODE. OpenTTD encoding is UTF8, local is wide-char
|
|
|
|
* @param name pointer to a valid string that will be converted
|
|
|
|
* @param utf8_buf pointer to a valid buffer that will receive the converted string
|
|
|
|
* @param buflen length in characters of the receiving buffer
|
|
|
|
* @return pointer to utf8_buf. If conversion fails the string is of zero-length */
|
|
|
|
char *convert_from_fs(const wchar_t *name, char *utf8_buf, size_t buflen)
|
|
|
|
{
|
|
|
|
int len = WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8_buf, buflen, NULL, NULL);
|
2006-11-28 14:42:31 +00:00
|
|
|
if (len == 0) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(misc, 0, "[utf8] error converting wide-string. Errno %d", GetLastError());
|
2006-12-03 17:47:53 +00:00
|
|
|
utf8_buf[0] = '\0';
|
2006-11-28 14:42:31 +00:00
|
|
|
}
|
|
|
|
|
2006-12-03 17:47:53 +00:00
|
|
|
return utf8_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Convert to OpenTTD's encoding from that of the local environment in
|
|
|
|
* UNICODE. OpenTTD encoding is UTF8, local is wide-char.
|
|
|
|
* The returned value's contents can only be guaranteed until the next call to
|
|
|
|
* this function. So if the value is needed for anything else, use convert_from_fs
|
|
|
|
* @param name pointer to a valid string that will be converted
|
|
|
|
* @return pointer to the converted string; if failed string is of zero-length */
|
|
|
|
const char *FS2OTTD(const wchar_t *name)
|
|
|
|
{
|
|
|
|
static char utf8_buf[512];
|
|
|
|
return convert_from_fs(name, utf8_buf, lengthof(utf8_buf));
|
2006-11-28 14:42:31 +00:00
|
|
|
}
|
2006-12-09 23:55:56 +00:00
|
|
|
|
|
|
|
/** Our very own SHGetFolderPath function for support of windows operating
|
|
|
|
* systems that don't have this function (eg Win9x, etc.). We try using the
|
|
|
|
* native function, and if that doesn't exist we will try a more crude approach
|
|
|
|
* of environment variables and hope for the best */
|
|
|
|
HRESULT OTTDSHGetFolderPath(HWND hwnd, int csidl, HANDLE hToken, DWORD dwFlags, LPTSTR pszPath)
|
|
|
|
{
|
|
|
|
static HRESULT (WINAPI *SHGetFolderPath)(HWND, int, HANDLE, DWORD, LPTSTR) = NULL;
|
|
|
|
static bool first_time = true;
|
|
|
|
|
|
|
|
/* We only try to load the library one time; if it fails, it fails */
|
|
|
|
if (first_time) {
|
|
|
|
#if defined(UNICODE)
|
|
|
|
# define W(x) x "W"
|
|
|
|
#else
|
|
|
|
# define W(x) x "A"
|
|
|
|
#endif
|
|
|
|
if (!LoadLibraryList((Function*)&SHGetFolderPath, "SHFolder.dll\0" W("SHGetFolderPath") "\0\0")) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(misc, 0, "Unable to load " W("SHGetFolderPath") "from SHFolder.dll");
|
2006-12-09 23:55:56 +00:00
|
|
|
}
|
|
|
|
#undef W
|
|
|
|
first_time = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SHGetFolderPath != NULL) return SHGetFolderPath(hwnd, csidl, hToken, dwFlags, pszPath);
|
|
|
|
|
|
|
|
/* SHGetFolderPath doesn't exist, try a more conservative approach,
|
|
|
|
* eg environment variables. This is only included for legacy modes
|
|
|
|
* MSDN says: that 'pszPath' is a "Pointer to a null-terminated string of
|
|
|
|
* length MAX_PATH which will receive the path" so let's assume that
|
|
|
|
* Windows 95 with Internet Explorer 5.0, Windows 98 with Internet Explorer 5.0,
|
|
|
|
* Windows 98 Second Edition (SE), Windows NT 4.0 with Internet Explorer 5.0,
|
|
|
|
* Windows NT 4.0 with Service Pack 4 (SP4) */
|
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
switch (csidl) {
|
|
|
|
case CSIDL_FONTS: /* Get the system font path, eg %WINDIR%\Fonts */
|
2006-12-15 15:21:13 +00:00
|
|
|
ret = GetEnvironmentVariable(_T("WINDIR"), pszPath, MAX_PATH);
|
2006-12-09 23:55:56 +00:00
|
|
|
if (ret == 0) break;
|
2006-12-15 15:21:13 +00:00
|
|
|
_tcsncat(pszPath, _T("\\Fonts"), MAX_PATH);
|
2006-12-09 23:55:56 +00:00
|
|
|
|
|
|
|
return (HRESULT)0;
|
|
|
|
break;
|
|
|
|
/* XXX - other types to go here when needed... */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|