extensions/topmenus/TopMenu.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Feb 08 19:57:20 2010 -0800 (2010-02-08)
changeset 2914 bf5f9aa53abe
parent 2510 3b2349600557
child 4078 483b617ef4f2
permissions -rw-r--r--
Fixed FindEntityByClassname crash in Synergy (bug 4235, r=dvander).
ds@1990
     1
/**
ds@1990
     2
 * vim: set ts=4 :
ds@1990
     3
 * =============================================================================
ds@1990
     4
 * SourceMod TopMenus Extension
ds@1990
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1990
     6
 * =============================================================================
ds@1990
     7
 *
ds@1990
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1990
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1990
    10
 * Free Software Foundation.
ds@1990
    11
 * 
ds@1990
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1990
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1990
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1990
    15
 * details.
ds@1990
    16
 *
ds@1990
    17
 * You should have received a copy of the GNU General Public License along with
ds@1990
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1990
    19
 *
ds@1990
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1990
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1990
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1990
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1990
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1990
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1990
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1990
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1990
    28
 *
ds@1990
    29
 * Version: $Id$
ds@1990
    30
 */
ds@1990
    31
ds@1990
    32
#include <stdlib.h>
ds@1990
    33
#include <stdarg.h>
ds@1990
    34
#include "TopMenu.h"
ds@1990
    35
ds@1990
    36
struct obj_by_name_t
ds@1990
    37
{
ds@1990
    38
	unsigned int obj_index;
ds@1990
    39
	char name[64];
ds@1990
    40
};
ds@1990
    41
ds@1990
    42
int _SortObjectNamesDescending(const void *ptr1, const void *ptr2);
ds@1990
    43
unsigned int strncopy(char *dest, const char *src, size_t count);
ds@1990
    44
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
ds@1990
    45
ds@1990
    46
TopMenu::TopMenu(ITopMenuObjectCallbacks *callbacks)
ds@1990
    47
{
ds@1990
    48
	m_clients = NULL;
ds@1990
    49
	m_SerialNo = 1;
ds@1990
    50
	m_pTitle = callbacks;
ds@1990
    51
	m_max_clients = 0;
ds@1990
    52
ds@1990
    53
	if (playerhelpers->IsServerActivated())
ds@1990
    54
	{
ds@1990
    55
		CreatePlayers(playerhelpers->GetMaxClients());
ds@1990
    56
	}
ds@1990
    57
}
ds@1990
    58
ds@1990
    59
TopMenu::~TopMenu()
ds@1990
    60
{
ds@1990
    61
	/* Delete all categories */
ds@1990
    62
	while (m_Categories.size())
ds@1990
    63
	{
ds@1990
    64
		RemoveFromMenu(m_Categories[0]->obj->object_id);
ds@1990
    65
	}
ds@1990
    66
ds@1990
    67
	/* Remove all objects */
ds@1990
    68
	for (size_t i = 0; i < m_Objects.size(); i++)
ds@1990
    69
	{
ds@1990
    70
		assert(m_Objects[i]->is_free == true);
ds@1990
    71
		delete m_Objects[i];
ds@1990
    72
	}
ds@1990
    73
ds@1990
    74
	m_pTitle->OnTopMenuObjectRemoved(this, 0);
ds@1990
    75
ds@1990
    76
	/* Delete all cached config entries */
ds@1990
    77
	for (size_t i = 0; i < m_Config.cats.size(); i++)
ds@1990
    78
	{
ds@1990
    79
		delete m_Config.cats[i];
ds@1990
    80
	}
ds@1990
    81
ds@1990
    82
	/* Sweep players */
ds@1990
    83
	for (size_t i = 0; i <= (size_t)m_max_clients; i++)
ds@1990
    84
	{
ds@1990
    85
		TearDownClient(&m_clients[i]);
ds@1990
    86
	}
ds@1990
    87
	delete [] m_clients;
ds@1990
    88
}
ds@1990
    89
ds@1990
    90
unsigned int TopMenu::CalcMemUsage()
ds@1990
    91
{
ds@1990
    92
	unsigned int size = sizeof(TopMenu);
ds@1990
    93
ds@1990
    94
	size += m_Config.strings.GetMemTable()->MemUsage();
ds@1990
    95
	size += (m_Config.cats.size() * sizeof(int));
pred@2323
    96
	size += (sizeof(topmenu_player_t) * (ABSOLUTE_PLAYER_LIMIT + 1));
ds@1990
    97
	size += (m_SortedCats.size() * sizeof(unsigned int));
ds@1990
    98
	size += (m_UnsortedCats.size() * sizeof(unsigned int));
ds@1990
    99
	size += (m_Categories.size() * (sizeof(topmenu_category_t *) + sizeof(topmenu_category_t)));
ds@1990
   100
	size += (m_Objects.size() * (sizeof(topmenu_object_t *) + sizeof(topmenu_object_t)));
ds@1990
   101
	size += m_ObjLookup.mem_usage();
ds@1990
   102
ds@1990
   103
	for (size_t i = 0; i < m_Categories.size(); i++)
ds@1990
   104
	{
ds@1990
   105
		size += m_Categories[i]->obj_list.size() * sizeof(topmenu_object_t *);
ds@1990
   106
		size += m_Categories[i]->sorted.size() * sizeof(topmenu_object_t *);
ds@1990
   107
		size += m_Categories[i]->unsorted.size() * sizeof(topmenu_object_t *);
ds@1990
   108
	}
ds@1990
   109
ds@1990
   110
	return size;
ds@1990
   111
}
ds@1990
   112
ds@1990
   113
void TopMenu::OnClientConnected(int client)
ds@1990
   114
{
ds@1990
   115
	if (m_clients == NULL)
ds@1990
   116
	{
ds@1990
   117
		return;
ds@1990
   118
	}
ds@1990
   119
ds@1990
   120
	topmenu_player_t *player = &m_clients[client];
ds@1990
   121
	TearDownClient(player);
ds@1990
   122
}
ds@1990
   123
ds@1990
   124
void TopMenu::OnClientDisconnected(int client)
ds@1990
   125
{
ds@1990
   126
	if (m_clients == NULL)
ds@1990
   127
	{
ds@1990
   128
		return;
ds@1990
   129
	}
ds@1990
   130
ds@1990
   131
	topmenu_player_t *player = &m_clients[client];
ds@1990
   132
	TearDownClient(player);
ds@1990
   133
}
ds@1990
   134
ds@1990
   135
void TopMenu::OnServerActivated(int max_clients)
ds@1990
   136
{
ds@1990
   137
	if (m_clients == NULL)
ds@1990
   138
	{
ds@1990
   139
		CreatePlayers(max_clients);
ds@1990
   140
	}
ds@1990
   141
}
ds@1990
   142
ds@1990
   143
unsigned int TopMenu::AddToMenu(const char *name,
ds@1990
   144
								TopMenuObjectType type,
ds@1990
   145
								ITopMenuObjectCallbacks *callbacks,
ds@1990
   146
								IdentityToken_t *owner,
ds@1990
   147
								const char *cmdname,
ds@1990
   148
								FlagBits flags,
ds@1990
   149
								unsigned int parent)
ds@1990
   150
{
ds@1990
   151
	return AddToMenu2(name, type, callbacks, owner, cmdname, flags, parent, NULL);
ds@1990
   152
}
ds@1990
   153
ds@1990
   154
