From: Pasi Kallinen Date: Tue, 31 Oct 2017 19:18:21 +0000 (+0200) Subject: X11: Fix renaming at player selection X-Git-Tag: NetHack-3.6.1_RC01~230 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fe9762d1cc82b30d7ff001128c49460a92f16d2c;p=nethack X11: Fix renaming at player selection Due to the new player selection dialog I did, it was possible to rename your character - but this didn't rename the lock files and tried to load a save from the wrong name. This is a bit of a hack, but seems to work and didn't seem to cause problems for the tty. --- diff --git a/include/flag.h b/include/flag.h index 3bccbaa18..81630bd27 100644 --- a/include/flag.h +++ b/include/flag.h @@ -191,6 +191,7 @@ struct instance_flags { * behaviour of various NetHack functions and probably warrant * a structure of their own elsewhere some day. */ + boolean defer_plname; /* X11 hack: askname() might not set plname */ boolean herecmd_menu; /* use menu when mouseclick on yourself */ boolean invis_goldsym; /* gold symbol is ' '? */ int parse_config_file_src; /* hack for parse_config_line() */ diff --git a/src/role.c b/src/role.c index 5a1a4ab56..fd23f3adb 100644 --- a/src/role.c +++ b/src/role.c @@ -1714,7 +1714,7 @@ plnamesuffix() do { if (!*plname) - askname(); /* fill plname[] if necessary */ + askname(); /* fill plname[] if necessary, or set defer_plname */ /* Look for tokens delimited by '-' */ if ((eptr = index(plname, '-')) != (char *) 0) @@ -1735,7 +1735,7 @@ plnamesuffix() else if ((i = str2align(sptr)) != ROLE_NONE) flags.initalign = i; } - } while (!*plname); + } while (!*plname && !iflags.defer_plname); /* commas in the plname confuse the record file, convert to spaces */ for (sptr = plname; *sptr; sptr++) { diff --git a/sys/unix/hints/linux-x11 b/sys/unix/hints/linux-x11 index a0d5552e2..05d7e7304 100644 --- a/sys/unix/hints/linux-x11 +++ b/sys/unix/hints/linux-x11 @@ -20,7 +20,7 @@ VARDIR = $(HACKDIR) POSTINSTALL= cp -n sys/unix/sysconf $(INSTDIR)/sysconf; $(CHOWN) $(GAMEUID) $(INSTDIR)/sysconf; $(CHGRP) $(GAMEGRP) $(INSTDIR)/sysconf; chmod $(VARFILEPERM) $(INSTDIR)/sysconf; POSTINSTALL+= bdftopcf win/X11/nh10.bdf > $(INSTDIR)/nh10.pcf; (cd $(INSTDIR); mkfontdir); -CFLAGS=-O -I../include -DNOTPARMDECL +CFLAGS=-g -O -I../include -DNOTPARMDECL CFLAGS+=-DSYSCF -DSYSCF_FILE=\"$(HACKDIR)/sysconf\" CFLAGS+=-DCOMPRESS=\"/bin/gzip\" -DCOMPRESS_EXTENSION=\".gz\" CFLAGS+=-DX11_GRAPHICS -DDEFAULT_WINDOW_SYS=\"X11\" -DNOTTYGRAPHICS diff --git a/sys/unix/unixmain.c b/sys/unix/unixmain.c index 2e8f41330..b9ab44526 100644 --- a/sys/unix/unixmain.c +++ b/sys/unix/unixmain.c @@ -55,6 +55,7 @@ char *argv[]; #endif boolean exact_username; boolean resuming = FALSE; /* assume new game */ + boolean plsel_once = FALSE; sys_early_init(); @@ -237,19 +238,6 @@ char *argv[]; (void) signal(SIGQUIT, SIG_IGN); (void) signal(SIGINT, SIG_IGN); } - /* - * getlock() complains and quits if there is already a game - * in progress for current character name (when locknum == 0) - * or if there are too many active games (when locknum > 0). - * When proceeding, it creates an empty .0 file to - * designate the current game. - * getlock() constructs based on the character - * name (for !locknum) or on first available of alock, block, - * clock, &c not currently in use in the playground directory - * (for locknum > 0). - */ - getlock(); - program_state.preserve_locks = 0; /* after getlock() */ dlb_init(); /* must be before newgame() */ @@ -266,7 +254,24 @@ char *argv[]; * We'll return here if new game player_selection() renames the hero. */ attempt_restore: - if ((fd = restore_saved_game()) >= 0) { + + /* + * getlock() complains and quits if there is already a game + * in progress for current character name (when locknum == 0) + * or if there are too many active games (when locknum > 0). + * When proceeding, it creates an empty .0 file to + * designate the current game. + * getlock() constructs based on the character + * name (for !locknum) or on first available of alock, block, + * clock, &c not currently in use in the playground directory + * (for locknum > 0). + */ + if (*plname) { + getlock(); + program_state.preserve_locks = 0; /* after getlock() */ + } + + if (*plname && (fd = restore_saved_game()) >= 0) { const char *fq_save = fqname(SAVEF, SAVEPREFIX, 1); (void) chmod(fq_save, 0); /* disallow parallel restores */ @@ -297,12 +302,17 @@ attempt_restore: } if (!resuming) { + boolean neednewlock = (!*plname); /* new game: start by choosing role, race, etc; player might change the hero's name while doing that, in which case we try to restore under the new name and skip selection this time if that didn't succeed */ - if (!iflags.renameinprogress) { - player_selection(); + if (!iflags.renameinprogress || iflags.defer_plname || neednewlock) { + if (!plsel_once) + player_selection(); + plsel_once = TRUE; + if (neednewlock && *plname) + goto attempt_restore; if (iflags.renameinprogress) { /* player has renamed the hero while selecting role; if locking alphabetically, the existing lock file diff --git a/win/X11/winX.c b/win/X11/winX.c index ff9b0ff51..6b285a2e5 100644 --- a/win/X11/winX.c +++ b/win/X11/winX.c @@ -1474,18 +1474,11 @@ X11_askname() { Widget popup, dialog; Arg args[1]; - char *defplname = (char *)0; - -#ifdef UNIX - defplname = get_login_name(); -#endif - (void) strncpy(plname, defplname ? defplname : "Mumbles", - sizeof plname - 1); - plname[sizeof plname - 1] = '\0'; if (iflags.wc_player_selection == VIA_DIALOG) { /* X11_player_selection_dialog() handles name query */ plsel_ask_name = TRUE; + iflags.defer_plname = TRUE; return; } /* else iflags.wc_player_selection == VIA_PROMPTS */ diff --git a/win/X11/winmisc.c b/win/X11/winmisc.c index 8ddb62a79..945e35fc4 100644 --- a/win/X11/winmisc.c +++ b/win/X11/winmisc.c @@ -352,8 +352,13 @@ plsel_dialog_acceptvalues() XtSetArg(args[0], nhStr(XtNstring), &s); XtGetValues(plsel_name_input, args, ONE); + (void) strncpy(plname, (char *) s, sizeof plname - 1); plname[sizeof plname - 1] = '\0'; + (void) mungspaces(plname); + if (strlen(plname) < 1) + (void) strcpy(plname, "Mumbles"); + iflags.renameinprogress = FALSE; } /* ARGSUSED */ @@ -1553,6 +1558,17 @@ void X11_player_selection() { if (iflags.wc_player_selection == VIA_DIALOG) { + if (!*plname) { +#ifdef UNIX + char *defplname = get_login_name(); +#else + char *defplname = (char *)0; +#endif + (void) strncpy(plname, defplname ? defplname : "Mumbles", + sizeof plname - 1); + plname[sizeof plname - 1] = '\0'; + iflags.renameinprogress = TRUE; + } X11_player_selection_dialog(); } else { /* iflags.wc_player_selection == VIA_PROMPTS */ X11_player_selection_prompts();