Merge branch 'NetHack-3.6.2'

This commit is contained in:
nhmall
2018-12-10 09:18:20 -05:00
35 changed files with 1685 additions and 419 deletions

561
win/share/safeproc.c Normal file
View File

@@ -0,0 +1,561 @@
/* NetHack 3.6 safeproc.c */
/* Copyright (c) Michael Allison, 2018 */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#include <stdio.h>
/*
* ***********************************************************
* This is a complete WindowPort implementation that can be
* assigned to the windowproc function pointers very early
* in the startup initialization, perhaps immediately even.
* It requires only the following call:
* windowprocs = *get_safe_procs(0);
*
* The game startup can trigger functions in other modules
* that make assumptions on a WindowPort being available
* and bad things can happen if any function pointers are
* null at that time.
*
* Some ports prior to 3.6.2 made attempts to early init
* various pieces of one of their WindowPorts, but that
* caused conflicts if that particular WindowPort wasn't
* the one that the user ended up selecting in their
* config file later. The WindowPort interfaced was designed
* to allow multiple WindowPorts to be linked into the same
* game binary.
*
* The base functions established by a call to get_safe_procs()
* accomplish the goal of preventing crashes, but not much
* else.
*
* There are also a few additional functions provided in here
* that can be selected optionally to provide some startup
* functionality for getting messages out to the user about
* issues that are being experienced during startup in
* general or during options parsing. The ones in here are
* deliberately free from any platforms or OS specific code.
* Please leave them using stdio C routines as much as
* possible. That isn't to say you can't do fancier functions
* prior to initialization of the primary WindowPort, but you
* can provide those platform-specific functions elsewhere,
* and assign them the same way that these more generic versions
* are assigned.
*
* The additional platform-independent, but more functional
* routines provided in here should be assigned after the
* windowprocs = *get_safe_procs(n)
* call.
*
* Usage:
*
* windowprocs = *get_safe_procs(0);
* initializes a set of winprocs function pointers that ensure
* none of the function pointers are left null, but that's all
* it does.
*
* windowprocs = *get_safe_procs(1);
* initializes a set of winprocs functions pointers that ensure
* none of the function pointers are left null, but also
* provides some basic output and input functionality using
* nothing other than C stdio routines (no platform-specific
* or OS-specific code).
*
* ***********************************************************
*/
struct window_procs safe_procs = {
"safe-startup", 0L, 0L,
safe_init_nhwindows, safe_player_selection, safe_askname, safe_get_nh_event,
safe_exit_nhwindows, safe_suspend_nhwindows, safe_resume_nhwindows,
safe_create_nhwindow, safe_clear_nhwindow, safe_display_nhwindow,
safe_destroy_nhwindow, safe_curs, safe_putstr, genl_putmixed,
safe_display_file, safe_start_menu, safe_add_menu, safe_end_menu,
safe_select_menu, safe_message_menu, safe_update_inventory, safe_mark_synch,
safe_wait_synch,
#ifdef CLIPPING
safe_cliparound,
#endif
#ifdef POSITIONBAR
safe_update_positionbar,
#endif
safe_print_glyph, safe_raw_print, safe_raw_print_bold, safe_nhgetch,
safe_nh_poskey, safe_nhbell, safe_doprev_message, safe_yn_function,
safe_getlin, safe_get_ext_cmd, safe_number_pad, safe_delay_output,
#ifdef CHANGE_COLOR /* the Mac uses a palette device */
safe_change_color,
#ifdef MAC
safe_change_background, set_safe_font_name,
#endif
safe_get_color_string,
#endif
safe_start_screen, safe_end_screen, genl_outrip,
safe_preference_update,
safe_getmsghistory, safe_putmsghistory,
safe_status_init,
safe_status_finish, safe_status_enablefield,
#ifdef STATUS_HILITES
safe_status_update,
#else
safe_status_update,
#endif
safe_can_suspend,
};
struct window_procs *
get_safe_procs(optn)
int optn;
{
if (optn) {
/* include the slightly more functional stdc versions */
safe_procs.win_raw_print = stdio_raw_print;
safe_procs.win_raw_print_bold = stdio_raw_print_bold;
safe_procs.win_nhgetch = stdio_nhgetch;
safe_procs.win_wait_synch = stdio_wait_synch;
}
return &safe_procs;
}
/*ARGSUSED*/
void
safe_init_nhwindows(argcp, argv)
int *argcp UNUSED;
char **argv UNUSED;
{
return;
}
void
safe_player_selection()
{
return;
}
void
safe_askname()
{
return;
}
void
safe_get_nh_event()
{
return;
}
void
safe_suspend_nhwindows(str)
const char *str;
{
return;
}
void
safe_resume_nhwindows()
{
return;
}
void
safe_exit_nhwindows(str)
const char *str;
{
return;
}
winid
safe_create_nhwindow(type)
int type;
{
return WIN_ERR;
}
void
safe_clear_nhwindow(window)
winid window;
{
return;
}
/*ARGSUSED*/
void
safe_display_nhwindow(window, blocking)
winid window;
boolean blocking;
{
return;
}
void
safe_dismiss_nhwindow(window)
winid window;
{
return;
}
void
safe_destroy_nhwindow(window)
winid window;
{
return;
}
void
safe_curs(window, x, y)
winid window;
int x, y;
{
return;
}
void
safe_putstr(window, attr, str)
winid window;
int attr;
const char *str;
{
return;
}
void
safe_display_file(fname, complain)
const char *fname;
boolean complain;
{
return;
}
void
safe_start_menu(window)
winid window;
{
return;
}
/*ARGSUSED*/
/*
* Add a menu item to the beginning of the menu list. This list is reversed
* later.
*/
void
safe_add_menu(window, glyph, identifier, ch, gch, attr, str, preselected)
winid window; /* window to use, must be of type NHW_MENU */
int glyph UNUSED; /* glyph to display with item (not used) */
const anything *identifier; /* what to return if selected */
char ch; /* keyboard accelerator (0 = pick our own) */
char gch; /* group accelerator (0 = no group) */
int attr; /* attribute for string (like safe_putstr()) */
const char *str; /* menu string */
boolean preselected; /* item is marked as selected */
{
return;
}
/*
* End a menu in this window, window must a type NHW_MENU.
*/
void
safe_end_menu(window, prompt)
winid window; /* menu to use */
const char *prompt; /* prompt to for menu */
{
return;
}
int
safe_select_menu(window, how, menu_list)
winid window;
int how;
menu_item **menu_list;
{
return 0;
}
/* special hack for treating top line --More-- as a one item menu */
char
safe_message_menu(let, how, mesg)
char let;
int how;
const char *mesg;
{
return '\033';
}
void
safe_update_inventory()
{
return;
}
void
safe_mark_synch()
{
}
void
safe_wait_synch()
{
}
#ifdef CLIPPING
void
safe_cliparound(x, y)
int x, y;
{
}
#endif /* CLIPPING */
/*
* safe_print_glyph
*
* Print the glyph to the output device. Don't flush the output device.
*/
void
safe_print_glyph(window, x, y, glyph, bkglyph)
winid window;
xchar x, y;
int glyph;
int bkglyph UNUSED;
{
return;
}
void
safe_raw_print(str)
const char *str;
{
return;
}
void
safe_raw_print_bold(str)
const char *str;
{
return;
}
int
safe_nhgetch()
{
return '\033';
}
/*
* return a key, or 0, in which case a mouse button was pressed
* mouse events should be returned as character postitions in the map window.
* Since normal tty's don't have mice, just return a key.
*/
/*ARGSUSED*/
int
safe_nh_poskey(x, y, mod)
int *x, *y, *mod;
{
return '\033';
}
void
win_safe_init(dir)
int dir;
{
return;
}
#ifdef POSITIONBAR
void
safe_update_positionbar(posbar)
char *posbar;
{
return;
}
#endif /* POSITIONBAR */
/*
* safe_status_init()
* -- initialize the port-specific data structures.
*/
void
safe_status_init()
{
return;
}
void
safe_can_suspend()
{
return;
}
void
safe_nhbell()
{
return;
}
int
safe_doprev_message()
{
return 0;
}
char
safe_yn_function(query, resp, def)
const char *query;
const char *resp;
char def;
{
return '\033';
}
/*ARGSUSED*/
static void
safe_getlin(prompt, outbuf)
const char *prompt UNUSED;
char *outbuf;
{
Strcpy(outbuf, "\033");
}
int
safe_get_ext_cmd()
{
return '\033';
}
void
safe_number_pad(mode)
int mode;
{
return;
}
void
safe_delay_output()
{
return;
}
void
safe_start_screen()
{
return;
}
void
safe_end_screen()
{
return;
}
void
safe_outrip(tmpwin, how, when)
winid tmpwin;
int how;
time_t when;
{
return;
}
/*ARGSUSED*/
void
safe_preference_update(pref)
const char *pref UNUSED;
{
return;
}
char *
safe_getmsghistory(init)
boolean init UNUSED;
{
return (char *) 0;
}
void
safe_putmsghistory(msg, is_restoring)
const char *msg;
boolean is_restoring;
{
}
void
safe_status_finish()
{
}
void
safe_status_enablefield(fieldidx, nm, fmt, enable)
int fieldidx;
const char *nm;
const char *fmt;
boolean enable;
{
}
#ifdef STATUS_HILITES
/* call once for each field, then call with BL_FLUSH to output the result */
void
safe_status_update(idx, ptr, chg, percent, color, colormasks)
int idx;
genericptr_t ptr;
int chg UNUSED, percent UNUSED, color UNUSED;
unsigned long *colormasks UNUSED;
{
}
#endif /* STATUS_HILITES */
/**************************************************************
* These are some optionally selectable routines that add
* some base functionality over the safe_* versions above.
* The safe_* versions are primarily designed to ensure that
* there are no null function pointers remaining at early
* game startup/initialization time.
*
* The slightly more functional versions in here should be kept
* free of platform-specific code or OS-specific code. If you
* want to use versions that involve platform-specific or
* OS-specific code, go right ahead but use your own replacement
* version of the functions in a platform-specific or
* OS-specific source file, not in here.
***************************************************************/
/* Add to your code: windowprocs.win_raw_print = stdio_wait_synch; */
void
stdio_wait_synch()
{
char valid[] = {' ', '\n', '\r', '\033', '\0'};
fprintf(stdout, "--More--");
(void) fflush(stdout);
while (!index(valid, nhgetch()))
;
}
/* Add to your code: windowprocs.win_raw_print = stdio_raw_print; */
void
stdio_raw_print(str)
const char *str;
{
if (str)
fprintf(stdout, "%s\n", str);
return;
}
/* Add to your code: windowprocs.win_raw_print_bold = stdio_raw_print_bold; */
void
stdio_raw_print_bold(str)
const char *str;
{
stdio_raw_print(str);
return;
}
/* Add to your code: windowprocs.win_nhgetch = stdio_nhgetch; */
int
stdio_nhgetch()
{
return getchar();
}
/* safeprocs.c */

