src/win32.cpp
author translators
Wed, 17 Dec 2008 18:01:19 +0000
changeset 10433 45f92593bd6e
parent 10310 ca2eb5811a07
permissions -rw-r--r--
(svn r14686) -Update: WebTranslator2 update to 2008-12-17 18:01:03
arabic_egypt - 200 fixed, 28 changed by Azoo4oozi (228)
czech - 3 changed by Hadez (3)
indonesian - 1 fixed, 4 changed by fanioz (5)
korean - 1 changed by leejaeuk5 (1)
latvian - 299 fixed, 3 changed by marismols (302)
lithuanian - 79 fixed by Zogg (79)
serbian - 25 fixed by Jenraux (25)
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2184
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2184
diff changeset
     2
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6089
diff changeset
     3
/** @file win32.cpp Implementation of MS Windows system calls */
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6089
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1885
diff changeset
     6
#include "openttd.h"
1302
29f313f85ec5 (svn r1806) Add missing includes (see r1803)
tron
parents: 1301
diff changeset
     7
#include "debug.h"
2162
b98e430ffd10 (svn r2672) Move saving/loading related declarations to saveload.h
tron
parents: 2160
diff changeset
     8
#include "saveload.h"
8125
6b033dc26af4 (svn r11686) -Fix (r11684): win32 compilation was broken again :)
glx
parents: 8122
diff changeset
     9
#include "gfx_func.h"
8107
bb7deea89175 (svn r11668) -Codechange: more refactoring aimed at reducing compile time and making it more logic where function definitions can be found.
rubidium
parents: 8106
diff changeset
    10
#include "textbuf_gui.h"
10040
ea25953ada79 (svn r14200) -Fix (r14199): some OSes failed to compile. Furthermore I hate wrong comments!
rubidium
parents: 10017
diff changeset
    11
#include "fileio_func.h"
ea25953ada79 (svn r14200) -Fix (r14199): some OSes failed to compile. Furthermore I hate wrong comments!
rubidium
parents: 10017
diff changeset
    12
#include "fios.h"
9085
8b3897a75aed (svn r12944) -Codechange: use rev.h instead of externs at many places
smatz
parents: 8970
diff changeset
    13
#include "rev.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include <windows.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include <winnt.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include <wininet.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#include <io.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
#include <fcntl.h>
5304
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
    19
#include <shlobj.h> // SHGetFolderPath
2160
981e2e09cb2a (svn r2670) Fix r2669
tron
parents: 2140
diff changeset
    20
