extensions/topmenus/TopMenu.cpp
author David Anderson <dvander@alliedmods.net>
Tue Sep 02 06:01:12 2008 +0000 (2008-09-02)
changeset 2246 b9bb53f4343f
parent 1990 de135d1f05b1
child 2289 44045ead61cd
permissions -rw-r--r--
Fixed amb1935 - When deleting a topmenu category, child objects were not removed from the name cache.
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));
ds@1990
    96
	size += (sizeof(topmenu_player_t) * m_max_clients);
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. */
ds@1990
   251
		m_Categories.push_back(cat);
ds@1990
   252
		m_SerialNo++;
ds@1990
   253
ds@1990
   254
		/* Updating sorting info */
ds@1990
   255
		m_bCatsNeedResort = true;
ds@1990
   256
	}
ds@1990
   257
	else if (obj->type == TopMenuObject_Item)
ds@1990
   258
	{
ds@1990
   259
		/* Update the category, mark it as needing changes */
ds@1990
   260
		parent_cat->obj_list.push_back(obj);
ds@1990
   261
		parent_cat->reorder = true;
ds@1990
   262
		parent_cat->serial++;
ds@1990
   263
ds@1990
   264
		/* If the category just went from 0 to 1 items, mark it as 
ds@1990
   265
		 * changed, so clients get the category drawn.
ds@1990
   266
		 */
ds@1990
   267
		if (parent_cat->obj_list.size() == 1)
ds@1990
   268
		{
ds@1990
   269
			m_SerialNo++;
ds@1990
   270
		}
ds@1990
   271
	}
ds@1990
   272
ds@1990
   273
	m_ObjLookup.insert(name, obj);
ds@1990
   274
ds@1990
   275
	return obj->object_id;
ds@1990
   276
}
ds@1990
   277
ds@1990
   278
const char *TopMenu::GetObjectInfoString(unsigned int object_id)
ds@1990
   279
{
ds@1990
   280
	if (object_id == 0 
ds@1990
   281
		|| object_id > m_Objects.size() 
ds@1990
   282
		|| m_Objects[object_id - 1]->is_free)
ds@1990
   283
	{
ds@1990
   284
		return NULL;
ds@1990
   285
	}
ds@1990
   286
ds@1990
   287
	topmenu_object_t *obj = m_Objects[object_id - 1];
ds@1990
   288
ds@1990
   289
	return obj->info;
ds@1990
   290
}
ds@1990
   291
ds@1990
   292
const char *TopMenu::GetObjectName(unsigned int object_id)
ds@1990
   293
{
ds@1990
   294
	if (object_id == 0 
ds@1990
   295
		|| object_id > m_Objects.size() 
ds@1990
   296
		|| m_Objects[object_id - 1]->is_free)
ds@1990
   297
	{
ds@1990
   298
		return NULL;
ds@1990
   299
	}
ds@1990
   300
ds@1990
   301
	topmenu_object_t *obj = m_Objects[object_id - 1];
ds@1990
   302
ds@1990
   303
	return obj->name;
ds@1990
   304
}
ds@1990
   305
ds@1990
   306
void TopMenu::RemoveFromMenu(unsigned int object_id)
ds@1990
   307
{
ds@1990
   308
	if (object_id == 0 
ds@1990
   309
		|| object_id > m_Objects.size() 
ds@1990
   310
		|| m_Objects[object_id - 1]->is_free)
ds@1990
   311
	{
ds@1990
   312
		return;
ds@1990
   313
	}
ds@1990
   314
ds@1990
   315
	topmenu_object_t *obj = m_Objects[object_id - 1];
ds@1990
   316
ds@1990
   317
	m_ObjLookup.remove(obj->name);
ds@1990
   318
ds@1990
   319
	if (obj->type == TopMenuObject_Category)
ds@1990
   320
	{
ds@1990
   321
		/* Find it in the category list. */
ds@1990
   322
		for (size_t i = 0; i < m_Categories.size(); i++)
ds@1990
   323
		{
ds@1990
   324
			if (m_Categories[i]->obj == obj)
ds@1990
   325
			{
ds@1990
   326
				/* Mark all children as removed + free.  Note we could
ds@1990
   327
				 * call into RemoveMenuItem() for this, but it'd be very 
ds@1990
   328
				 * inefficient!
ds@1990
   329
				 */
ds@1990
   330
				topmenu_category_t *cat = m_Categories[i];
ds@1990
   331
				for (size_t j = 0; j < m_Categories[i]->obj_list.size(); j++)
ds@1990
   332
				{
dvander@2246
   333
					m_ObjLookup.remove(cat->obj_list[j]->name);
ds@1990
   334
					cat->obj_list[j]->callbacks->OnTopMenuObjectRemoved(this, cat->obj_list[j]->object_id);
ds@1990
   335
					cat->obj_list[j]->is_free = true;
ds@1990
   336
				}
ds@1990
   337
				
ds@1990
   338
				/* Remove the category from the list, then delete it. */
ds@1990
   339
				m_Categories.erase(m_Categories.iterAt(i));
ds@1990
   340
				delete cat;
ds@1990
   341
				break;
ds@1990
   342
			}
ds@1990
   343
		}
ds@1990
   344
ds@1990
   345
		/* Update the root as changed. */
ds@1990
   346
		m_SerialNo++;
ds@1990
   347
		m_bCatsNeedResort = true;
ds@1990
   348
	}
ds@1990
   349
	else if (obj->type == TopMenuObject_Item)
ds@1990
   350
	{
ds@1990
   351
		/* Find the category this item is in. */
ds@1990
   352
		topmenu_category_t *parent_cat = NULL;
ds@1990
   353
		for (size_t i = 0; i < m_Categories.size(); i++)
ds@1990
   354
		{
ds@1990
   355
			if (m_Categories[i]->obj == obj->parent)
ds@1990
   356
			{
ds@1990
   357
				parent_cat = m_Categories[i];
ds@1990
   358
				break;
ds@1990
   359
			}
ds@1990
   360
		}
ds@1990
   361
ds@1990
   362
		/* Erase it from the category's lists. */
ds@1990
   363
		if (parent_cat)
ds@1990
   364
		{
ds@1990
   365
			for (size_t i = 0; i < parent_cat->obj_list.size(); i++)
ds@1990
   366
			{
ds@1990
   367
				if (parent_cat->obj_list[i] == obj)
ds@1990
   368
				{
ds@1990
   369
					parent_cat->obj_list.erase(parent_cat->obj_list.iterAt(i));
ds@1990
   370
ds@1990
   371
					/* If this category now has no items, mark root as changed 
ds@1990
   372
					 * so clients won't get the category drawn anymore.
ds@1990
   373
					 */
ds@1990
   374
					if (parent_cat->obj_list.size() == 0)
ds@1990
   375
					{
ds@1990
   376
						m_SerialNo++;
ds@1990
   377
					}
ds@1990
   378
					break;
ds@1990
   379
				}
ds@1990
   380
			}
ds@1990
   381
ds@1990
   382
			/* Update the category as changed. */
ds@1990
   383
			parent_cat->reorder = true;
ds@1990
   384
			parent_cat->serial++;
ds@1990
   385
		}
ds@1990
   386
	}
ds@1990
   387
ds@1990
   388
	/* The callbacks pointer is still valid, so fire away! */
ds@1990
   389
	obj->callbacks->OnTopMenuObjectRemoved(this, object_id);
ds@1990
   390
ds@1990
   391
	/* Finally, mark the object as free. */
ds@1990
   392
	obj->is_free = true;
ds@1990
   393
}
ds@1990
   394
