Changeset - r5520:aa1247f0561e
[Not reviewed]
master
0 1 0
Darkvater - 18 years ago 2007-01-04 17:23:55
darkvater@openttd.org
(svn r7823) -Fix (r7384 / r7368 / r3757 / r7388): News windows could still cause crashes because
DeleteVehicleNews shuffles around _news_items which can wreak havoc with the NewsItem*
of a currently open news window. While here also correctly update _current_news and
_forced_news for the same reasons. Should really work now.
1 file changed with 30 insertions and 11 deletions:
0 comments (0 inline, 0 general)
src/news_gui.c
Show inline comments
 
@@ -266,76 +266,79 @@ void AddNewsItem(StringID string, uint32
 
{
 
	NewsID l_news;
 

	
 
	if (_game_mode == GM_MENU) return;
 

	
 
	// check the rare case that the oldest (to be overwritten) news item is open
 
	if (_total_news == MAX_NEWS && (_oldest_news == _current_news || _oldest_news == _forced_news))
 
		MoveToNextItem();
 

	
 
	_forced_news = INVALID_NEWS;
 
	if (_total_news < MAX_NEWS) _total_news++;
 

	
 
	/* Increase _latest_news. If we have no news yet, use _oldest news as an
 
	 * index. We cannot use 0 as _oldest_news can jump around due to
 
	 * DeleteVehicleNews */
 
	l_news = _latest_news;
 
	_latest_news = (_latest_news == INVALID_NEWS) ? _oldest_news : increaseIndex(_latest_news);
 

	
 
	/* If the fifo-buffer is full, overwrite the oldest entry */
 
	if (l_news != INVALID_NEWS && _latest_news == _oldest_news) {
 
		assert(_total_news == MAX_NEWS);
 
		_oldest_news = increaseIndex(_oldest_news);
 
	}
 

	
 
	/*DEBUG(misc, 0) ("+cur %3d, old %2d, lat %3d, for %3d, tot %2d",
 
	  _current_news, _oldest_news, _latest_news, _forced_news, _total_news); */
 

	
 
	{ /* Add news to _latest_news */
 
		Window *w;
 
		NewsItem *ni = &_news_items[_latest_news];
 
		memset(ni, 0, sizeof(*ni));
 

	
 
		ni->string_id = string;
 
		ni->display_mode = (byte)flags;
 
		ni->flags = (byte)(flags >> 8);
 

	
 
		// show this news message in color?
 
		if (_cur_year >= _patches.colored_news_year) ni->flags |= NF_INCOLOR;
 

	
 
		ni->type = (byte)(flags >> 16);
 
		ni->callback = (byte)(flags >> 24);
 
		ni->data_a = data_a;
 
		ni->data_b = data_b;
 
		ni->date = _date;
 
		COPY_OUT_DPARAM(ni->params, 0, lengthof(ni->params));
 

	
 
		w = FindWindowById(WC_MESSAGE_HISTORY, 0);
 
		if (w == NULL) return;
 
		SetWindowDirty(w);
 
		w->vscroll.count = _total_news;
 
	}
 
}
 

	
 

	
 
// don't show item if it's older than x days
 
/* Don't show item if it's older than x days, corresponds with NewsType in news.h */
 
static const byte _news_items_age[] = {60, 60, 90, 60, 90, 30, 150, 30, 90, 180};
 

	
 
static const Widget _news_type13_widgets[] = {
 
{      WWT_PANEL,   RESIZE_NONE,    15,     0,   429,     0,   169, 0x0, STR_NULL},
 
{      WWT_PANEL,   RESIZE_NONE,    15,     0,    10,     0,    11, 0x0, STR_NULL},
 
{   WIDGETS_END},
 
};
 

	
 
static WindowDesc _news_type13_desc = {
 
	WDP_CENTER, 476, 430, 170,
 
	WC_NEWS_WINDOW, 0,
 
	WDF_DEF_WIDGET,
 
	_news_type13_widgets,
 
	NewsWindowProc
 
};
 

	
 
static const Widget _news_type2_widgets[] = {
 
{      WWT_PANEL,   RESIZE_NONE,    15,     0,   429,     0,   129, 0x0, STR_NULL},
 
{      WWT_PANEL,   RESIZE_NONE,    15,     0,    10,     0,    11, 0x0, STR_NULL},
 
{   WIDGETS_END},
 
};
 

	
 
static WindowDesc _news_type2_desc = {
 
	WDP_CENTER, 476, 430, 130,
 
@@ -417,48 +420,52 @@ static void ShowNewspaper(NewsItem *ni)
 
			if (ni->flags & NF_VIEWPORT)
 
				AssignWindowViewport(w, 2, 58, 0x1AA, 0x6E,
 
					ni->data_a | (ni->flags & NF_VEHICLE ? 0x80000000 : 0), 0);
 
			break;
 
		}
 

	
 
		case NM_THIN: {
 
			_news_type2_desc.top = top;
 
			w = AllocateWindowDesc(&_news_type2_desc);
 
			if (ni->flags & NF_VIEWPORT)
 
				AssignWindowViewport(w, 2, 58, 0x1AA, 0x46,
 
					ni->data_a | (ni->flags & NF_VEHICLE ? 0x80000000 : 0), 0);
 
			break;
 
		}
 

	
 
		default: {
 
			_news_type0_desc.top = top;
 
			w = AllocateWindowDesc(&_news_type0_desc);
 
			if (ni->flags & NF_VIEWPORT)
 
				AssignWindowViewport(w, 3, 17, 0x112, 0x2F,
 
					ni->data_a | (ni->flags & NF_VEHICLE ? 0x80000000 : 0), 0);
 
			break;
 
		}
 
	}
 

	
 
	/*DEBUG(misc, 0) (" cur %3d, old %2d, lat %3d, for %3d, tot %2d",
 
	  _current_news, _oldest_news, _latest_news, _forced_news, _total_news); */
 

	
 
	WP(w, news_d).ni = &_news_items[_forced_news == INVALID_NEWS ? _current_news : _forced_news];
 
	w->flags4 |= WF_DISABLE_VP_SCROLL;
 
}
 

	
 
