1 /* aNetHack 0.0.1 winhack.c $ANH-Date: 1432512799 2015/05/25 00:13:19 $ $ANH-Branch: master $:$ANH-Revision: 1.18 $ */
2 /* Copyright (C) 2001 by Alex Kompel */
3 // winhack.cpp : Defines the entry point for the application.
12 extern char orgdir
[PATHLEN
]; /* also used in pcsys.c, amidos.c */
14 extern void FDECL(anethack_exit
, (int));
15 static TCHAR
*_get_cmd_arg(TCHAR
*pCmdLine
);
18 NHWinApp _anethack_app
;
20 // Foward declarations of functions included in this code module:
21 BOOL
InitInstance(HINSTANCE
, int);
23 static void win_hack_init(int, char **);
24 static void __cdecl
mswin_moveloop(void *);
25 static BOOL
setMapTiles(const char *fname
);
27 extern boolean
FDECL(pcmain
, (int, char **));
29 #define MAX_CMDLINE_PARAM 255
32 WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPWSTR lpCmdLine
,
35 INITCOMMONCONTROLSEX InitCtrls
;
38 char *argv
[MAX_CMDLINE_PARAM
];
41 TCHAR wbuf
[NHSTR_BUFSIZE
];
42 char buf
[NHSTR_BUFSIZE
];
47 /* ensure that we don't access violate on a panic() */
48 windowprocs
.win_raw_print
= mswin_raw_print
;
49 windowprocs
.win_raw_print_bold
= mswin_raw_print_bold
;
51 /* init applicatio structure */
52 _anethack_app
.hApp
= hInstance
;
53 _anethack_app
.nCmdShow
= nCmdShow
;
54 _anethack_app
.hAccelTable
=
55 LoadAccelerators(hInstance
, (LPCTSTR
) IDC_WINHACK
);
56 _anethack_app
.hMainWnd
= NULL
;
57 _anethack_app
.hPopupWnd
= NULL
;
58 _anethack_app
.hMenuBar
= NULL
;
59 _anethack_app
.bmpTiles
= LoadBitmap(hInstance
, MAKEINTRESOURCE(IDB_TILES
));
60 if (_anethack_app
.bmpTiles
== NULL
)
61 panic("cannot load tiles bitmap");
62 _anethack_app
.bmpPetMark
=
63 LoadBitmap(hInstance
, MAKEINTRESOURCE(IDB_PETMARK
));
64 if (_anethack_app
.bmpPetMark
== NULL
)
65 panic("cannot load pet mark bitmap");
66 _anethack_app
.bmpMapTiles
= _anethack_app
.bmpTiles
;
67 _anethack_app
.mapTile_X
= TILE_X
;
68 _anethack_app
.mapTile_Y
= TILE_Y
;
69 _anethack_app
.mapTilesPerLine
= TILES_PER_LINE
;
70 _anethack_app
.bNoHScroll
= FALSE
;
71 _anethack_app
.bNoVScroll
= FALSE
;
73 _anethack_app
.bCmdPad
= !mswin_has_keyboard();
75 _anethack_app
.bWrapText
= TRUE
;
76 _anethack_app
.bFullScreen
= TRUE
;
78 #if defined(WIN_CE_SMARTPHONE)
79 _anethack_app
.bHideScrollBars
= TRUE
;
81 _anethack_app
.bHideScrollBars
= FALSE
;
84 _anethack_app
.bUseSIP
= TRUE
;
86 // check for running anethack programs
87 anethackWnd
= FindWindow(szMainWindowClass
, NULL
);
90 SetForegroundWindow(anethackWnd
);
95 ZeroMemory(&InitCtrls
, sizeof(InitCtrls
));
96 InitCtrls
.dwSize
= sizeof(InitCtrls
);
97 InitCtrls
.dwICC
= ICC_LISTVIEW_CLASSES
;
98 if (!InitCommonControlsEx(&InitCtrls
)) {
99 MessageBox(NULL
, TEXT("Cannot init common controls"), TEXT("ERROR"),
100 MB_OK
| MB_ICONSTOP
);
104 // Perform application initialization:
105 if (!InitInstance(hInstance
, nCmdShow
)) {
109 /* get command line parameters */
111 #if defined(WIN_CE_PS2xx) || defined(WIN32_PLATFORM_HPCPRO)
117 for (argc
= 1; p
&& argc
< MAX_CMDLINE_PARAM
; argc
++) {
120 argv
[argc
] = _strdup(NH_W2A(p
, buf
, BUFSZ
));
124 p
= _get_cmd_arg(NULL
);
126 GetModuleFileName(NULL
, wbuf
, BUFSZ
);
127 argv
[0] = _strdup(NH_W2A(wbuf
, buf
, BUFSZ
));
129 resuming
= pcmain(argc
, argv
);
137 // FUNCTION: InitInstance(HANDLE, int)
139 // PURPOSE: Saves instance handle and creates main window
143 // In this function, we save the instance handle in a global variable
145 // create and display the main program window.
148 InitInstance(HINSTANCE hInstance
, int nCmdShow
)
156 return &_anethack_app
;
164 /* cannot use maxledgerno() here, because we need to find a lock name
165 * before starting everything (including the dungeon initialization
166 * that sets astral_level, needed for maxledgerno()) up
168 for (i
= 1; i
<= MAXDUNGEON
* MAXLEVEL
+ 1; i
++) {
169 /* try to remove all */
170 set_levelfile_name(lock
, i
);
171 (void) unlink(fqname(lock
, LEVELPREFIX
, 0));
173 set_levelfile_name(lock
, 0);
174 if (unlink(fqname(lock
, LEVELPREFIX
, 0)))
175 return 0; /* cannot remove it */
176 return (1); /* success! */
189 /* regularize(lock); */ /* already done in pcmain */
190 Sprintf(tbuf
, "%s", fqname(lock
, LEVELPREFIX
, 0));
191 set_levelfile_name(lock
, 0);
192 fq_lock
= fqname(lock
, LEVELPREFIX
, 1);
194 f
= CreateFile(NH_A2W(fq_lock
, wbuf
, BUFSZ
), GENERIC_READ
, 0, NULL
,
195 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
196 if (f
== INVALID_HANDLE_VALUE
) {
197 if (GetLastError() == ERROR_FILE_NOT_FOUND
)
198 goto gotlock
; /* no such file */
199 error("Cannot open %s", fq_lock
);
204 /* prompt user that the game alredy exist */
205 choice
= MessageBox(GetNHApp()->hMainWnd
,
206 TEXT("There are files from a game in progress under "
207 "your name. Recover?"),
208 TEXT("Nethack"), MB_YESNO
| MB_DEFBUTTON1
);
211 if (recover_savefile()) {
214 error("Couldn't recover old game.");
220 error("%s", "Cannot start a new game.");
225 fd
= creat(fq_lock
, FCMASK
);
227 error("cannot creat lock file (%s.)", fq_lock
);
229 if (write(fd
, (char *) &hackpid
, sizeof(hackpid
))
230 != sizeof(hackpid
)) {
231 error("cannot write lock (%s)", fq_lock
);
233 if (close(fd
) == -1) {
234 error("cannot close lock (%s)", fq_lock
);
241 VA_DECL(const char *, s
)
245 DWORD last_error
= GetLastError();
248 VA_INIT(s
, const char *);
249 /* error() may get called before tty is initialized */
250 if (iflags
.window_inited
)
253 vsprintf(buf
, s
, VA_ARGS
);
254 NH_A2W(buf
, wbuf
, sizeof(wbuf
) / sizeof(wbuf
[0]));
255 if (last_error
> 0) {
257 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
258 | FORMAT_MESSAGE_FROM_SYSTEM
259 | FORMAT_MESSAGE_IGNORE_INSERTS
,
261 0, // Default language
262 (LPTSTR
) &lpMsgBuf
, 0, NULL
)) {
263 _tcsncat(wbuf
, TEXT("\nSystem Error: "),
264 sizeof(wbuf
) / sizeof(wbuf
[0]) - _tcslen(wbuf
));
265 _tcsncat(wbuf
, lpMsgBuf
,
266 sizeof(wbuf
) / sizeof(wbuf
[0]) - _tcslen(wbuf
));
272 MessageBox(NULL
, wbuf
, TEXT("Error"), MB_OK
| MB_ICONERROR
);
278 _get_cmd_arg(TCHAR
*pCmdLine
)
280 static TCHAR
*pArgs
= NULL
;
284 if (!pCmdLine
&& !pArgs
)
289 /* skip whitespace */
290 for (pRetArg
= pArgs
; *pRetArg
&& _istspace(*pRetArg
);
291 pRetArg
= CharNext(pRetArg
))
298 /* check for quote */
299 if (*pRetArg
== TEXT('"')) {
301 pRetArg
= CharNext(pRetArg
);
302 pArgs
= _tcschr(pRetArg
, TEXT('"'));
304 /* skip to whitespace */
305 for (pArgs
= pRetArg
; *pArgs
&& !_istspace(*pArgs
);
306 pArgs
= CharNext(pArgs
))
310 if (pArgs
&& *pArgs
) {
313 pArgs
= CharNext(pArgs
);
323 * Strip out troublesome file system characters.
326 void nt_regularize(s
) /* normalize file name */
329 register unsigned char *lp
;
331 for (lp
= s
; *lp
; lp
++)
332 if (*lp
== '?' || *lp
== '"' || *lp
== '\\' || *lp
== '/'
333 || *lp
== '>' || *lp
== '<' || *lp
== '*' || *lp
== '|'
334 || *lp
== ':' || (*lp
> 127))
350 char *portstr
= PORT_CE_PLATFORM
" " PORT_CE_CPU
;
351 Sprintf(eos(buf
), " %s", portstr
);