ds@1990
   395
bool TopMenu::DisplayMenu(int client, unsigned int hold_time, TopMenuPosition position)
ds@1990
   396
{
ds@1990
   397
	if (m_clients == NULL)
ds@1990
   398
	{
ds@1990
   399
		return false;
ds@1990
   400
	}
ds@1990
   401
ds@1990
   402
	IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client);
ds@1990
   403
	if (!pPlayer->IsInGame())
ds@1990
   404
	{
ds@1990
   405
		return false;
ds@1990
   406
	}
ds@1990
   407
ds@1990
   408
	UpdateClientRoot(client, pPlayer);
ds@1990
   409
ds@1990
   410
	topmenu_player_t *pClient = &m_clients[client];
ds@1990
   411
	if (pClient->root == NULL)
ds@1990
   412
	{
ds@1990
   413
		return false;
ds@1990
   414
	}
ds@1990
   415
ds@1990
   416
	bool return_value = false;
ds@1990
   417
ds@1990
   418
	if (position == TopMenuPosition_LastCategory && 
ds@1990
   419
		pClient->last_category < m_Categories.size())
ds@1990
   420
	{
ds@1990
   421
		return_value = DisplayCategory(client, pClient->last_category, hold_time, true);
ds@1990
   422
		if (!return_value)
ds@1990
   423
		{
ds@1990
   424
			return_value = pClient->root->DisplayAtItem(client, hold_time, pClient->last_root_pos);
ds@1990
   425
		}
ds@1990
   426
	}
ds@1990
   427
	else if (position == TopMenuPosition_LastRoot)
ds@1990
   428
	{
ds@1990
   429
		pClient->root->DisplayAtItem(client, hold_time, pClient->last_root_pos);
ds@1990
   430
	}
ds@1990
   431
	else if (position == TopMenuPosition_Start)
ds@1990
   432
	{
ds@1990
   433
		pClient->last_position = 0;
ds@1990
   434
		pClient->last_category = 0;
ds@1990
   435
		return_value = pClient->root->Display(client, hold_time);
ds@1990
   436
	}
ds@1990
   437
ds@1990
   438
	return return_value;
ds@1990
   439
}
ds@1990
   440
ds@1990
   441
bool TopMenu::DisplayCategory(int client, unsigned int category, unsigned int hold_time, bool last_position)
ds@1990
   442
{
ds@1990
   443
	UpdateClientCategory(client, category);
ds@1990
   444
ds@1990
   445
	topmenu_player_t *pClient = &m_clients[client];
ds@1990
   446
	if (category >= pClient->cat_count
ds@1990
   447
		|| pClient->cats[category].menu == NULL)
ds@1990
   448
	{
ds@1990
   449
		return false;
ds@1990
   450
	}
ds@1990
   451
ds@1990
   452
	bool return_value = false;
ds@1990
   453
ds@1990
   454
	topmenu_player_category_t *player_cat = &(pClient->cats[category]);
ds@1990
   455
ds@1990
   456
	pClient->last_category = category;
ds@1990
   457
	if (last_position)
ds@1990
   458
	{
ds@1990
   459
		return_value = player_cat->menu->DisplayAtItem(client, hold_time, pClient->last_position);
ds@1990
   460
	}
ds@1990
   461
	else
ds@1990
   462
	{
ds@1990
   463
		return_value = player_cat->menu->Display(client, hold_time);
ds@1990
   464
	}
ds@1990
   465
ds@1990
   466
	return return_value;
ds@1990
   467
}
ds@1990
   468
ds@1990
   469