unsigned int TopMenu::AddToMenu2(const char *name,
ds@1990
   155
								 TopMenuObjectType type,
ds@1990
   156
								 ITopMenuObjectCallbacks *callbacks,
ds@1990
   157
								 IdentityToken_t *owner,
ds@1990
   158
								 const char *cmdname,
ds@1990
   159
								 FlagBits flags,
ds@1990
   160
								 unsigned int parent,
ds@1990
   161
								 const char *info_string)
ds@1990
   162
{
ds@1990
   163
	/* Sanity checks */
ds@1990
   164
	if (type == TopMenuObject_Category && parent != 0)
ds@1990
   165
	{
ds@1990
   166
		return 0;
ds@1990
   167
	}
ds@1990
   168
	else if (type == TopMenuObject_Item && parent == 0)
ds@1990
   169
	{
ds@1990
   170
		return 0;
ds@1990
   171
	}
ds@1990
   172
	else if (m_ObjLookup.retrieve(name) != NULL)
ds@1990
   173
	{
ds@1990
   174
		return 0;
ds@1990
   175
	}
ds@1990
   176
	else if (type != TopMenuObject_Item && type != TopMenuObject_Category)
ds@1990
   177
	{
ds@1990
   178
		return 0;
ds@1990
   179
	}
ds@1990
   180
ds@1990
   181
	/* If we're adding an item, make sure the parent is valid, 
ds@1990
   182
	 * and that the parent is a category.
ds@1990
   183
	 */
ds@1990
   184
	topmenu_object_t *parent_obj = NULL;
ds@1990
   185
	topmenu_category_t *parent_cat = NULL;
ds@1990
   186
	if (type == TopMenuObject_Item)
ds@1990
   187
	{
ds@1990
   188
		/* Check parent index.  Note it will be >= 1 here. */
ds@1990
   189
		if (parent > m_Objects.size() || m_Objects[parent - 1]->is_free)
ds@1990
   190
		{
ds@1990
   191
			return 0;
ds@1990
   192
		}
ds@1990
   193
		parent_obj = m_Objects[parent - 1];
ds@1990
   194
ds@1990
   195
		/* Find an equivalent pointer in the category array. */
ds@1990
   196
		for (size_t i = 0; i < m_Categories.size(); i++)
ds@1990
   197
		{
ds@1990
   198
			if (m_Categories[i]->obj == parent_obj)
ds@1990
   199
			{
ds@1990
   200
				parent_cat = m_Categories[i];
ds@1990
   201
				break;
ds@1990
   202
			}
ds@1990
   203
		}
ds@1990
   204
ds@1990
   205
		/* If none was found, leave. */
ds@1990
   206
		if (parent_cat == NULL)
ds@1990
   207
		{
ds@1990
   208
			return 0;
ds@1990
   209
		}
ds@1990
   210
	}
ds@1990
   211
ds@1990
   212
	/* Re-use an old object pointer if we can. */
ds@1990
   213
	topmenu_object_t *obj = NULL;
ds@1990
   214
	for (size_t i = 0; i < m_Objects.size(); i++)
ds@1990
   215
	{
ds@1990
   216
		if (m_Objects[i]->is_free == true)
ds@1990
   217
		{
ds@1990
   218
			obj = m_Objects[i];
ds@1990
   219
			break;
ds@1990
   220
		}
ds@1990
   221
	}
ds@1990
   222
ds@1990
   223
	/* Otherwise, allocate a new one. */
ds@1990
   224
	if (obj == NULL)
ds@1990
   225
	{
ds@1990
   226
		obj = new topmenu_object_t;
ds@1990
   227
		obj->object_id = ((unsigned int)m_Objects.size()) + 1;
ds@1990
   228
		m_Objects.push_back(obj);
ds@1990
   229
	}
ds@1990
   230
ds@1990
   231
	/* Initialize the object's properties. */
ds@1990
   232
	obj->callbacks = callbacks;
ds@1990
   233
	obj->flags = flags;
ds@1990
   234
	obj->owner = owner;
ds@1990
   235
	obj->type = type;
ds@1990
   236
	obj->is_free = false;
ds@1990
   237
	obj->parent = parent_obj;
ds@1990
   238
	strncopy(obj->name, name, sizeof(obj->name));
ds@1990
   239
	strncopy(obj->cmdname, cmdname ? cmdname : "", sizeof(obj->cmdname));
ds@1990
   240
	strncopy(obj->info, info_string ? info_string : "", sizeof(obj->info));
ds@1990
   241
ds@1990
   242
	if (obj->type == TopMenuObject_Category)
ds@1990
   243
	{
ds@1990
   244
		/* Create a new category entry */
ds@1990
   245
		topmenu_category_t *cat = new topmenu_category_t;
ds@1990
   246
		cat->obj = obj;
ds@1990
   247
		cat->reorder = false;
ds@1990
   248
		cat->serial = 1;
ds@1990
   249
ds@1990
   250
		/* Add it, then update our serial change number. */
dvander@2510
   251
		obj->cat_id = m_Categories.size();
ds@1990
   252
		m_Categories.push_back(cat);
ds@1990
   253
		m_SerialNo++;
ds@1990
   254
ds@1990
   255
		/* Updating sorting info */
ds@1990
   256
		m_bCatsNeedResort = true;
ds@1990
   257
	}
ds@1990
   258
	else if (obj->type == TopMenuObject_Item)
ds@1990
   259
	{
ds@1990
   260
		/* Update the category, mark it as needing changes */
dvander@2510
   261
		obj->cat_id = 0;
ds@1990
   262
		parent_cat->obj_list.push_back(obj);
ds@1990
   263
		parent_cat->reorder = true;
ds@1990
   264
		parent_cat->serial++;
ds@1990
   265
ds@1990
   266
		/* If the category just went from 0 to 1 items, mark it as 
ds@1990
   267
		 * changed, so clients get the category drawn.
ds@1990
   268
		 */
ds@1990
   269
		if (parent_cat->obj_list.size() == 1)
ds@1990
   270
		{
ds@1990
   271
			m_SerialNo++;
ds@1990
   272
		}
ds@1990
   273
	}
ds@1990
   274
ds@1990
   275
	m_ObjLookup.insert(name, obj);
ds@1990
   276
ds@1990
   277
	return obj->object_id;
ds@1990
   278
}
ds@1990
   279
ds@1990
   280
const char *TopMenu::GetObjectInfoString(unsigned int object_id)
ds@1990
   281
{
ds@1990
   282
	if (object_id == 0 
ds@1990
   283
		|| object_id > m_Objects.size() 
ds@1990
   284
		|| m_Objects[object_id - 1]->is_free)
ds@1990
   285
	{
ds@1990
   286
		return NULL;
ds@1990
   287
	}
ds@1990
   288
ds@1990
   289
	topmenu_object_t *obj = m_Objects[object_id - 1];
ds@1990
   290
ds@1990
   291
	return obj->info;
ds@1990
   292
}
ds@1990
   293
ds@1990
   294
const char *TopMenu::GetObjectName(unsigned int object_id)
ds@1990
   295
{
ds@1990
   296
	if (object_id == 0 
ds@1990
   297
		|| object_id > m_Objects.size() 
ds@1990
   298
		|| m_Objects[object_id - 1]->is_free)
ds@1990
   299
	{
ds@1990
   300
		return NULL;
ds@1990
   301
	}
ds@1990
   302
ds@1990
   303
	topmenu_object_t *obj = m_Objects[object_id - 1];
ds@1990
   304
ds@1990
   305
	return obj->name;
ds@1990
   306
}
ds@1990
   307
ds@1990
   308
