From f80d3d9d478fa9548759c41a2d04efd80e337b01 Mon Sep 17 00:00:00 2001 From: Nax Date: Thu, 22 Dec 2016 23:25:21 +0100 Subject: [PATCH] Dumplog - 1.0.0 --- include/config.h | 2 + include/extern.h | 5 ++ include/global.h | 13 ++++ src/end.c | 146 ++++++++++++++++++++++++++++++------------ src/pline.c | 12 ++++ src/windows.c | 190 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 326 insertions(+), 42 deletions(-) diff --git include/config.h include/config.h index 141a6845..59d25001 100644 --- include/config.h +++ include/config.h @@ -453,6 +453,8 @@ typedef unsigned char uchar; but it isn't necessary for successful operation of the program */ #define FREE_ALL_MEMORY /* free all memory at exit */ +/* #define DUMPLOG */ + /* End of Section 4 */ #include "global.h" /* Define everything else according to choices above */ diff --git include/extern.h include/extern.h index 3806f5be..08df8ae4 100644 --- include/extern.h +++ include/extern.h @@ -2658,6 +2658,11 @@ E void FDECL(genl_status_threshold, (int, int, anything, int, int, int)); #endif #endif +E void FDECL(dump_open_log, (time_t)); +E void NDECL(dump_close_log); +E void FDECL(dump_redirect, (boolean)); +E void FDECL(dump_forward_putstr, (winid, int, const char*, int)); + /* ### wizard.c ### */ E void NDECL(amulet); diff --git include/global.h include/global.h index b7a61bd1..d63bc551 100644 --- include/global.h +++ include/global.h @@ -352,4 +352,17 @@ struct savefile_info { #define PANICTRACE_GDB #endif +/* Dumplog patch */ +#ifdef DUMPLOG + +#ifndef DUMPLOG_MSG_COUNT +#define DUMPLOG_MSG_COUNT 50 +#endif + +#ifndef DUMPLOG_FILE +#define DUMPLOG_FILE "/tmp/nethack.%n.%d.log" +#endif + +#endif + #endif /* GLOBAL_H */ diff --git src/end.c src/end.c index e245e30e..210a6870 100644 --- src/end.c +++ src/end.c @@ -619,6 +619,63 @@ char *defquery; return TRUE; } +#if defined(DUMPLOG) +STATIC_OVL void +dump_plines() +{ + int i; + char* str; + extern char* saved_plines[]; + + for (i = 0; i < DUMPLOG_MSG_COUNT; ++i) + { + str = saved_plines[DUMPLOG_MSG_COUNT - 1 - i]; + if (str) + putstr(0, 0, str); +#if defined(FREE_ALL_MEMORY) + free(str); +#endif + } +} +#endif + +STATIC_OVL void +dump_everything(how, taken) +int how; +boolean taken; +{ +#if defined(DUMPLOG) + struct obj* obj; + struct topl* topl; + + for (obj = invent; obj; obj = obj->nobj) { + makeknown(obj->otyp); + obj->known = obj->bknown = obj->dknown = obj->rknown = 1; + if (Is_container(obj) || obj->otyp == STATUE) + obj->cknown = obj->lknown = 1; + } + dump_redirect(TRUE); + dump_plines(); + putstr(0, 0, ""); + putstr(0, 0, "Inventory:"); + display_inventory((char *) 0, TRUE); + container_contents(invent, TRUE, TRUE, FALSE); + enlightenment((BASICENLIGHTENMENT | MAGICENLIGHTENMENT), + (how >= PANICKED) ? ENL_GAMEOVERALIVE + : ENL_GAMEOVERDEAD); + putstr(0, 0, ""); + list_vanquished('a', FALSE); + putstr(0, 0, ""); + list_genocided('a', FALSE); + putstr(0, 0, ""); + show_conduct((how >= PANICKED) ? 1 : 2); + putstr(0, 0, ""); + show_overview((how >= PANICKED) ? 1 : 2, how); + putstr(0, 0, ""); + dump_redirect(FALSE); +#endif +} + STATIC_OVL void disclose(how, taken) int how; @@ -954,6 +1011,7 @@ int how; endtime = getnow(); urealtime.realtime += (long) (endtime - urealtime.restored); + dump_open_log(endtime); /* Sometimes you die on the first move. Life's not fair. * On those rare occasions you get hosed immediately, go out * smiling... :-) -3. @@ -1017,6 +1075,7 @@ int how; if (strcmp(flags.end_disclose, "none") && how != PANICKED) disclose(how, taken); + dump_everything(how, taken); /* finish_paybill should be called after disclosure but before bones */ if (bones_ok && taken) @@ -1115,6 +1174,11 @@ int how; } else done_stopprint = 1; /* just avoid any more output */ +#ifdef DUMPLOG + dump_redirect(TRUE); + genl_outrip(0, how, endtime); + dump_redirect(FALSE); +#endif if (u.uhave.amulet) { Strcat(killer.name, " (with the Amulet)"); } else if (how == ESCAPED) { @@ -1125,16 +1189,14 @@ int how; /* don't bother counting to see whether it should be plural */ } - if (!done_stopprint) { - Sprintf(pbuf, "%s %s the %s...", Goodbye(), plname, - (how != ASCENDED) - ? (const char *) ((flags.female && urole.name.f) - ? urole.name.f - : urole.name.m) - : (const char *) (flags.female ? "Demigoddess" : "Demigod")); - putstr(endwin, 0, pbuf); - putstr(endwin, 0, ""); - } + Sprintf(pbuf, "%s %s the %s...", Goodbye(), plname, + (how != ASCENDED) + ? (const char *) ((flags.female && urole.name.f) + ? urole.name.f + : urole.name.m) + : (const char *) (flags.female ? "Demigoddess" : "Demigod")); + dump_forward_putstr(endwin, 0, pbuf, done_stopprint); + dump_forward_putstr(endwin, 0, "", done_stopprint); if (how == ESCAPED || how == ASCENDED) { struct monst *mtmp; @@ -1159,45 +1221,48 @@ int how; /* count the points for artifacts */ artifact_score(invent, TRUE, endwin); +#ifdef DUMPLOG + dump_redirect(TRUE); + artifact_score(invent, TRUE, endwin); + dump_redirect(FALSE); +#endif viz_array[0][0] |= IN_SIGHT; /* need visibility for naming */ mtmp = mydogs; - if (!done_stopprint) - Strcpy(pbuf, "You"); + Strcpy(pbuf, "You"); if (!Schroedingers_cat) /* check here in case disclosure was off */ Schroedingers_cat = odds_and_ends(invent, CAT_CHECK); if (Schroedingers_cat) { int mhp, m_lev = adj_lev(&mons[PM_HOUSECAT]); mhp = d(m_lev, 8); nowrap_add(u.urexp, mhp); - if (!done_stopprint) - Strcat(eos(pbuf), " and Schroedinger's cat"); + Strcat(eos(pbuf), " and Schroedinger's cat"); } if (mtmp) { while (mtmp) { - if (!done_stopprint) - Sprintf(eos(pbuf), " and %s", mon_nam(mtmp)); + Sprintf(eos(pbuf), " and %s", mon_nam(mtmp)); if (mtmp->mtame) nowrap_add(u.urexp, mtmp->mhp); mtmp = mtmp->nmon; } - if (!done_stopprint) - putstr(endwin, 0, pbuf); + dump_forward_putstr(endwin, 0, pbuf, done_stopprint); pbuf[0] = '\0'; } else { - if (!done_stopprint) - Strcat(pbuf, " "); - } - if (!done_stopprint) { - Sprintf(eos(pbuf), "%s with %ld point%s,", - how == ASCENDED ? "went to your reward" - : "escaped from the dungeon", - u.urexp, plur(u.urexp)); - putstr(endwin, 0, pbuf); + Strcat(pbuf, " "); } + Sprintf(eos(pbuf), "%s with %ld point%s,", + how == ASCENDED ? "went to your reward" + : "escaped from the dungeon", + u.urexp, plur(u.urexp)); + dump_forward_putstr(endwin, 0, pbuf, done_stopprint); if (!done_stopprint) artifact_score(invent, FALSE, endwin); /* list artifacts */ +#if DUMPLOG + dump_redirect(TRUE); + artifact_score(invent, FALSE, 0); + dump_redirect(FALSE); +#endif /* list valuables here */ for (val = valuables; val->list; val++) { @@ -1224,11 +1289,11 @@ int how; Sprintf(pbuf, "%8ld worthless piece%s of colored glass,", count, plur(count)); } - putstr(endwin, 0, pbuf); + dump_forward_putstr(endwin, 0, pbuf, 0); } } - } else if (!done_stopprint) { + } else { /* did not escape or ascend */ if (u.uz.dnum == 0 && u.uz.dlevel <= 0) { /* level teleported out of the dungeon; `how' is DIED, @@ -1248,26 +1313,23 @@ int how; } Sprintf(eos(pbuf), " with %ld point%s,", u.urexp, plur(u.urexp)); - putstr(endwin, 0, pbuf); + dump_forward_putstr(endwin, 0, pbuf, done_stopprint); } - if (!done_stopprint) { - Sprintf(pbuf, "and %ld piece%s of gold, after %ld move%s.", umoney, - plur(umoney), moves, plur(moves)); - putstr(endwin, 0, pbuf); - } - if (!done_stopprint) { - Sprintf(pbuf, - "You were level %d with a maximum of %d hit point%s when you %s.", - u.ulevel, u.uhpmax, plur(u.uhpmax), ends[how]); - putstr(endwin, 0, pbuf); - putstr(endwin, 0, ""); - } + Sprintf(pbuf, "and %ld piece%s of gold, after %ld move%s.", umoney, + plur(umoney), moves, plur(moves)); + dump_forward_putstr(endwin, 0, pbuf, done_stopprint); + Sprintf(pbuf, + "You were level %d with a maximum of %d hit point%s when you %s.", + u.ulevel, u.uhpmax, plur(u.uhpmax), ends[how]); + dump_forward_putstr(endwin, 0, pbuf, done_stopprint); + dump_forward_putstr(endwin, 0, "", done_stopprint); if (!done_stopprint) display_nhwindow(endwin, TRUE); if (endwin != WIN_ERR) destroy_nhwindow(endwin); + dump_close_log(); /* "So when I die, the first thing I will see in Heaven is a * score list?" */ if (have_windows && !iflags.toptenwin) diff --git src/pline.c src/pline.c index ce4b6070..810e1239 100644 --- src/pline.c +++ src/pline.c @@ -11,6 +11,10 @@ static char prevmsg[BUFSZ]; static char *FDECL(You_buf, (int)); +#ifdef DUMPLOG +char* saved_plines[DUMPLOG_MSG_COUNT] = {0}; +#endif + /*VARARGS1*/ /* Note that these declarations rely on knowledge of the internals * of the variable argument handling stuff in "tradstdc.h" @@ -83,6 +87,14 @@ VA_DECL(const char *, line) iflags.last_msg = PLNMSG_UNKNOWN; return; } +#ifdef DUMPLOG + /* We hook here early to have options-agnostic output. */ + free(saved_plines[DUMPLOG_MSG_COUNT - 1]); + for (ln = 0; ln < DUMPLOG_MSG_COUNT - 1; ++ln) + saved_plines[DUMPLOG_MSG_COUNT - ln - 1] = saved_plines[DUMPLOG_MSG_COUNT - ln - 2]; + saved_plines[0] = malloc(strlen(line) + 1); + (void) strcpy(saved_plines[0], line); +#endif #ifndef MAC if (no_repeat && !strcmp(line, toplines)) return; diff --git src/windows.c src/windows.c index 1423f8cf..51a9757a 100644 --- src/windows.c +++ src/windows.c @@ -932,4 +932,194 @@ anything threshold; #endif /* STATUS_HILITES */ #endif /* STATUS_VIA_WINDOWPORT */ +STATIC_VAR struct window_procs dumplog_windowprocs_backup; +STATIC_PTR FILE* dumplog_file; + +void +dump_open_log(now) +time_t now; +{ +#if defined(DUMPLOG) + static const char* fname = DUMPLOG_FILE; + char buf[1024]; + int i; + int j; + char c; + + j = 0; + for (i = 0; i < strlen(fname); ++i) + { + c = fname[i]; + if (c == '%') + { + c = fname[i + 1]; + if (c == '%') + buf[j++] = '%'; + else if (c == 'n') + { + strncpy(buf + j, plname, 1023 - j); + j += strlen(plname); + } + else if (c == 'd') + j += strftime(buf + j, 1023 - j, "%Y%m%d%H%M%S", localtime(&now)); + i++; + } + else + buf[j++] = c; + if (j >= 1023) + break; + } + buf[j] = 0; + dumplog_file = fopen(buf, "w"); + dumplog_windowprocs_backup = windowprocs; +#endif +} + +void +dump_close_log() +{ + if (dumplog_file) + { + fclose(dumplog_file); + dumplog_file = NULL; + } +} + +void +dump_putc(ch) +int ch; +{ + /* Not very efficient, but we mostly don't care. */ + if (dumplog_file) + putc(ch, dumplog_file); +} + +void +dump_forward_putstr(win, attr, str, no_forward) +winid win; +int attr; +const char* str; +int no_forward; +{ + if (dumplog_file) + fprintf(dumplog_file, "%s\n", str); + if (!no_forward) + putstr(win, attr, str); +} + +STATIC_OVL void +dump_putstr(win, attr, str) +winid win; +int attr; +const char* str; +{ + if (dumplog_file) + fprintf(dumplog_file, "%s\n", str); +} + +STATIC_OVL winid +dump_create_nhwindow(dummy) +int dummy; +{ + return dummy; +} + +STATIC_OVL void +dump_clear_nhwindow(win) +winid win; +{ + +} + +STATIC_OVL void +dump_display_nhwindow(win, p) +winid win; +BOOLEAN_P p; +{ + +} + +STATIC_OVL void +dump_destroy_nhwindow(win) +winid win; +{ + +} + +STATIC_OVL void +dump_start_menu(win) +winid win; +{ + +} + +STATIC_OVL void +dump_add_menu(win, glyph, identifier, ch, gch, attr, str, preselected) +winid win; +int glyph; +const ANY_P* identifier; +CHAR_P ch; +CHAR_P gch; +int attr; +const char* str; +BOOLEAN_P preselected; +{ + if (dumplog_file) + { + if (glyph == NO_GLYPH) + fprintf(dumplog_file, "%s\n", str); + else + fprintf(dumplog_file, "%c - %s\n", ch, str); + } +} + +STATIC_OVL void +dump_end_menu(win, str) +winid win; +const char* str; +{ + if (dumplog_file) + { + if (str) + fprintf(dumplog_file, "%s\n", str); + else + fputs("\n", dumplog_file); + } +} + +STATIC_OVL int +dump_select_menu(win, index, item) +winid win; +int index; +MENU_ITEM_P** item; +{ + *item = NULL; + return 0; +} + +void +dump_redirect(flag) +boolean flag; +{ + if (dumplog_file) + { + if (flag) + { + windowprocs.win_create_nhwindow = dump_create_nhwindow; + windowprocs.win_clear_nhwindow = dump_clear_nhwindow; + windowprocs.win_display_nhwindow = dump_display_nhwindow; + windowprocs.win_destroy_nhwindow = dump_destroy_nhwindow; + windowprocs.win_start_menu = dump_start_menu; + windowprocs.win_add_menu = dump_add_menu; + windowprocs.win_end_menu = dump_end_menu; + windowprocs.win_select_menu = dump_select_menu; + windowprocs.win_putstr = dump_putstr; + } + else + { + windowprocs = dumplog_windowprocs_backup; + } + } +} + /*windows.c*/ -- 2.11.0