void TopMenu::OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page)
ds@1990
   470
{
ds@1990
   471
	const char *item_name = menu->GetItemInfo(item, NULL);
ds@1990
   472
	if (!item_name)
ds@1990
   473
	{
ds@1990
   474
		return;
ds@1990
   475
	}
ds@1990
   476
ds@1990
   477
	topmenu_object_t *obj;
ds@1990
   478
	topmenu_player_t *pClient = &m_clients[client];
ds@1990
   479
	topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name);
ds@1990
   480
	if (pObject == NULL)
ds@1990
   481
	{
ds@1990
   482
		return;
ds@1990
   483
	}
ds@1990
   484
ds@1990
   485
	obj = *pObject;
ds@1990
   486
ds@1990
   487
	/* We now have the object... what do we do with it? */
ds@1990
   488
	if (obj->type == TopMenuObject_Category)
ds@1990
   489
	{
ds@1990
   490
		/* If it's a category, the user wants to view it.. */
ds@1990
   491
		for (size_t i = 0; i < m_Categories.size(); i++)
ds@1990
   492
		{
ds@1990
   493
			if (m_Categories[i]->obj == obj)
ds@1990
   494
			{
ds@1990
   495
				pClient->last_root_pos = item_on_page;
ds@1990
   496
				if (!DisplayCategory(client, (unsigned int)i, MENU_TIME_FOREVER, false))
ds@1990
   497
				{
ds@1990
   498
					/* If we can't display the category, re-display the root menu */
ds@1990
   499
					DisplayMenu(client, MENU_TIME_FOREVER, TopMenuPosition_LastRoot);
ds@1990
   500
				}
ds@1990
   501
				break;
ds@1990
   502
			}
ds@1990
   503
		}
ds@1990
   504
	}
ds@1990
   505
	else
ds@1990
   506
	{
ds@1990
   507
		pClient->last_position = item_on_page;
ds@1990
   508
		
ds@1990
   509
		/* Re-check access in case this user had their credentials revoked */
ds@1990
   510
		if (obj->cmdname[0] != '\0' && !adminsys->CheckAccess(client, obj->cmdname, obj->flags, false))
ds@1990
   511
		{
ds@1990
   512
			DisplayMenu(client, 0, TopMenuPosition_LastCategory);
ds@1990
   513
			return;
ds@1990
   514
		}
ds@1990
   515
		
ds@1990
   516
		/* Pass the information on to the callback */
ds@1990
   517
		obj->callbacks->OnTopMenuSelectOption(this, client, obj->object_id);
ds@1990
   518
	}
ds@1990
   519
}
ds@1990
   520
ds@1990
   521
void TopMenu::OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style)
ds@1990
   522
{
ds@1990
   523
	const char *item_name = menu->GetItemInfo(item, NULL);
ds@1990
   524
	if (!item_name)
ds@1990
   525
	{
ds@1990
   526
		return;
ds@1990
   527
	}
ds@1990
   528
ds@1990
   529
	topmenu_object_t *obj;
ds@1990
   530
	topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name);
ds@1990
   531
	if (pObject == NULL)
ds@1990
   532
	{
ds@1990
   533
		return;
ds@1990
   534
	}
ds@1990
   535
ds@1990
   536
	obj = *pObject;
ds@1990
   537
ds@1990
   538
	style = obj->callbacks->OnTopMenuDrawOption(this, client, obj->object_id);
ds@1990
   539
	if (style != ITEMDRAW_DEFAULT)
ds@1990
   540
	{
ds@1990
   541
		return;
ds@1990
   542
	}
ds@1990
   543
ds@1990
   544
	if (obj->cmdname[0] == '\0')
ds@1990
   545
	{
ds@1990
   546
		return;
ds@1990
   547
	}
ds@1990
   548
ds@1990
   549
	if (!adminsys->CheckAccess(client, obj->cmdname, obj->flags, false))
ds@1990
   550
	{
ds@1990
   551
		style = ITEMDRAW_IGNORE;
ds@1990
   552
	}
ds@1990
   553
}
ds@1990
   554
ds@1990
   555
unsigned int TopMenu::OnMenuDisplayItem(IBaseMenu *menu,
ds@1990
   556
								int client,
ds@1990
   557
								IMenuPanel *panel,
ds@1990
   558
								unsigned int item,
ds@1990
   559
								const ItemDrawInfo &dr)
ds@1990
   560
{
ds@1990
   561
	const char *item_name = menu->GetItemInfo(item, NULL);
ds@1990
   562
	if (!item_name)
ds@1990
   563
	{
ds@1990
   564
		return 0;
ds@1990
   565
	}
ds@1990
   566
ds@1990
   567
	topmenu_object_t *obj;
ds@1990
   568
	topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name);
ds@1990
   569
	if (pObject == NULL)
ds@1990
   570
	{
ds@1990
   571
		return 0;
ds@1990
   572
	}
ds@1990
   573
ds@1990
   574
	obj = *pObject;
ds@1990
   575
ds@1990
   576
	/* Ask the object to render the text for this client */
ds@1990
   577
	char renderbuf[64];
ds@1990
   578
	obj->callbacks->OnTopMenuDisplayOption(this, client, obj->object_id, renderbuf, sizeof(renderbuf));
ds@1990
   579
ds@1990
   580
	/* Build the new draw info */
ds@1990
   581
	ItemDrawInfo new_dr = dr;
ds@1990
   582
	new_dr.display = renderbuf;
ds@1990
   583
ds@1990
   584
	/* Man I love the menu API.  Ask the panel to draw the item and give the position 
ds@1990
   585
	 * back to Core's renderer.  This way we don't have to worry about keeping the 
ds@1990
   586
	 * render buffer static!
ds@1990
   587
	 */
ds@1990
   588
	return panel->DrawItem(new_dr);