void TopMenu::RemoveFromMenu(unsigned int object_id)
ds@1990
   309
{
ds@1990
   310
	if (object_id == 0 
ds@1990
   311
		|| object_id > m_Objects.size() 
ds@1990
   312
		|| m_Objects[object_id - 1]->is_free)
ds@1990
   313
	{
ds@1990
   314
		return;
ds@1990
   315
	}
ds@1990
   316
ds@1990
   317
	topmenu_object_t *obj = m_Objects[object_id - 1];
ds@1990
   318
ds@1990
   319
	m_ObjLookup.remove(obj->name);
ds@1990
   320
ds@1990
   321
	if (obj->type == TopMenuObject_Category)
ds@1990
   322
	{
ds@1990
   323
		/* Find it in the category list. */
ds@1990
   324
		for (size_t i = 0; i < m_Categories.size(); i++)
ds@1990
   325
		{
ds@1990
   326
			if (m_Categories[i]->obj == obj)
ds@1990
   327
			{
ds@1990
   328
				/* Mark all children as removed + free.  Note we could
ds@1990
   329
				 * call into RemoveMenuItem() for this, but it'd be very 
ds@1990
   330
				 * inefficient!
ds@1990
   331
				 */
ds@1990
   332
				topmenu_category_t *cat = m_Categories[i];
ds@1990
   333
				for (size_t j = 0; j < m_Categories[i]->obj_list.size(); j++)
ds@1990
   334
				{
dvander@2246
   335
					m_ObjLookup.remove(cat->obj_list[j]->name);
ds@1990
   336
					cat->obj_list[j]->callbacks->OnTopMenuObjectRemoved(this, cat->obj_list[j]->object_id);
ds@1990
   337
					cat->obj_list[j]->is_free = true;
ds@1990
   338
				}
ds@1990
   339
				
ds@1990
   340
				/* Remove the category from the list, then delete it. */
ds@1990
   341
				m_Categories.erase(m_Categories.iterAt(i));
ds@1990
   342
				delete cat;
ds@1990
   343
				break;
ds@1990
   344
			}
ds@1990
   345
		}
ds@1990
   346
ds@1990
   347
		/* Update the root as changed. */
ds@1990
   348
		m_SerialNo++;
ds@1990
   349
		m_bCatsNeedResort = true;
ds@1990
   350
	}
ds@1990
   351
	else if (obj->type == TopMenuObject_Item)
ds@1990
   352
	{
ds@1990
   353
		/* Find the category this item is in. */
ds@1990
   354
		topmenu_category_t *parent_cat = NULL;
ds@1990
   355
		for (size_t i = 0; i < m_Categories.size(); i++)
ds@1990
   356
		{
ds@1990
   357
			if (m_Categories[i]->obj == obj->parent)
ds@1990
   358
			{
ds@1990
   359
				parent_cat = m_Categories[i];
ds@1990
   360
				break;
ds@1990
   361
			}
ds@1990
   362
		}
ds@1990
   363
ds@1990
   364
		/* Erase it from the category's lists. */
ds@1990
   365
		if (parent_cat)
ds@1990
   366
		{
ds@1990
   367
			for (size_t i = 0; i < parent_cat->obj_list.size(); i++)
ds@1990
   368
			{
ds@1990
   369
				if (parent_cat->obj_list[i] == obj)
ds@1990
   370
				{
ds@1990
   371
					parent_cat->obj_list.erase(parent_cat->obj_list.iterAt(i));
ds@1990
   372
ds@1990
   373
					/* If this category now has no items, mark root as changed 
ds@1990
   374
					 * so clients won't get the category drawn anymore.
ds@1990
   375
					 */
ds@1990
   376
					if (parent_cat->obj_list.size() == 0)
ds@1990
   377
					{
ds@1990
   378
						m_SerialNo++;
ds@1990
   379
					}
ds@1990
   380
					break;
ds@1990
   381
				}
ds@1990
   382
			}
ds@1990
   383
ds@1990
   384
			/* Update the category as changed. */
ds@1990
   385
			parent_cat->reorder = true;
ds@1990
   386
			parent_cat->serial++;
ds@1990
   387
		}
ds@1990
   388
	}
ds@1990
   389
ds@1990
   390
	/* The callbacks pointer is still valid, so fire away! */
ds@1990
   391
	obj->callbacks->OnTopMenuObjectRemoved(this, object_id);
ds@1990
   392
ds@1990
   393
	/* Finally, mark the object as free. */
ds@1990
   394
	obj->is_free = true;
ds@1990
   395
}
ds@1990
   396
ds@1990
   397
bool TopMenu::DisplayMenu(int client, unsigned int hold_time, TopMenuPosition position)
ds@1990
   398
{
ds@1990
   399
	if (m_clients == NULL)
ds@1990
   400
	{
ds@1990
   401
		return false;
ds@1990
   402
	}
ds@1990
   403
ds@1990
   404
	IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client);
ds@1990
   405
	if (!pPlayer->IsInGame())
ds@1990
   406
	{
ds@1990
   407
		return false;
ds@1990
   408
	}
ds@1990
   409
ds@1990
   410
	UpdateClientRoot(client, pPlayer);
ds@1990
   411
dvander@2510
   412
	/* This is unfortunate but it's the best solution. */
dvander@2510
   413
	for (size_t i = 0; i < m_Categories.size(); i++)
dvander@2510
   414
	{
dvander@2510
   415
		UpdateClientCategory(client, i, true);
dvander@2510
   416
	}
dvander@2510
   417
ds@1990
   418
	topmenu_player_t *pClient = &m_clients[client];
ds@1990
   419
	if (pClient->root == NULL)
ds@1990
   420
	{
ds@1990
   421
		return false;
ds@1990
   422
	}
ds@1990
   423
ds@1990
   424
	bool return_value = false;
ds@1990
   425
ds@1990
   426
	if (position == TopMenuPosition_LastCategory && 
ds@1990
   427
		pClient->last_category < m_Categories.size())
ds@1990
   428
	{
ds@1990
   429
		return_value = DisplayCategory(client, pClient->last_category, hold_time, true);
ds@1990
   430
		if (!return_value)
ds@1990
   431
		{
ds@1990
   432
			return_value = pClient->root->DisplayAtItem(client, hold_time, pClient->last_root_pos);
ds@1990
   433
		}
ds@1990
   434
	}
ds@1990
   435
	else if (position == TopMenuPosition_LastRoot)
ds@1990
   436
	{
ds@1990
   437
		pClient->root->DisplayAtItem(client, hold_time, pClient->last_root_pos);
ds@1990
   438
	}
ds@1990
   439
	else if (position == TopMenuPosition_Start)
ds@1990
   440
	{
ds@1990
   441
		pClient->last_position = 0;
ds@1990
   442
		pClient->last_category = 0;
ds@1990
   443
		return_value = pClient->root->Display(client, hold_time);
ds@1990
   444
	}
ds@1990
   445
ds@1990
   446
	return return_value;
ds@1990
   447
}
ds@1990
   448
ds@1990
   449
