cssdm_weapons.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Wed Jul 18 13:44:57 2012 -0400 (2012-07-18)
changeset 92 52cc55d67b2f
parent 32 9a3116df1513
child 121 a5f1a07726c8
permissions -rw-r--r--
Added support for CS:GO (bug 5342, r=psychonic)
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * By David "BAILOPAN" Anderson
     6  * All rights reserved.
     7  * ===============================================================
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or (at
    12  * your option) any later version.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but
    15  * WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    17  * General Public License for more details.
    18  * 
    19  * You should have received a copy of the GNU General Public License
    20  * along with this program; see the file COPYING; if not, write to the
    21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
    22  * MA 02110-1301 USA
    23  * 
    24  * Version: $Id$
    25  */
    26 
    27 #include <string.h>
    28 #include "cssdm_weapons.h"
    29 #include "cssdm_headers.h"
    30 #include <sm_trie_tpl.h>
    31 #include <sh_vector.h>
    32 
    33 using namespace SourceHook;
    34 
    35 KTrie<dm_weapon_t *> g_WeaponLookup;
    36 CVector<dm_weapon_t *> g_Weapons;
    37 
    38 dm_weapon_t *DM_FindWeapon(const char *name)
    39 {
    40 	dm_weapon_t **pWp;
    41 
    42 	if ((pWp = g_WeaponLookup.retrieve(name)) == NULL)
    43 	{
    44 		return NULL;
    45 	}
    46 
    47 	return *pWp;
    48 }
    49 
    50 dm_weapon_t *DM_GetWeapon(unsigned int index)
    51 {
    52 	if (index >= g_Weapons.size())
    53 	{
    54 		return NULL;
    55 	}
    56 
    57 	return g_Weapons[index];
    58 }
    59 
    60 char *DM_CopyString(const char *str)
    61 {
    62 	size_t length = strlen(str);
    63 	char *ptr = new char[length+1];
    64 	strcpy(ptr, str);
    65 	return ptr;
    66 }
    67 
    68 bool DM_ParseWeapons(char *error, size_t maxlength)
    69 {
    70 	char path[PLATFORM_MAX_PATH];
    71 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "cfg/cssdm/cssdm.weapons.txt");
    72 
    73 	KeyValues *kv = new KeyValues("Weapons");
    74 	if (!kv->LoadFromFile(basefilesystem, path))
    75 	{
    76 		kv->deleteThis();
    77 		snprintf(error, maxlength, "Could not load weapons file \"%s\"", path);
    78 		return false;
    79 	}
    80 
    81 	const char *game = g_pSM->GetGameFolderName();
    82 	KeyValues *weapons = kv->FindKey(game, false);
    83 	if (!weapons)
    84 	{
    85 		kv->deleteThis();
    86 		snprintf(error, maxlength, "Could not find \"%s\" section in weapons file", game);
    87 		return false;
    88 	}
    89 
    90 	for (weapons = weapons->GetFirstTrueSubKey(); weapons != NULL; weapons = weapons->GetNextTrueSubKey())
    91 	{
    92 		dm_weapon_t *wp = new dm_weapon_t;
    93 
    94 		/* Deal with section name */
    95 		char name[64];
    96 		snprintf(name, sizeof(name), "weapon_%s", weapons->GetName());
    97 		wp->classname = DM_CopyString(name);
    98 
    99 		/* Deal with other strings */
   100 		wp->display = DM_CopyString(weapons->GetString("name", weapons->GetName()));
   101 
   102 		const char *type = weapons->GetString("type", "");
   103 		wp->type = WeaponType_Invalid;
   104 		if (strcmp(type, "primary") == 0)
   105 		{
   106 			wp->type = WeaponType_Primary;
   107 		} else if (strcmp(type, "secondary") == 0) {
   108 			wp->type = WeaponType_Secondary;
   109 		} else if (strcmp(type, "grenade") == 0) {
   110 			wp->type = WeaponType_Grenade;
   111 		} else if (strcmp(type, "c4") == 0) {
   112 			wp->type = WeaponType_C4;
   113 		}
   114 
   115 		if (wp->type != WeaponType_Invalid)
   116 		{
   117 			wp->id = (int)g_Weapons.size();
   118 			g_WeaponLookup.insert(weapons->GetName(), wp);
   119 			g_Weapons.push_back(wp);
   120 		}
   121 	}
   122 
   123 	kv->deleteThis();
   124 
   125 	return true;
   126 }
   127 
   128 void DM_FreeWeapons()
   129 {
   130 	for (size_t i=0; i<g_Weapons.size(); i++)
   131 	{
   132 		dm_weapon_t *wp = g_Weapons[i];
   133 		delete [] wp->classname;
   134 		delete [] wp->display;
   135 		delete wp;
   136 	}
   137 	g_Weapons.clear();
   138 	g_WeaponLookup.clear();
   139 }