ds@1990
   589
}
ds@1990
   590
ds@1990
   591
void TopMenu::OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
ds@1990
   592
{
ds@1990
   593
	if (reason == MenuCancel_ExitBack)
ds@1990
   594
	{
ds@1990
   595
		/* If the client chose exit back, they were on a category menu, and we can
ds@1990
   596
		 * now display the root menu from the last known position.
ds@1990
   597
		 */
ds@1990
   598
		DisplayMenu(client, 0, TopMenuPosition_LastRoot);
ds@1990
   599
	}
ds@1990
   600
}
ds@1990
   601
ds@1990
   602
void TopMenu::UpdateClientRoot(int client, IGamePlayer *pGamePlayer)
ds@1990
   603
{
ds@1990
   604
	topmenu_player_t *pClient = &m_clients[client];
ds@1990
   605
	IGamePlayer *pPlayer = pGamePlayer ? pGamePlayer : playerhelpers->GetGamePlayer(client);
ds@1990
   606
ds@1990
   607
	/* Determine if an update is necessary */
ds@1990
   608
	bool is_update_needed = false;
ds@1990
   609
	if (pClient->menu_serial != m_SerialNo)
ds@1990
   610
	{
ds@1990
   611
		is_update_needed = true;
ds@1990
   612
	}
ds@1990
   613
	else if (pPlayer->GetUserId() != pClient->user_id)
ds@1990
   614
	{
ds@1990
   615
		is_update_needed = true;
ds@1990
   616
	}
ds@1990
   617
ds@1990
   618
	/* If no update is needed at the root level, just leave now */
ds@1990
   619
	if (!is_update_needed)
ds@1990
   620
	{
ds@1990
   621
		return;
ds@1990
   622
	}
ds@1990
   623
ds@1990
   624
	/* First we need to flush the cache... */
ds@1990
   625
	TearDownClient(pClient);
ds@1990
   626
ds@1990
   627
	/* Now, rebuild the category list, but don't create menus */
ds@1990
   628
	if (m_Categories.size() == 0)
ds@1990
   629
	{
ds@1990
   630
		pClient->cat_count = 0;
ds@1990
   631
		pClient->cats = NULL;
ds@1990
   632
	}
ds@1990
   633
	else
ds@1990
   634
	{
ds@1990
   635
		pClient->cat_count = (unsigned int)m_Categories.size();
ds@1990
   636
		pClient->cats = new topmenu_player_category_t[pClient->cat_count];
ds@1990
   637
		memset(pClient->cats, 0, sizeof(topmenu_player_category_t) * pClient->cat_count);
ds@1990
   638
	}
ds@1990
   639
ds@1990
   640
	/* Re-sort the root categories if needed */
ds@1990
   641
	SortCategoriesIfNeeded();
ds@1990
   642
ds@1990
   643
	/* Build the root menu */
ds@1990
   644
	IBaseMenu *root_menu = menus->GetDefaultStyle()->CreateMenu(this, myself->GetIdentity());
ds@1990
   645
ds@1990
   646
	/* Add the sorted items */
ds@1990
   647
	for (size_t i = 0; i < m_SortedCats.size(); i++)
ds@1990
   648
	{
ds@1990
   649
		if (m_Categories[m_SortedCats[i]]->obj_list.size() == 0)
ds@1990
   650
		{
ds@1990
   651
			continue;
ds@1990
   652
		}
ds@1990
   653
		root_menu->AppendItem(m_Categories[m_SortedCats[i]]->obj->name, ItemDrawInfo(""));
ds@1990
   654
	}
ds@1990
   655
ds@1990
   656
	/* Now we need to handle un-sorted items.  This is slightly trickier, as we need to 
ds@1990
   657
	 * pre-render each category name, and cache those names.  Phew!
ds@1990
   658
	 */
ds@1990
   659
	if (m_UnsortedCats.size())
ds@1990
   660
	{
ds@1990
   661
		obj_by_name_t *item_list = new obj_by_name_t[m_UnsortedCats.size()];
ds@1990
   662
		for (size_t i = 0; i < m_UnsortedCats.size(); i++)
ds@1990
   663
		{
ds@1990
   664
			obj_by_name_t *temp_obj = &item_list[i];
ds@1990
   665
			topmenu_object_t *obj = m_Categories[m_UnsortedCats[i]]->obj;
ds@1990
   666
			obj->callbacks->OnTopMenuDisplayOption(this,
ds@1990
   667
				client, 
ds@1990
   668
				obj->object_id,
ds@1990
   669
				temp_obj->name,
ds@1990
   670
				sizeof(temp_obj->name));
ds@1990
   671
			temp_obj->obj_index = m_UnsortedCats[i];
ds@1990
   672
		}
ds@1990
   673
ds@1990
   674
		/* Sort our temp list */
ds@1990
   675
		qsort(item_list, m_UnsortedCats.size(), sizeof(obj_by_name_t), _SortObjectNamesDescending);
ds@1990
   676
ds@1990
   677
		/* Add the new sorted categories */
ds@1990
   678
		for (size_t i = 0; i < m_UnsortedCats.size(); i++)
ds@1990
   679
		{
ds@1990
   680
			if (m_Categories[item_list[i].obj_index]->obj_list.size() == 0)
ds@1990
   681
			{
ds@1990
   682
				continue;
ds@1990
   683
			}
ds@1990
   684
			root_menu->AppendItem(m_Categories[item_list[i].obj_index]->obj->name, ItemDrawInfo(""));
ds@1990
   685
		}
ds@1990
   686
ds@1990
   687
		delete [] item_list;
ds@1990
   688
	}
ds@1990
   689
ds@1990
   690
	/* Set the menu's title */
ds@1990
   691
	char renderbuf[128];
ds@1990
   692
	m_pTitle->OnTopMenuDisplayTitle(this, client, 0, renderbuf, sizeof(renderbuf));
ds@1990
   693
	root_menu->SetDefaultTitle(renderbuf);
ds@1990
   694
ds@1990
   695
	/* The client is now fully updated */
ds@1990
   696
	pClient->root = root_menu;
ds@1990
   697
	pClient->user_id = pPlayer->GetUserId();
ds@1990
   698
	pClient->menu_serial = m_SerialNo;
ds@1990
   699
	pClient->last_position = 0;
ds@1990
   700
	pClient->last_category = 0;
ds@1990
   701
	pClient->last_root_pos = 0;
ds@1990
   702
}
ds@1990
   703