bool TopMenu::DisplayCategory(int client, unsigned int category, unsigned int hold_time, bool last_position)
ds@1990
   450
{
ds@1990
   451
	UpdateClientCategory(client, category);
ds@1990
   452
ds@1990
   453
	topmenu_player_t *pClient = &m_clients[client];
dvander@2510
   454
	if (category >= pClient->cat_count || pClient->cats[category].menu == NULL)
ds@1990
   455
	{
ds@1990
   456
		return false;
ds@1990
   457
	}
ds@1990
   458
ds@1990
   459
	bool return_value = false;
ds@1990
   460
ds@1990
   461
	topmenu_player_category_t *player_cat = &(pClient->cats[category]);
ds@1990
   462
ds@1990
   463
	pClient->last_category = category;
ds@1990
   464
	if (last_position)
ds@1990
   465
	{
ds@1990
   466
		return_value = player_cat->menu->DisplayAtItem(client, hold_time, pClient->last_position);
ds@1990
   467
	}
ds@1990
   468
	else
ds@1990
   469
	{
ds@1990
   470
		return_value = player_cat->menu->Display(client, hold_time);
ds@1990
   471
	}
ds@1990
   472
ds@1990
   473
	return return_value;
ds@1990
   474
}
ds@1990
   475
ds@1990
   476
void TopMenu::OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page)
ds@1990
   477
{
ds@1990
   478
	const char *item_name = menu->GetItemInfo(item, NULL);
ds@1990
   479
	if (!item_name)
ds@1990
   480
	{
ds@1990
   481
		return;
ds@1990
   482
	}
ds@1990
   483
ds@1990
   484
	topmenu_object_t *obj;
ds@1990
   485
	topmenu_player_t *pClient = &m_clients[client];
ds@1990
   486
	topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name);
ds@1990
   487
	if (pObject == NULL)
ds@1990
   488
	{
ds@1990
   489
		return;
ds@1990
   490
	}
ds@1990
   491
ds@1990
   492
	obj = *pObject;
ds@1990
   493
ds@1990
   494
	/* We now have the object... what do we do with it? */
ds@1990
   495
	if (obj->type == TopMenuObject_Category)
ds@1990
   496
	{
ds@1990
   497
		/* If it's a category, the user wants to view it.. */
dvander@2510
   498
		assert(obj->cat_id < m_Categories.size());
dvander@2510
   499
		assert(m_Categories[obj->cat_id]->obj == obj);
dvander@2510
   500
		pClient->last_root_pos = item_on_page;
dvander@2510
   501
		if (!DisplayCategory(client, obj->cat_id, MENU_TIME_FOREVER, false))
ds@1990
   502
		{
dvander@2510
   503
			/* If we can't display the category, re-display the root menu.
dvander@2510
   504
			 * This code should be dead as of bug 3256, which disables categories 
dvander@2510
   505
			 * that cannot be displayed.
dvander@2510
   506
			 */
dvander@2510
   507
			DisplayMenu(client, MENU_TIME_FOREVER, TopMenuPosition_LastRoot);
ds@1990
   508
		}
ds@1990
   509
	}
ds@1990
   510
	else
ds@1990
   511
	{
ds@1990
   512
		pClient->last_position = item_on_page;
ds@1990
   513
		
ds@1990
   514
		/* Re-check access in case this user had their credentials revoked */
ds@1990
   515
		if (obj->cmdname[0] != '\0' && !adminsys->CheckAccess(client, obj->cmdname, obj->flags, false))
ds@1990
   516
		{
ds@1990
   517
			DisplayMenu(client, 0, TopMenuPosition_LastCategory);
ds@1990
   518
			return;
ds@1990
   519
		}
ds@1990
   520
		
ds@1990
   521
		/* Pass the information on to the callback */
ds@1990
   522
		obj->callbacks->OnTopMenuSelectOption(this, client, obj->object_id);
ds@1990
   523
	}
ds@1990
   524
}
ds@1990
   525
ds@1990
   526
void TopMenu::OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style)
ds@1990
   527
{
ds@1990
   528
	const char *item_name = menu->GetItemInfo(item, NULL);
ds@1990
   529
	if (!item_name)
ds@1990
   530
	{
ds@1990
   531
		return;
ds@1990
   532
	}
ds@1990
   533
ds@1990
   534
	topmenu_object_t *obj;
ds@1990
   535
	topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name);
ds@1990
   536
	if (pObject == NULL)
ds@1990
   537
	{
ds@1990
   538
		return;
ds@1990
   539
	}
ds@1990
   540
ds@1990
   541
	obj = *pObject;
ds@1990
   542
dvander@2510
   543
	/* If the category has nothing to display, disable it. */
dvander@2510
   544
	if (obj->type == TopMenuObject_Category)
dvander@2510
   545
	{
dvander@2510
   546
		assert(obj->cat_id < m_Categories.size());
dvander@2510
   547
		assert(m_Categories[obj->cat_id]->obj == obj);
dvander@2510
   548
		topmenu_player_t *pClient = &m_clients[client];
dvander@2510
   549
		if (obj->cat_id >= pClient->cat_count || pClient->cats[obj->cat_id].menu == NULL)
dvander@2510
   550
		{
dvander@2510
   551
			style = ITEMDRAW_IGNORE;
dvander@2510
   552
			return;
dvander@2510
   553
		}
dvander@2510
   554
	}
dvander@2510
   555
ds@1990
   556
	style = obj->callbacks->OnTopMenuDrawOption(this, client, obj->object_id);
ds@1990
   557
	if (style != ITEMDRAW_DEFAULT)
ds@1990
   558
	{
ds@1990
   559
		return;
ds@1990
   560
	}
ds@1990
   561
ds@1990
   562
	if (obj->cmdname[0] == '\0')
ds@1990
   563
	{
ds@1990
   564
		return;
ds@1990
   565
	}
ds@1990
   566
ds@1990
   567
	if (!adminsys->CheckAccess(client, obj->cmdname, obj->flags, false))
ds@1990
   568
	{
ds@1990
   569
		style = ITEMDRAW_IGNORE;
ds@1990
   570
	}
ds@1990
   571
}
ds@1990
   572
ds@1990
   573
unsigned int TopMenu::OnMenuDisplayItem(IBaseMenu *menu,
ds@1990
   574
								int client,
ds@1990
   575
								IMenuPanel *panel,
ds@1990
   576
								unsigned int item,
ds@1990
   577
								const ItemDrawInfo &dr)
ds@1990
   578
{
ds@1990
   579
	const char *item_name = menu->GetItemInfo(item, NULL);
ds@1990
   580
	if (!item_name)
ds@1990
   581
	{
ds@1990
   582
		return 0;
ds@1990
   583
	}
ds@1990
   584
ds@1990
   585
	topmenu_object_t *obj;
ds@1990
   586
	topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name);
ds@1990
   587
	if (pObject == NULL)
ds@1990
   588
	{
ds@1990
   589
		return 0;
ds@1990
   590
	}
ds@1990
   591
ds@1990
   592
	obj = *pObject;
ds@1990
   593
ds@1990
   594
	/* Ask the object to render the text for this client */
ds@1990
   595
	char renderbuf[64];
ds@1990
   596
	obj->callbacks->OnTopMenuDisplayOption(this, client, obj->object_id, renderbuf, sizeof(renderbuf));
ds@1990
   597
ds@1990
   598
	/* Build the new draw info */
ds@1990
   599
	ItemDrawInfo new_dr = dr;
ds@1990
   600
	new_dr.display = renderbuf;
ds@1990
   601
ds@1990
   602
	/* Man I love the menu API.  Ask the panel to draw the item and give the position 
ds@1990
   603
	 * back to Core's renderer.  This way we don't have to worry about keeping the 
ds@1990
   604
	 * render buffer static!
ds@1990
   605
	 */
ds@1990
   606
	return panel->DrawItem(new_dr);
ds@1990
   607
}
ds@1990
   608
ds@1990
   609