// show news item in the ticker
 
static void ShowTicker(const NewsItem *ni)
 
{
 
	Window *w;
 

	
 
	if (_news_ticker_sound) SndPlayFx(SND_16_MORSE);
 

	
 
	_statusbar_news_item = *ni;
 
	w = FindWindowById(WC_STATUS_BAR, 0);
 
	if (w != NULL) WP(w, def_d).data_1 = 360;
 
}
 

	
 

	
 
// Are we ready to show another news item?
 
// Only if nothing is in the newsticker and no newspaper is displayed
 
static bool ReadyForNextItem(void)
 
{
 
	const Window *w;
 
	NewsID item = (_forced_news == INVALID_NEWS) ? _current_news : _forced_news;
 
	NewsItem *ni;
 
@@ -864,59 +871,71 @@ static const WindowDesc _message_options
 
};
 

	
 
void ShowMessageOptions(void)
 
{
 
	DeleteWindowById(WC_GAME_OPTIONS, 0);
 
	AllocateWindowDesc(&_message_options_desc);
 
}
 

	
 

	
 
void DeleteVehicleNews(VehicleID vid, StringID news)
 
{
 
	NewsID n;
 

	
 
	for (n = _oldest_news; _latest_news != INVALID_NEWS; n = increaseIndex(n)) {
 
		const NewsItem *ni = &_news_items[n];
 

	
 
		if (ni->flags & NF_VEHICLE &&
 
				ni->data_a == vid &&
 
				(news == INVALID_STRING_ID || ni->string_id == news)) {
 
			Window *w;
 

	
 
			if (_forced_news == n || _current_news == n) MoveToNextItem();
 
			_total_news--;
 

	
 
			// If this is the last news item, invalidate _latest_news
 
			if (_latest_news == _oldest_news) {
 
				assert(_total_news == 0);
 
			/* If this is the last news item, invalidate _latest_news */
 
			if (_total_news == 0) {
 
				assert(_latest_news == _oldest_news);
 
				_latest_news = INVALID_NEWS;
 
			}
 

	
 
			/* Since we only imitate a FIFO removing an arbitrary element does need
 
			 * some magic. Remove the item by shifting head towards the tail. eg
 
			 *    oldest    remove  last
 
			 *        |        |     |
 
			 * [------O--------n-----L--]
 
			 * will become (change dramatized to make clear)
 
			 * [---------O-----------L--]
 
			 * Also update the current news item in case it was pointing to the
 
			 * oldest, now shifted item */
 
			 * We also need an update of the current, forced and visible (open window)
 
			 * news's as this shifting could change the items they were pointing to */
 
			if (_total_news != 0) {
 
				NewsID i;
 
				for (i = n; i != _oldest_news; i = decreaseIndex(i)) {
 
				NewsID i, visible_news;
 
				w = FindWindowById(WC_NEWS_WINDOW, 0);
 
				visible_news = (w != NULL) ? (NewsID)(WP(w, news_d).ni - _news_items) : INVALID_NEWS;
 

	
 
				i = n;
 
				do {
 
					_news_items[i] = _news_items[decreaseIndex(i)];
 

	
 
					if (i == _current_news) _current_news = increaseIndex(_current_news);
 
					if (i == _forced_news) _forced_news = increaseIndex(_forced_news);
 
					if (i == visible_news) WP(w, news_d).ni = &_news_items[increaseIndex(visible_news)];
 

	
 
					i = decreaseIndex(i);
 
				} while (i != _oldest_news);
 

	
 
				_oldest_news = increaseIndex(_oldest_news);
 
				}
 

	
 
				if (_current_news == _oldest_news) _current_news = increaseIndex(_current_news);
 
				_oldest_news = increaseIndex(_oldest_news);
 
			}
 
			/*DEBUG(misc, 0) ("-cur %3d, old %2d, lat %3d, for %3d, tot %2d",
 
			  _current_news, _oldest_news, _latest_news, _forced_news, _total_news); */
 

	
 
			w = FindWindowById(WC_MESSAGE_HISTORY, 0);
 
			if (w != NULL) {
 
				SetWindowDirty(w);
 
				w->vscroll.count = _total_news;
 
			}
 
		}
 

	
 
		if (n == _latest_news) break;
 
	}
 
}
0 comments (0 inline, 0 general)