ds@1990
   704
void TopMenu::UpdateClientCategory(int client, unsigned int category)
ds@1990
   705
{
ds@1990
   706
	/* Update the client's root menu just in case it needs it.  This 
ds@1990
   707
	 * will validate that we have both a valid client and a valid
ds@1990
   708
	 * category structure for that client.
ds@1990
   709
	 */
ds@1990
   710
	UpdateClientRoot(client);
ds@1990
   711
ds@1990
   712
	/* Now it's guaranteed that our category tables will be usable */
ds@1990
   713
	topmenu_player_t *pClient = &m_clients[client];
ds@1990
   714
	topmenu_category_t *cat = m_Categories[category];
ds@1990
   715
	topmenu_player_category_t *player_cat = &(pClient->cats[category]);
ds@1990
   716
ds@1990
   717
	/* Does the category actually need updating? */
ds@1990
   718
	if (player_cat->serial == cat->serial)
ds@1990
   719
	{
ds@1990
   720
		return;
ds@1990
   721
	}
ds@1990
   722
ds@1990
   723
	/* Destroy any existing menu */
ds@1990
   724
	if (player_cat->menu)
ds@1990
   725
	{
ds@1990
   726
		player_cat->menu->Destroy();
ds@1990
   727
		player_cat->menu = NULL;
ds@1990
   728
	}
ds@1990
   729
ds@1990
   730
	if (pClient->last_category == category)
ds@1990
   731
	{
ds@1990
   732
		pClient->last_position = 0;
ds@1990
   733
	}
ds@1990
   734
ds@1990
   735
	IBaseMenu *cat_menu = menus->GetDefaultStyle()->CreateMenu(this, myself->GetIdentity());
ds@1990
   736
	
ds@1990
   737
	/* Categories get an "exit back" button */
ds@1990
   738
	cat_menu->SetMenuOptionFlags(cat_menu->GetMenuOptionFlags() | MENUFLAG_BUTTON_EXITBACK);
ds@1990
   739
ds@1990
   740
	/* Re-sort the category if needed */
ds@1990
   741
	SortCategoryIfNeeded(category);
ds@1990
   742
ds@1990
   743
	/* Build the menu with the sorted items first */
ds@1990
   744
	for (size_t i = 0; i < cat->sorted.size(); i++)
ds@1990
   745
	{
ds@1990
   746
		cat_menu->AppendItem(cat->sorted[i]->name, ItemDrawInfo(""));
ds@1990
   747
	}
ds@1990
   748
ds@1990
   749
	/* Now handle unsorted items */
ds@1990
   750
	if (cat->unsorted.size())
ds@1990
   751
	{
ds@1990
   752
		/* Build a list of the item names */
ds@1990
   753
		obj_by_name_t *item_list = new obj_by_name_t[cat->unsorted.size()];
ds@1990
   754
		for (size_t i = 0; i < cat->unsorted.size(); i++)
ds@1990
   755
		{
ds@1990
   756
			obj_by_name_t *item = &item_list[i];
ds@1990
   757
			topmenu_object_t *obj = cat->unsorted[i];
ds@1990
   758
			obj->callbacks->OnTopMenuDisplayOption(this,
ds@1990
   759
				client,
ds@1990
   760
				obj->object_id,
ds@1990
   761
				item->name,
ds@1990
   762
				sizeof(item->name));
ds@1990
   763
			item->obj_index = (unsigned int)i;
ds@1990
   764
		}
ds@1990
   765
ds@1990
   766
		/* Sort the names */
ds@1990
   767
		qsort(item_list, cat->unsorted.size(), sizeof(obj_by_name_t), _SortObjectNamesDescending);
ds@1990
   768
ds@1990
   769
		/* Add to the menu */
ds@1990
   770
		for (size_t i = 0; i < cat->unsorted.size(); i++)
ds@1990
   771
		{
ds@1990
   772
			cat_menu->AppendItem(cat->unsorted[item_list[i].obj_index]->name, ItemDrawInfo(""));
ds@1990
   773
		}
ds@1990
   774
ds@1990
   775
		delete [] item_list;
ds@1990
   776
	}
ds@1990
   777
ds@1990
   778
	/* Set the menu's title */
ds@1990
   779
	char renderbuf[128];
ds@1990
   780
	cat->obj->callbacks->OnTopMenuDisplayTitle(this, 
ds@1990
   781
		client, 
ds@1990
   782
		cat->obj->object_id, 
ds@1990
   783
		renderbuf, 
ds@1990
   784
		sizeof(renderbuf));
ds@1990
   785
	cat_menu->SetDefaultTitle(renderbuf);
ds@1990
   786
ds@1990
   787
	/* We're done! */
ds@1990
   788
	player_cat->menu = cat_menu;
ds@1990
   789
	player_cat->serial = cat->serial;
ds@1990
   790
}
ds@1990
   791