void TopMenu::OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
ds@1990
   610
{
ds@1990
   611
	if (reason == MenuCancel_ExitBack)
ds@1990
   612
	{
ds@1990
   613
		/* If the client chose exit back, they were on a category menu, and we can
ds@1990
   614
		 * now display the root menu from the last known position.
ds@1990
   615
		 */
ds@1990
   616
		DisplayMenu(client, 0, TopMenuPosition_LastRoot);
ds@1990
   617
	}
ds@1990
   618
}
ds@1990
   619
ds@1990
   620
void TopMenu::UpdateClientRoot(int client, IGamePlayer *pGamePlayer)
ds@1990
   621
{
ds@1990
   622
	topmenu_player_t *pClient = &m_clients[client];
ds@1990
   623
	IGamePlayer *pPlayer = pGamePlayer ? pGamePlayer : playerhelpers->GetGamePlayer(client);
ds@1990
   624
ds@1990
   625
	/* Determine if an update is necessary */
ds@1990
   626
	bool is_update_needed = false;
ds@1990
   627
	if (pClient->menu_serial != m_SerialNo)
ds@1990
   628
	{
ds@1990
   629
		is_update_needed = true;
ds@1990
   630
	}
ds@1990
   631
	else if (pPlayer->GetUserId() != pClient->user_id)
ds@1990
   632
	{
ds@1990
   633
		is_update_needed = true;
ds@1990
   634
	}
ds@1990
   635
ds@1990
   636
	/* If no update is needed at the root level, just leave now */
ds@1990
   637
	if (!is_update_needed)
ds@1990
   638
	{
ds@1990
   639
		return;
ds@1990
   640
	}
ds@1990
   641
ds@1990
   642
	/* First we need to flush the cache... */
ds@1990
   643
	TearDownClient(pClient);
ds@1990
   644
ds@1990
   645
	/* Now, rebuild the category list, but don't create menus */
ds@1990
   646
	if (m_Categories.size() == 0)
ds@1990
   647
	{
ds@1990
   648
		pClient->cat_count = 0;
ds@1990
   649
		pClient->cats = NULL;
ds@1990
   650
	}
ds@1990
   651
	else
ds@1990
   652
	{
ds@1990
   653
		pClient->cat_count = (unsigned int)m_Categories.size();
ds@1990
   654
		pClient->cats = new topmenu_player_category_t[pClient->cat_count];
ds@1990
   655
		memset(pClient->cats, 0, sizeof(topmenu_player_category_t) * pClient->cat_count);
ds@1990
   656
	}
ds@1990
   657
ds@1990
   658
	/* Re-sort the root categories if needed */
ds@1990
   659
	SortCategoriesIfNeeded();
ds@1990
   660
ds@1990
   661
	/* Build the root menu */
ds@1990
   662
	IBaseMenu *root_menu = menus->GetDefaultStyle()->CreateMenu(this, myself->GetIdentity());
ds@1990
   663
ds@1990
   664
	/* Add the sorted items */
ds@1990
   665
	for (size_t i = 0; i < m_SortedCats.size(); i++)
ds@1990
   666
	{
ds@1990
   667
		if (m_Categories[m_SortedCats[i]]->obj_list.size() == 0)
ds@1990
   668
		{
ds@1990
   669
			continue;
ds@1990
   670
		}
ds@1990
   671
		root_menu->AppendItem(m_Categories[m_SortedCats[i]]->obj->name, ItemDrawInfo(""));
ds@1990
   672
	}
ds@1990
   673
ds@1990
   674
	/* Now we need to handle un-sorted items.  This is slightly trickier, as we need to 
ds@1990
   675
	 * pre-render each category name, and cache those names.  Phew!
ds@1990
   676
	 */
ds@1990
   677
	if (m_UnsortedCats.size())
ds@1990
   678
	{
ds@1990
   679
		obj_by_name_t *item_list = new obj_by_name_t[m_UnsortedCats.size()];
ds@1990
   680
		for (size_t i = 0; i < m_UnsortedCats.size(); i++)
ds@1990
   681
		{
ds@1990
   682
			obj_by_name_t *temp_obj = &item_list[i];
ds@1990
   683
			topmenu_object_t *obj = m_Categories[m_UnsortedCats[i]]->obj;
ds@1990
   684
			obj->callbacks->OnTopMenuDisplayOption(this,
ds@1990
   685
				client, 
ds@1990
   686
				obj->object_id,
ds@1990
   687
				temp_obj->name,
ds@1990
   688
				sizeof(temp_obj->name));
ds@1990
   689
			temp_obj->obj_index = m_UnsortedCats[i];
ds@1990
   690
		}
ds@1990
   691
ds@1990
   692
		/* Sort our temp list */
ds@1990
   693
		qsort(item_list, m_UnsortedCats.size(), sizeof(obj_by_name_t), _SortObjectNamesDescending);
ds@1990
   694
ds@1990
   695
		/* Add the new sorted categories */
ds@1990
   696
		for (size_t i = 0; i < m_UnsortedCats.size(); i++)
ds@1990
   697
		{
ds@1990
   698
			if (m_Categories[item_list[i].obj_index]->obj_list.size() == 0)
ds@1990
   699
			{
ds@1990
   700
				continue;
ds@1990
   701
			}
ds@1990
   702
			root_menu->AppendItem(m_Categories[item_list[i].obj_index]->obj->name, ItemDrawInfo(""));
ds@1990
   703
		}
ds@1990
   704
ds@1990
   705
		delete [] item_list;
ds@1990
   706
	}
ds@1990
   707
ds@1990
   708
	/* Set the menu's title */
ds@1990
   709
	char renderbuf[128];
ds@1990
   710
	m_pTitle->OnTopMenuDisplayTitle(this, client, 0, renderbuf, sizeof(renderbuf));
ds@1990
   711
	root_menu->SetDefaultTitle(renderbuf);
ds@1990
   712
ds@1990
   713
	/* The client is now fully updated */
ds@1990
   714
	pClient->root = root_menu;
ds@1990
   715
	pClient->user_id = pPlayer->GetUserId();
ds@1990
   716
	pClient->menu_serial = m_SerialNo;
ds@1990
   717
	pClient->last_position = 0;
ds@1990
   718
	pClient->last_category = 0;
ds@1990
   719
	pClient->last_root_pos = 0;
ds@1990
   720
}
ds@1990
   721
dvander@2510
   722
