From 43f72cc3578f8a29ad1ffeb2d6146133a03af1d4 Mon Sep 17 00:00:00 2001 From: "nethack.rankin" Date: Sat, 17 Feb 2007 05:25:36 +0000 Subject: [PATCH] yet more wizard mode handling (trunk only) Reorganize the recent wizard mode control: move set_playmode() from xxxmain.c to the core, and have it call new authorize_wizard_mode() to do the port-specific part. If the set_playmode() call during startup doesn't result in running in wizard mode (either because not allowed or user didn't request it), it will be called again during restore if the save file is from a wizard mode game. For ports which check character name for authorization, players will have to use `nethack -u whatever -D' (or options for name and playmode) to restore a wizard mode save file if WIZARD has been changed from "wizard". plname[] from a wizard mode saved game will always have that value, so if it's not the right one players will need to get authorized by the startup code before loading the save file. --- include/extern.h | 9 ++++++++- src/options.c | 20 ++++++++++++++++++++ src/restore.c | 13 ++++++------- sys/be/bemain.c | 21 ++++----------------- sys/mac/macmain.c | 21 ++++----------------- sys/share/pcmain.c | 23 ++++------------------- sys/unix/unixmain.c | 24 +++++------------------- sys/vms/vmsmain.c | 22 ++++------------------ 8 files changed, 55 insertions(+), 98 deletions(-) diff --git a/include/extern.h b/include/extern.h index 4a3320078..a878d569c 100644 --- a/include/extern.h +++ b/include/extern.h @@ -988,6 +988,10 @@ E int FDECL(macwrite, (int,void *,unsigned)); E long FDECL(macseek, (int,long,short)); E int FDECL(macunlink, (const char *)); +/* ### macmain.c ### */ + +E boolean NDECL(authorize_wizard_mode); + /* ### macsnd.c ### */ E void FDECL(mac_speaker, (struct obj *,char *)); @@ -1574,6 +1578,7 @@ E int FDECL(load_symset, (const char *,int)); E void FDECL(parsesymbols, (char *)); E struct symparse *FDECL(match_sym, (char *)); #endif +E void NDECL(set_playmode); /* ### pager.c ### */ @@ -1592,6 +1597,7 @@ E int FDECL(do_look, (int, coord *)); # ifdef CHDIR E void FDECL(chdirx, (char *,BOOLEAN_P)); # endif /* CHDIR */ +E boolean NDECL(authorize_wizard_mode); #endif /* MICRO || WIN32 */ /* ### pcsys.c ### */ @@ -2280,9 +2286,9 @@ E int FDECL(flash_hits_mon, (struct monst *,struct obj *)); E void NDECL(port_help); # endif E void FDECL(sethanguphandler, (void (*)(int))); +E boolean NDECL(authorize_wizard_mode); #endif /* UNIX */ - /* ### unixtty.c ### */ #if defined(UNIX) || defined(__BEOS__) @@ -2409,6 +2415,7 @@ E int FDECL(main, (int, char **)); E void FDECL(chdirx, (const char *,BOOLEAN_P)); # endif /* CHDIR */ E void FDECL(sethanguphandler, (void (*)(int))); +E boolean NDECL(authorize_wizard_mode); /* ### vmsmisc.c ### */ diff --git a/src/options.c b/src/options.c index e3e8e9c35..ed39ac0e7 100644 --- a/src/options.c +++ b/src/options.c @@ -4485,6 +4485,26 @@ char *op; return 1; } +/* set up for wizard mode if player or save file has requested to it; + called from port-specific startup code to handle `nethack -D' or + OPTIONS=playmode:debug, or from dorecover()'s restgamestate() if + restoring a game which was saved in wizard mode */ +void +set_playmode() +{ + if (wizard) { +#ifdef WIZARD + if (authorize_wizard_mode()) + Strcpy(plname, "wizard"); + else +#endif + wizard = FALSE; /* not allowed or not available */ + /* force explore mode if we didn't make it into wizard mode */ + discover = !wizard; + } + /* don't need to do anything special for explore mode or normal play */ +} + #endif /* OPTION_LISTS_ONLY */ /*options.c*/ diff --git a/src/restore.c b/src/restore.c index 39dc2def9..ed50634e1 100644 --- a/src/restore.c +++ b/src/restore.c @@ -546,13 +546,12 @@ unsigned int *stuckid, *steedid; /* STEED */ /* wizard and discover are actually flags.debug and flags.explore; player might be overriding the save file values for them */ if (newgameflags.explore) discover = TRUE; - if (newgameflags.debug) wizard = TRUE; - if (wizard) { -#ifdef WIZARD - discover = FALSE; -#else - discover = TRUE, wizard = FALSE; -#endif + if (newgameflags.debug) { + /* authorized by startup code; wizard mode exists and is allowed */ + wizard = TRUE, discover = FALSE; + } else if (wizard) { + /* specified by save file; check authorization now */ + set_playmode(); } #ifdef SYSFLAGS newgamesysflags = sysflags; diff --git a/sys/be/bemain.c b/sys/be/bemain.c index bae1c1e3c..4050d10fb 100644 --- a/sys/be/bemain.c +++ b/sys/be/bemain.c @@ -11,8 +11,6 @@ static void process_options(int argc, char **argv); static void chdirx(const char *dir); static void getlock(void); -static void NDECL(set_playmode); - #ifdef __begui__ #define MAIN nhmain int nhmain(int argc, char **argv); @@ -238,26 +236,15 @@ void getlock(void) } /* validate wizard mode if player has requested access to it */ -static void -set_playmode() +boolean +authorize_wizard_mode() { - if (wizard) { #ifdef WIZARD /* other ports validate user name or character name here */ + return TRUE; #else - wizard = FALSE; -#endif - - if (!wizard) { - discover = TRUE; -#ifdef WIZARD - } else { - discover = FALSE; /* paranoia */ - Strcpy(plname, "wizard"); + return FALSE; #endif - } - } - /* don't need to do anything special for explore mode or normal play */ } #ifndef __begui__ diff --git a/sys/mac/macmain.c b/sys/mac/macmain.c index 051118d73..976352b43 100644 --- a/sys/mac/macmain.c +++ b/sys/mac/macmain.c @@ -24,8 +24,6 @@ #include #endif -static void NDECL(set_playmode); - static void finder_file_request(void); int main(void); @@ -271,26 +269,15 @@ finder_file_request(void) } /* validate wizard mode if player has requested access to it */ -static void -set_playmode() +boolean +authorize_wizard_mode() { - if (wizard) { #ifdef WIZARD /* other ports validate user name or character name here */ + return TRUE; #else - wizard = FALSE; -#endif - - if (!wizard) { - discover = TRUE; -#ifdef WIZARD - } else { - discover = FALSE; /* paranoia */ - Strcpy(plname, "wizard"); + return FALSE; #endif - } - } - /* don't need to do anything special for explore mode or normal play */ } /*macmain.c*/ diff --git a/sys/share/pcmain.c b/sys/share/pcmain.c index d970dab48..9cf939004 100644 --- a/sys/share/pcmain.c +++ b/sys/share/pcmain.c @@ -66,8 +66,6 @@ extern int redirect_stdout; /* from sys/share/pcsys.c */ extern void NDECL(mswin_destroy_reg); #endif -STATIC_DCL void NDECL(set_playmode); - #ifdef EXEPATH STATIC_DCL char *FDECL(exepath,(char *)); #endif @@ -697,26 +695,13 @@ port_help() #endif /* validate wizard mode if player has requested access to it */ -STATIC_OVL void -set_playmode() +boolean +authorize_wizard_mode() { - if (wizard) { #ifdef WIZARD - if (strcmp(plname, WIZARD_NAME)) wizard = FALSE; -#else - wizard = FALSE; + if (!strcmp(plname, WIZARD_NAME)) return TRUE; #endif - - if (!wizard) { - discover = TRUE; -#ifdef WIZARD - } else { - discover = FALSE; /* paranoia */ - Strcpy(plname, "wizard"); -#endif - } - } - /* don't need to do anything special for explore mode or normal play */ + return FALSE; } #ifdef EXEPATH diff --git a/sys/unix/unixmain.c b/sys/unix/unixmain.c index 2ab066e39..5d40e5f02 100644 --- a/sys/unix/unixmain.c +++ b/sys/unix/unixmain.c @@ -39,7 +39,6 @@ extern void NDECL(check_linux_console); extern void NDECL(init_linux_cons); #endif -static void NDECL(set_playmode); static void NDECL(wd_message); static boolean wiz_error_flag = FALSE; @@ -507,10 +506,9 @@ port_help() #endif /* validate wizard mode if player has requested access to it */ -static void -set_playmode() +boolean +authorize_wizard_mode() { - if (wizard) { #ifdef WIZARD char *user; int uid; @@ -532,21 +530,9 @@ set_playmode() pw = getpwuid(uid); } } - if (!pw || strcmp(pw->pw_name, WIZARD_NAME)) wizard = FALSE; -#else /* !WIZARD */ - wizard = FALSE; -#endif /* ?WIZARD */ - - if (!wizard) { - discover = wiz_error_flag = TRUE; -#ifdef WIZARD - } else { - discover = FALSE; /* paranoia */ - Strcpy(plname, "wizard"); -#endif - } - } - /* don't need to do anything special for explore mode or normal play */ + if (pw && !strcmp(pw->pw_name, WIZARD_NAME)) return TRUE; +#endif /* WIZARD */ + return FALSE; } static void diff --git a/sys/vms/vmsmain.c b/sys/vms/vmsmain.c index 9af7b859f..3bdb5dcb0 100644 --- a/sys/vms/vmsmain.c +++ b/sys/vms/vmsmain.c @@ -22,7 +22,6 @@ static vms_handler_type FDECL(vms_handler, (genericptr_t,genericptr_t)); #include /* system service status codes */ #endif -static void NDECL(set_playmode); static void NDECL(wd_message); static boolean wiz_error_flag = FALSE; @@ -435,26 +434,13 @@ port_help() to match it, avoiding need to test which one to use in string ops */ /* validate wizard mode if player has requested access to it */ -static void -set_playmode() +boolean +authorize_wizard_mode() { - if (wizard) { -#ifdef WIZARD - if (strcmpi(nh_getenv("USER"), WIZARD_NAME)) wizard = FALSE; -#else - wizard = FALSE; -#endif - - if (!wizard) { - discover = wiz_error_flag = TRUE; #ifdef WIZARD - } else { - discover = FALSE; /* paranoia */ - Strcpy(plname, "wizard"); + if (!strcmpi(nh_getenv("USER"), WIZARD_NAME)) return TRUE; #endif - } - } - /* don't need to do anything special for explore mode or normal play */ + return FALSE; } static void -- 2.40.0