ds@1990
   792
void TopMenu::SortCategoryIfNeeded(unsigned int category)
ds@1990
   793
{
ds@1990
   794
	topmenu_category_t *cat = m_Categories[category];
ds@1990
   795
	if (!cat->reorder)
ds@1990
   796
	{
ds@1990
   797
		return;
ds@1990
   798
	}
ds@1990
   799
ds@1990
   800
	cat->sorted.clear();
ds@1990
   801
	cat->unsorted.clear();
ds@1990
   802
ds@1990
   803
	if (cat->obj_list.size() == 0)
ds@1990
   804
	{
ds@1990
   805
		cat->reorder = false;
ds@1990
   806
		return;
ds@1990
   807
	}
ds@1990
   808
ds@1990
   809
	CVector<unsigned int> to_sort;
ds@1990
   810
	for (size_t i = 0; i < cat->obj_list.size(); i++)
ds@1990
   811
	{
ds@1990
   812
		to_sort.push_back(i);
ds@1990
   813
	}
ds@1990
   814
ds@1990
   815
	/* Find a matching category in the configs */
ds@1990
   816
	config_category_t *config_cat = NULL; 
ds@1990
   817
	for (size_t i = 0; i < m_Config.cats.size(); i++)
ds@1990
   818
	{
ds@1990
   819
		if (strcmp(m_Config.strings.GetString(m_Config.cats[i]->name), cat->obj->name) == 0)
ds@1990
   820
		{
ds@1990
   821
			config_cat = m_Config.cats[i];
ds@1990
   822
			break;
ds@1990
   823
		}
ds@1990
   824
	}
ds@1990
   825
ds@1990
   826
	/* If there is a matching category, build a pre-sorted item list */
ds@1990
   827
	if (config_cat != NULL)
ds@1990
   828
	{
ds@1990
   829
		/* Find matching objects in this category */
ds@1990
   830
		for (size_t i = 0; i < config_cat->commands.size(); i++)
ds@1990
   831
		{
ds@1990
   832
			const char *config_name = m_Config.strings.GetString(config_cat->commands[i]);
ds@1990
   833
			for (size_t j = 0; j < to_sort.size(); j++)
ds@1990
   834
			{
ds@1990
   835
				if (strcmp(config_name, cat->obj_list[to_sort[j]]->name) == 0)
ds@1990
   836
				{
ds@1990
   837
					/* Place in the final list, then remove from the temporary list */
ds@1990
   838
					cat->sorted.push_back(cat->obj_list[to_sort[j]]);
ds@1990
   839
					to_sort.erase(to_sort.iterAt(j));
ds@1990
   840
					break;
ds@1990
   841
				}
ds@1990
   842
			}
ds@1990
   843
		}
ds@1990
   844
	}
ds@1990
   845
ds@1990
   846
	/* Push any remaining items onto the unsorted list */
ds@1990
   847
	for (size_t i = 0; i < to_sort.size(); i++)
ds@1990
   848
	{
ds@1990
   849
		cat->unsorted.push_back(cat->obj_list[to_sort[i]]);
ds@1990
   850
	}
ds@1990
   851
ds@1990
   852
	cat->reorder = false;
ds@1990
   853
}
ds@1990
   854
ds@1990
   855