void TopMenu::UpdateClientCategory(int client, unsigned int category, bool bSkipRootCheck)
ds@1990
   723
{
dvander@2510
   724
	bool has_access = false;
dvander@2510
   725
ds@1990
   726
	/* Update the client's root menu just in case it needs it.  This 
ds@1990
   727
	 * will validate that we have both a valid client and a valid
ds@1990
   728
	 * category structure for that client.
ds@1990
   729
	 */
dvander@2510
   730
	if (!bSkipRootCheck)
dvander@2510
   731
	{
dvander@2510
   732
		UpdateClientRoot(client);
dvander@2510
   733
	}
ds@1990
   734
ds@1990
   735
	/* Now it's guaranteed that our category tables will be usable */
ds@1990
   736
	topmenu_player_t *pClient = &m_clients[client];
ds@1990
   737
	topmenu_category_t *cat = m_Categories[category];
ds@1990
   738
	topmenu_player_category_t *player_cat = &(pClient->cats[category]);
ds@1990
   739
ds@1990
   740
	/* Does the category actually need updating? */
ds@1990
   741
	if (player_cat->serial == cat->serial)
ds@1990
   742
	{
ds@1990
   743
		return;
ds@1990
   744
	}
ds@1990
   745
ds@1990
   746
	/* Destroy any existing menu */
ds@1990
   747
	if (player_cat->menu)
ds@1990
   748
	{
ds@1990
   749
		player_cat->menu->Destroy();
ds@1990
   750
		player_cat->menu = NULL;
ds@1990
   751
	}
ds@1990
   752
ds@1990
   753
	if (pClient->last_category == category)
ds@1990
   754
	{
ds@1990
   755
		pClient->last_position = 0;
ds@1990
   756
	}
ds@1990
   757
ds@1990
   758
	IBaseMenu *cat_menu = menus->GetDefaultStyle()->CreateMenu(this, myself->GetIdentity());
ds@1990
   759
	
ds@1990
   760
	/* Categories get an "exit back" button */
ds@1990
   761
	cat_menu->SetMenuOptionFlags(cat_menu->GetMenuOptionFlags() | MENUFLAG_BUTTON_EXITBACK);
ds@1990
   762
ds@1990
   763
	/* Re-sort the category if needed */
ds@1990
   764
	SortCategoryIfNeeded(category);
ds@1990
   765
ds@1990
   766
	/* Build the menu with the sorted items first */
ds@1990
   767
	for (size_t i = 0; i < cat->sorted.size(); i++)
ds@1990
   768
	{
ds@1990
   769
		cat_menu->AppendItem(cat->sorted[i]->name, ItemDrawInfo(""));
dvander@2510
   770
		if (!has_access && adminsys->CheckAccess(client,
dvander@2510
   771
												 cat->sorted[i]->cmdname,
dvander@2510
   772
												 cat->sorted[i]->flags,
dvander@2510
   773
												 false))
dvander@2510
   774
		{
dvander@2510
   775
			has_access = true;
dvander@2510
   776
		}
ds@1990
   777
	}
ds@1990
   778
ds@1990
   779
	/* Now handle unsorted items */
ds@1990
   780
	if (cat->unsorted.size())
ds@1990
   781
	{
ds@1990
   782
		/* Build a list of the item names */
ds@1990
   783
		obj_by_name_t *item_list = new obj_by_name_t[cat->unsorted.size()];
ds@1990
   784
		for (size_t i = 0; i < cat->unsorted.size(); i++)
ds@1990
   785
		{
ds@1990
   786
			obj_by_name_t *item = &item_list[i];
ds@1990
   787
			topmenu_object_t *obj = cat->unsorted[i];
ds@1990
   788
			obj->callbacks->OnTopMenuDisplayOption(this,
ds@1990
   789
				client,
ds@1990
   790
				obj->object_id,
ds@1990
   791
				item->name,
ds@1990
   792
				sizeof(item->name));
ds@1990
   793
			item->obj_index = (unsigned int)i;
dvander@2510
   794
			if (!has_access && adminsys->CheckAccess(client, obj->cmdname, obj->flags, false))
dvander@2510
   795
			{
dvander@2510
   796
				has_access = true;
dvander@2510
   797
			}
ds@1990
   798
		}
ds@1990
   799
dvander@2510
   800
		if (has_access)
dvander@2510
   801
		{
dvander@2510
   802
			/* Sort the names */
dvander@2510
   803
			qsort(item_list,
dvander@2510
   804
				cat->unsorted.size(),
dvander@2510
   805
				sizeof(obj_by_name_t),
dvander@2510
   806
				_SortObjectNamesDescending);
ds@1990
   807
dvander@2510
   808
			/* Add to the menu */
dvander@2510
   809
			for (size_t i = 0; i < cat->unsorted.size(); i++)
dvander@2510
   810
			{
dvander@2510
   811
				cat_menu->AppendItem(cat->unsorted[item_list[i].obj_index]->name, ItemDrawInfo(""));
dvander@2510
   812
			}
ds@1990
   813
		}
ds@1990
   814
ds@1990
   815
		delete [] item_list;
ds@1990
   816
	}
ds@1990
   817
dvander@2510
   818
	/* If the player has access to no items, don't draw a menu. */
dvander@2510
   819
	if (!has_access)
dvander@2510
   820
	{
dvander@2510
   821
		cat_menu->Destroy();
dvander@2510
   822
		player_cat->menu = NULL;
dvander@2510
   823
		player_cat->serial = cat->serial;
dvander@2510
   824
		return;
dvander@2510
   825
	}
dvander@2510
   826
ds@1990
   827
	/* Set the menu's title */
ds@1990
   828
	char renderbuf[128];
ds@1990
   829
	cat->obj->callbacks->OnTopMenuDisplayTitle(this, 
ds@1990
   830
		client, 
ds@1990
   831
		cat->obj->object_id, 
ds@1990
   832
		renderbuf, 
ds@1990
   833
		sizeof(renderbuf));
ds@1990
   834
	cat_menu->SetDefaultTitle(renderbuf);
ds@1990
   835
ds@1990
   836
	/* We're done! */
ds@1990
   837
	player_cat->menu = cat_menu;
ds@1990
   838
	player_cat->serial = cat->serial;
ds@1990
   839
}
ds@1990
   840
ds@1990
   841
void TopMenu::SortCategoryIfNeeded(unsigned int category)
ds@1990
   842
{
ds@1990
   843
	topmenu_category_t *cat = m_Categories[category];
ds@1990
   844
	if (!cat->reorder)
ds@1990
   845
	{
ds@1990
   846
		return;
ds@1990
   847
	}
ds@1990
   848
ds@1990
   849
	cat->sorted.clear();
ds@1990
   850
	cat->unsorted.clear();
ds@1990
   851
ds@1990
   852
	if (cat->obj_list.size() == 0)
ds@1990
   853
	{
ds@1990
   854
		cat->reorder = false;
ds@1990
   855
		return;
ds@1990
   856
	}
ds@1990
   857
ds@1990
   858
	CVector<unsigned int> to_sort;
ds@1990
   859
	for (size_t i = 0; i < cat->obj_list.size(); i++)
ds@1990
   860
	{
ds@1990
   861
		to_sort.push_back(i);
ds@1990
   862
	}
ds@1990
   863
ds@1990
   864
	/* Find a matching category in the configs */
ds@1990
   865
	config_category_t *config_cat = NULL; 
ds@1990
   866
	for (size_t i = 0; i < m_Config.cats.size(); i++)
ds@1990
   867
	{
ds@1990
   868
		if (strcmp(m_Config.strings.GetString(m_Config.cats[i]->name), cat->obj->name) == 0)
ds@1990
   869
		{
ds@1990
   870
			config_cat = m_Config.cats[i];
ds@1990
   871
			break;
ds@1990
   872
		}
ds@1990
   873
	}
ds@1990
   874
ds@1990
   875
	/* If there is a matching category, build a pre-sorted item list */
ds@1990
   876
	if (config_cat != NULL)
ds@1990
   877
	{
ds@1990
   878
		/* Find matching objects in this category */
ds@1990
   879
		for (size_t i = 0; i < config_cat->commands.size(); i++)
ds@1990
   880
		{
ds@1990
   881
			const char *config_name = m_Config.strings.GetString(config_cat->commands[i]);
ds@1990
   882
			for (size_t j = 0; j < to_sort.size(); j++)
ds@1990
   883
			{
ds@1990
   884
				if (strcmp(config_name, cat->obj_list[to_sort[j]]->name) == 0)
ds@1990
   885
				{
ds@1990
   886
					/* Place in the final list, then remove from the temporary list */
ds@1990
   887
					cat->sorted.push_back(cat->obj_list[to_sort[j]]);
ds@1990
   888
					to_sort.erase(to_sort.iterAt(j));
ds@1990
   889
					break;
ds@1990
   890
				}
ds@1990
   891
			}
ds@1990
   892
		}
ds@1990
   893
	}
ds@1990
   894
ds@1990
   895
	/* Push any remaining items onto the unsorted list */
ds@1990
   896
	for (size_t i = 0; i < to_sort.size(); i++)
ds@1990
   897
	{
ds@1990
   898
		cat->unsorted.push_back(cat->obj_list[to_sort[i]]);
ds@1990
   899
	}
ds@1990
   900
ds@1990
   901
	cat->reorder = false;
ds@1990
   902
}
ds@1990
   903
