From 4000c36207d475d52ea646d9a66dd05e04828cd5 Mon Sep 17 00:00:00 2001 From: haranp Date: Sun, 8 Feb 2009 23:01:50 +0000 Subject: Fix [2151183]: SIGHUP with the range view annotator active maintained the annotated colours when saving. Fixed by adding the ability to store arbitrary exit hooks to be executed on (unusual) shutdown; for now they're executed FIFO, but it should probably be LIFO with a stack. Anyway the only thing that uses this now is the range view annotator. Oh, and it only works if you're using libunix.cc. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@8999 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/crash-u.cc | 2 +- crawl-ref/source/directn.cc | 76 ++++++++++++++++++++++++++------------------- crawl-ref/source/directn.h | 18 ++++++++--- crawl-ref/source/externs.h | 3 +- crawl-ref/source/libunix.cc | 6 ++++ crawl-ref/source/state.h | 11 +++++++ 6 files changed, 77 insertions(+), 39 deletions(-) (limited to 'crawl-ref') diff --git a/crawl-ref/source/crash-u.cc b/crawl-ref/source/crash-u.cc index 8a1f3d000d..ee8d033f24 100644 --- a/crawl-ref/source/crash-u.cc +++ b/crawl-ref/source/crash-u.cc @@ -150,7 +150,7 @@ void write_stack_trace(FILE* file, int ignore_count) if (symbols == NULL) { - fprintf(stderr, "Out of memroy." EOL); + fprintf(stderr, "Out of memory." EOL); fprintf(file, "Out of memory." EOL); // backtrace_symbols_fd() can print out the stack trace even if diff --git a/crawl-ref/source/directn.cc b/crawl-ref/source/directn.cc index 4d314402b2..3bfc704e25 100644 --- a/crawl-ref/source/directn.cc +++ b/crawl-ref/source/directn.cc @@ -20,7 +20,7 @@ REVISION("$Rev$"); #include #ifdef DOS - #include +#include #endif #include "externs.h" @@ -718,14 +718,25 @@ static int _mlist_letter_to_index(char idx) } #endif -class range_view_annotator +crawl_exit_hook::crawl_exit_hook() { -public: - range_view_annotator(int range) { - do_anything = (range >= 0); - if (range < 0) - return; + crawl_state.exit_hooks.push_back(this); +} + +crawl_exit_hook::~crawl_exit_hook() +{ + crawl_state.exit_hooks.erase(std::remove(crawl_state.exit_hooks.begin(), + crawl_state.exit_hooks.end(), + this), + crawl_state.exit_hooks.end()); +} + +range_view_annotator::range_view_annotator(int range) +{ + do_anything = (range >= 0); + if (do_anything) + { // Save and replace grid colours. -1 means unchanged. orig_colours.init(-1); const coord_def offset(ENV_SHOW_OFFSET, ENV_SHOW_OFFSET); @@ -757,38 +768,39 @@ public: // Repaint. viewwindow(true, false); } +} - ~range_view_annotator() { +range_view_annotator::~range_view_annotator() +{ + restore_state(); + viewwindow(true, false); +} - if (!do_anything) - return; +void range_view_annotator::restore_state() +{ + if (!do_anything) + return; - // Restore grid colours. - coord_def c; - const coord_def offset(ENV_SHOW_OFFSET, ENV_SHOW_OFFSET); - for ( c.x = 0; c.x < ENV_SHOW_DIAMETER; ++c.x ) + // Restore grid colours. + coord_def c; + const coord_def offset(ENV_SHOW_OFFSET, ENV_SHOW_OFFSET); + for ( c.x = 0; c.x < ENV_SHOW_DIAMETER; ++c.x ) + { + for ( c.y = 0; c.y < ENV_SHOW_DIAMETER; ++c.y ) { - for ( c.y = 0; c.y < ENV_SHOW_DIAMETER; ++c.y ) - { - const int old_colour = orig_colours(c); - if ( old_colour != -1 ) - env.grid_colours(you.pos() + c - offset) = old_colour; - } + const int old_colour = orig_colours(c); + if ( old_colour != -1 ) + env.grid_colours(you.pos() + c - offset) = old_colour; } + } - // Restore monster colours. - for (int i = 0; i < MAX_MONSTERS; ++i) - if (orig_mon_colours[i] != -1) - menv[i].colour = orig_mon_colours[i]; + // Restore monster colours. + for (int i = 0; i < MAX_MONSTERS; ++i) + if (orig_mon_colours[i] != -1) + menv[i].colour = orig_mon_colours[i]; - // Repaint. - viewwindow(true, false); - } -private: - bool do_anything; - FixedArray orig_colours; - int orig_mon_colours[MAX_MONSTERS]; -}; + do_anything = false; +} bool _dist_ok(const dist& moves, int range, targ_mode_type mode, bool may_target_self, bool cancel_at_self) diff --git a/crawl-ref/source/directn.h b/crawl-ref/source/directn.h index c20a3c792d..381e130ac0 100644 --- a/crawl-ref/source/directn.h +++ b/crawl-ref/source/directn.h @@ -14,6 +14,19 @@ #include "enum.h" #include "libgui.h" #include "ray.h" +#include "state.h" + +class range_view_annotator : public crawl_exit_hook +{ +public: + range_view_annotator(int range); + virtual ~range_view_annotator(); + virtual void restore_state(); +private: + bool do_anything; + FixedArray orig_colours; + int orig_mon_colours[MAX_MONSTERS]; +}; class crawl_view_buffer { @@ -28,11 +41,6 @@ private: screen_buffer_t *buffer; }; -// last updated 12may2000 {dlb} -/* *********************************************************************** - * called from: acr - debug - effects - it_use3 - item_use - spells1 - - * spells2 - spells3 - spells4 - * *********************************************************************** */ struct crawl_view_geometry { public: diff --git a/crawl-ref/source/externs.h b/crawl-ref/source/externs.h index 7149b5c265..41e4a0fecf 100644 --- a/crawl-ref/source/externs.h +++ b/crawl-ref/source/externs.h @@ -1679,6 +1679,7 @@ private: typedef FixedArray feature_grid; typedef FixedArray env_show_grid; +class crawl_exit_hook; struct crawl_environment { @@ -1728,7 +1729,7 @@ public: map_markers markers; // Place to associate arbitrary data with a particular level. - // Sort of like player::atribute + // Sort of like player::attribute CrawlHashTable properties; // Rate at which random monsters spawn, with lower numbers making diff --git a/crawl-ref/source/libunix.cc b/crawl-ref/source/libunix.cc index e1d3b71df4..c259c45146 100644 --- a/crawl-ref/source/libunix.cc +++ b/crawl-ref/source/libunix.cc @@ -335,6 +335,12 @@ static void handle_hangup(int) crawl_state.saving_game = true; if (crawl_state.need_save) { + // Clean up all the hooks. + for (unsigned i = 0; i < crawl_state.exit_hooks.size(); ++i) + crawl_state.exit_hooks[i]->restore_state(); + + crawl_state.exit_hooks.clear(); + // save_game(true) also exits, saving us the trouble of doing so. save_game(true); } diff --git a/crawl-ref/source/state.h b/crawl-ref/source/state.h index ddfc73b969..397bd510ef 100644 --- a/crawl-ref/source/state.h +++ b/crawl-ref/source/state.h @@ -15,6 +15,14 @@ class monsters; class mon_acting; +class crawl_exit_hook +{ +public: + crawl_exit_hook(); + virtual ~crawl_exit_hook(); + virtual void restore_state() = 0; +}; + struct god_act_state { public: @@ -76,6 +84,9 @@ struct game_state std::vector input_line_strs; unsigned int input_line_curr; + // Hooks to call if get shut down unexpectedly. + std::vector exit_hooks; + bool level_annotation_shown; protected: -- cgit v1.2.3-54-g00ecf