void TopMenu::SortCategoriesIfNeeded()
ds@1990
   856
{
ds@1990
   857
	if (!m_bCatsNeedResort)
ds@1990
   858
	{
ds@1990
   859
		return;
ds@1990
   860
	}
ds@1990
   861
ds@1990
   862
	/* Clear sort results */
ds@1990
   863
	m_SortedCats.clear();
ds@1990
   864
	m_UnsortedCats.clear();
ds@1990
   865
ds@1990
   866
	if (m_Categories.size() == 0)
ds@1990
   867
	{
ds@1990
   868
		m_bCatsNeedResort = false;
ds@1990
   869
		return;
ds@1990
   870
	}
ds@1990
   871
ds@1990
   872
	CVector<unsigned int> to_sort;
ds@1990
   873
	for (unsigned int i = 0; i < (unsigned int)m_Categories.size(); i++)
ds@1990
   874
	{
ds@1990
   875
		to_sort.push_back(i);
ds@1990
   876
	}
ds@1990
   877
ds@1990
   878
	/* If we have any predefined categories, add them in as they appear. */
ds@1990
   879
	for (size_t i= 0; i < m_Config.cats.size(); i++)
ds@1990
   880
	{
ds@1990
   881
		/* Find this category and map it in if we can */
ds@1990
   882
		for (size_t j = 0; j < to_sort.size(); j++)
ds@1990
   883
		{
ds@1990
   884
			if (strcmp(m_Config.strings.GetString(m_Config.cats[i]->name), 
ds@1990
   885
					   m_Categories[to_sort[j]]->obj->name) == 0)
ds@1990
   886
			{
ds@1990
   887
				/* Add to the real list and remove from the temporary */
ds@1990
   888
				m_SortedCats.push_back(to_sort[j]);
ds@1990
   889
				to_sort.erase(to_sort.iterAt(j));
ds@1990
   890
				break;
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
		m_UnsortedCats.push_back(to_sort[i]);
ds@1990
   899
	}
ds@1990
   900
ds@1990
   901
	m_bCatsNeedResort = false;
ds@1990
   902
}
ds@1990
   903
ds@1990
   904
void TopMenu::CreatePlayers(int max_clients)
ds@1990
   905
{
ds@1990
   906
	m_max_clients = max_clients;
ds@1990
   907
	m_clients = (topmenu_player_t *)malloc(sizeof(topmenu_player_t) * (max_clients + 1));
ds@1990
   908
	memset(m_clients, 0, sizeof(topmenu_player_t) * (max_clients + 1));
ds@1990
   909
}
ds@1990
   910
ds@1990
   911
void TopMenu::TearDownClient(topmenu_player_t *player)
ds@1990
   912
{
ds@1990
   913
	if (player->cats != NULL)
ds@1990
   914
	{
ds@1990
   915
		for (unsigned int i = 0; i < player->cat_count; i++)
ds@1990
   916
		{
ds@1990
   917
			topmenu_player_category_t *player_cat = &(player->cats[i]);
ds@1990
   918
			if (player_cat->menu != NULL)
ds@1990
   919
			{
ds@1990
   920
				player_cat->menu->Destroy();
ds@1990
   921
			}
ds@1990
   922
		}
ds@1990
   923
		delete [] player->cats;
ds@1990
   924
	}
ds@1990
   925
ds@1990
   926
	if (player->root != NULL)
ds@1990
   927
	{
ds@1990
   928
		player->root->Destroy();
ds@1990
   929
	}
ds@1990
   930
ds@1990
   931
	memset(player, 0, sizeof(topmenu_player_t));
ds@1990
   932
}
ds@1990
   933
ds@1990
   934
bool TopMenu::LoadConfiguration(const char *file, char *error, size_t maxlength)
ds@1990
   935
{
ds@1990
   936
	SMCError err;
ds@1990
   937
	SMCStates states;
ds@1990
   938
ds@1990
   939
	if ((err = textparsers->ParseFile_SMC(file, this, &states))
ds@1990
   940
		!= SMCError_Okay)
ds@1990
   941
	{
ds@1990
   942
		const char *err_string = textparsers->GetSMCErrorString(err);
ds@1990
   943
		if (!err_string)
ds@1990
   944
		{
ds@1990
   945
			err_string = "Unknown";
ds@1990
   946
		}
ds@1990
   947
ds@1990
   948
		UTIL_Format(error, maxlength, "%s", err_string);
ds@1990
   949
ds@1990
   950
		return false;
ds@1990
   951
	}
ds@1990
   952
ds@1990
   953
	return true;
ds@1990
   954
}
ds@1990
   955
ds@1990
   956
bool TopMenu::OnIdentityRemoval(IdentityToken_t *owner)
ds@1990
   957
{
ds@1990
   958
	/* First sweep the categories owned by us */
ds@1990
   959
	CVector<unsigned int> obj_list;
ds@1990
   960
	for (size_t i = 0; i < m_Categories.size(); i++)
ds@1990
   961
	{
ds@1990
   962
		if (m_Categories[i]->obj->owner == owner)
ds@1990
   963
		{
ds@1990
   964
			obj_list.push_back(m_Categories[i]->obj->object_id);
ds@1990
   965
		}
ds@1990
   966
	}
ds@1990
   967
ds@1990
   968
	for (size_t i = 0; i < obj_list.size(); i++)
ds@1990
   969
	{
ds@1990
   970
		RemoveFromMenu(obj_list[i]);
ds@1990
   971
	}
ds@1990
   972
ds@1990
   973
	/* Now we can look for actual items */
ds@1990
   974
	for (size_t i = 0; i < m_Objects.size(); i++)
ds@1990
   975
	{
ds@1990
   976
		if (m_Objects[i]->is_free)
ds@1990
   977
		{
ds@1990
   978
			continue;
ds@1990
   979
		}
ds@1990
   980
		if (m_Objects[i]->owner == owner)
ds@1990
   981
		{
ds@1990
   982
			assert(m_Objects[i]->type != TopMenuObject_Category);
ds@1990
   983
			RemoveFromMenu(m_Objects[i]->object_id);
ds@1990
   984
		}
ds@1990
   985
	}
ds@1990
   986
ds@1990
   987
	return true;
ds@1990
   988
}
ds@1990
   989
ds@1990
   990
#define PARSE_STATE_NONE		0
ds@1990
   991
#define PARSE_STATE_MAIN		1
ds@1990
   992
#define PARSE_STATE_CATEGORY	2
ds@1990
   993
unsigned int ignore_parse_level = 0;
ds@1990
   994
unsigned int current_parse_state = 0;
ds@1990
   995
config_category_t *cur_cat = NULL;
ds@1990
   996
ds@1990
   997
void TopMenu::ReadSMC_ParseStart()
ds@1990
   998
{
ds@1990
   999
	current_parse_state = PARSE_STATE_NONE;
ds@1990
  1000
	ignore_parse_level = 0;
ds@1990
  1001
	cur_cat = NULL;
ds@1990
  1002
ds@1990
  1003
	/* Reset the old config */
ds@1990
  1004
	m_Config.strings.Reset();
ds@1990
  1005
	for (size_t i = 0; i < m_Config.cats.size(); i++)
ds@1990
  1006
	{
ds@1990
  1007
		delete m_Config.cats[i];
ds@1990
  1008
	}
ds@1990
  1009
	m_Config.cats.clear();
ds@1990
  1010
}
ds@1990
  1011
ds@1990
  1012
SMCResult TopMenu::ReadSMC_NewSection(const SMCStates *states, const char *name)
ds@1990
  1013
{
ds@1990
  1014
	if (ignore_parse_level)
ds@1990
  1015
	{
ds@1990
  1016
		ignore_parse_level++;
ds@1990
  1017
	}
ds@1990
  1018
	else
ds@1990
  1019
	{
ds@1990
  1020
		if (current_parse_state == PARSE_STATE_NONE)
ds@1990
  1021
		{
ds@1990
  1022
			if (strcmp(name, "Menu") == 0)
ds@1990
  1023
			{
ds@1990
  1024
				current_parse_state = PARSE_STATE_MAIN;
ds@1990
  1025
			}
ds@1990
  1026
			else
ds@1990
  1027
			{
ds@1990
  1028
				ignore_parse_level = 1;
ds@1990
  1029
			}
ds@1990
  1030
		}
ds@1990
  1031
		else if (current_parse_state == PARSE_STATE_MAIN)
ds@1990
  1032
		{
ds@1990
  1033
			cur_cat = new config_category_t;
ds@1990
  1034
			cur_cat->name = m_Config.strings.AddString(name);
ds@1990
  1035
			m_Config.cats.push_back(cur_cat);
ds@1990
  1036
			current_parse_state = PARSE_STATE_CATEGORY;
ds@1990
  1037
		}
ds@1990
  1038
		else
ds@1990
  1039
		{
ds@1990
  1040
			ignore_parse_level = 1;
ds@1990
  1041
		}
ds@1990
  1042
	}
ds@1990
  1043
ds@1990
  1044
	return SMCResult_Continue;
ds@1990
  1045
}
ds@1990
  1046
ds@1990
  1047
SMCResult TopMenu::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
ds@1990
  1048
{
ds@1990
  1049
	if (ignore_parse_level > 0
ds@1990
  1050
		|| current_parse_state != PARSE_STATE_CATEGORY
ds@1990
  1051
		|| cur_cat == NULL)
ds@1990
  1052
	{
ds@1990
  1053
		return SMCResult_Continue;
ds@1990
  1054
	}
ds@1990
  1055
ds@1990
  1056
	if (strcmp(key, "item") == 0)
ds@1990
  1057
	{
ds@1990
  1058
		cur_cat->commands.push_back(m_Config.strings.AddString(value));
ds@1990
  1059
	}
ds@1990
  1060
ds@1990
  1061
	return SMCResult_Continue;
ds@1990
  1062
}
ds@1990
  1063
ds@1990
  1064
SMCResult TopMenu::ReadSMC_LeavingSection(const SMCStates *states)
ds@1990
  1065
{
ds@1990
  1066
	if (ignore_parse_level)
ds@1990
  1067
	{
ds@1990
  1068
		ignore_parse_level--;
ds@1990
  1069
	}
ds@1990
  1070
	else
ds@1990
  1071
	{
ds@1990
  1072
		if (current_parse_state == PARSE_STATE_CATEGORY)
ds@1990
  1073
		{
ds@1990
  1074
			cur_cat = NULL;
ds@1990
  1075
			current_parse_state = PARSE_STATE_MAIN;
ds@1990
  1076
		}
ds@1990
  1077
		else if (current_parse_state == PARSE_STATE_MAIN)
ds@1990
  1078
		{
ds@1990
  1079
			current_parse_state = PARSE_STATE_NONE;
ds@1990
  1080
		}
ds@1990
  1081
	}
ds@1990
  1082
ds@1990
  1083
	return SMCResult_Continue;
ds@1990
  1084
}
ds@1990
  1085
ds@1990
  1086
unsigned int TopMenu::FindCategory(const char *name)
ds@1990
  1087
{
ds@1990
  1088
	topmenu_object_t **p_obj = m_ObjLookup.retrieve(name);
ds@1990
  1089
	if (!p_obj)
ds@1990
  1090
	{
ds@1990
  1091
		return 0;
ds@1990
  1092
	}
ds@1990
  1093
ds@1990
  1094
	topmenu_object_t *obj = *p_obj;
ds@1990
  1095
	if (obj->type != TopMenuObject_Category)
ds@1990
  1096
	{
ds@1990
  1097
		return 0;
ds@1990
  1098
	}
ds@1990
  1099
ds@1990
  1100
	return obj->object_id;
ds@1990
  1101
}
ds@1990
  1102
ds@1990
  1103
int _SortObjectNamesDescending(const void *ptr1, const void *ptr2)
ds@1990
  1104
{
ds@1990
  1105
	obj_by_name_t *obj1 = (obj_by_name_t *)ptr1;
ds@1990
  1106
	obj_by_name_t *obj2 = (obj_by_name_t *)ptr2;
ds@1990
  1107
	return strcmp(obj1->name, obj2->name);
ds@1990
  1108
}
ds@1990
  1109
ds@1990
  1110
unsigned int strncopy(char *dest, const char *src, size_t count)
ds@1990
  1111
{
ds@1990
  1112
	if (!count)
ds@1990
  1113
	{
ds@1990
  1114
		return 0;
ds@1990
  1115
	}
ds@1990
  1116
ds@1990
  1117
	char *start = dest;
ds@1990
  1118
	while ((*src) && (--count))
ds@1990
  1119
	{
ds@1990
  1120
		*dest++ = *src++;
ds@1990
  1121
	}
ds@1990
  1122
	*dest = '\0';
ds@1990
  1123
ds@1990
  1124
	return (dest - start);
ds@1990
  1125
}
ds@1990
  1126
ds@1990
  1127
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
ds@1990
  1128
{
ds@1990
  1129
	va_list ap;
ds@1990
  1130
	va_start(ap, fmt);
ds@1990
  1131
	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
ds@1990
  1132
	va_end(ap);
ds@1990
  1133
ds@1990
  1134
	if (len >= maxlength)
ds@1990
  1135
	{
ds@1990
  1136
		buffer[maxlength - 1] = '\0';
ds@1990
  1137
		return (maxlength - 1);
ds@1990
  1138
	}
ds@1990
  1139
	else
ds@1990
  1140
	{
ds@1990
  1141
		return len;
ds@1990
  1142
	}
ds@1990
  1143
}