View File

@@ -9,6 +9,9 @@
#include "winMS.h"
#define MIN_FONT_WIDTH 9
#define MIN_FONT_HEIGHT 12
typedef struct cached_font {
int code;
HFONT hFont;

View File

@@ -15,7 +15,7 @@
#include "color.h"
#include "patchlevel.h"
#define NHMAP_FONT_NAME TEXT("Terminal")
#define NHMAP_FONT_NAME TEXT("Courier New")
#define MAXWINDOWTEXT 255
#define CURSOR_BLINK_INTERVAL 1000 // milliseconds
@@ -160,25 +160,33 @@ mswin_map_stretch(HWND hWnd, LPSIZE map_size, BOOL redraw)
// calculate back buffer scale
data->monitorScale = win10_monitor_scale(hWnd);
if (data->bAsciiMode || Is_rogue_level(&u.uz)) {
boolean bText = data->bAsciiMode ||
(u.uz.dlevel != 0 && Is_rogue_level(&u.uz));
if (bText && !data->bFitToScreenMode)
data->backScale = data->monitorScale;
} else {
else
data->backScale = 1.0;
}
/* set back buffer tile size */
data->xBackTile = (int) (data->tileWidth * data->backScale);
data->yBackTile = (int) (data->tileHeight * data->backScale);
if (bText && data->bFitToScreenMode) {
data->xBackTile = wnd_size.cx / COLNO;
data->yBackTile = wnd_size.cy / ROWNO;
data->yBackTile = max(data->yBackTile, 12);
} else {
data->xBackTile = (int)(data->tileWidth * data->backScale);
data->yBackTile = (int)(data->tileHeight * data->backScale);
}
if (data->bAsciiMode || Is_rogue_level(&u.uz)) {
if (bText) {
LOGFONT lgfnt;
ZeroMemory(&lgfnt, sizeof(lgfnt));
lgfnt.lfHeight = -data->yBackTile; // height of font
lgfnt.lfWidth = -data->xBackTile; // average character width
lgfnt.lfHeight = -data->yBackTile; // height of font
lgfnt.lfWidth = 0; // average character width
lgfnt.lfEscapement = 0; // angle of escapement
lgfnt.lfOrientation = 0; // base-line orientation angle
lgfnt.lfWeight = FW_NORMAL; // font weight
lgfnt.lfWeight = FW_SEMIBOLD; // font weight
lgfnt.lfItalic = FALSE; // italic attribute option
lgfnt.lfUnderline = FALSE; // underline attribute option
lgfnt.lfStrikeOut = FALSE; // strikeout attribute option
@@ -195,25 +203,26 @@ mswin_map_stretch(HWND hWnd, LPSIZE map_size, BOOL redraw)
}
TEXTMETRIC textMetrics;
HFONT font;
HFONT font = NULL;
while (1) {
if (font != NULL)
DeleteObject(font);
font = CreateFontIndirect(&lgfnt);
SelectObject(data->backBufferDC, font);
GetTextMetrics(data->backBufferDC, &textMetrics);
if (textMetrics.tmHeight > data->yBackTile) {
if ((textMetrics.tmHeight > data->yBackTile ||
textMetrics.tmAveCharWidth > data->xBackTile) &&
lgfnt.lfHeight < -MIN_FONT_HEIGHT) {
lgfnt.lfHeight++;
continue;
}
if (textMetrics.tmAveCharWidth > data->xBackTile) {
lgfnt.lfWidth++;
continue;
}
break;
}
@@ -256,7 +265,7 @@ mswin_map_stretch(HWND hWnd, LPSIZE map_size, BOOL redraw)
/* calculate front buffer tile size */
if (wnd_size.cx > 0 && wnd_size.cy > 0 && data->bFitToScreenMode) {
if (wnd_size.cx > 0 && wnd_size.cy > 0 && !bText && data->bFitToScreenMode) {
double windowAspectRatio =
(double) wnd_size.cx / (double) wnd_size.cy;
@@ -270,7 +279,7 @@ mswin_map_stretch(HWND hWnd, LPSIZE map_size, BOOL redraw)
} else {
if (data->bAsciiMode || Is_rogue_level(&u.uz)) {
if (bText) {
data->frontScale = 1.0;
} else {
data->frontScale = data->monitorScale;
@@ -893,9 +902,22 @@ paintGlyph(PNHMapWindow data, int i, int j, RECT * rect)
#endif
if (data->bUnicodeFont) {
wch = winos_ascii_to_wide(ch);
DrawTextW(data->backBufferDC, &wch, 1, rect,
DT_CENTER | DT_VCENTER | DT_NOPREFIX
| DT_SINGLELINE);
if (wch == 0x2591 || wch == 0x2592) {
int level = 80;
HBRUSH brush = CreateSolidBrush(RGB(level, level, level));
FillRect(data->backBufferDC, rect, brush);
DeleteObject(brush);
level = (wch == 0x2591 ? 100 : 200);
brush = CreateSolidBrush(RGB(level, level, level));
RECT smallRect = { rect->left + 1, rect->top + 1,
rect->right - 1, rect->bottom - 1 };
FillRect(data->backBufferDC, &smallRect, brush);
DeleteObject(brush);
} else {
DrawTextW(data->backBufferDC, &wch, 1, rect,
DT_CENTER | DT_VCENTER | DT_NOPREFIX
| DT_SINGLELINE);
}
} else {
DrawTextA(data->backBufferDC, &ch, 1, rect,
DT_CENTER | DT_VCENTER | DT_NOPREFIX

View File

@@ -718,6 +718,13 @@ mswin_exit_nhwindows(const char *str)
/* Write Window settings to the registry */
mswin_write_reg();
/* set things back to failsafes */
windowprocs = *get_safe_procs(0);
/* and make sure there is still a way to communicate something */
windowprocs.win_raw_print = mswin_raw_print;
windowprocs.win_raw_print_bold = mswin_raw_print_bold;
windowprocs.win_wait_synch = mswin_wait_synch;
}
/* Prepare the window to be suspended. */

View File

@@ -34,7 +34,7 @@
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<OmitFramePointers>true</OmitFramePointers>
<AdditionalIncludeDirectories>$(WinWin32Dir);$(IncDir);$(SysWinntDir);$(SysShareDir);$(WinShareDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>TILES;WIN32CON;DLB;MSWIN_GRAPHICS;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>TILES;WIN32CON;DLB;MSWIN_GRAPHICS;SAFEPROCS;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;winmm.lib;Winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -151,7 +151,6 @@
<ClCompile Include="$(SrcDir)zap.c" />
<ClCompile Include="$(SysShareDir)cppregex.cpp" />
<ClCompile Include="$(SysShareDir)nhlan.c" />
<ClCompile Include="$(SysShareDir)pcmain.c" />
<ClCompile Include="$(SysShareDir)pcsys.c" />
<ClCompile Include="$(SysShareDir)pcunix.c" />
<ClCompile Include="$(SysShareDir)random.c" />
@@ -160,7 +159,9 @@
<ClCompile Include="$(SysWinntDir)stubs.c">
<PreprocessorDefinitions>GUISTUB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<ClCompile Include="$(SysWinntDir)windmain.c" />
<ClCompile Include="$(SysWinntDir)winnt.c" />
<ClCompile Include="$(WinShareDir)safeproc.c" />
<ClCompile Include="$(WinTtyDir)getline.c" />
<ClCompile Include="$(WinTtyDir)topl.c" />
<ClCompile Include="$(WinTtyDir)wintty.c" />
@@ -254,4 +255,4 @@
<Target Name="AfterRebuild">
<MSBuild Projects="afternethack.proj" Targets="Build" Properties="Configuration=$(Configuration)" />
</Target>
</Project>
</Project>

View File

@@ -20,7 +20,7 @@
<Optimization>Disabled</Optimization>
<OmitFramePointers>true</OmitFramePointers>
<AdditionalIncludeDirectories>$(WinWin32Dir);$(IncDir);$(SysWinntDir);$(SysShareDir);$(WinShareDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>TILES;_WINDOWS;DLB;MSWIN_GRAPHICS;NOTTYGRAPHICS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>TILES;_WINDOWS;DLB;MSWIN_GRAPHICS;SAFEPROCS;NOTTYGRAPHICS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
@@ -146,7 +146,6 @@
<ClCompile Include="$(SrcDir)zap.c" />
<ClCompile Include="$(SysShareDir)cppregex.cpp" />
<ClCompile Include="$(SysShareDir)nhlan.c" />
<ClCompile Include="$(SysShareDir)pcmain.c" />
<ClCompile Include="$(SysShareDir)pcsys.c" />
<ClCompile Include="$(SysShareDir)pcunix.c" />
<ClCompile Include="$(SysShareDir)random.c" />
@@ -154,8 +153,10 @@
<ClCompile Include="$(SysWinntDir)stubs.c">
<PreprocessorDefinitions>TTYSTUB;</PreprocessorDefinitions>
</ClCompile>
<ClCompile Include="$(SysWinntDir)windmain.c" />
<ClCompile Include="$(SysWinntDir)winnt.c" />
<ClCompile Include="$(SysWinntDir)win10.c" />
<ClCompile Include="$(WinShareDir)safeproc.c" />
<ClCompile Include="$(WinWin32Dir)mhaskyn.c" />
<ClCompile Include="$(WinWin32Dir)mhdlg.c" />
<ClCompile Include="$(WinWin32Dir)mhfont.c" />
@@ -200,4 +201,4 @@
<Target Name="AfterRebuild">
<MSBuild Projects="afternethack.proj" Targets="Build" Properties="Configuration=$(Configuration)" />
</Target>
</Project>
</Project>

View File

@@ -15,6 +15,10 @@
#include "mhmain.h"
#include "mhmap.h"
#if !defined(SAFEPROCS)
#error You must #define SAFEPROCS to build winhack.c
#endif
/* Borland and MinGW redefine "boolean" in shlwapi.h,
so just use the little bit we need */
typedef struct _DLLVERSIONINFO {
@@ -59,6 +63,8 @@ _nhapply_image_transparent(HDC hDC, int x, int y, int width, int height,
// Global Variables:
NHWinApp _nethack_app;
extern int GUILaunched; /* We tell shared startup code in windmain.c
that the GUI was launched via this */
#ifdef __BORLANDC__
#define _stricmp(s1, s2) stricmp(s1, s2)
@@ -66,7 +72,7 @@ NHWinApp _nethack_app;
#endif
// Foward declarations of functions included in this code module:
extern boolean FDECL(pcmain, (int, char **));
extern boolean FDECL(main, (int, char **));
static void __cdecl mswin_moveloop(void *);
#define MAX_CMDLINE_PARAM 255
@@ -82,24 +88,44 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
TCHAR *p;
TCHAR wbuf[BUFSZ];
char buf[BUFSZ];
DWORD major, minor;
boolean resuming;
/* OSVERSIONINFO osvi; */
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);
/* We must initialize state sufficiently to support calls to panic */
/*
* Get a set of valid safe windowport function
* pointers during early startup initialization.
*
* When get_safe_procs is called with 0 as the param,
* non-functional, but safe function pointers are set
* for all windowport routines.
*
* When get_safe_procs is called with 1 as the param,
* raw_print, raw_print_bold, and wait_synch, and nhgetch
* are set to use C stdio routines via stdio_raw_print,
* stdio_raw_print_bold, stdio_wait_synch, and
* stdio_nhgetch.
*/
windowprocs = *get_safe_procs(0);
/*
* Now we are going to override a couple
* of the windowprocs functions so that
* error messages are handled in a suitable
* way for the graphical version.
*/
windowprocs.win_raw_print = mswin_raw_print;
windowprocs.win_raw_print_bold = mswin_raw_print_bold;
windowprocs.win_wait_synch = mswin_wait_synch;
win10_init();
sys_early_init();
/* init applicatio structure */
/* init application structure */
_nethack_app.hApp = hInstance;
_nethack_app.hAccelTable =
LoadAccelerators(hInstance, (LPCTSTR) IDC_NETHACKW);
@@ -209,10 +235,9 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
}
free(savefile);
}
resuming = pcmain(argc, argv);
moveloop(resuming);
GUILaunched = 1;
/* let main do the argument processing */
(void) main(argc, argv);
return 0;
}