#include "variables.h"
2183
b88b90aba8bc (svn r2697) Make compiling with SDL on Windows work again (missing #includes) and fix some warnings
tron
parents: 2177
diff changeset
    21
#include "win32.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8125
diff changeset
    22
#include "core/alloc_func.hpp"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
    23
#include "functions.h"
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
    24
#include "core/random_func.hpp"
8216
3e467b27f7ce (svn r11779) -Fix: win32 compile failed. Also removed a duplicate include
glx
parents: 8214
diff changeset
    25
#include "core/bitmath_func.hpp"
3e467b27f7ce (svn r11779) -Fix: win32 compile failed. Also removed a duplicate include
glx
parents: 8214
diff changeset
    26
#include "string_func.h"
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
    27
#include "gamelog.h"
4210
654309f99970 (svn r5698) - Fix [r5697]: don't remove ctype include for win32.c; needed for toupper...ugh
Darkvater
parents: 4209
diff changeset
    28
#include <ctype.h>
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
    29
#include <tchar.h>
4218
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
    30
#include <errno.h>
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
    31
#include <sys/types.h>
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
    32
#include <sys/stat.h>
7457
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
    33
#if defined(_MSC_VER) && !defined(WINCE)
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
    34
	#include <dbghelp.h>
9638
60ab041499cf (svn r13701) -Codechange: add some more info in crash.log
glx
parents: 9552
diff changeset
    35
	#include "strings_func.h"
7457
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
    36
#endif
2171
60334c9ca477 (svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents: 2163
diff changeset
    37
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
static bool _has_console;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
2208
4d1cc67d7cbb (svn r2726) Fix last commit
tron
parents: 2207
diff changeset
    40
static bool cursor_visible = true;
4d1cc67d7cbb (svn r2726) Fix last commit
tron
parents: 2207
diff changeset
    41
2207
be1ef1e1fc58 (svn r2725) Move MyShowCursor() back into win32.c, it fits better there
tron
parents: 2195
diff changeset
    42
bool MyShowCursor(bool show)
be1ef1e1fc58 (svn r2725) Move MyShowCursor() back into win32.c, it fits better there
tron
parents: 2195
diff changeset
    43
{
2209
ef0762882a63 (svn r2727) Fix last commit, don't ask
tron
parents: 2208
diff changeset
    44
	if (cursor_visible == show) return show;
2207
be1ef1e1fc58 (svn r2725) Move MyShowCursor() back into win32.c, it fits better there
tron
parents: 2195
diff changeset
    45
2209
ef0762882a63 (svn r2727) Fix last commit, don't ask
tron
parents: 2208
diff changeset
    46
	cursor_visible = show;
2207
be1ef1e1fc58 (svn r2725) Move MyShowCursor() back into win32.c, it fits better there
tron
parents: 2195
diff changeset
    47
	ShowCursor(show);
be1ef1e1fc58 (svn r2725) Move MyShowCursor() back into win32.c, it fits better there
tron
parents: 2195
diff changeset
    48
be1ef1e1fc58 (svn r2725) Move MyShowCursor() back into win32.c, it fits better there
tron
parents: 2195
diff changeset
    49
	return !show;
be1ef1e1fc58 (svn r2725) Move MyShowCursor() back into win32.c, it fits better there
tron
parents: 2195
diff changeset
    50
}
be1ef1e1fc58 (svn r2725) Move MyShowCursor() back into win32.c, it fits better there
tron
parents: 2195
diff changeset
    51
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
    52
/** Helper function needed by dynamically loading libraries
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
    53
 * XXX: Hurray for MS only having an ANSI GetProcAddress function
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
    54
 * on normal windows and no Wide version except for in Windows Mobile/CE */
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
    55
bool LoadLibraryList(Function proc[], const char *dll)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
{
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    57
	while (*dll != '\0') {
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
    58
		HMODULE lib;
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
    59
		lib = LoadLibrary(MB_TO_WIDE(dll));
2183
b88b90aba8bc (svn r2697) Make compiling with SDL on Windows work again (missing #includes) and fix some warnings
tron
parents: 2177
diff changeset
    60
b88b90aba8bc (svn r2697) Make compiling with SDL on Windows work again (missing #includes) and fix some warnings
tron
parents: 2177
diff changeset
    61
		if (lib == NULL) return false;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3799
diff changeset
    62
		for (;;) {
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3799
diff changeset
    63
			FARPROC p;
2183
b88b90aba8bc (svn r2697) Make compiling with SDL on Windows work again (missing #includes) and fix some warnings
tron
parents: 2177
diff changeset
    64
9215
33a4cddae7d1 (svn r13081) -Fix: Empty while-loop warnings from gcc 4.3+
glx
parents: 9156
diff changeset
    65
			while (*dll++ != '\0') { /* Nothing */ }
2183
b88b90aba8bc (svn r2697) Make compiling with SDL on Windows work again (missing #includes) and fix some warnings
tron
parents: 2177
diff changeset
    66
			if (*dll == '\0') break;
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
    67
#if defined(WINCE)
5758
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
    68
			p = GetProcAddress(lib, MB_TO_WIDE(dll));
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
    69
#else
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
			p = GetProcAddress(lib, dll);
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
    71
#endif
2183
b88b90aba8bc (svn r2697) Make compiling with SDL on Windows work again (missing #includes) and fix some warnings
tron
parents: 2177
diff changeset
    72
			if (p == NULL) return false;
b88b90aba8bc (svn r2697) Make compiling with SDL on Windows work again (missing #includes) and fix some warnings
tron
parents: 2177
diff changeset
    73
			*proc++ = (Function)p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    74
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
		dll++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
796
ffa693752831 (svn r1266) -Fix: fix some cygwin/mingw warnings
darkvater
parents: 792
diff changeset
    80
#ifdef _MSC_VER
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
    81
static const char *_exception_string = NULL;
8051
0cb6e84eb367 (svn r11611) -Codechange: it is now possible to use a define to enable asserts and show them in crash.log for MSVC release builds
glx
parents: 8037
diff changeset
    82
void SetExceptionString(const char *s, ...)
0cb6e84eb367 (svn r11611) -Codechange: it is now possible to use a define to enable asserts and show them in crash.log for MSVC release builds
glx
parents: 8037
diff changeset
    83
{
0cb6e84eb367 (svn r11611) -Codechange: it is now possible to use a define to enable asserts and show them in crash.log for MSVC release builds
glx
parents: 8037
diff changeset
    84
	va_list va;
0cb6e84eb367 (svn r11611) -Codechange: it is now possible to use a define to enable asserts and show them in crash.log for MSVC release builds
glx
parents: 8037
diff changeset
    85
	char buf[512];
0cb6e84eb367 (svn r11611) -Codechange: it is now possible to use a define to enable asserts and show them in crash.log for MSVC release builds
glx
parents: 8037
diff changeset
    86
0cb6e84eb367 (svn r11611) -Codechange: it is now possible to use a define to enable asserts and show them in crash.log for MSVC release builds
glx
parents: 8037
diff changeset
    87
	va_start(va, s);
0cb6e84eb367 (svn r11611) -Codechange: it is now possible to use a define to enable asserts and show them in crash.log for MSVC release builds
glx
parents: 8037
diff changeset
    88
	vsnprintf(buf, lengthof(buf), s, va);
0cb6e84eb367 (svn r11611) -Codechange: it is now possible to use a define to enable asserts and show them in crash.log for MSVC release builds
glx
parents: 8037
diff changeset
    89
	va_end(va);
0cb6e84eb367 (svn r11611) -Codechange: it is now possible to use a define to enable asserts and show them in crash.log for MSVC release builds
glx
parents: 8037
diff changeset
    90
0cb6e84eb367 (svn r11611) -Codechange: it is now possible to use a define to enable asserts and show them in crash.log for MSVC release builds
glx
parents: 8037
diff changeset
    91
	_exception_string = strdup(buf);
0cb6e84eb367 (svn r11611) -Codechange: it is now possible to use a define to enable asserts and show them in crash.log for MSVC release builds
glx
parents: 8037
diff changeset
    92
}
2428
a27141f22960 (svn r2954) - Force assertion message to be output to a messagebox instead to the console which MinGW32 had the habit of doing. Using undocumented (in mingw) __set_error_mode() function.
Darkvater
parents: 2338
diff changeset
    93
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9457
diff changeset
    95
void ShowOSErrorBox(const char *buf, bool system)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    96
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
	MyShowCursor(true);
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
    98
	MessageBox(GetActiveWindow(), MB_TO_WIDE(buf), _T("Error!"), MB_ICONSTOP);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6424
diff changeset
   100
/* if exception tracker is enabled, we crash here to let the exception handler handle it. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
#if defined(WIN32_EXCEPTION_TRACKER) && !defined(_DEBUG)
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9457
diff changeset
   102
	if (system) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
		_exception_string = buf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
		*(byte*)0 = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   109
#if defined(_MSC_VER) && !defined(WINCE)
2428
a27141f22960 (svn r2954) - Force assertion message to be output to a messagebox instead to the console which MinGW32 had the habit of doing. Using undocumented (in mingw) __set_error_mode() function.
Darkvater
parents: 2338
diff changeset
   110
a27141f22960 (svn r2954) - Force assertion message to be output to a messagebox instead to the console which MinGW32 had the habit of doing. Using undocumented (in mingw) __set_error_mode() function.
Darkvater
parents: 2338
diff changeset
   111
static void *_safe_esp;
a27141f22960 (svn r2954) - Force assertion message to be output to a messagebox instead to the console which MinGW32 had the habit of doing. Using undocumented (in mingw) __set_error_mode() function.
Darkvater
parents: 2338
diff changeset
   112
static char *_crash_msg;
a27141f22960 (svn r2954) - Force assertion message to be output to a messagebox instead to the console which MinGW32 had the habit of doing. Using undocumented (in mingw) __set_error_mode() function.
Darkvater
parents: 2338
diff changeset
   113
static bool _expanded;
a27141f22960 (svn r2954) - Force assertion message to be output to a messagebox instead to the console which MinGW32 had the habit of doing. Using undocumented (in mingw) __set_error_mode() function.
Darkvater
parents: 2338
diff changeset
   114
static bool _did_emerg_save;
a27141f22960 (svn r2954) - Force assertion message to be output to a messagebox instead to the console which MinGW32 had the habit of doing. Using undocumented (in mingw) __set_error_mode() function.
Darkvater
parents: 2338
diff changeset
   115
static int _ident;
a27141f22960 (svn r2954) - Force assertion message to be output to a messagebox instead to the console which MinGW32 had the habit of doing. Using undocumented (in mingw) __set_error_mode() function.
Darkvater
parents: 2338
diff changeset
   116
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   117
struct DebugFileInfo {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
	uint32 size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
	uint32 crc32;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
	SYSTEMTIME file_time;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   121
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
static uint32 *_crc_table;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
7321
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 7171
diff changeset
   125
static void MakeCRCTable(uint32 *table)
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 7171
diff changeset
   126
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
	uint32 crc, poly = 0xEDB88320L;
2026
567e3bc9af72 (svn r2535) Tabs
tron
parents: 1995
diff changeset
   128
	int i;
567e3bc9af72 (svn r2535) Tabs
tron
parents: 1995
diff changeset
   129
	int j;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   130
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
	_crc_table = table;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   133
	for (i = 0; i != 256; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
		crc = i;
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   135
		for (j = 8; j != 0; j--) {
4078
7a8f56da0af0 (svn r5393) CodeChange : Little typo correction. Nothing spectacular. Only broke in MSVC
belugas
parents: 4077
diff changeset
   136
			crc = (crc & 1 ? (crc >> 1) ^ poly : crc >> 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
		table[i] = crc;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
7321
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 7171
diff changeset
   142
static uint32 CalcCRC(byte *data, uint size, uint32 crc)
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 7171
diff changeset
   143
{
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   144
	for (; size > 0; size--) {
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   145
		crc = ((crc >> 8) & 0x00FFFFFF) ^ _crc_table[(crc ^ *data++) & 0xFF];
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   146
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   147
	return crc;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   148
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   150
static void GetFileInfo(DebugFileInfo *dfi, const TCHAR *filename)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
{
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   152
	HANDLE file;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
	memset(dfi, 0, sizeof(dfi));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   155
	file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   156
	if (file != INVALID_HANDLE_VALUE) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
		byte buffer[1024];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
		DWORD numread;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   159
		uint32 filesize = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
		FILETIME write_time;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
		uint32 crc = (uint32)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   163
		for (;;) {
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   164
			if (ReadFile(file, buffer, sizeof(buffer), &numread, NULL) == 0 || numread == 0)
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   165
				break;
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   166
			filesize += numread;
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   167
			crc = CalcCRC(buffer, numread, crc);
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   168
		}
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   169
		dfi->size = filesize;
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   170
		dfi->crc32 = crc ^ (uint32)-1;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   171
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   172
		if (GetFileTime(file, NULL, NULL, &write_time)) {
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   173
			FileTimeToSystemTime(&write_time, &dfi->file_time);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
		}
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   175
		CloseHandle(file);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   180
static char *PrintModuleInfo(char *output, const char *last, HMODULE mod)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
{
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   182
	TCHAR buffer[MAX_PATH];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
	DebugFileInfo dfi;
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   184
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
	GetModuleFileName(mod, buffer, MAX_PATH);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
	GetFileInfo(&dfi, buffer);
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   187
	output += seprintf(output, last, " %-20s handle: %p size: %d crc: %.8X date: %d-%.2d-%.2d %.2d:%.2d:%.2d\r\n",
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   188
		WIDE_TO_MB(buffer),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
		mod,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   190
		dfi.size,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   191
		dfi.crc32,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   192
		dfi.file_time.wYear,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
		dfi.file_time.wMonth,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   194
		dfi.file_time.wDay,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   195
		dfi.file_time.wHour,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
		dfi.file_time.wMinute,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   197
		dfi.file_time.wSecond
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
	);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
	return output;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   202
static char *PrintModuleList(char *output, const char *last)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
{
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   204
	BOOL (WINAPI *EnumProcessModules)(HANDLE, HMODULE*, DWORD, LPDWORD);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   206
	if (LoadLibraryList((Function*)&EnumProcessModules, "psapi.dll\0EnumProcessModules\0\0")) {
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   207
		HMODULE modules[100];
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   208
		DWORD needed;
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   209
		BOOL res;
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   210
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   211
		HANDLE proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   212
		if (proc != NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   213
			res = EnumProcessModules(proc, modules, sizeof(modules), &needed);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   214
			CloseHandle(proc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
			if (res) {
9146
dbe2317185eb (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 9121
diff changeset
   216
				size_t count = min(needed / sizeof(HMODULE), lengthof(modules));
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   217
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   218
				for (size_t i = 0; i != count; i++) output = PrintModuleInfo(output, last, modules[i]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
				return output;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   222
	}
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   223
	output = PrintModuleInfo(output, last, NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
	return output;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   225
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   227
static const TCHAR _crash_desc[] =
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   228
	_T("A serious fault condition occured in the game. The game will shut down.\n")
5530
24c52cff1a34 (svn r7843) -Codechange (r7840): left out word 'file' making crash-text look un-english-ish.
Darkvater
parents: 5529
diff changeset
   229
	_T("Please send the crash information and the crash.dmp file (if any) to the developers.\n")
5529
25e90c3f693d (svn r7840) -Codechange: Change the crash window a bit. Tell about Flyspray and crash.dmp. Remove
Darkvater
parents: 5475
diff changeset
   230
	_T("This will greatly help debugging. The correct place to do this is http://bugs.openttd.org. ")
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   231
	_T("The information contained in the report is displayed below.\n")
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   232
	_T("Press \"Emergency save\" to attempt saving the game.");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   234
static const TCHAR _save_succeeded[] =
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   235
	_T("Emergency save succeeded.\n")
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   236
	_T("Be aware that critical parts of the internal game state may have become ")
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   237
	_T("corrupted. The saved game is not guaranteed to work.");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   238
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6240
diff changeset
   239
static bool EmergencySave()
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1829
diff changeset
   240
{
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
   241
	SaveOrLoad("crash.sav", SL_SAVE, BASE_DIR);
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1829
diff changeset
   242
	return true;
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1829
diff changeset
   243
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   244
5311
109d82ecab59 (svn r7467) -Cleanup: [win32] Disable the submit-savegame-on-crash code as we (webserver)
Darkvater
parents: 5310
diff changeset
   245
/* Disable the crash-save submit code as it's not used */
109d82ecab59 (svn r7467) -Cleanup: [win32] Disable the submit-savegame-on-crash code as we (webserver)
Darkvater
parents: 5310
diff changeset
   246
#if 0
109d82ecab59 (svn r7467) -Cleanup: [win32] Disable the submit-savegame-on-crash code as we (webserver)
Darkvater
parents: 5310
diff changeset
   247
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   248
struct WinInetProcs {
8969
6d1c74e0e2cd (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents: 8216
diff changeset
   249
	HINTERNET (WINAPI *InternetOpen)(LPCTSTR, DWORD, LPCTSTR, LPCTSTR, DWORD);
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   250
	HINTERNET (WINAPI *InternetConnect)(HINTERNET, LPCTSTR, INTERNET_PORT, LPCTSTR, LPCTSTR, DWORD, DWORD, DWORD);
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   251
	HINTERNET (WINAPI *HttpOpenRequest)(HINTERNET, LPCTSTR, LPCTSTR, LPCTSTR, LPCTSTR, LPCTSTR *, DWORD, DWORD);
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   252
	BOOL (WINAPI *HttpSendRequest)(HINTERNET, LPCTSTR, DWORD, LPVOID, DWORD);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
	BOOL (WINAPI *InternetCloseHandle)(HINTERNET);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   254
	BOOL (WINAPI *HttpQueryInfo)(HINTERNET, DWORD, LPVOID, LPDWORD, LPDWORD);
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   255
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   257
#define M(x) x "\0"
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   258
#if defined(UNICODE)
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   259
# define W(x) x "W"
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   260
#else
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   261
# define W(x) x "A"
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   262
#endif
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   263
static const char wininet_files[] =
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   264
	M("wininet.dll")
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   265
	M(W("InternetOpen"))
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   266
	M(W("InternetConnect"))
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   267
	M(W("HttpOpenRequest"))
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   268
	M(W("HttpSendRequest"))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
	M("InternetCloseHandle")
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   270
	M(W("HttpQueryInfo"))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   271
	M("");
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   272
#undef W
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
#undef M
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   274
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   275
static WinInetProcs _wininet;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   277
static const TCHAR *SubmitCrashReport(HWND wnd, void *msg, size_t msglen, const TCHAR *arg)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   278
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   279
	HINTERNET inet, conn, http;
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   280
	const TCHAR *err = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
	DWORD code, len;
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   282
	static TCHAR buf[100];
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   283
	TCHAR buff[100];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   285
	if (_wininet.InternetOpen == NULL && !LoadLibraryList((Function*)&_wininet, wininet_files)) return _T("can't load wininet.dll");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   287
	inet = _wininet.InternetOpen(_T("OTTD"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   288
	if (inet == NULL) { err = _T("internetopen failed"); goto error1; }
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   289
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   290
	conn = _wininet.InternetConnect(inet, _T("www.openttd.org"), INTERNET_DEFAULT_HTTP_PORT, _T(""), _T(""), INTERNET_SERVICE_HTTP, 0, 0);
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   291
	if (conn == NULL) { err = _T("internetconnect failed"); goto error2; }
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   292
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   293
	_sntprintf(buff, lengthof(buff), _T("/crash.php?file=%s&ident=%d"), arg, _ident);
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   294
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   295
	http = _wininet.HttpOpenRequest(conn, _T("POST"), buff, NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE , 0);
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   296
	if (http == NULL) { err = _T("httpopenrequest failed"); goto error3; }
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   297
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   298
	if (!_wininet.HttpSendRequest(http, _T("Content-type: application/binary"), -1, msg, (DWORD)msglen)) { err = _T("httpsendrequest failed"); goto error4; }
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   299
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
	len = sizeof(code);
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   301
	if (!_wininet.HttpQueryInfo(http, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &code, &len, 0)) { err = _T("httpqueryinfo failed"); goto error4; }
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   302
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   303
	if (code != 200) {
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   304
		int l = _sntprintf(buf, lengthof(buf), _T("Server said: %d "), code);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
		len = sizeof(buf) - l;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
		_wininet.HttpQueryInfo(http, HTTP_QUERY_STATUS_TEXT, buf + l, &len, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
		err = buf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   308
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
error4:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   311
	_wininet.InternetCloseHandle(http);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   312
error3:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   313
	_wininet.InternetCloseHandle(conn);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   314
error2:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   315
	_wininet.InternetCloseHandle(inet);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
error1:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   317
	return err;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   318
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   320
static void SubmitFile(HWND wnd, const TCHAR *file)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   322
	HANDLE h;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
	unsigned long size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   324
	unsigned long read;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   325
	void *mem;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   326
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   327
	h = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   328
	if (h == NULL) return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   330
	size = GetFileSize(h, NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
	if (size > 500000) goto error1;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   332
8037
8aa4ace04383 (svn r11597) -Change: replace all remaining instances of (re|m|c)alloc with (Re|M|C)allocT and add a check for out-of-memory situations to the *allocT functions.
rubidium
parents: 7935
diff changeset
   333
	mem = MallocT<byte>(size);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   334
	if (mem == NULL) goto error1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   335
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   336
	if (!ReadFile(h, mem, size, &read, NULL) || read != size) goto error2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   337
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   338
	SubmitCrashReport(wnd, mem, size, file);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   340
error2:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   341
	free(mem);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   342
error1:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   343
	CloseHandle(h);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   345
5311
109d82ecab59 (svn r7467) -Cleanup: [win32] Disable the submit-savegame-on-crash code as we (webserver)
Darkvater
parents: 5310
diff changeset
   346
#endif /* Disabled crash-submit procedures */
109d82ecab59 (svn r7467) -Cleanup: [win32] Disable the submit-savegame-on-crash code as we (webserver)
Darkvater
parents: 5310
diff changeset
   347
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   348
static const TCHAR * const _expand_texts[] = {_T("S&how report >>"), _T("&Hide report <<") };
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   349
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
static void SetWndSize(HWND wnd, int mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   351
{
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6443
diff changeset
   352
	RECT r, r2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
	int offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
	GetWindowRect(wnd, &r);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   356
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
	SetDlgItemText(wnd, 15, _expand_texts[mode == 1]);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   358
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   359
	if (mode >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   360
		GetWindowRect(GetDlgItem(wnd, 11), &r2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   361
		offs = r2.bottom - r2.top + 10;
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   362
		if (!mode) offs = -offs;
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   363
		SetWindowPos(wnd, HWND_TOPMOST, 0, 0,
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   364
			r.right - r.left, r.bottom - r.top + offs, SWP_NOMOVE | SWP_NOZORDER);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   365
	} else {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   366
		SetWindowPos(wnd, HWND_TOPMOST,
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   367
			(GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2,
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   368
			(GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
			0, 0, SWP_NOSIZE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   370
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   371
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
static bool DoEmergencySave(HWND wnd)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
	bool b = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
	EnableWindow(GetDlgItem(wnd, 13), FALSE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   378
	_did_emerg_save = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   379
	__try {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
		b = EmergencySave();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
	} __except (1) {}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
	return b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   383
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6443
diff changeset
   385
static INT_PTR CALLBACK CrashDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
{
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2940
diff changeset
   387
	switch (msg) {
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   388
		case WM_INITDIALOG: {
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   389
#if defined(UNICODE)
5312
ce01037b54f1 (svn r7468) -Codechange: [win32] Add some comments to MB/WIDE_TO_WIDE/MB_[BUFFER] macros and
Darkvater
parents: 5311
diff changeset
   390
			/* We need to put the crash-log in a seperate buffer because the default
5722
62abdb0d0662 (svn r8239) -Regression (r7278): Help window was empty for UNICODE builds
Darkvater
parents: 5674
diff changeset
   391
			 * buffer in MB_TO_WIDE is not large enough (512 chars) */
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   392
			wchar_t crash_msgW[8096];
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   393
#endif
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   394
			SetDlgItemText(wnd, 10, _crash_desc);
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   395
			SetDlgItemText(wnd, 11, MB_TO_WIDE_BUFFER(_crash_msg, crash_msgW, lengthof(crash_msgW)));
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   396
			SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   397
			SetWndSize(wnd, -1);
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   398
		} return TRUE;
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   399
		case WM_COMMAND:
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   400
			switch (wParam) {
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   401
				case 12: /* Close */
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   402
					ExitProcess(0);
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   403
				case 13: /* Emergency save */
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   404
					if (DoEmergencySave(wnd)) {
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   405
						MessageBox(wnd, _save_succeeded, _T("Save successful"), MB_ICONINFORMATION);
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   406
					} else {
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   407
						MessageBox(wnd, _T("Save failed"), _T("Save failed"), MB_ICONINFORMATION);
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   408
					}
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   409
					break;
5311
109d82ecab59 (svn r7467) -Cleanup: [win32] Disable the submit-savegame-on-crash code as we (webserver)
Darkvater
parents: 5310
diff changeset
   410
/* Disable the crash-save submit code as it's not used */
109d82ecab59 (svn r7467) -Cleanup: [win32] Disable the submit-savegame-on-crash code as we (webserver)
Darkvater
parents: 5310
diff changeset
   411
#if 0
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   412
				case 14: { /* Submit crash report */
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   413
					const TCHAR *s;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   415
					SetCursor(LoadCursor(NULL, IDC_WAIT));
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   416
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   417
					s = SubmitCrashReport(wnd, _crash_msg, strlen(_crash_msg), _T(""));
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   418
					if (s != NULL) {
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   419
						MessageBox(wnd, s, _T("Error"), MB_ICONSTOP);
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   420
						break;
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   421
					}
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   422
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   423
					// try to submit emergency savegame
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   424
					if (_did_emerg_save || DoEmergencySave(wnd)) SubmitFile(wnd, _T("crash.sav"));
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   425
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   426
					// try to submit the autosaved game
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   427
					if (_opt.autosave) {
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   428
						TCHAR buf[40];
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   429
						_sntprintf(buf, lengthof(buf), _T("autosave%d.sav"), (_autosave_ctr - 1) & 3);
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   430
						SubmitFile(wnd, buf);
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   431
					}
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   432
					EnableWindow(GetDlgItem(wnd, 14), FALSE);
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   433
					SetCursor(LoadCursor(NULL, IDC_ARROW));
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   434
					MessageBox(wnd, _T("Crash report submitted. Thank you."), _T("Crash Report"), MB_ICONINFORMATION);
6492
286a52449b54 (svn r9673) -Cleanup: remove spaces before tabs and replace non-indenting tabs with spaces.
rubidium
parents: 6491
diff changeset
   435
				} break;
5311
109d82ecab59 (svn r7467) -Cleanup: [win32] Disable the submit-savegame-on-crash code as we (webserver)
Darkvater
parents: 5310
diff changeset
   436
#endif /* Disabled crash-submit procedures */
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   437
				case 15: /* Expand window to show crash-message */
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   438
					_expanded ^= 1;
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   439
					SetWndSize(wnd, _expanded);
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   440
					break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   441
			}
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   442
			return TRUE;
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   443
		case WM_CLOSE: ExitProcess(0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   444
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
	return FALSE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   448
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6240
diff changeset
   449
static void Handler2()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   451
	ShowCursor(TRUE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
	ShowWindow(GetActiveWindow(), FALSE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
	DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(100), NULL, CrashDialogFunc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6240
diff changeset
   456
extern bool CloseConsoleLogIfActive();
1023
1ae7b7a78602 (svn r1524) -"Feature": when windows exception tracker is enabled (release builds) and the game crashes for any reason any active log file is closed first. This ensures the log file and ingame debug messages can be used to debug a problem. Any *nix versions are welcome
darkvater
parents: 1022
diff changeset
   457
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   458
static HANDLE _file_crash_log;
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   459
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   460
static void GamelogPrintCrashLogProc(const char *s)
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   461
{
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   462
	DWORD num_written;
9552
738b51676005 (svn r13572) -Fix: MSVC x64 complains.
rubidium
parents: 9488
diff changeset
   463
	WriteFile(_file_crash_log, s, (DWORD)strlen(s), &num_written, NULL);
738b51676005 (svn r13572) -Fix: MSVC x64 complains.
rubidium
parents: 9488
diff changeset
   464
	WriteFile(_file_crash_log, "\r\n", (DWORD)strlen("\r\n"), &num_written, NULL);
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   465
}
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   466
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   467
/** Amount of output for the execption handler. */
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   468
static const int EXCEPTION_OUTPUT_SIZE = 8192;
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   469
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   470
static LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   471
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   472
	char *output;
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   473
	static bool had_exception = false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   475
	if (had_exception) ExitProcess(0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   476
	had_exception = true;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   477
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   478
	_ident = GetTickCount(); // something pretty unique
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   479
9488
963e9e278125 (svn r13456) -Codechange: use AllocaM() macro instead of alloca() at most places
smatz
parents: 9470
diff changeset
   480
	MakeCRCTable(AllocaM(uint32, 256));
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   481
	_crash_msg = output = (char*)LocalAlloc(LMEM_FIXED, EXCEPTION_OUTPUT_SIZE);
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   482
	const char *last = output + EXCEPTION_OUTPUT_SIZE - 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   483
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   484
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   485
		SYSTEMTIME time;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   486
		GetLocalTime(&time);
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   487
		output += seprintf(output, last,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   488
			"*** OpenTTD Crash Report ***\r\n"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   489
			"Date: %d-%.2d-%.2d %.2d:%.2d:%.2d\r\n"
4236
8f21cc1d7095 (svn r5801) - Codechange: The exception dialog showed the last modification-date of win32.c instead of the last compilation-date. Use __DATE__ __TIME__ instead of __TIMESTAMP__; only affected windows not built with mingw/cygwin.
Darkvater
parents: 4224
diff changeset
   490
			"Build: %s built on " __DATE__ " " __TIME__ "\r\n",
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
			time.wYear,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
			time.wMonth,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   493
			time.wDay,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   494
			time.wHour,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
			time.wMinute,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   496
			time.wSecond,
3204
1d8c8114e073 (svn r3872) - [win32] Show the revision in crash.txt and enable the button to show the crash text in the crash-window
Darkvater
parents: 2952
diff changeset
   497
			_openttd_revision
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
		);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   501
	if (_exception_string)
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   502
		output += seprintf(output, last, "Reason: %s\r\n", _exception_string);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   504
	output += seprintf(output, last, "Language: %s\r\n", _dynlang.curr_file);
9638
60ab041499cf (svn r13701) -Codechange: add some more info in crash.log
glx
parents: 9552
diff changeset
   505
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   506
#ifdef _M_AMD64
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   507
	output += seprintf(output, last, "Exception %.8X at %.16IX\r\n"
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   508
		"Registers:\r\n"
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   509
		"RAX: %.16llX RBX: %.16llX RCX: %.16llX RDX: %.16llX\r\n"
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   510
		"RSI: %.16llX RDI: %.16llX RBP: %.16llX RSP: %.16llX\r\n"
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   511
		"R8:  %.16llX R9:  %.16llX R10: %.16llX R11: %.16llX\r\n"
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   512
		"R12: %.16llX R13: %.16llX R14: %.16llX R15: %.16llX\r\n"
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   513
		"RIP: %.16llX EFLAGS: %.8X\r\n"
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   514
		"\r\nBytes at CS:RIP:\r\n",
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   515
		ep->ExceptionRecord->ExceptionCode,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   516
		ep->ExceptionRecord->ExceptionAddress,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   517
		ep->ContextRecord->Rax,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   518
		ep->ContextRecord->Rbx,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   519
		ep->ContextRecord->Rcx,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   520
		ep->ContextRecord->Rdx,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   521
		ep->ContextRecord->Rsi,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   522
		ep->ContextRecord->Rdi,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   523
		ep->ContextRecord->Rbp,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   524
		ep->ContextRecord->Rsp,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   525
		ep->ContextRecord->R8,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   526
		ep->ContextRecord->R9,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   527
		ep->ContextRecord->R10,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   528
		ep->ContextRecord->R11,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   529
		ep->ContextRecord->R12,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   530
		ep->ContextRecord->R13,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   531
		ep->ContextRecord->R14,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   532
		ep->ContextRecord->R15,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   533
		ep->ContextRecord->Rip,
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   534
		ep->ContextRecord->EFlags
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   535
	);
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   536
#else
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   537
	output += seprintf(output, last, "Exception %.8X at %.8p\r\n"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
		"Registers:\r\n"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
		" EAX: %.8X EBX: %.8X ECX: %.8X EDX: %.8X\r\n"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
		" ESI: %.8X EDI: %.8X EBP: %.8X ESP: %.8X\r\n"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
		" EIP: %.8X EFLAGS: %.8X\r\n"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
		"\r\nBytes at CS:EIP:\r\n",
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
		ep->ExceptionRecord->ExceptionCode,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
		ep->ExceptionRecord->ExceptionAddress,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
		ep->ContextRecord->Eax,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
		ep->ContextRecord->Ebx,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
		ep->ContextRecord->Ecx,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
		ep->ContextRecord->Edx,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   549
		ep->ContextRecord->Esi,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
		ep->ContextRecord->Edi,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
		ep->ContextRecord->Ebp,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
		ep->ContextRecord->Esp,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
		ep->ContextRecord->Eip,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
		ep->ContextRecord->EFlags
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
	);
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   556
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   558
	{
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   559
#ifdef _M_AMD64
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   560
		byte *b = (byte*)ep->ContextRecord->Rip;
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   561
#else
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   562
		byte *b = (byte*)ep->ContextRecord->Eip;
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   563
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   564
		int i;
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   565
		for (i = 0; i != 24; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
			if (IsBadReadPtr(b, 1)) {
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   567
				output += seprintf(output, last, " ??"); // OCR: WAS: , 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
			} else {
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   569
				output += seprintf(output, last, " %.2X", *b);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
			b++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
		}
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   573
		output += seprintf(output, last,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
			"\r\n"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
			"\r\nStack trace: \r\n"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
		);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
	{
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6443
diff changeset
   580
		int i, j;
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   581
#ifdef _M_AMD64
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   582
		uint32 *b = (uint32*)ep->ContextRecord->Rsp;
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   583
#else
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   584
		uint32 *b = (uint32*)ep->ContextRecord->Esp;
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   585
#endif
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   586
		for (j = 0; j != 24; j++) {
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   587
			for (i = 0; i != 8; i++) {
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6443
diff changeset
   588
				if (IsBadReadPtr(b, sizeof(uint32))) {
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   589
					output += seprintf(output, last, " ????????"); //OCR: WAS - , 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   590
				} else {
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   591
					output += seprintf(output, last, " %.8X", *b);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
				b++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   594
			}
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   595
			output += seprintf(output, last, "\r\n");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   597
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   598
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   599
	output += seprintf(output, last, "\r\nModule information:\r\n");
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   600
	output = PrintModuleList(output, last);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
	{
10186
6e6d49ff4e8c (svn r14397) -Fix: possible buffer overrun, wrong parameter type passed to printf and && where || is meant. Found by MSVC's code analysis (Darkvater)
rubidium
parents: 10040
diff changeset
   603
		_OSVERSIONINFOA os;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
		os.dwOSVersionInfoSize = sizeof(os);
10186
6e6d49ff4e8c (svn r14397) -Fix: possible buffer overrun, wrong parameter type passed to printf and && where || is meant. Found by MSVC's code analysis (Darkvater)
rubidium
parents: 10040
diff changeset
   605
		GetVersionExA(&os);
10301
76966696a338 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium
parents: 10280
diff changeset
   606
		output += seprintf(output, last, "\r\nSystem information:\r\n"
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   607
			" Windows version %d.%d %d %s\r\n\r\n",
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   608
			os.dwMajorVersion, os.dwMinorVersion, os.dwBuildNumber, os.szCSDVersion);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   611
	_file_crash_log = CreateFile(_T("crash.log"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   612
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   613
	if (_file_crash_log != INVALID_HANDLE_VALUE) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
		DWORD num_written;
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   615
		WriteFile(_file_crash_log, _crash_msg, output - _crash_msg, &num_written, NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
7457
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   618
#if !defined(_DEBUG)
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   619
	HMODULE dbghelp = LoadLibrary(_T("dbghelp.dll"));
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   620
	if (dbghelp != NULL) {
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   621
		typedef BOOL (WINAPI *MiniDumpWriteDump_t)(HANDLE, DWORD, HANDLE,
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   622
				MINIDUMP_TYPE,
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   623
				CONST PMINIDUMP_EXCEPTION_INFORMATION,
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   624
				CONST PMINIDUMP_USER_STREAM_INFORMATION,
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   625
				CONST PMINIDUMP_CALLBACK_INFORMATION);
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   626
		MiniDumpWriteDump_t funcMiniDumpWriteDump = (MiniDumpWriteDump_t)GetProcAddress(dbghelp, "MiniDumpWriteDump");
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   627
		if (funcMiniDumpWriteDump != NULL) {
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   628
			HANDLE file  = CreateFile(_T("crash.dmp"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   629
			HANDLE proc  = GetCurrentProcess();
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   630
			DWORD procid = GetCurrentProcessId();
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   631
			MINIDUMP_EXCEPTION_INFORMATION mdei;
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   632
			MINIDUMP_USER_STREAM userstream;
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   633
			MINIDUMP_USER_STREAM_INFORMATION musi;
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   634
			char msg[] = "****** Built on " __DATE__ " " __TIME__ ". ******";
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   635
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   636
			userstream.Type        = LastReservedStream + 1;
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   637
			userstream.Buffer      = msg;
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   638
			userstream.BufferSize  = sizeof(msg);
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   639
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   640
			musi.UserStreamCount   = 1;
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   641
			musi.UserStreamArray   = &userstream;
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   642
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   643
			mdei.ThreadId = GetCurrentThreadId();
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   644
			mdei.ExceptionPointers  = ep;
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   645
			mdei.ClientPointers     = false;
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   646
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   647
			funcMiniDumpWriteDump(proc, procid, file, MiniDumpWithDataSegs, &mdei, &musi, NULL);
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   648
		}
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   649
		FreeLibrary(dbghelp);
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   650
	}
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   651
#endif
8ed8d61ace6b (svn r10948) -Add: [win32] crash.dmp support for MSVC release builds
glx
parents: 7408
diff changeset
   652
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   653
	if (_file_crash_log != INVALID_HANDLE_VALUE) {
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   654
		GamelogPrint(&GamelogPrintCrashLogProc);
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   655
		CloseHandle(_file_crash_log);
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   656
	}
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9443
diff changeset
   657
1023
1ae7b7a78602 (svn r1524) -"Feature": when windows exception tracker is enabled (release builds) and the game crashes for any reason any active log file is closed first. This ensures the log file and ingame debug messages can be used to debug a problem. Any *nix versions are welcome
darkvater
parents: 1022
diff changeset
   658
	/* Close any possible log files */
1ae7b7a78602 (svn r1524) -"Feature": when windows exception tracker is enabled (release builds) and the game crashes for any reason any active log file is closed first. This ensures the log file and ingame debug messages can be used to debug a problem. Any *nix versions are welcome
darkvater
parents: 1022
diff changeset
   659
	CloseConsoleLogIfActive();
1ae7b7a78602 (svn r1524) -"Feature": when windows exception tracker is enabled (release builds) and the game crashes for any reason any active log file is closed first. This ensures the log file and ingame debug messages can be used to debug a problem. Any *nix versions are welcome
darkvater
parents: 1022
diff changeset
   660
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   661
	if (_safe_esp) {
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   662
#ifdef _M_AMD64
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   663
		ep->ContextRecord->Rip = (DWORD64)Handler2;
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   664
		ep->ContextRecord->Rsp = (DWORD64)_safe_esp;
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   665
#else
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   666
		ep->ContextRecord->Eip = (DWORD)Handler2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   667
		ep->ContextRecord->Esp = (DWORD)_safe_esp;
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   668
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   669
		return EXCEPTION_CONTINUE_EXECUTION;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   670
	}
1023
1ae7b7a78602 (svn r1524) -"Feature": when windows exception tracker is enabled (release builds) and the game crashes for any reason any active log file is closed first. This ensures the log file and ingame debug messages can be used to debug a problem. Any *nix versions are welcome
darkvater
parents: 1022
diff changeset
   671
1ae7b7a78602 (svn r1524) -"Feature": when windows exception tracker is enabled (release builds) and the game crashes for any reason any active log file is closed first. This ensures the log file and ingame debug messages can be used to debug a problem. Any *nix versions are welcome
darkvater
parents: 1022
diff changeset
   672
1ae7b7a78602 (svn r1524) -"Feature": when windows exception tracker is enabled (release builds) and the game crashes for any reason any active log file is closed first. This ensures the log file and ingame debug messages can be used to debug a problem. Any *nix versions are welcome
darkvater
parents: 1022
diff changeset
   673
	return EXCEPTION_EXECUTE_HANDLER;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   674
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   675
6171
fb9997933c24 (svn r8933) -Fix [Win64]: rdtsc now uses intrinsic on VC8 (michi_cc)
KUDr
parents: 6165
diff changeset
   676
#ifdef _M_AMD64
10017
c0eb11a864d7 (svn r14176) -Fix: better 'safe' than ... 'save'
smatz
parents: 9638
diff changeset
   677
extern "C" void *_get_safe_esp();
6171
fb9997933c24 (svn r8933) -Fix [Win64]: rdtsc now uses intrinsic on VC8 (michi_cc)
KUDr
parents: 6165
diff changeset
   678
#endif
fb9997933c24 (svn r8933) -Fix [Win64]: rdtsc now uses intrinsic on VC8 (michi_cc)
KUDr
parents: 6165
diff changeset
   679
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6240
diff changeset
   680
static void Win32InitializeExceptions()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   681
{
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   682
#ifdef _M_AMD64
10017
c0eb11a864d7 (svn r14176) -Fix: better 'safe' than ... 'save'
smatz
parents: 9638
diff changeset
   683
	_safe_esp = _get_safe_esp();
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   684
#else
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   685
	_asm {
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   686
		mov _safe_esp, esp
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   687
	}
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   688
#endif
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   689
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   690
	SetUnhandledExceptionFilter(ExceptionHandler);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   691
}
3341
b20541ef8945 (svn r4125) - Feature: Add a general TIC() TOC() mechanism using rdtsc or something similar on non-i386 architectures to performance-tune (critical) code. Some systems are probably missing, but those can be added later.
Darkvater
parents: 3340
diff changeset
   692
#endif /* _MSC_VER */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   693
4218
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   694
/* Code below for windows version of opendir/readdir/closedir copied and
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   695
 * modified from Jan Wassenberg's GPL implementation posted over at
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   696
 * http://www.gamedev.net/community/forums/topic.asp?topic_id=364584&whichpage=1&#2398903 */
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   697
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   698
/* suballocator - satisfies most requests with a reusable static instance.
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   699
 * this avoids hundreds of alloc/free which would fragment the heap.
5891
9601f4469a37 (svn r8508) -Codechange (r5762): [win32] Use an atomic operation to query and set the value of _global_dir_is_in_use for opendir etc to guarantee concurrency.
Darkvater
parents: 5758
diff changeset
   700
 * To guarantee concurrency, we fall back to malloc if the instance is
4218
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   701
 * already in use (it's important to avoid suprises since this is such a
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   702
 * low-level routine). */
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   703
static DIR _global_dir;
5891
9601f4469a37 (svn r8508) -Codechange (r5762): [win32] Use an atomic operation to query and set the value of _global_dir_is_in_use for opendir etc to guarantee concurrency.
Darkvater
parents: 5758
diff changeset
   704
static LONG _global_dir_is_in_use = false;
4218
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   705
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6240
diff changeset
   706
static inline DIR *dir_calloc()
4218
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   707
{
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   708
	DIR *d;
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   709
5891
9601f4469a37 (svn r8508) -Codechange (r5762): [win32] Use an atomic operation to query and set the value of _global_dir_is_in_use for opendir etc to guarantee concurrency.
Darkvater
parents: 5758
diff changeset
   710
	if (InterlockedExchange(&_global_dir_is_in_use, true) == (LONG)true) {
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
   711
		d = CallocT<DIR>(1);
4218
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   712
	} else {
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   713
		d = &_global_dir;
4224
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   714
		memset(d, 0, sizeof(*d));
4218
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   715
	}
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   716
	return d;
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   717
}
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   718
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   719
static inline void dir_free(DIR *d)
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   720
{
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   721
	if (d == &_global_dir) {
5891
9601f4469a37 (svn r8508) -Codechange (r5762): [win32] Use an atomic operation to query and set the value of _global_dir_is_in_use for opendir etc to guarantee concurrency.
Darkvater
parents: 5758
diff changeset
   722
		_global_dir_is_in_use = (LONG)false;
4218
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   723
	} else {
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   724
		free(d);
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   725
	}
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   726
}
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   727
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
   728
DIR *opendir(const TCHAR *path)
4218
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   729
{
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   730
	DIR *d;
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   731
	UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
   732
	DWORD fa = GetFileAttributes(path);
4218
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   733
4224
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   734
	if ((fa != INVALID_FILE_ATTRIBUTES) && (fa & FILE_ATTRIBUTE_DIRECTORY)) {
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   735
		d = dir_calloc();
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   736
		if (d != NULL) {
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
   737
			TCHAR search_path[MAX_PATH];
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
   738
			bool slash = path[_tcslen(path) - 1] == '\\';
6165
9c245e46bb23 (svn r8919) -Regression (UTF8) (try #2): Win9x is very picky about trailing slashes in paths, so C:\\* will not work (but C:\Windows\\* does; go figure). Thanks glx for pointing it out and for the initial fix.
Darkvater
parents: 6124
diff changeset
   739
9c245e46bb23 (svn r8919) -Regression (UTF8) (try #2): Win9x is very picky about trailing slashes in paths, so C:\\* will not work (but C:\Windows\\* does; go figure). Thanks glx for pointing it out and for the initial fix.
Darkvater
parents: 6124
diff changeset
   740
			/* build search path for FindFirstFile, try not to append additional slashes
9c245e46bb23 (svn r8919) -Regression (UTF8) (try #2): Win9x is very picky about trailing slashes in paths, so C:\\* will not work (but C:\Windows\\* does; go figure). Thanks glx for pointing it out and for the initial fix.
Darkvater
parents: 6124
diff changeset
   741
			 * as it throws Win9x off its groove for root directories */
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
   742
			_sntprintf(search_path, lengthof(search_path), _T("%s%s*"), path, slash ? _T("") : _T("\\"));
6089
533364efa098 (svn r8824) -Fix (r8821): don't use MSVC2005 runtime library-only _snwprintf_s function, but use 'insecure/deprecated' version and zero-terminate string ourselves. Thanks for testing glx.
Darkvater
parents: 6086
diff changeset
   743
			*lastof(search_path) = '\0';
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
   744
			d->hFind = FindFirstFile(search_path, &d->fd);
4224
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   745
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   746
			if (d->hFind != INVALID_HANDLE_VALUE ||
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   747
					GetLastError() == ERROR_NO_MORE_FILES) { // the directory is empty
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   748
				d->ent.dir = d;
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   749
				d->at_first_entry = true;
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   750
			} else {
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   751
				dir_free(d);
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   752
				d = NULL;
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   753
			}
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   754
		} else {
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   755
			errno = ENOMEM;
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   756
		}
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   757
	} else {
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   758
		/* path not found or not a directory */
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   759
		d = NULL;
4218
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   760
		errno = ENOENT;
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   761
	}
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   762
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   763
	SetErrorMode(sem); // restore previous setting
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   764
	return d;
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   765
}
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   766
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   767
struct dirent *readdir(DIR *d)
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   768
{
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   769
	DWORD prev_err = GetLastError(); // avoid polluting last error
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   770
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   771
	if (d->at_first_entry) {
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   772
		/* the directory was empty when opened */
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   773
		if (d->hFind == INVALID_HANDLE_VALUE) return NULL;
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   774
		d->at_first_entry = false;
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
   775
	} else if (!FindNextFile(d->hFind, &d->fd)) { // determine cause and bail
4218
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   776
		if (GetLastError() == ERROR_NO_MORE_FILES) SetLastError(prev_err);
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   777
		return NULL;
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   778
	}
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   779
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   780
	/* This entry has passed all checks; return information about it.
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   781
	 * (note: d_name is a pointer; see struct dirent definition) */
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   782
	d->ent.d_name = d->fd.cFileName;
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   783
	return &d->ent;
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   784
}
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   785
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   786
int closedir(DIR *d)
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   787
{
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   788
	FindClose(d->hFind);
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   789
	dir_free(d);
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   790
	return 0;
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   791
}
4033c207d304 (svn r5762) - Codechange: Implementation of POSIX-style opendir/readdir/closedir functions for windows using FindFirstFile/FindNextFile/FindClose.
Darkvater
parents: 4210
diff changeset
   792
4221
ff4a040f30c6 (svn r5765) - Codechange: Fully unify the OS-specific FiosGet...List functions into fios.c, as well as FiosGetSavegameList and FiosGetScenarioList functions with the help of some callbacks.
Darkvater
parents: 4220
diff changeset
   793
bool FiosIsRoot(const char *file)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   794
{
4221
ff4a040f30c6 (svn r5765) - Codechange: Fully unify the OS-specific FiosGet...List functions into fios.c, as well as FiosGetSavegameList and FiosGetScenarioList functions with the help of some callbacks.
Darkvater
parents: 4220
diff changeset
   795
	return file[3] == '\0'; // C:\...
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   796
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   797
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6240
diff changeset
   798
void FiosGetDrives()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   799
{
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   800
#if defined(WINCE)
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   801
	/* WinCE only knows one drive: / */
9443
e22cf32235dd (svn r13361) -Fix (r13360): regexp failed
glx
parents: 9442
diff changeset
   802
	FiosItem *fios = _fios_items.Append();
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   803
	fios->type = FIOS_TYPE_DRIVE;
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   804
	fios->mtime = 0;
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   805
	snprintf(fios->name, lengthof(fios->name), PATHSEP "");
10310
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10301
diff changeset
   806
	strecpy(fios->title, fios->name, lastof(fios->title));
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   807
#else
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   808
	TCHAR drives[256];
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   809
	const TCHAR *s;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   810
10186
6e6d49ff4e8c (svn r14397) -Fix: possible buffer overrun, wrong parameter type passed to printf and && where || is meant. Found by MSVC's code analysis (Darkvater)
rubidium
parents: 10040
diff changeset
   811
	GetLogicalDriveStrings(lengthof(drives), drives);
4221
ff4a040f30c6 (svn r5765) - Codechange: Fully unify the OS-specific FiosGet...List functions into fios.c, as well as FiosGetSavegameList and FiosGetScenarioList functions with the help of some callbacks.
Darkvater
parents: 4220
diff changeset
   812
	for (s = drives; *s != '\0';) {
9443
e22cf32235dd (svn r13361) -Fix (r13360): regexp failed
glx
parents: 9442
diff changeset
   813
		FiosItem *fios = _fios_items.Append();
4221
ff4a040f30c6 (svn r5765) - Codechange: Fully unify the OS-specific FiosGet...List functions into fios.c, as well as FiosGetSavegameList and FiosGetScenarioList functions with the help of some callbacks.
Darkvater
parents: 4220
diff changeset
   814
		fios->type = FIOS_TYPE_DRIVE;
ff4a040f30c6 (svn r5765) - Codechange: Fully unify the OS-specific FiosGet...List functions into fios.c, as well as FiosGetSavegameList and FiosGetScenarioList functions with the help of some callbacks.
Darkvater
parents: 4220
diff changeset
   815
		fios->mtime = 0;
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   816
		snprintf(fios->name, lengthof(fios->name),  "%c:", s[0] & 0xFF);
10310
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10301
diff changeset
   817
		strecpy(fios->title, fios->name, lastof(fios->title));
9215
33a4cddae7d1 (svn r13081) -Fix: Empty while-loop warnings from gcc 4.3+
glx
parents: 9156
diff changeset
   818
		while (*s++ != '\0') { /* Nothing */ }
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
	}
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   820
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   822
4221
ff4a040f30c6 (svn r5765) - Codechange: Fully unify the OS-specific FiosGet...List functions into fios.c, as well as FiosGetSavegameList and FiosGetScenarioList functions with the help of some callbacks.
Darkvater
parents: 4220
diff changeset
   823
bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   824
{
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6424
diff changeset
   825
	/* hectonanoseconds between Windows and POSIX epoch */
4247
091fb0c37c8c (svn r5836) - Silence a warning on mingw. Thanks glx.
Darkvater
parents: 4245
diff changeset
   826
	static const int64 posix_epoch_hns = 0x019DB1DED53E8000LL;
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
   827
	const WIN32_FIND_DATA *fd = &ent->dir->fd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
4224
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   829
	sb->st_size  = ((uint64) fd->nFileSizeHigh << 32) + fd->nFileSizeLow;
4245
e8d08dbda199 (svn r5831) - Fix (r5765): regression regarding windows filetimes. st->st_mtime's type time_t is only 64bit on windows64, so we need to convert it.
Darkvater
parents: 4236
diff changeset
   830
	/* UTC FILETIME to seconds-since-1970 UTC
e8d08dbda199 (svn r5831) - Fix (r5765): regression regarding windows filetimes. st->st_mtime's type time_t is only 64bit on windows64, so we need to convert it.
Darkvater
parents: 4236
diff changeset
   831
	 * we just have to subtract POSIX epoch and scale down to units of seconds.
e8d08dbda199 (svn r5831) - Fix (r5765): regression regarding windows filetimes. st->st_mtime's type time_t is only 64bit on windows64, so we need to convert it.
Darkvater
parents: 4236
diff changeset
   832
	 * http://www.gamedev.net/community/forums/topic.asp?topic_id=294070&whichpage=1&#1860504
e8d08dbda199 (svn r5831) - Fix (r5765): regression regarding windows filetimes. st->st_mtime's type time_t is only 64bit on windows64, so we need to convert it.
Darkvater
parents: 4236
diff changeset
   833
	 * XXX - not entirely correct, since filetimes on FAT aren't UTC but local,
e8d08dbda199 (svn r5831) - Fix (r5765): regression regarding windows filetimes. st->st_mtime's type time_t is only 64bit on windows64, so we need to convert it.
Darkvater
parents: 4236
diff changeset
   834
	 * this won't entirely be correct, but we use the time only for comparsion. */
e8d08dbda199 (svn r5831) - Fix (r5765): regression regarding windows filetimes. st->st_mtime's type time_t is only 64bit on windows64, so we need to convert it.
Darkvater
parents: 4236
diff changeset
   835
	sb->st_mtime = (time_t)((*(uint64*)&fd->ftLastWriteTime - posix_epoch_hns) / 1E7);
4221
ff4a040f30c6 (svn r5765) - Codechange: Fully unify the OS-specific FiosGet...List functions into fios.c, as well as FiosGetSavegameList and FiosGetScenarioList functions with the help of some callbacks.
Darkvater
parents: 4220
diff changeset
   836
	sb->st_mode  = (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)? S_IFDIR : S_IFREG;
4224
72c4669d95fa (svn r5769) - Fix (r5762): fix some mistakes in the opendir code and filesize calculation. Thanks to Tron for noticing (and fixing).
Darkvater
parents: 4223
diff changeset
   837
4221
ff4a040f30c6 (svn r5765) - Codechange: Fully unify the OS-specific FiosGet...List functions into fios.c, as well as FiosGetSavegameList and FiosGetScenarioList functions with the help of some callbacks.
Darkvater
parents: 4220
diff changeset
   838
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   839
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   840
6240
9d9874ef9f0b (svn r9043) -Fix [FS#652]: the personal (.openttd) directories were hidden in the load/save directory listings (stepancheg).
rubidium
parents: 6195
diff changeset
   841
bool FiosIsHiddenFile(const struct dirent *ent)
9d9874ef9f0b (svn r9043) -Fix [FS#652]: the personal (.openttd) directories were hidden in the load/save directory listings (stepancheg).
rubidium
parents: 6195
diff changeset
   842
{
9d9874ef9f0b (svn r9043) -Fix [FS#652]: the personal (.openttd) directories were hidden in the load/save directory listings (stepancheg).
rubidium
parents: 6195
diff changeset
   843
	return (ent->dir->fd.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
9d9874ef9f0b (svn r9043) -Fix [FS#652]: the personal (.openttd) directories were hidden in the load/save directory listings (stepancheg).
rubidium
parents: 6195
diff changeset
   844
}
9d9874ef9f0b (svn r9043) -Fix [FS#652]: the personal (.openttd) directories were hidden in the load/save directory listings (stepancheg).
rubidium
parents: 6195
diff changeset
   845
4222
36e9bd216011 (svn r5766) - Cleanup: Unify FiosBrowseTo and FiosGetDescText
Darkvater
parents: 4221
diff changeset
   846
bool FiosGetDiskFreeSpace(const char *path, uint32 *tot)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   847
{
1596
483007886b59 (svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents: 1582
diff changeset
   848
	UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS);  // disable 'no-disk' message box
4222
36e9bd216011 (svn r5766) - Cleanup: Unify FiosBrowseTo and FiosGetDescText
Darkvater
parents: 4221
diff changeset
   849
	bool retval = false;
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   850
	TCHAR root[4];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   851
	DWORD spc, bps, nfc, tnc;
1596
483007886b59 (svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents: 1582
diff changeset
   852
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   853
	_sntprintf(root, lengthof(root), _T("%c:") _T(PATHSEP), path[0]);
1596
483007886b59 (svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents: 1582
diff changeset
   854
	if (tot != NULL && GetDiskFreeSpace(root, &spc, &bps, &nfc, &tnc)) {
483007886b59 (svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents: 1582
diff changeset
   855
		*tot = ((spc * bps) * (uint64)nfc) >> 20;
4222
36e9bd216011 (svn r5766) - Cleanup: Unify FiosBrowseTo and FiosGetDescText
Darkvater
parents: 4221
diff changeset
   856
		retval = true;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3799
diff changeset
   857
	}
1596
483007886b59 (svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents: 1582
diff changeset
   858
483007886b59 (svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents: 1582
diff changeset
   859
	SetErrorMode(sem); // reset previous setting
4222
36e9bd216011 (svn r5766) - Cleanup: Unify FiosBrowseTo and FiosGetDescText
Darkvater
parents: 4221
diff changeset
   860
	return retval;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   861
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   862
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   863
static int ParseCommandLine(char *line, char **argv, int max_argc)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   864
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   865
	int n = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   866
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   867
	do {
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6424
diff changeset
   868
		/* skip whitespace */
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   869
		while (*line == ' ' || *line == '\t') line++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   870
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6424
diff changeset
   871
		/* end? */
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   872
		if (*line == '\0') break;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   873
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6424
diff changeset
   874
		/* special handling when quoted */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   875
		if (*line == '"') {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   876
			argv[n++] = ++line;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   877
			while (*line != '"') {
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   878
				if (*line == '\0') return n;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   879
				line++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   880
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   881
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   882
			argv[n++] = line;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   883
			while (*line != ' ' && *line != '\t') {
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   884
				if (*line == '\0') return n;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   885
				line++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   886
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   887
		}
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   888
		*line++ = '\0';
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   889
	} while (n != max_argc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   890
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   891
	return n;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   892
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6240
diff changeset
   894
void CreateConsole()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   895
{
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   896
#if defined(WINCE)
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   897
	/* WinCE doesn't support console stuff */
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   898
#else
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   899
	HANDLE hand;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   900
	CONSOLE_SCREEN_BUFFER_INFO coninfo;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   901
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   902
	if (_has_console) return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
	_has_console = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   904
4572
6800fdfcd5c0 (svn r6421) -Revert r6420: The dedicated server's console actually needs to work. It misses half the input at the moment. Sorry, too hasty commit.
Darkvater
parents: 4571
diff changeset
   905
	AllocConsole();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   906
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   907
	hand = GetStdHandle(STD_OUTPUT_HANDLE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   908
	GetConsoleScreenBufferInfo(hand, &coninfo);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   909
	coninfo.dwSize.Y = 500;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   910
	SetConsoleScreenBufferSize(hand, coninfo.dwSize);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   911
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6424
diff changeset
   912
	/* redirect unbuffered STDIN, STDOUT, STDERR to the console */
2172
a6b0d5ff583f (svn r2686) Revert accidently commited change
tron
parents: 2171
diff changeset
   913
#if !defined(__CYGWIN__)
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   914
	*stdout = *_fdopen( _open_osfhandle((intptr_t)hand, _O_TEXT), "w" );
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   915
	*stdin = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT), "r" );
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2458
diff changeset
   916
	*stderr = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT), "w" );
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   917
#else
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6424
diff changeset
   918
	/* open_osfhandle is not in cygwin */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   919
	*stdout = *fdopen(1, "w" );
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   920
	*stdin = *fdopen(0, "r" );
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   921
	*stderr = *fdopen(2, "w" );
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   922
#endif
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   923
1468
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   924
	setvbuf(stdin, NULL, _IONBF, 0);
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   925
	setvbuf(stdout, NULL, _IONBF, 0);
87ffabf776f2 (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
   926
	setvbuf(stderr, NULL, _IONBF, 0);
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   927
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   928
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   929
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   930
void ShowInfo(const char *str)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   931
{
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3799
diff changeset
   932
	if (_has_console) {
5674
abe7d6b7b1d4 (svn r8134) -Fix (r8013): Add a newline after the ShowInfo console output
Darkvater
parents: 5609
diff changeset
   933
		fprintf(stderr, "%s\n", str);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3799
diff changeset
   934
	} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
		bool old;
5722
62abdb0d0662 (svn r8239) -Regression (r7278): Help window was empty for UNICODE builds
Darkvater
parents: 5674
diff changeset
   936
#if defined(UNICODE)
62abdb0d0662 (svn r8239) -Regression (r7278): Help window was empty for UNICODE builds
Darkvater
parents: 5674
diff changeset
   937
			/* We need to put the text in a seperate buffer because the default
62abdb0d0662 (svn r8239) -Regression (r7278): Help window was empty for UNICODE builds
Darkvater
parents: 5674
diff changeset
   938
			 * buffer in MB_TO_WIDE might not be large enough (512 chars) */
62abdb0d0662 (svn r8239) -Regression (r7278): Help window was empty for UNICODE builds
Darkvater
parents: 5674
diff changeset
   939
			wchar_t help_msgW[4096];
62abdb0d0662 (svn r8239) -Regression (r7278): Help window was empty for UNICODE builds
Darkvater
parents: 5674
diff changeset
   940
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   941
		ReleaseCapture();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   942
		_left_button_clicked =_left_button_down = false;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   943
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
		old = MyShowCursor(true);
5722
62abdb0d0662 (svn r8239) -Regression (r7278): Help window was empty for UNICODE builds
Darkvater
parents: 5674
diff changeset
   945
		if (MessageBox(GetActiveWindow(), MB_TO_WIDE_BUFFER(str, help_msgW, lengthof(help_msgW)), _T("OpenTTD"), MB_ICONINFORMATION | MB_OKCANCEL) == IDCANCEL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   946
			CreateConsole();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   947
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
		MyShowCursor(old);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   950
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   952
#if defined(WINCE)
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   953
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   954
#else
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
   955
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   956
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   957
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
	int argc;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   959
	char *argv[64]; // max 64 command line arguments
5312
ce01037b54f1 (svn r7468) -Codechange: [win32] Add some comments to MB/WIDE_TO_WIDE/MB_[BUFFER] macros and
Darkvater
parents: 5311
diff changeset
   960
	char *cmdline;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   961
6276
2bac13107db6 (svn r9085) -Codechange/Fix: [win32] Move the initialisation of _codepage (non-UNICODE) to winMain as a dedicated server, or different video driver will not have a win32 messageloop.
Darkvater
parents: 6275
diff changeset
   962
#if !defined(UNICODE)
2bac13107db6 (svn r9085) -Codechange/Fix: [win32] Move the initialisation of _codepage (non-UNICODE) to winMain as a dedicated server, or different video driver will not have a win32 messageloop.
Darkvater
parents: 6275
diff changeset
   963
	_codepage = GetACP(); // get system codepage as some kind of a default
2bac13107db6 (svn r9085) -Codechange/Fix: [win32] Move the initialisation of _codepage (non-UNICODE) to winMain as a dedicated server, or different video driver will not have a win32 messageloop.
Darkvater
parents: 6275
diff changeset
   964
#endif /* UNICODE */
2bac13107db6 (svn r9085) -Codechange/Fix: [win32] Move the initialisation of _codepage (non-UNICODE) to winMain as a dedicated server, or different video driver will not have a win32 messageloop.
Darkvater
parents: 6275
diff changeset
   965
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   966
#if defined(UNICODE)
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   967
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   968
#if !defined(WINCE)
6614
abe46ba511ba (svn r9834) -Add: win9x check in win32 builds
glx
parents: 6492
diff changeset
   969
	/* Check if a win9x user started the win32 version */
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9457
diff changeset
   970
	if (HasBit(GetVersion(), 31)) usererror("This version of OpenTTD doesn't run on windows 95/98/ME.\nPlease download the win9x binary and try again.");
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   971
#endif
6614
abe46ba511ba (svn r9834) -Add: win9x check in win32 builds
glx
parents: 6492
diff changeset
   972
5312
ce01037b54f1 (svn r7468) -Codechange: [win32] Add some comments to MB/WIDE_TO_WIDE/MB_[BUFFER] macros and
Darkvater
parents: 5311
diff changeset
   973
	/* For UNICODE we need to convert the commandline to char* _AND_
ce01037b54f1 (svn r7468) -Codechange: [win32] Add some comments to MB/WIDE_TO_WIDE/MB_[BUFFER] macros and
Darkvater
parents: 5311
diff changeset
   974
	 * save it because argv[] points into this buffer and thus needs to
ce01037b54f1 (svn r7468) -Codechange: [win32] Add some comments to MB/WIDE_TO_WIDE/MB_[BUFFER] macros and
Darkvater
parents: 5311
diff changeset
   975
	 * be available between subsequent calls to FS2OTTD() */
ce01037b54f1 (svn r7468) -Codechange: [win32] Add some comments to MB/WIDE_TO_WIDE/MB_[BUFFER] macros and
Darkvater
parents: 5311
diff changeset
   976
	char cmdlinebuf[MAX_PATH];
6276
2bac13107db6 (svn r9085) -Codechange/Fix: [win32] Move the initialisation of _codepage (non-UNICODE) to winMain as a dedicated server, or different video driver will not have a win32 messageloop.
Darkvater
parents: 6275
diff changeset
   977
#endif /* UNICODE */
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   978
5312
ce01037b54f1 (svn r7468) -Codechange: [win32] Add some comments to MB/WIDE_TO_WIDE/MB_[BUFFER] macros and
Darkvater
parents: 5311
diff changeset
   979
	cmdline = WIDE_TO_MB_BUFFER(GetCommandLine(), cmdlinebuf, lengthof(cmdlinebuf));
ce01037b54f1 (svn r7468) -Codechange: [win32] Add some comments to MB/WIDE_TO_WIDE/MB_[BUFFER] macros and
Darkvater
parents: 5311
diff changeset
   980
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
#if defined(_DEBUG)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   982
	CreateConsole();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   984
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   985
#if !defined(WINCE)
2428
a27141f22960 (svn r2954) - Force assertion message to be output to a messagebox instead to the console which MinGW32 had the habit of doing. Using undocumented (in mingw) __set_error_mode() function.
Darkvater
parents: 2338
diff changeset
   986
	_set_error_mode(_OUT_TO_MSGBOX); // force assertion output to messagebox
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
   987
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   988
5310
4bb8cfc73019 (svn r7466) -Cleanup: [win32] Coding style, indentation, variable localization.
Darkvater
parents: 5304
diff changeset
   989
	/* setup random seed to something quite random */
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents: 7928
diff changeset
   990
	SetRandomSeed(GetTickCount());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   992
	argc = ParseCommandLine(cmdline, argv, lengthof(argv));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   993
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   994
#if defined(WIN32_EXCEPTION_TRACKER)
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
   995
	Win32InitializeExceptions();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   996
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   997
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
#if defined(WIN32_EXCEPTION_TRACKER_DEBUG)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   999
	_try {
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 5167
diff changeset
  1000
		LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1001
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1002
		ttd_main(argc, argv);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1004
#if defined(WIN32_EXCEPTION_TRACKER_DEBUG)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1005
	} _except (ExceptionHandler(_exception_info())) {}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1006
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1007
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1008
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1010
5758
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1011
#if defined(WINCE)
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1012
void GetCurrentDirectoryW(int length, wchar_t *path)
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1013
{
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1014
	/* Get the name of this module */
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1015
	GetModuleFileName(NULL, path, length);
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1016
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1017
	/* Remove the executable name, this we call CurrentDir */
6124
9f822ae6c086 (svn r8860) -Cleanup: some style changes, proper #endif comments, variable initialisation, WINCE ifdef and a vsprintf to vsnprintf change.
Darkvater
parents: 6117
diff changeset
  1018
	wchar_t *pDest = wcsrchr(path, '\\');
5758
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1019
	if (pDest != NULL) {
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1020
		int result = pDest - path + 1;
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1021
		path[result] = '\0';
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1022
	}
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1023
}
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1024
#endif
4b10d28b54c7 (svn r8309) [WinCE] -Fix: WinCE doesn't know resolution changing
truelight
parents: 5722
diff changeset
  1025
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6614
diff changeset
  1026
char *getcwd(char *buf, size_t size)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1027
{
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
  1028
#if defined(WINCE)
8970
da7261f48b7e (svn r12762) -Fix: tabs after the first non-tab character are generally not okay (or lines starting with a space and then tabs).
rubidium
parents: 8969
diff changeset
  1029
	TCHAR path[MAX_PATH];
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
  1030
	GetModuleFileName(NULL, path, MAX_PATH);
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
  1031
	convert_from_fs(path, buf, size);
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
  1032
	/* GetModuleFileName returns dir with file, so remove everything behind latest '\\' */
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
  1033
	char *p = strrchr(buf, '\\');
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
  1034
	if (p != NULL) *p = '\0';
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7372
diff changeset
  1035
#elif defined(UNICODE)
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1036
	TCHAR path[MAX_PATH];
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1037
	GetCurrentDirectory(MAX_PATH - 1, path);
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6614
diff changeset
  1038
	convert_from_fs(path, buf, size);
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1039
#else
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6614
diff changeset
  1040
	GetCurrentDirectory(size, buf);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6614
diff changeset
  1041
#endif
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6614
diff changeset
  1042
	return buf;
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6614
diff changeset
  1043
}
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6614
diff changeset
  1044
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6614
diff changeset
  1045
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6614
diff changeset
  1046
void DetermineBasePaths(const char *exe)
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6614
diff changeset
  1047
{
7589
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7457
diff changeset
  1048
	extern void ScanForTarFiles();
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1049
	char tmp[MAX_PATH];
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1050
	TCHAR path[MAX_PATH];
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1051
#ifdef WITH_PERSONAL_DIR
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1052
	SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, path);
10201
4c181fbe8f2a (svn r14414) -Fix: replace instances of strncpy with strecpy as strncpy doesn't guarantee the resulting string is '\0'-terminated.
rubidium
parents: 10186
diff changeset
  1053
	strecpy(tmp, WIDE_TO_MB_BUFFER(path, tmp, lengthof(tmp)), lastof(tmp));
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1054
	AppendPathSeparator(tmp, MAX_PATH);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1055
	ttd_strlcat(tmp, PERSONAL_DIR, MAX_PATH);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1056
	AppendPathSeparator(tmp, MAX_PATH);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1057
	_searchpaths[SP_PERSONAL_DIR] = strdup(tmp);
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6614
diff changeset
  1058
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1059
	SHGetFolderPath(NULL, CSIDL_COMMON_DOCUMENTS, NULL, SHGFP_TYPE_CURRENT, path);
10201
4c181fbe8f2a (svn r14414) -Fix: replace instances of strncpy with strecpy as strncpy doesn't guarantee the resulting string is '\0'-terminated.
rubidium
parents: 10186
diff changeset
  1060
	strecpy(tmp, WIDE_TO_MB_BUFFER(path, tmp, lengthof(tmp)), lastof(tmp));
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1061
	AppendPathSeparator(tmp, MAX_PATH);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1062
	ttd_strlcat(tmp, PERSONAL_DIR, MAX_PATH);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1063
	AppendPathSeparator(tmp, MAX_PATH);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1064
	_searchpaths[SP_SHARED_DIR] = strdup(tmp);
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6614
diff changeset
  1065
#else
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1066
	_searchpaths[SP_PERSONAL_DIR] = NULL;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1067
	_searchpaths[SP_SHARED_DIR]   = NULL;
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1068
#endif
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1069
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1070
	/* Get the path to working directory of OpenTTD */
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1071
	getcwd(tmp, lengthof(tmp));
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1072
	AppendPathSeparator(tmp, MAX_PATH);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1073
	_searchpaths[SP_WORKING_DIR] = strdup(tmp);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1074
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1075
	if (!GetModuleFileName(NULL, path, lengthof(path))) {
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1076
		DEBUG(misc, 0, "GetModuleFileName failed (%d)\n", GetLastError());
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1077
		_searchpaths[SP_BINARY_DIR] = NULL;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1078
	} else {
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1079
		TCHAR exec_dir[MAX_PATH];
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1080
		_tcsncpy(path, MB_TO_WIDE_BUFFER(exe, path, lengthof(path)), lengthof(path));
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1081
		if (!GetFullPathName(path, lengthof(exec_dir), exec_dir, NULL)) {
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1082
			DEBUG(misc, 0, "GetFullPathName failed (%d)\n", GetLastError());
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1083
			_searchpaths[SP_BINARY_DIR] = NULL;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1084
		} else {
10201
4c181fbe8f2a (svn r14414) -Fix: replace instances of strncpy with strecpy as strncpy doesn't guarantee the resulting string is '\0'-terminated.
rubidium
parents: 10186
diff changeset
  1085
			strecpy(tmp, WIDE_TO_MB_BUFFER(exec_dir, tmp, lengthof(tmp)), lastof(tmp));
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1086
			char *s = strrchr(tmp, PATHSEPCHAR);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1087
			*(s + 1) = '\0';
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1088
			_searchpaths[SP_BINARY_DIR] = strdup(tmp);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1089
		}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1090
	}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1091
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1092
	_searchpaths[SP_INSTALLATION_DIR]       = NULL;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6834
diff changeset
  1093
	_searchpaths[SP_APPLICATION_BUNDLE_DIR] = NULL;
7589
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7457
diff changeset
  1094
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7457
diff changeset
  1095
	ScanForTarFiles();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1096
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1097
1390
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1098
/**
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1099
 * Insert a chunk of text from the clipboard onto the textbuffer. Get TEXT clipboard
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1100
 * and append this up to the maximum length (either absolute or screenlength). If maxlength
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1101
 * is zero, we don't care about the screenlength but only about the physical length of the string
6443
63fbe9f76801 (svn r9580) -Documentation: Correction of doxygen function declarations
belugas
parents: 6432
diff changeset
  1102
 * @param tb Textbuf type to be changed
7372
6fac48ff7c4d (svn r10736) -Fix: Correct all mispellings of 'successful'.
peter1138
parents: 7350
diff changeset
  1103
 * @return true on successful change of Textbuf, or false otherwise
1390
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1104
 */
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1105
bool InsertTextBufferClipboard(Textbuf *tb)
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1106
{
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1107
	HGLOBAL cbuf;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1108
	char utf8_buf[512];
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1109
	const char *ptr;
1390
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1110
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1111
	WChar c;
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9215
diff changeset
  1112
	uint16 width, length;
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1113
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1114
	if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1115
		OpenClipboard(NULL);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1116
		cbuf = GetClipboardData(CF_UNICODETEXT);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1117
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1118
		ptr = (const char*)GlobalLock(cbuf);
6275
9d330e242010 (svn r9084) -Fix: [win9x] Clipboard paste for Windows95 (doesn't have CF_UNICODETEXT) correctly converts the input to the current locale.
Darkvater
parents: 6251
diff changeset
  1119
		const char *ret = convert_from_fs((wchar_t*)ptr, utf8_buf, lengthof(utf8_buf));
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1120
		GlobalUnlock(cbuf);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1121
		CloseClipboard();
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1122
5312
ce01037b54f1 (svn r7468) -Codechange: [win32] Add some comments to MB/WIDE_TO_WIDE/MB_[BUFFER] macros and
Darkvater
parents: 5311
diff changeset
  1123
		if (*ret == '\0') return false;
6275
9d330e242010 (svn r9084) -Fix: [win9x] Clipboard paste for Windows95 (doesn't have CF_UNICODETEXT) correctly converts the input to the current locale.
Darkvater
parents: 6251
diff changeset
  1124
#if !defined(UNICODE)
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1125
	} else if (IsClipboardFormatAvailable(CF_TEXT)) {
1390
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1126
		OpenClipboard(NULL);
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1127
		cbuf = GetClipboardData(CF_TEXT);
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1128
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1129
		ptr = (const char*)GlobalLock(cbuf);
10310
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10301
diff changeset
  1130
		strecpy(utf8_buf, FS2OTTD(ptr), lastof(utf8_buf));
6275
9d330e242010 (svn r9084) -Fix: [win9x] Clipboard paste for Windows95 (doesn't have CF_UNICODETEXT) correctly converts the input to the current locale.
Darkvater
parents: 6251
diff changeset
  1131
1390
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1132
		GlobalUnlock(cbuf);
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1133
		CloseClipboard();
6275
9d330e242010 (svn r9084) -Fix: [win9x] Clipboard paste for Windows95 (doesn't have CF_UNICODETEXT) correctly converts the input to the current locale.
Darkvater
parents: 6251
diff changeset
  1134
#endif /* UNICODE */
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1135
	} else {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1136
		return false;
1390
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1137
	}
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1138
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1139
	width = length = 0;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1140
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1141
	for (ptr = utf8_buf; (c = Utf8Consume(&ptr)) != '\0';) {
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1142
		if (!IsPrintable(c)) break;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1143
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9215
diff changeset
  1144
		byte len = Utf8CharLen(c);
10280
9897206074cc (svn r14518) -Fix (r14514): forgot win32 and OS/2 files (glx)
smatz
parents: 10201
diff changeset
  1145
		if (tb->size + length + len > tb->maxsize) break;
6195
e2f112f5d50f (svn r8975) -Regression: [win32] Possible buffer overflow if unicode text is pasted into an input box and needs trimming. The last character was wrongly assumed to be of length 1 (tb->maxlength - 1), while a unicode character can be up to 4 long.
Darkvater
parents: 6171
diff changeset
  1146
e2f112f5d50f (svn r8975) -Regression: [win32] Possible buffer overflow if unicode text is pasted into an input box and needs trimming. The last character was wrongly assumed to be of length 1 (tb->maxlength - 1), while a unicode character can be up to 4 long.
Darkvater
parents: 6171
diff changeset
  1147
		byte charwidth = GetCharacterWidth(FS_NORMAL, c);
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1148
		if (tb->maxwidth != 0 && width + tb->width + charwidth > tb->maxwidth) break;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1149
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1150
		width += charwidth;
6195
e2f112f5d50f (svn r8975) -Regression: [win32] Possible buffer overflow if unicode text is pasted into an input box and needs trimming. The last character was wrongly assumed to be of length 1 (tb->maxlength - 1), while a unicode character can be up to 4 long.
Darkvater
parents: 6171
diff changeset
  1151
		length += len;
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1152
	}
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1153
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1154
	if (length == 0) return false;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1155
10280
9897206074cc (svn r14518) -Fix (r14514): forgot win32 and OS/2 files (glx)
smatz
parents: 10201
diff changeset
  1156
	memmove(tb->buf + tb->caretpos + length, tb->buf + tb->caretpos, tb->size - tb->caretpos);
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1157
	memcpy(tb->buf + tb->caretpos, utf8_buf, length);
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1158
	tb->width += width;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1159
	tb->caretxoffs += width;
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1160
10280
9897206074cc (svn r14518) -Fix (r14514): forgot win32 and OS/2 files (glx)
smatz
parents: 10201
diff changeset
  1161
	tb->size += length;
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1162
	tb->caretpos += length;
10280
9897206074cc (svn r14518) -Fix (r14514): forgot win32 and OS/2 files (glx)
smatz
parents: 10201
diff changeset
  1163
	assert(tb->size <= tb->maxsize);
9897206074cc (svn r14518) -Fix (r14514): forgot win32 and OS/2 files (glx)
smatz
parents: 10201
diff changeset
  1164
	tb->buf[tb->size - 1] = '\0'; // terminating zero
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1165
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 4572
diff changeset
  1166
	return true;
1390
e7cdf3ce0fb6 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
  1167
}
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1881
diff changeset
  1168
2073
2d63300e4a44 (svn r2583) Move OS specific code out of misc.c
ludde
parents: 2062
diff changeset
  1169
2d63300e4a44 (svn r2583) Move OS specific code out of misc.c
ludde
parents: 2062
diff changeset
  1170
void CSleep(int milliseconds)
2d63300e4a44 (svn r2583) Move OS specific code out of misc.c
ludde
parents: 2062
diff changeset
  1171
{
2d63300e4a44 (svn r2583) Move OS specific code out of misc.c
ludde
parents: 2062
diff changeset
  1172
	Sleep(milliseconds);
2099
02c5a49863e9 (svn r2609) - Feature: remove extension from savegames/scenarios when browsing the folders.
Darkvater
parents: 2091
diff changeset
  1173
}
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2099
diff changeset
  1174
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2099
diff changeset
  1175
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6424
diff changeset
  1176
/** Utility function to get the current timestamp in milliseconds
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6424
diff changeset
  1177
 * Useful for profiling */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6240
diff changeset
  1178
int64 GetTS()
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2099
diff changeset
  1179
{
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2099
diff changeset
  1180
	static double freq;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2099
diff changeset
  1181
	__int64 value;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2099
diff changeset
  1182
	if (!freq) {
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2099
diff changeset
  1183
		QueryPerformanceFrequency((LARGE_INTEGER*)&value);
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2099
diff changeset
  1184
		freq = (double)1000000 / value;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2099
diff changeset
  1185
	}
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2099
diff changeset
  1186
	QueryPerformanceCounter((LARGE_INTEGER*)&value);
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2099
diff changeset
  1187
	return (__int64)(value * freq);
2139
63d965b6e229 (svn r2649) Get rid of 2 warnings.
ludde
parents: 2125
diff changeset
  1188
}
5167
12c46fb7eccf (svn r7274) -Codechange [utf8]: Add input/output unicode support. This actually only changes win32
Darkvater
parents: 5120
diff changeset
  1189
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1190
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1191
/**
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1192
 * Convert to OpenTTD's encoding from that of the local environment.
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1193
 * When the project is built in UNICODE, the system codepage is irrelevant and
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1194
 * the input string is wide. In ANSI mode, the string is in the
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1195
 * local codepage which we'll convert to wide-char, and then to UTF-8.
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1196
 * OpenTTD internal encoding is UTF8.
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1197
 * The returned value's contents can only be guaranteed until the next call to
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1198
 * this function. So if the value is needed for anything else, use convert_from_fs
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1199
 * @param name pointer to a valid string that will be converted (local, or wide)
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1200
 * @return pointer to the converted string; if failed string is of zero-length
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1201
 * @see the current code-page comes from video\win32_v.cpp, event-notification
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1202
 * WM_INPUTLANGCHANGE */
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1203
const char *FS2OTTD(const TCHAR *name)
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1204
{
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1205
	static char utf8_buf[512];
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1206
#if defined(UNICODE)
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1207
	return convert_from_fs(name, utf8_buf, lengthof(utf8_buf));
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1208
#else
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1209
	char *s = utf8_buf;
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1210
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1211
	for (; *name != '\0'; name++) {
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1212
		wchar_t w;
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1213
		int len = MultiByteToWideChar(_codepage, 0, name, 1, &w, 1);
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1214
		if (len != 1) {
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1215
			DEBUG(misc, 0, "[utf8] M2W error converting '%c'. Errno %d", *name, GetLastError());
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1216
			continue;
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1217
		}
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1218
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1219
		if (s + Utf8CharLen(w) >= lastof(utf8_buf)) break;
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1220
		s += Utf8Encode(s, w);
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1221
	}
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1222
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1223
	*s = '\0';
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1224
	return utf8_buf;
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1225
#endif /* UNICODE */
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1226
}
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1227
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1228
/**
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1229
 * Convert from OpenTTD's encoding to that of the local environment.
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1230
 * When the project is built in UNICODE the system codepage is irrelevant and
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1231
 * the converted string is wide. In ANSI mode, the UTF8 string is converted
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1232
 * to multi-byte.
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1233
 * OpenTTD internal encoding is UTF8.
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1234
 * The returned value's contents can only be guaranteed until the next call to
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1235
 * this function. So if the value is needed for anything else, use convert_from_fs
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1236
 * @param name pointer to a valid string that will be converted (UTF8)
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1237
 * @return pointer to the converted string; if failed string is of zero-length
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1238
 * @see the current code-page comes from video\win32_v.cpp, event-notification
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1239
 * WM_INPUTLANGCHANGE */
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1240
const TCHAR *OTTD2FS(const char *name)
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1241
{
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1242
	static TCHAR system_buf[512];
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1243
#if defined(UNICODE)
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1244
	return convert_to_fs(name, system_buf, lengthof(system_buf));
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1245
#else
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1246
	char *s = system_buf;
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1247
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1248
	for (WChar c; (c = Utf8Consume(&name)) != '\0';) {
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1249
		if (s >= lastof(system_buf)) break;
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1250
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1251
		char mb;
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1252
		int len = WideCharToMultiByte(_codepage, 0, (wchar_t*)&c, 1, &mb, 1, NULL, NULL);
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1253
		if (len != 1) {
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1254
			DEBUG(misc, 0, "[utf8] W2M error converting '0x%X'. Errno %d", c, GetLastError());
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1255
			continue;
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1256
		}
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1257
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1258
		*s++ = mb;
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1259
	}
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1260
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1261
	*s = '\0';
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1262
	return system_buf;
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1263
#endif /* UNICODE */
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1264
}
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1265
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1266
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1267
/** Convert to OpenTTD's encoding from that of the environment in
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1268
 * UNICODE. OpenTTD encoding is UTF8, local is wide
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1269
 * @param name pointer to a valid string that will be converted
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1270
 * @param utf8_buf pointer to a valid buffer that will receive the converted string
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1271
 * @param buflen length in characters of the receiving buffer
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1272
 * @return pointer to utf8_buf. If conversion fails the string is of zero-length */
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1273
char *convert_from_fs(const wchar_t *name, char *utf8_buf, size_t buflen)
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1274
{
9156
b9618a02f28b (svn r13019) -Fix [FS#1997]: silence some more MSVC x64 warnings (michi_cc)
glx
parents: 9146
diff changeset
  1275
	int len = WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8_buf, (int)buflen, NULL, NULL);
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1276
	if (len == 0) {
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1277
		DEBUG(misc, 0, "[utf8] W2M error converting wide-string. Errno %d", GetLastError());
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1278
		utf8_buf[0] = '\0';
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1279
	}
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1280
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1281
	return utf8_buf;
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1282
}
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1283
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1284
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1285
/** Convert from OpenTTD's encoding to that of the environment in
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1286
 * UNICODE. OpenTTD encoding is UTF8, local is wide
5167
12c46fb7eccf (svn r7274) -Codechange [utf8]: Add input/output unicode support. This actually only changes win32
Darkvater
parents: 5120
diff changeset
  1287
 * @param name pointer to a valid string that will be converted
5218
bde1f7566777 (svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
Darkvater
parents: 5168
diff changeset
  1288
 * @param utf16_buf pointer to a valid wide-char buffer that will receive the
bde1f7566777 (svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
Darkvater
parents: 5168
diff changeset
  1289
 * converted string
bde1f7566777 (svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
Darkvater
parents: 5168
diff changeset
  1290
 * @param buflen length in wide characters of the receiving buffer
bde1f7566777 (svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
Darkvater
parents: 5168
diff changeset
  1291
 * @return pointer to utf16_buf. If conversion fails the string is of zero-length */
bde1f7566777 (svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
Darkvater
parents: 5168
diff changeset
  1292
wchar_t *convert_to_fs(const char *name, wchar_t *utf16_buf, size_t buflen)
bde1f7566777 (svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
Darkvater
parents: 5168
diff changeset
  1293
{
9156
b9618a02f28b (svn r13019) -Fix [FS#1997]: silence some more MSVC x64 warnings (michi_cc)
glx
parents: 9146
diff changeset
  1294
	int len = MultiByteToWideChar(CP_UTF8, 0, name, -1, utf16_buf, (int)buflen);
5218
bde1f7566777 (svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
Darkvater
parents: 5168
diff changeset
  1295
	if (len == 0) {
6251
2ad87f850eb9 (svn r9055) -Codechange: Change windows unicode handling and allow a pure non-unicode build to function. Win9x binaries will be possible with mingw/nightly system.
Darkvater
parents: 6248
diff changeset
  1296
		DEBUG(misc, 0, "[utf8] M2W error converting '%s'. Errno %d", name, GetLastError());
5218
bde1f7566777 (svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
Darkvater
parents: 5168
diff changeset
  1297
		utf16_buf[0] = '\0';
bde1f7566777 (svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
Darkvater
parents: 5168
diff changeset
  1298
	}
bde1f7566777 (svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
Darkvater
parents: 5168
diff changeset
  1299
bde1f7566777 (svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
Darkvater
parents: 5168
diff changeset
  1300
	return utf16_buf;
bde1f7566777 (svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
Darkvater
parents: 5168
diff changeset
  1301
}
bde1f7566777 (svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
Darkvater
parents: 5168
diff changeset
  1302
5304
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1303
/** Our very own SHGetFolderPath function for support of windows operating
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1304
 * systems that don't have this function (eg Win9x, etc.). We try using the
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1305
 * native function, and if that doesn't exist we will try a more crude approach
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1306
 * of environment variables and hope for the best */
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1307
HRESULT OTTDSHGetFolderPath(HWND hwnd, int csidl, HANDLE hToken, DWORD dwFlags, LPTSTR pszPath)
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1308
{
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1309
	static HRESULT (WINAPI *SHGetFolderPath)(HWND, int, HANDLE, DWORD, LPTSTR) = NULL;
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1310
	static bool first_time = true;
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1311
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1312
	/* We only try to load the library one time; if it fails, it fails */
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1313
	if (first_time) {
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1314
#if defined(UNICODE)
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1315
# define W(x) x "W"
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1316
#else
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1317
# define W(x) x "A"
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1318
#endif
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1319
		if (!LoadLibraryList((Function*)&SHGetFolderPath, "SHFolder.dll\0" W("SHGetFolderPath") "\0\0")) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5336
diff changeset
  1320
			DEBUG(misc, 0, "Unable to load " W("SHGetFolderPath") "from SHFolder.dll");
5304
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1321
		}
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1322
#undef W
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1323
		first_time = false;
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1324
	}
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1325
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1326
	if (SHGetFolderPath != NULL) return SHGetFolderPath(hwnd, csidl, hToken, dwFlags, pszPath);
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1327
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1328
	/* SHGetFolderPath doesn't exist, try a more conservative approach,
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1329
	 * eg environment variables. This is only included for legacy modes
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1330
	 * MSDN says: that 'pszPath' is a "Pointer to a null-terminated string of
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1331
	 * length MAX_PATH which will receive the path" so let's assume that
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1332
	 * Windows 95 with Internet Explorer 5.0, Windows 98 with Internet Explorer 5.0,
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1333
	 * Windows 98 Second Edition (SE), Windows NT 4.0 with Internet Explorer 5.0,
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1334
	 * Windows NT 4.0 with Service Pack 4 (SP4) */
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1335
	{
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1336
		DWORD ret;
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1337
		switch (csidl) {
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1338
			case CSIDL_FONTS: /* Get the system font path, eg %WINDIR%\Fonts */
5336
f76077fef235 (svn r7500) -Fix (r7460): GetEnvironmentVariable and strncat take the number of characters and
Darkvater
parents: 5312
diff changeset
  1339
				ret = GetEnvironmentVariable(_T("WINDIR"), pszPath, MAX_PATH);
5304
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1340
				if (ret == 0) break;
5336
f76077fef235 (svn r7500) -Fix (r7460): GetEnvironmentVariable and strncat take the number of characters and
Darkvater
parents: 5312
diff changeset
  1341
				_tcsncat(pszPath, _T("\\Fonts"), MAX_PATH);
5304
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1342
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1343
				return (HRESULT)0;
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1344
				break;
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1345
			/* XXX - other types to go here when needed... */
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1346
		}
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1347
	}
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1348
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1349
	return E_INVALIDARG;
288359176ea1 (svn r7460) -Fix (r7337): [win32] If the underlying OS didn't have support for SHGetFolderPath the
Darkvater
parents: 5296
diff changeset
  1350
}
7348
aca2f2517ec1 (svn r10711) -Fix [Win32]: take default language from current user's locale on Windows (In-)
KUDr
parents: 7321
diff changeset
  1351
aca2f2517ec1 (svn r10711) -Fix [Win32]: take default language from current user's locale on Windows (In-)
KUDr
parents: 7321
diff changeset
  1352
/** Determine the current user's locale. */
aca2f2517ec1 (svn r10711) -Fix [Win32]: take default language from current user's locale on Windows (In-)
KUDr
parents: 7321
diff changeset
  1353
const char *GetCurrentLocale(const char *)
aca2f2517ec1 (svn r10711) -Fix [Win32]: take default language from current user's locale on Windows (In-)
KUDr
parents: 7321
diff changeset
  1354
{
7350
33dfdde26b36 (svn r10713) -Cleanup (r10711): too long string buffers, unsafe sprintf used (Darkvater)
KUDr
parents: 7349
diff changeset
  1355
	char lang[9], country[9];
7348
aca2f2517ec1 (svn r10711) -Fix [Win32]: take default language from current user's locale on Windows (In-)
KUDr
parents: 7321
diff changeset
  1356
	if (GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME, lang, lengthof(lang)) == 0 ||
aca2f2517ec1 (svn r10711) -Fix [Win32]: take default language from current user's locale on Windows (In-)
KUDr
parents: 7321
diff changeset
  1357
	    GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SISO3166CTRYNAME, country, lengthof(country)) == 0) {
aca2f2517ec1 (svn r10711) -Fix [Win32]: take default language from current user's locale on Windows (In-)
KUDr
parents: 7321
diff changeset
  1358
		/* Unable to retrieve the locale. */
aca2f2517ec1 (svn r10711) -Fix [Win32]: take default language from current user's locale on Windows (In-)
KUDr
parents: 7321
diff changeset
  1359
		return NULL;
aca2f2517ec1 (svn r10711) -Fix [Win32]: take default language from current user's locale on Windows (In-)
KUDr
parents: 7321
diff changeset
  1360
	}
aca2f2517ec1 (svn r10711) -Fix [Win32]: take default language from current user's locale on Windows (In-)
KUDr
parents: 7321
diff changeset
  1361
	/* Format it as 'en_us'. */
7350
33dfdde26b36 (svn r10713) -Cleanup (r10711): too long string buffers, unsafe sprintf used (Darkvater)
KUDr
parents: 7349
diff changeset
  1362
	static char retbuf[6] = {lang[0], lang[1], '_', country[0], country[1], 0};
7348
aca2f2517ec1 (svn r10711) -Fix [Win32]: take default language from current user's locale on Windows (In-)
KUDr
parents: 7321
diff changeset
  1363
	return retbuf;
aca2f2517ec1 (svn r10711) -Fix [Win32]: take default language from current user's locale on Windows (In-)
KUDr
parents: 7321
diff changeset
  1364
}