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-27 20:08:52 +00:00
|
|
|
#include "hal.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-11-25 10:47:30 +00:00
|
|
|
#include "table/strings.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>
|
2005-07-21 22:18:02 +00:00
|
|
|
#include "variables.h"
|
2005-07-24 06:09:54 +00:00
|
|
|
#include "win32.h"
|
2005-11-10 17:21:05 +00:00
|
|
|
#include <ctype.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
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
#if defined(__MINGW32__) || defined(__CYGWIN__)
|
|
|
|
#define __TIMESTAMP__ __DATE__ __TIME__
|
2005-03-24 20:47:21 +00:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Helper function needed by dynamically loading SDL
|
2005-07-24 06:09:54 +00:00
|
|
|
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') {
|
2005-07-24 06:09:54 +00:00
|
|
|
HMODULE lib = LoadLibrary(dll);
|
|
|
|
|
|
|
|
if (lib == NULL) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
while (true) {
|
2005-07-24 06:09:54 +00:00
|
|
|
FARPROC p;
|
|
|
|
|
|
|
|
while (*dll++ != '\0');
|
|
|
|
if (*dll == '\0') break;
|
2004-08-09 17:04:08 +00:00
|
|
|
p = GetProcAddress(lib, dll);
|
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
|
2005-10-02 22:39:56 +00:00
|
|
|
# ifdef _M_AMD64
|
|
|
|
void* _get_save_esp(void);
|
|
|
|
uint64 _rdtsc(void);
|
|
|
|
# endif
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static const char *_exception_string;
|
2005-09-15 21:59:07 +00:00
|
|
|
#endif
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
void ShowOSErrorBox(const char *buf)
|
|
|
|
{
|
|
|
|
MyShowCursor(true);
|
|
|
|
MessageBoxA(GetActiveWindow(), buf, "Error!", MB_ICONSTOP);
|
|
|
|
|
|
|
|
// 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--) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (crc & 1)
|
|
|
|
crc = (crc >> 1) ^ poly;
|
|
|
|
else
|
2005-03-09 16:56:26 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GetFileInfo(DebugFileInfo *dfi, const char *filename)
|
|
|
|
{
|
|
|
|
memset(dfi, 0, sizeof(dfi));
|
|
|
|
|
|
|
|
{
|
|
|
|
HANDLE file;
|
|
|
|
byte buffer[1024];
|
|
|
|
DWORD numread;
|
|
|
|
uint32 filesize = 0;
|
|
|
|
FILETIME write_time;
|
|
|
|
uint32 crc = (uint32)-1;
|
|
|
|
|
2005-03-09 16:56:26 +00:00
|
|
|
file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
|
|
|
|
OPEN_EXISTING, 0, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (file != INVALID_HANDLE_VALUE) {
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2005-03-09 16:56:26 +00:00
|
|
|
if (ReadFile(file, buffer, sizeof(buffer), &numread, NULL) == 0 ||
|
|
|
|
numread == 0)
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
filesize += numread;
|
|
|
|
crc = CalcCRC(buffer, numread, crc);
|
|
|
|
}
|
|
|
|
dfi->size = filesize;
|
|
|
|
dfi->crc32 = crc ^ (uint32)-1;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (GetFileTime(file, NULL, NULL, &write_time)) {
|
|
|
|
FileTimeToSystemTime(&write_time, &dfi->file_time);
|
|
|
|
}
|
|
|
|
CloseHandle(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *PrintModuleInfo(char *output, HMODULE mod)
|
|
|
|
{
|
2004-09-27 12:36:59 +00:00
|
|
|
char 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",
|
2004-08-09 17:04:08 +00:00
|
|
|
buffer,
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
BOOL (WINAPI *EnumProcessModules)(HANDLE,HMODULE*,DWORD,LPDWORD);
|
|
|
|
HANDLE proc;
|
|
|
|
HMODULE modules[100];
|
|
|
|
DWORD needed;
|
|
|
|
BOOL res;
|
|
|
|
int count,i;
|
|
|
|
|
2005-07-24 06:09:54 +00:00
|
|
|
if (LoadLibraryList((Function*)&EnumProcessModules, "psapi.dll\0EnumProcessModules\0")) {
|
2004-08-09 17:04:08 +00:00
|
|
|
proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
|
|
|
|
if (proc) {
|
|
|
|
res = EnumProcessModules(proc, modules, sizeof(modules), &needed);
|
|
|
|
CloseHandle(proc);
|
|
|
|
if (res) {
|
2005-03-09 16:56:26 +00:00
|
|
|
count =
|
|
|
|
min(needed / sizeof(HMODULE), lengthof(modules));
|
|
|
|
for (i = 0; i != count; i++)
|
2004-08-09 17:04:08 +00:00
|
|
|
output = PrintModuleInfo(output, modules[i]);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output = PrintModuleInfo(output, NULL);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
static const char _crash_desc[] =
|
2004-08-09 17:04:08 +00:00
|
|
|
"A serious fault condition occured in the game. The game will shut down.\n"
|
|
|
|
"Press \"Submit report\" to send crash information to the developers. "
|
2005-03-09 16:56:26 +00:00
|
|
|
"This will greatly help debugging. "
|
|
|
|
"The information contained in the report is displayed below.\n"
|
2004-08-09 17:04:08 +00:00
|
|
|
"Press \"Emergency save\" to attempt saving the game.";
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
static const char _save_succeeded[] =
|
2005-03-09 16:56:26 +00:00
|
|
|
"Emergency save succeeded.\n"
|
|
|
|
"Be aware that critical parts of the internal game state may have become "
|
|
|
|
"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
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
HINTERNET (WINAPI *InternetOpenA)(LPCSTR,DWORD, LPCSTR, LPCSTR, DWORD);
|
|
|
|
HINTERNET (WINAPI *InternetConnectA)(HINTERNET, LPCSTR, INTERNET_PORT, LPCSTR, LPCSTR, DWORD, DWORD, DWORD);
|
|
|
|
HINTERNET (WINAPI *HttpOpenRequestA)(HINTERNET, LPCSTR, LPCSTR, LPCSTR, LPCSTR, LPCSTR *, DWORD, DWORD);
|
|
|
|
BOOL (WINAPI *HttpSendRequestA)(HINTERNET, LPCSTR, DWORD, LPVOID, DWORD);
|
|
|
|
BOOL (WINAPI *InternetCloseHandle)(HINTERNET);
|
|
|
|
BOOL (WINAPI *HttpQueryInfo)(HINTERNET, DWORD, LPVOID, LPDWORD, LPDWORD);
|
|
|
|
} WinInetProcs;
|
|
|
|
|
|
|
|
#define M(x) x "\0"
|
2004-09-10 19:02:27 +00:00
|
|
|
static const char wininet_files[] =
|
2004-08-09 17:04:08 +00:00
|
|
|
M("wininet.dll")
|
|
|
|
M("InternetOpenA")
|
|
|
|
M("InternetConnectA")
|
|
|
|
M("HttpOpenRequestA")
|
|
|
|
M("HttpSendRequestA")
|
|
|
|
M("InternetCloseHandle")
|
|
|
|
M("HttpQueryInfoA")
|
|
|
|
M("");
|
|
|
|
#undef M
|
|
|
|
|
|
|
|
static WinInetProcs _wininet;
|
|
|
|
|
|
|
|
|
2005-07-24 06:09:54 +00:00
|
|
|
static const char *SubmitCrashReport(HWND wnd, void *msg, size_t msglen, const char *arg)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
HINTERNET inet, conn, http;
|
2005-07-24 06:09:54 +00:00
|
|
|
const char *err = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
DWORD code, len;
|
|
|
|
static char buf[100];
|
|
|
|
char buff[100];
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-07-24 06:09:54 +00:00
|
|
|
if (_wininet.InternetOpen == NULL && !LoadLibraryList((Function*)&_wininet, wininet_files)) return "can't load wininet.dll";
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-06-20 19:39:49 +00:00
|
|
|
inet = _wininet.InternetOpen("OTTD", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
|
2004-08-09 17:04:08 +00:00
|
|
|
if (inet == NULL) { err = "internetopen failed"; goto error1; }
|
|
|
|
|
|
|
|
conn = _wininet.InternetConnect(inet, "openttd.com", INTERNET_DEFAULT_HTTP_PORT, "", "", INTERNET_SERVICE_HTTP, 0, 0);
|
|
|
|
if (conn == NULL) { err = "internetconnect failed"; goto error2; }
|
|
|
|
|
|
|
|
sprintf(buff, "/crash.php?file=%s&ident=%d", arg, _ident);
|
|
|
|
|
|
|
|
http = _wininet.HttpOpenRequest(conn, "POST", buff, NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE , 0);
|
|
|
|
if (http == NULL) { err = "httpopenrequest failed"; goto error3; }
|
|
|
|
|
|
|
|
if (!_wininet.HttpSendRequest(http, "Content-type: application/binary", -1, msg, msglen)) { err = "httpsendrequest failed"; goto error4; }
|
|
|
|
|
|
|
|
len = sizeof(code);
|
|
|
|
if (!_wininet.HttpQueryInfo(http, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &code, &len, 0)) { err = "httpqueryinfo failed"; goto error4; }
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (code != 200) {
|
|
|
|
int l = sprintf(buf, "Server said: %d ", code);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SubmitFile(HWND wnd, const char *file)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char * const _expand_texts[] = {"S&how report >>", "&Hide report <<" };
|
|
|
|
|
|
|
|
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) {
|
2004-08-09 17:04:08 +00:00
|
|
|
case WM_INITDIALOG:
|
|
|
|
SetDlgItemText(wnd, 10, _crash_desc);
|
|
|
|
SetDlgItemText(wnd, 11, _crash_msg);
|
|
|
|
SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
|
|
|
|
SetWndSize(wnd, -1);
|
|
|
|
return TRUE;
|
|
|
|
case WM_COMMAND:
|
2006-02-01 07:36:15 +00:00
|
|
|
switch (wParam) {
|
2004-08-09 17:04:08 +00:00
|
|
|
case 12: // Close
|
|
|
|
ExitProcess(0);
|
|
|
|
case 13: { // Emergency save
|
|
|
|
if (DoEmergencySave(wnd))
|
|
|
|
MessageBoxA(wnd, _save_succeeded, "Save successful", MB_ICONINFORMATION);
|
|
|
|
else
|
|
|
|
MessageBoxA(wnd, "Save failed", "Save failed", MB_ICONINFORMATION);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 14: { // Submit crash report
|
2005-07-24 06:09:54 +00:00
|
|
|
const char *s;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
SetCursor(LoadCursor(NULL, IDC_WAIT));
|
|
|
|
|
|
|
|
s = SubmitCrashReport(wnd, _crash_msg, strlen(_crash_msg), "");
|
|
|
|
if (s) {
|
|
|
|
MessageBoxA(wnd, s, "Error", MB_ICONSTOP);
|
|
|
|
break;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// try to submit emergency savegame
|
|
|
|
if (_did_emerg_save || DoEmergencySave(wnd)) {
|
|
|
|
SubmitFile(wnd, "crash.sav");
|
|
|
|
}
|
|
|
|
// try to submit the autosaved game
|
|
|
|
if (_opt.autosave) {
|
|
|
|
char buf[40];
|
|
|
|
sprintf(buf, "autosave%d.sav", (_autosave_ctr - 1) & 3);
|
|
|
|
SubmitFile(wnd, buf);
|
|
|
|
}
|
|
|
|
EnableWindow(GetDlgItem(wnd, 14), FALSE);
|
|
|
|
SetCursor(LoadCursor(NULL, IDC_ARROW));
|
|
|
|
MessageBoxA(wnd, "Crash report submitted. Thank you.", "Crash Report", MB_ICONINFORMATION);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 15: // Expand
|
|
|
|
_expanded ^= 1;
|
|
|
|
SetWndSize(wnd, _expanded);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
case WM_CLOSE:
|
|
|
|
ExitProcess(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
static bool had_exception;
|
|
|
|
|
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);
|
2004-09-10 19:02:27 +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"
|
|
|
|
"Build: %s built on " __TIMESTAMP__ "\r\n",
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
HANDLE file = CreateFile("crash.log", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
|
|
|
|
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
|
|
|
|
_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);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char *_fios_path;
|
|
|
|
static char *_fios_save_path;
|
|
|
|
static char *_fios_scn_path;
|
|
|
|
static FiosItem *_fios_items;
|
|
|
|
static int _fios_count, _fios_alloc;
|
|
|
|
|
2005-01-23 10:34:39 +00:00
|
|
|
static FiosItem *FiosAlloc(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
if (_fios_count == _fios_alloc) {
|
|
|
|
_fios_alloc += 256;
|
|
|
|
_fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem));
|
|
|
|
}
|
|
|
|
return &_fios_items[_fios_count++];
|
|
|
|
}
|
|
|
|
|
2005-03-28 13:30:51 +00:00
|
|
|
static HANDLE MyFindFirstFile(const char *path, const char *file, WIN32_FIND_DATA *fd)
|
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
|
|
|
|
HANDLE h;
|
2004-08-09 17:04:08 +00:00
|
|
|
char paths[MAX_PATH];
|
2005-10-18 20:18:05 +00:00
|
|
|
const char *s = strrchr(path, '\\');
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-10-18 20:18:05 +00:00
|
|
|
snprintf(paths, sizeof(paths), "%s%s%s", path, (s[1] == '\0') ? "" : "\\", file);
|
2005-03-28 13:30:51 +00:00
|
|
|
h = FindFirstFile(paths, fd);
|
|
|
|
|
|
|
|
SetErrorMode(sem); // restore previous setting
|
|
|
|
return h;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
int CDECL compare_FiosItems(const void *a, const void *b)
|
|
|
|
{
|
2005-03-09 16:56:26 +00:00
|
|
|
const FiosItem *da = (const FiosItem *)a;
|
|
|
|
const FiosItem *db = (const FiosItem *)b;
|
2004-08-09 17:04:08 +00:00
|
|
|
int r;
|
|
|
|
|
2005-10-17 19:57:00 +00:00
|
|
|
if (_savegame_sort_order & SORT_BY_NAME) {
|
2005-07-17 15:58:47 +00:00
|
|
|
r = strcasecmp(da->title, db->title);
|
2005-10-17 19:57:00 +00:00
|
|
|
} else {
|
|
|
|
r = da->mtime < db->mtime ? -1 : 1;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-10-17 19:57:00 +00:00
|
|
|
if (_savegame_sort_order & SORT_DESCENDING) r = -r;
|
2004-08-09 17:04:08 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Get a list of savegames
|
|
|
|
FiosItem *FiosGetSavegameList(int *num, int mode)
|
|
|
|
{
|
|
|
|
WIN32_FIND_DATA fd;
|
|
|
|
HANDLE h;
|
|
|
|
FiosItem *fios;
|
|
|
|
int sort_start;
|
|
|
|
|
|
|
|
if (_fios_save_path == NULL) {
|
|
|
|
_fios_save_path = malloc(MAX_PATH);
|
|
|
|
strcpy(_fios_save_path, _path.save_dir);
|
|
|
|
}
|
|
|
|
|
2005-07-17 15:58:47 +00:00
|
|
|
_fios_path = _fios_save_path;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Parent directory, only if not of the type C:\.
|
2005-10-18 20:18:05 +00:00
|
|
|
if (_fios_path[3] != '\0') {
|
2004-08-09 17:04:08 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_PARENT;
|
2005-03-11 14:14:54 +00:00
|
|
|
fios->mtime = 0;
|
2005-03-26 18:04:42 +00:00
|
|
|
strcpy(fios->name, "..");
|
2004-08-09 17:04:08 +00:00
|
|
|
strcpy(fios->title, ".. (Parent directory)");
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Show subdirectories first
|
|
|
|
h = MyFindFirstFile(_fios_path, "*.*", &fd);
|
|
|
|
if (h != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
|
|
|
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
|
2005-03-09 16:56:26 +00:00
|
|
|
strcmp(fd.cFileName, ".") != 0 &&
|
|
|
|
strcmp(fd.cFileName, "..") != 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_DIR;
|
2005-03-11 14:14:54 +00:00
|
|
|
fios->mtime = 0;
|
|
|
|
ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name));
|
2005-07-17 15:58:47 +00:00
|
|
|
snprintf(fios->title, lengthof(fios->title), "%s\\ (Directory)", fd.cFileName);
|
2006-01-31 12:08:49 +00:00
|
|
|
str_validate(fios->title);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} while (FindNextFile(h, &fd));
|
|
|
|
FindClose(h);
|
|
|
|
}
|
|
|
|
|
2005-08-12 06:37:48 +00:00
|
|
|
{
|
|
|
|
/* XXX ugly global variables ... */
|
|
|
|
byte order = _savegame_sort_order;
|
2005-10-17 19:57:00 +00:00
|
|
|
_savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
|
2005-08-12 06:37:48 +00:00
|
|
|
qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems);
|
|
|
|
_savegame_sort_order = order;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// this is where to start sorting
|
|
|
|
sort_start = _fios_count;
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
/* Show savegame files
|
|
|
|
* .SAV OpenTTD saved game
|
|
|
|
* .SS1 Transport Tycoon Deluxe preset game
|
|
|
|
* .SV1 Transport Tycoon Deluxe (Patch) saved game
|
|
|
|
* .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
|
|
|
h = MyFindFirstFile(_fios_path, "*.*", &fd);
|
|
|
|
if (h != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
2005-03-11 14:14:54 +00:00
|
|
|
char *t;
|
|
|
|
|
|
|
|
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
|
|
|
|
|
|
|
|
t = strrchr(fd.cFileName, '.');
|
2005-08-13 21:35:31 +00:00
|
|
|
if (t == NULL) continue;
|
|
|
|
|
|
|
|
if (strcasecmp(t, ".sav") == 0) { // OpenTTD
|
2005-03-11 14:14:54 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_FILE;
|
|
|
|
fios->mtime = *(uint64*)&fd.ftLastWriteTime;
|
|
|
|
ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name));
|
2005-07-17 15:58:47 +00:00
|
|
|
|
|
|
|
*t = '\0'; // strip extension
|
|
|
|
ttd_strlcpy(fios->title, fd.cFileName, lengthof(fios->title));
|
2006-01-31 12:08:49 +00:00
|
|
|
str_validate(fios->title);
|
2005-03-11 14:14:54 +00:00
|
|
|
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
|
2005-08-13 21:35:31 +00:00
|
|
|
if (strcasecmp(t, ".ss1") == 0 ||
|
|
|
|
strcasecmp(t, ".sv1") == 0 ||
|
|
|
|
strcasecmp(t, ".sv2") == 0) { // TTDLX(Patch)
|
2005-03-11 14:14:54 +00:00
|
|
|
char buf[MAX_PATH];
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
fios = FiosAlloc();
|
2005-03-11 14:14:54 +00:00
|
|
|
fios->type = FIOS_TYPE_OLDFILE;
|
2004-08-09 17:04:08 +00:00
|
|
|
fios->mtime = *(uint64*)&fd.ftLastWriteTime;
|
2005-03-11 14:14:54 +00:00
|
|
|
ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name));
|
2004-08-09 17:04:08 +00:00
|
|
|
sprintf(buf, "%s\\%s", _fios_path, fd.cFileName);
|
2005-03-11 14:14:54 +00:00
|
|
|
GetOldSaveGameName(fios->title, buf);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (FindNextFile(h, &fd));
|
|
|
|
FindClose(h);
|
|
|
|
}
|
|
|
|
|
|
|
|
qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems);
|
|
|
|
|
|
|
|
// Drives
|
|
|
|
{
|
|
|
|
char drives[256];
|
2005-03-11 14:14:54 +00:00
|
|
|
const char *s;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
GetLogicalDriveStrings(sizeof(drives), drives);
|
2005-03-11 14:14:54 +00:00
|
|
|
for (s = drives; *s != '\0';) {
|
2004-08-09 17:04:08 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_DRIVE;
|
2005-03-27 10:35:01 +00:00
|
|
|
sprintf(fios->name, "%c:", s[0]);
|
2005-03-11 14:14:54 +00:00
|
|
|
sprintf(fios->title, "%c:", s[0]);
|
|
|
|
while (*s++ != '\0') {}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-11 14:14:54 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
*num = _fios_count;
|
|
|
|
return _fios_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a list of scenarios
|
|
|
|
FiosItem *FiosGetScenarioList(int *num, int mode)
|
|
|
|
{
|
|
|
|
FiosItem *fios;
|
|
|
|
WIN32_FIND_DATA fd;
|
|
|
|
HANDLE h;
|
|
|
|
int sort_start;
|
|
|
|
|
2005-07-17 15:58:47 +00:00
|
|
|
if (_fios_scn_path == NULL) {
|
|
|
|
_fios_scn_path = malloc(MAX_PATH);
|
2004-08-09 17:04:08 +00:00
|
|
|
strcpy(_fios_scn_path, _path.scenario_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
_fios_path = _fios_scn_path;
|
|
|
|
|
|
|
|
// Parent directory, only if not of the type C:\.
|
2005-03-09 16:56:26 +00:00
|
|
|
if (_fios_path[3] != '\0' && mode != SLD_NEW_GAME) {
|
2004-08-09 17:04:08 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_PARENT;
|
2005-03-11 14:14:54 +00:00
|
|
|
fios->mtime = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
strcpy(fios->title, ".. (Parent directory)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show subdirectories first
|
|
|
|
h = MyFindFirstFile(_fios_scn_path, "*.*", &fd);
|
|
|
|
if (h != INVALID_HANDLE_VALUE && mode != SLD_NEW_GAME) {
|
|
|
|
do {
|
|
|
|
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
|
2005-03-09 16:56:26 +00:00
|
|
|
strcmp(fd.cFileName, ".") != 0 &&
|
|
|
|
strcmp(fd.cFileName, "..") != 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_DIR;
|
2005-03-11 14:14:54 +00:00
|
|
|
fios->mtime = 0;
|
|
|
|
ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name));
|
2005-07-17 15:58:47 +00:00
|
|
|
snprintf(fios->title, lengthof(fios->title), "%s\\ (Directory)", fd.cFileName);
|
2006-01-31 12:08:49 +00:00
|
|
|
str_validate(fios->title);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} while (FindNextFile(h, &fd));
|
|
|
|
FindClose(h);
|
|
|
|
}
|
|
|
|
|
2005-08-12 06:37:48 +00:00
|
|
|
{
|
|
|
|
/* XXX ugly global variables ... */
|
|
|
|
byte order = _savegame_sort_order;
|
2005-10-17 19:57:00 +00:00
|
|
|
_savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
|
2005-08-12 06:37:48 +00:00
|
|
|
qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems);
|
|
|
|
_savegame_sort_order = order;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// this is where to start sorting
|
|
|
|
sort_start = _fios_count;
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
/* Show scenario files
|
|
|
|
* .SCN OpenTTD style scenario file
|
|
|
|
* .SV0 Transport Tycoon Deluxe (Patch) scenario
|
|
|
|
* .SS0 Transport Tycoon Deluxe preset scenario
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
|
|
|
h = MyFindFirstFile(_fios_scn_path, "*.*", &fd);
|
|
|
|
if (h != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
2005-03-11 14:14:54 +00:00
|
|
|
char *t;
|
|
|
|
|
|
|
|
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue;
|
|
|
|
|
|
|
|
t = strrchr(fd.cFileName, '.');
|
2005-08-13 21:35:31 +00:00
|
|
|
if (t == NULL) continue;
|
|
|
|
|
|
|
|
if (strcasecmp(t, ".scn") == 0) { // OpenTTD
|
2005-03-11 14:14:54 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_SCENARIO;
|
|
|
|
fios->mtime = *(uint64*)&fd.ftLastWriteTime;
|
|
|
|
ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name));
|
2005-07-17 15:58:47 +00:00
|
|
|
|
2005-08-12 06:37:48 +00:00
|
|
|
*t = '\0'; // strip extension
|
2005-07-17 15:58:47 +00:00
|
|
|
ttd_strlcpy(fios->title, fd.cFileName, lengthof(fios->title));
|
2006-01-31 12:08:49 +00:00
|
|
|
str_validate(fios->title);
|
2005-03-11 14:14:54 +00:00
|
|
|
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO ||
|
|
|
|
mode == SLD_NEW_GAME) {
|
2005-08-13 21:35:31 +00:00
|
|
|
if (strcasecmp(t, ".sv0") == 0 ||
|
|
|
|
strcasecmp(t, ".ss0") == 0) { // TTDLX(Patch)
|
2005-03-11 14:14:54 +00:00
|
|
|
char buf[MAX_PATH];
|
2005-03-09 16:56:26 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
fios = FiosAlloc();
|
2005-03-11 14:14:54 +00:00
|
|
|
fios->type = FIOS_TYPE_OLD_SCENARIO;
|
2004-08-09 17:04:08 +00:00
|
|
|
fios->mtime = *(uint64*)&fd.ftLastWriteTime;
|
|
|
|
sprintf(buf, "%s\\%s", _fios_path, fd.cFileName);
|
2005-03-11 14:14:54 +00:00
|
|
|
GetOldScenarioGameName(fios->title, buf);
|
|
|
|
ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (FindNextFile(h, &fd));
|
|
|
|
FindClose(h);
|
|
|
|
}
|
|
|
|
|
|
|
|
qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems);
|
|
|
|
|
|
|
|
// Drives
|
|
|
|
if (mode != SLD_NEW_GAME) {
|
|
|
|
char drives[256];
|
2005-03-09 16:56:26 +00:00
|
|
|
const char *s;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
GetLogicalDriveStrings(sizeof(drives), drives);
|
2005-03-09 16:56:26 +00:00
|
|
|
for (s = drives; *s != '\0';) {
|
2004-08-09 17:04:08 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_DRIVE;
|
2005-03-27 10:35:01 +00:00
|
|
|
sprintf(fios->name, "%c:", s[0]);
|
2005-03-09 16:56:26 +00:00
|
|
|
sprintf(fios->title, "%c:", s[0]);
|
|
|
|
while (*s++ != '\0') {}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*num = _fios_count;
|
|
|
|
return _fios_items;
|
|
|
|
}
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Free the list of savegames
|
2005-01-23 10:34:39 +00:00
|
|
|
void FiosFreeSavegameList(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
free(_fios_items);
|
|
|
|
_fios_items = NULL;
|
|
|
|
_fios_alloc = _fios_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Browse to
|
|
|
|
char *FiosBrowseTo(const FiosItem *item)
|
|
|
|
{
|
|
|
|
char *path = _fios_path;
|
2005-03-11 14:14:54 +00:00
|
|
|
char *s;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-09 16:56:26 +00:00
|
|
|
switch (item->type) {
|
2005-03-11 14:14:54 +00:00
|
|
|
case FIOS_TYPE_DRIVE:
|
2005-10-18 20:18:05 +00:00
|
|
|
sprintf(path, "%c:\\", item->title[0]);
|
2005-03-11 14:14:54 +00:00
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
case FIOS_TYPE_PARENT:
|
|
|
|
s = strrchr(path, '\\');
|
2005-10-18 20:18:05 +00:00
|
|
|
if (s != path + 2)
|
|
|
|
s[0] = '\0';
|
|
|
|
else
|
|
|
|
s[1] = '\0';
|
2005-03-11 14:14:54 +00:00
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
case FIOS_TYPE_DIR:
|
2005-08-12 06:37:48 +00:00
|
|
|
if (path[3] != '\0') strcat(path, "\\");
|
2005-03-11 14:14:54 +00:00
|
|
|
strcat(path, item->name);
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-10-28 00:09:59 +00:00
|
|
|
case FIOS_TYPE_DIRECT:
|
|
|
|
sprintf(path, "%s\\", item->name);
|
2005-10-28 00:18:57 +00:00
|
|
|
s = strrchr(path, '\\');
|
|
|
|
if (s[1] == '\0') s[0] = '\0'; // strip trailing slash
|
2005-10-28 00:09:59 +00:00
|
|
|
break;
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
case FIOS_TYPE_FILE:
|
|
|
|
case FIOS_TYPE_OLDFILE:
|
|
|
|
case FIOS_TYPE_SCENARIO:
|
|
|
|
case FIOS_TYPE_OLD_SCENARIO: {
|
|
|
|
static char str_buffr[512];
|
2005-03-09 16:56:26 +00:00
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
sprintf(str_buffr, "%s\\%s", path, item->name);
|
|
|
|
return str_buffr;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-03-28 13:30:51 +00:00
|
|
|
/**
|
|
|
|
* Get descriptive texts. Returns the path and free space
|
|
|
|
* left on the device
|
|
|
|
* @param path string describing the path
|
|
|
|
* @param tfs total free space in megabytes, optional (can be NULL)
|
|
|
|
* @return StringID describing the path (free space or failure)
|
|
|
|
*/
|
|
|
|
StringID FiosGetDescText(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
|
2004-08-09 17:04:08 +00:00
|
|
|
char root[4];
|
|
|
|
DWORD spc, bps, nfc, tnc;
|
2005-03-28 13:30:51 +00:00
|
|
|
StringID sid;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
*path = _fios_path;
|
|
|
|
|
2005-03-09 16:56:26 +00:00
|
|
|
sprintf(root, "%c:\\", _fios_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;
|
|
|
|
sid = STR_4005_BYTES_FREE;
|
|
|
|
} else
|
|
|
|
sid = STR_4006_UNABLE_TO_READ_DRIVE;
|
|
|
|
|
|
|
|
SetErrorMode(sem); // reset previous setting
|
|
|
|
return sid;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FiosMakeSavegameName(char *buf, const char *name)
|
|
|
|
{
|
2005-03-15 14:06:28 +00:00
|
|
|
const char* extension;
|
|
|
|
const char* period;
|
|
|
|
|
2005-03-09 16:56:26 +00:00
|
|
|
if (_game_mode == GM_EDITOR)
|
2005-03-15 14:06:28 +00:00
|
|
|
extension = ".scn";
|
2004-08-09 17:04:08 +00:00
|
|
|
else
|
2005-03-15 14:06:28 +00:00
|
|
|
extension = ".sav";
|
|
|
|
|
|
|
|
// Don't append the extension, if it is already there
|
|
|
|
period = strrchr(name, '.');
|
|
|
|
if (period != NULL && strcasecmp(period, extension) == 0) extension = "";
|
|
|
|
|
|
|
|
sprintf(buf, "%s\\%s%s", _fios_path, name, extension);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-08-01 00:14:22 +00:00
|
|
|
bool FiosDelete(const char *name)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-02-07 12:32:35 +00:00
|
|
|
char path[512];
|
|
|
|
|
2005-03-12 09:24:27 +00:00
|
|
|
snprintf(path, lengthof(path), "%s\\%s", _fios_path, name);
|
2005-08-01 00:14:22 +00:00
|
|
|
return DeleteFile(path) != 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileExists(const char *filename)
|
|
|
|
{
|
|
|
|
HANDLE hand = CreateFile(filename, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
|
|
|
|
if (hand == INVALID_HANDLE_VALUE) return false;
|
|
|
|
CloseHandle(hand);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int CDECL LanguageCompareFunc(const void *a, const void *b)
|
|
|
|
{
|
2004-09-12 21:49:38 +00:00
|
|
|
return strcmp(*(const char* const *)a, *(const char* const *)b);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int GetLanguageList(char **languages, int max)
|
|
|
|
{
|
|
|
|
HANDLE hand;
|
|
|
|
int num = 0;
|
|
|
|
char filedir[MAX_PATH];
|
|
|
|
WIN32_FIND_DATA fd;
|
|
|
|
sprintf(filedir, "%s*.lng", _path.lang_dir);
|
|
|
|
|
|
|
|
hand = FindFirstFile(filedir, &fd);
|
|
|
|
if (hand != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
|
|
|
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
|
|
|
languages[num++] = strdup(fd.cFileName);
|
|
|
|
if (num == max) break;
|
|
|
|
}
|
|
|
|
} while (FindNextFile(hand, &fd));
|
|
|
|
FindClose(hand);
|
|
|
|
}
|
|
|
|
|
|
|
|
qsort(languages, num, sizeof(char*), LanguageCompareFunc);
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ParseCommandLine(char *line, char **argv, int max_argc)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
// skip whitespace
|
|
|
|
while (*line == ' ' || *line == '\t')
|
|
|
|
line++;
|
|
|
|
|
|
|
|
// end?
|
2005-03-09 16:56:26 +00:00
|
|
|
if (*line == '\0')
|
2004-08-09 17:04:08 +00:00
|
|
|
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-10-02 22:39:56 +00:00
|
|
|
#if defined(_MSC_VER) && !defined(_M_AMD64)
|
|
|
|
uint64 _declspec(naked) _rdtsc(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
_asm {
|
|
|
|
rdtsc
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (_has_console)
|
|
|
|
puts(str);
|
|
|
|
else {
|
|
|
|
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);
|
|
|
|
if (MessageBoxA(GetActiveWindow(), str, "OpenTTD", MB_ICONINFORMATION | MB_OKCANCEL) == IDCANCEL) {
|
|
|
|
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
|
|
|
|
2005-03-09 16:56:26 +00:00
|
|
|
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
|
|
|
LPTSTR lpCmdLine, int nCmdShow)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int argc;
|
|
|
|
char *argv[64]; // max 64 command line arguments
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
// setup random seed to something quite random
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
{
|
2005-10-02 22:39:56 +00:00
|
|
|
ULARGE_INTEGER seed; seed.QuadPart = _rdtsc();
|
|
|
|
_random_seeds[0][0] = seed.LowPart;
|
|
|
|
_random_seeds[0][1] = seed.HighPart;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
#else
|
2004-09-11 19:34:11 +00:00
|
|
|
_random_seeds[0][0] = GetTickCount();
|
|
|
|
_random_seeds[0][1] = _random_seeds[0][0] * 0x1234567;
|
2004-08-09 17:04:08 +00:00
|
|
|
#endif
|
2005-07-15 20:29:06 +00:00
|
|
|
SeedMT(_random_seeds[0][0]);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
argc = ParseCommandLine(GetCommandLine(), argv, lengthof(argv));
|
|
|
|
|
|
|
|
#if defined(WIN32_EXCEPTION_TRACKER)
|
2004-09-10 19:02:27 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
Win32InitializeExceptions();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WIN32_EXCEPTION_TRACKER_DEBUG)
|
|
|
|
_try {
|
|
|
|
uint32 _stdcall ExceptionHandler(void *ep);
|
|
|
|
#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
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
char *cfg;
|
|
|
|
|
|
|
|
_path.personal_dir = _path.game_data_dir = cfg = malloc(MAX_PATH);
|
|
|
|
GetCurrentDirectory(MAX_PATH - 1, cfg);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-10-28 00:09:59 +00:00
|
|
|
cfg[0] = toupper(cfg[0]);
|
2004-08-09 17:04:08 +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
|
|
|
|
|
|
|
_path.save_dir = str_fmt("%ssave", cfg);
|
|
|
|
_path.autosave_dir = str_fmt("%s\\autosave", _path.save_dir);
|
|
|
|
_path.scenario_dir = str_fmt("%sscenario", cfg);
|
|
|
|
_path.gm_dir = str_fmt("%sgm\\", cfg);
|
|
|
|
_path.data_dir = str_fmt("%sdata\\", cfg);
|
|
|
|
_path.lang_dir = str_fmt("%slang\\", cfg);
|
|
|
|
|
2005-03-10 21:44:17 +00:00
|
|
|
if (_config_file == NULL)
|
|
|
|
_config_file = str_fmt("%sopenttd.cfg", _path.personal_dir);
|
|
|
|
|
2005-01-11 00:54:06 +00:00
|
|
|
_highscore_file = str_fmt("%shs.dat", _path.personal_dir);
|
2004-12-18 14:19:21 +00:00
|
|
|
_log_file = str_fmt("%sopenttd.log", _path.personal_dir);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// make (auto)save and scenario folder
|
|
|
|
CreateDirectory(_path.save_dir, NULL);
|
|
|
|
CreateDirectory(_path.autosave_dir, NULL);
|
|
|
|
CreateDirectory(_path.scenario_dir, NULL);
|
|
|
|
}
|
|
|
|
|
2005-01-15 15:48:05 +00:00
|
|
|
int CDECL snprintf(char *str, size_t size, const char *format, ...)
|
2004-12-04 17:54:56 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
ret = vsnprintf(str, size, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-12-16 22:01:38 +00:00
|
|
|
#ifdef _MSC_VER
|
2005-10-02 22:39:56 +00:00
|
|
|
#if _MSC_VER < 1400 /* Already defined in VS 2005 */
|
2005-01-15 15:48:05 +00:00
|
|
|
int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap)
|
2004-12-04 17:54:56 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = _vsnprintf(str, size, format, ap);
|
|
|
|
if (ret < 0) str[size - 1] = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
2005-10-02 22:39:56 +00:00
|
|
|
#endif
|
2005-12-16 22:01:38 +00:00
|
|
|
#endif
|
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)
|
|
|
|
{
|
|
|
|
if (IsClipboardFormatAvailable(CF_TEXT)) {
|
|
|
|
HGLOBAL cbuf;
|
|
|
|
const byte *data, *dataptr;
|
|
|
|
uint16 width = 0;
|
|
|
|
uint16 length = 0;
|
|
|
|
|
|
|
|
OpenClipboard(NULL);
|
|
|
|
cbuf = GetClipboardData(CF_TEXT);
|
|
|
|
data = GlobalLock(cbuf); // clipboard data
|
|
|
|
dataptr = data;
|
|
|
|
|
|
|
|
for (; IsValidAsciiChar(*dataptr) && (tb->length + length) < tb->maxlength - 1 &&
|
|
|
|
(tb->maxwidth == 0 || width + tb->width + GetCharacterWidth((byte)*dataptr) <= tb->maxwidth); dataptr++) {
|
|
|
|
width += GetCharacterWidth((byte)*dataptr);
|
|
|
|
length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
memmove(tb->buf + tb->caretpos + length, tb->buf + tb->caretpos, tb->length - tb->caretpos);
|
|
|
|
memcpy(tb->buf + tb->caretpos, data, length);
|
|
|
|
tb->width += width;
|
|
|
|
tb->caretxoffs += width;
|
|
|
|
|
|
|
|
tb->length += length;
|
|
|
|
tb->caretpos += length;
|
|
|
|
tb->buf[tb->length + 1] = '\0'; // terminating zero
|
|
|
|
|
|
|
|
GlobalUnlock(cbuf);
|
|
|
|
CloseClipboard();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
}
|