ds@1990
   904
void TopMenu::SortCategoriesIfNeeded()
ds@1990
   905
{
ds@1990
   906
	if (!m_bCatsNeedResort)
ds@1990
   907
	{
ds@1990
   908
		return;
ds@1990
   909
	}
ds@1990
   910
ds@1990
   911
	/* Clear sort results */
ds@1990
   912
	m_SortedCats.clear();
ds@1990
   913
	m_UnsortedCats.clear();
ds@1990
   914
ds@1990
   915
	if (m_Categories.size() == 0)
ds@1990
   916
	{
ds@1990
   917
		m_bCatsNeedResort = false;
ds@1990
   918
		return;
ds@1990
   919
	}
ds@1990
   920
ds@1990
   921
	CVector<unsigned int> to_sort;
ds@1990
   922
	for (unsigned int i = 0; i < (unsigned int)m_Categories.size(); i++)
ds@1990
   923
	{
ds@1990
   924
		to_sort.push_back(i);
ds@1990
   925
	}
ds@1990
   926
ds@1990
   927
	/* If we have any predefined categories, add them in as they appear. */
ds@1990
   928
	for (size_t i= 0; i < m_Config.cats.size(); i++)
ds@1990
   929
	{
ds@1990
   930
		/* Find this category and map it in if we can */
ds@1990
   931
		for (size_t j = 0; j < to_sort.size(); j++)
ds@1990
   932
		{
ds@1990
   933
			if (strcmp(m_Config.strings.GetString(m_Config.cats[i]->name), 
ds@1990
   934
					   m_Categories[to_sort[j]]->obj->name) == 0)
ds@1990
   935
			{
ds@1990
   936
				/* Add to the real list and remove from the temporary */
ds@1990
   937
				m_SortedCats.push_back(to_sort[j]);
ds@1990
   938
				to_sort.erase(to_sort.iterAt(j));
ds@1990
   939
				break;
ds@1990
   940
			}
ds@1990
   941
		}
ds@1990
   942
	}
ds@1990
   943
ds@1990
   944
	/* Push any remaining items onto the unsorted list */
ds@1990
   945
	for (size_t i = 0; i < to_sort.size(); i++)
ds@1990
   946
	{
ds@1990
   947
		m_UnsortedCats.push_back(to_sort[i]);
ds@1990
   948
	}
ds@1990
   949
ds@1990
   950
	m_bCatsNeedResort = false;
ds@1990
   951
}
ds@1990
   952
ds@1990
   953
void TopMenu::CreatePlayers(int max_clients)
ds@1990
   954
{
ds@1990
   955
	m_max_clients = max_clients;
pred@2323
   956
	m_clients = (topmenu_player_t *)malloc(sizeof(topmenu_player_t) * (ABSOLUTE_PLAYER_LIMIT + 1));
pred@2323
   957
	memset(m_clients, 0, sizeof(topmenu_player_t) * (ABSOLUTE_PLAYER_LIMIT + 1));
ds@1990
   958
}
ds@1990
   959
ds@1990
   960
void TopMenu::TearDownClient(topmenu_player_t *player)
ds@1990
   961
{
ds@1990
   962
	if (player->cats != NULL)
ds@1990
   963
	{
ds@1990
   964
		for (unsigned int i = 0; i < player->cat_count; i++)
ds@1990
   965
		{
ds@1990
   966
			topmenu_player_category_t *player_cat = &(player->cats[i]);
ds@1990
   967
			if (player_cat->menu != NULL)
ds@1990
   968
			{
ds@1990
   969
				player_cat->menu->Destroy();
ds@1990
   970
			}
ds@1990
   971
		}
ds@1990
   972
		delete [] player->cats;
ds@1990
   973
	}
ds@1990
   974
ds@1990
   975
	if (player->root != NULL)
ds@1990
   976
	{
ds@1990
   977
		player->root->Destroy();
ds@1990
   978
	}
ds@1990
   979
ds@1990
   980
	memset(player, 0, sizeof(topmenu_player_t));
ds@1990
   981
}
ds@1990
   982
ds@1990
   983
bool TopMenu::LoadConfiguration(const char *file, char *error, size_t maxlength)
ds@1990
   984
{
ds@1990
   985
	SMCError err;
ds@1990
   986
	SMCStates states;
ds@1990
   987
ds@1990
   988
	if ((err = textparsers->ParseFile_SMC(file, this, &states))
ds@1990
   989
		!= SMCError_Okay)
ds@1990
   990
	{
ds@1990
   991
		const char *err_string = textparsers->GetSMCErrorString(err);
ds@1990
   992
		if (!err_string)
ds@1990
   993
		{
ds@1990
   994
			err_string = "Unknown";
ds@1990
   995
		}
ds@1990
   996
ds@1990
   997
		UTIL_Format(error, maxlength, "%s", err_string);
ds@1990
   998
ds@1990
   999
		return false;
ds@1990
  1000
	}
ds@1990
  1001
ds@1990
  1002
	return true;
ds@1990
  1003
}
ds@1990
  1004
ds@1990
  1005
bool TopMenu::OnIdentityRemoval(IdentityToken_t *owner)
ds@1990
  1006
{
ds@1990
  1007
	/* First sweep the categories owned by us */
ds@1990
  1008
	CVector<unsigned int> obj_list;
ds@1990
  1009
	for (size_t i = 0; i < m_Categories.size(); i++)
ds@1990
  1010
	{
ds@1990
  1011
		if (m_Categories[i]->obj->owner == owner)
ds@1990
  1012
		{
ds@1990
  1013
			obj_list.push_back(m_Categories[i]->obj->object_id);
ds@1990
  1014
		}
ds@1990
  1015
	}
ds@1990
  1016
ds@1990
  1017
	for (size_t i = 0; i < obj_list.size(); i++)
ds@1990
  1018
	{
ds@1990
  1019
		RemoveFromMenu(obj_list[i]);
ds@1990
  1020
	}
ds@1990
  1021
ds@1990
  1022
	/* Now we can look for actual items */
ds@1990
  1023
	for (size_t i = 0; i < m_Objects.size(); i++)
ds@1990
  1024
	{
ds@1990
  1025
		if (m_Objects[i]->is_free)
ds@1990
  1026
		{
ds@1990
  1027
			continue;
ds@1990
  1028
		}
ds@1990
  1029
		if (m_Objects[i]->owner == owner)
ds@1990
  1030
		{
ds@1990
  1031
			assert(m_Objects[i]->type != TopMenuObject_Category);
ds@1990
  1032
			RemoveFromMenu(m_Objects[i]->object_id);
ds@1990
  1033
		}
ds@1990
  1034
	}
ds@1990
  1035
ds@1990
  1036
	return true;
ds@1990
  1037
}
ds@1990
  1038
ds@1990
  1039
#define PARSE_STATE_NONE		0
ds@1990
  1040
#define PARSE_STATE_MAIN		1
ds@1990
  1041
#define PARSE_STATE_CATEGORY	2
ds@1990
  1042
unsigned int ignore_parse_level = 0;
ds@1990
  1043
unsigned int current_parse_state = 0;
ds@1990
  1044
config_category_t *cur_cat = NULL;
ds@1990
  1045
ds@1990
  1046
void TopMenu::ReadSMC_ParseStart()
ds@1990
  1047
{
ds@1990
  1048
	current_parse_state = PARSE_STATE_NONE;
ds@1990
  1049
	ignore_parse_level = 0;
ds@1990
  1050
	cur_cat = NULL;
ds@1990
  1051
ds@1990
  1052
	/* Reset the old config */
ds@1990
  1053
	m_Config.strings.Reset();
ds@1990
  1054
	for (size_t i = 0; i < m_Config.cats.size(); i++)
ds@1990
  1055
	{
ds@1990
  1056
		delete m_Config.cats[i];
ds@1990
  1057
	}
ds@1990
  1058
	m_Config.cats.clear();
ds@1990
  1059
}
ds@1990
  1060
ds@1990
  1061
SMCResult TopMenu::ReadSMC_NewSection(const SMCStates *states, const char *name)
ds@1990
  1062
{
ds@1990
  1063
	if (ignore_parse_level)
ds@1990
  1064
	{
ds@1990
  1065
		ignore_parse_level++;
ds@1990
  1066
	}
ds@1990
  1067
	else
ds@1990
  1068
	{
ds@1990
  1069
		if (current_parse_state == PARSE_STATE_NONE)
ds@1990
  1070
		{
ds@1990
  1071
			if (strcmp(name, "Menu") == 0)
ds@1990
  1072
			{
ds@1990
  1073
				current_parse_state = PARSE_STATE_MAIN;
ds@1990
  1074
			}
ds@1990
  1075
			else
ds@1990
  1076
			{
ds@1990
  1077
				ignore_parse_level = 1;
ds@1990
  1078
			}
ds@1990
  1079
		}
ds@1990
  1080
		else if (current_parse_state == PARSE_STATE_MAIN)
ds@1990
  1081
		{
ds@1990
  1082
			cur_cat = new config_category_t;
ds@1990
  1083
			cur_cat->name = m_Config.strings.AddString(name);
ds@1990
  1084
			m_Config.cats.push_back(cur_cat);
ds@1990
  1085
			current_parse_state = PARSE_STATE_CATEGORY;
ds@1990
  1086
		}
ds@1990
  1087
		else
ds@1990
  1088
		{
ds@1990
  1089
			ignore_parse_level = 1;
ds@1990
  1090
		}
ds@1990
  1091
	}
ds@1990
  1092
ds@1990
  1093
	return SMCResult_Continue;
ds@1990
  1094
}
ds@1990
  1095
ds@1990
  1096
SMCResult TopMenu::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
ds@1990
  1097
{
ds@1990
  1098
	if (ignore_parse_level > 0
ds@1990
  1099
		|| current_parse_state != PARSE_STATE_CATEGORY
ds@1990
  1100
		|| cur_cat == NULL)
ds@1990
  1101
	{
ds@1990
  1102
		return SMCResult_Continue;
ds@1990
  1103
	}
ds@1990
  1104
ds@1990
  1105
	if (strcmp(key, "item") == 0)
ds@1990
  1106
	{
ds@1990
  1107
		cur_cat->commands.push_back(m_Config.strings.AddString(value));
ds@1990
  1108
	}
ds@1990
  1109
ds@1990
  1110
	return SMCResult_Continue;
ds@1990
  1111
}
ds@1990
  1112
ds@1990
  1113
SMCResult TopMenu::ReadSMC_LeavingSection(const SMCStates *states)
ds@1990
  1114
{
ds@1990
  1115
	if (ignore_parse_level)
ds@1990
  1116
	{
ds@1990
  1117
		ignore_parse_level--;
ds@1990
  1118
	}
ds@1990
  1119
	else
ds@1990
  1120
	{
ds@1990
  1121
		if (current_parse_state == PARSE_STATE_CATEGORY)
ds@1990
  1122
		{
ds@1990
  1123
			cur_cat = NULL;
ds@1990
  1124
			current_parse_state = PARSE_STATE_MAIN;
ds@1990
  1125
		}
ds@1990
  1126
		else if (current_parse_state == PARSE_STATE_MAIN)
ds@1990
  1127
		{
ds@1990
  1128
			current_parse_state = PARSE_STATE_NONE;
ds@1990
  1129
		}
ds@1990
  1130
	}
ds@1990
  1131
ds@1990
  1132
	return SMCResult_Continue;
ds@1990
  1133
}
ds@1990
  1134
ds@1990
  1135
unsigned int TopMenu::FindCategory(const char *name)
ds@1990
  1136
{
ds@1990
  1137
	topmenu_object_t **p_obj = m_ObjLookup.retrieve(name);
ds@1990
  1138
	if (!p_obj)
ds@1990
  1139
	{
ds@1990
  1140
		return 0;
ds@1990
  1141
	}
ds@1990
  1142
ds@1990
  1143
	topmenu_object_t *obj = *p_obj;
ds@1990
  1144
	if (obj->type != TopMenuObject_Category)
ds@1990
  1145
	{
ds@1990
  1146
		return 0;
ds@1990
  1147
	}
ds@1990
  1148
ds@1990
  1149
	return obj->object_id;
ds@1990
  1150
}
ds@1990
  1151
pred@2323
  1152
void TopMenu::OnMaxPlayersChanged( int newvalue )
pred@2323
  1153
{
pred@2323
  1154
	m_max_clients = newvalue;
pred@2323
  1155
}
pred@2323
  1156
ds@1990
  1157
int _SortObjectNamesDescending(const void *ptr1, const void *ptr2)
ds@1990
  1158
{
ds@1990
  1159
	obj_by_name_t *obj1 = (obj_by_name_t *)ptr1;
ds@1990
  1160
	obj_by_name_t *obj2 = (obj_by_name_t *)ptr2;
ds@1990
  1161
	return strcmp(obj1->name, obj2->name);
ds@1990
  1162
}
ds@1990
  1163
ds@1990
  1164
unsigned int strncopy(char *dest, const char *src, size_t count)
ds@1990
  1165
{
ds@1990
  1166
	if (!count)
ds@1990
  1167
	{
ds@1990
  1168
		return 0;
ds@1990
  1169
	}
ds@1990
  1170
ds@1990
  1171
	char *start = dest;
ds@1990
  1172
	while ((*src) && (--count))
ds@1990
  1173
	{
ds@1990
  1174
		*dest++ = *src++;
ds@1990
  1175
	}
ds@1990
  1176
	*dest = '\0';
ds@1990
  1177
ds@1990
  1178
	return (dest - start);
ds@1990
  1179
}
ds@1990
  1180
ds@1990
  1181
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
ds@1990
  1182
{
ds@1990
  1183
	va_list ap;
ds@1990
  1184
	va_start(ap, fmt);
ds@1990
  1185
	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
ds@1990
  1186
	va_end(ap);
ds@1990
  1187
ds@1990
  1188
	if (len >= maxlength)
ds@1990
  1189
	{
ds@1990
  1190
		buffer[maxlength - 1] = '\0';
ds@1990
  1191
		return (maxlength - 1);
ds@1990
  1192
	}
ds@1990
  1193
	else
ds@1990
  1194
	{
ds@1990
  1195
		return len;
ds@1990
  1196
	}
ds@1990
  1197
}