cssdm_weapons.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Mon Dec 24 11:05:03 2012 -0500 (2012-12-24)
changeset 121 a5f1a07726c8
parent 92 52cc55d67b2f
child 123 e931a2297b3b
permissions -rw-r--r--
Fix Weapon Parsing
     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 char *DM_StringToLower(const char *str)
    39 {
    40 	size_t length = strlen(str);
    41 	char *ptr = new char[length+1];
    42 
    43 	for(size_t i = 0; i < length; i++)
    44 	{
    45 		ptr[i] = tolower(str[i]);
    46 	}
    47 
    48 	return ptr;
    49 }
    50 
    51 dm_weapon_t *DM_FindWeapon(const char *name)
    52 {
    53 	dm_weapon_t **pWp;
    54 
    55 	if ((pWp = g_WeaponLookup.retrieve(name)) == NULL)
    56 	{
    57 		return NULL;
    58 	}
    59 
    60 	return *pWp;
    61 }
    62 
    63 dm_weapon_t *DM_GetWeapon(unsigned int index)
    64 {
    65 	if (index >= g_Weapons.size())
    66 	{
    67 		return NULL;
    68 	}
    69 
    70 	return g_Weapons[index];
    71 }
    72 
    73 char *DM_CopyString(const char *str)
    74 {
    75 	size_t length = strlen(str);
    76 	char *ptr = new char[length+1];
    77 	strcpy(ptr, str);
    78 	return ptr;
    79 }
    80 
    81 bool DM_ParseWeapons(char *error, size_t maxlength)
    82 {
    83 	char path[PLATFORM_MAX_PATH];
    84 	g_pSM->BuildPath(Path_Game, path, sizeof(path), "cfg/cssdm/cssdm.weapons.txt");
    85 
    86 	KeyValues *kv = new KeyValues("Weapons");
    87 	if (!kv->LoadFromFile(basefilesystem, path))
    88 	{
    89 		kv->deleteThis();
    90 		snprintf(error, maxlength, "Could not load weapons file \"%s\"", path);
    91 		return false;
    92 	}
    93 
    94 	const char *game = g_pSM->GetGameFolderName();
    95 	KeyValues *weapons = kv->FindKey(game, false);
    96 	if (!weapons)
    97 	{
    98 		kv->deleteThis();
    99 		snprintf(error, maxlength, "Could not find \"%s\" section in weapons file", game);
   100 		return false;
   101 	}
   102 
   103 	for (weapons = weapons->GetFirstTrueSubKey(); weapons != NULL; weapons = weapons->GetNextTrueSubKey())
   104 	{
   105 		dm_weapon_t *wp = new dm_weapon_t;
   106 
   107 		/* Deal with section name */
   108 		char name[64];
   109 		char *pName = DM_StringToLower(weapons->GetName());
   110 		snprintf(name, sizeof(name), "weapon_%s", pName);
   111 		wp->classname = DM_CopyString(name);
   112 
   113 		/* Deal with other strings */
   114 		wp->display = DM_CopyString(weapons->GetString("name", pName));
   115 
   116 		const char *type = weapons->GetString("type", "");
   117 		wp->type = WeaponType_Invalid;
   118 		if (strcmp(type, "primary") == 0)
   119 		{
   120 			wp->type = WeaponType_Primary;
   121 		} else if (strcmp(type, "secondary") == 0) {
   122 			wp->type = WeaponType_Secondary;
   123 		} else if (strcmp(type, "grenade") == 0) {
   124 			wp->type = WeaponType_Grenade;
   125 		} else if (strcmp(type, "c4") == 0) {
   126 			wp->type = WeaponType_C4;
   127 		}
   128 
   129 		if (wp->type != WeaponType_Invalid)
   130 		{
   131 			wp->id = (int)g_Weapons.size();
   132 			g_WeaponLookup.insert(pName, wp);
   133 			g_Weapons.push_back(wp);
   134 		}
   135 	}
   136 
   137 	kv->deleteThis();
   138 
   139 	return true;
   140 }
   141 
   142 void DM_FreeWeapons()
   143 {
   144 	for (size_t i=0; i<g_Weapons.size(); i++)
   145 	{
   146 		dm_weapon_t *wp = g_Weapons[i];
   147 		delete [] wp->classname;
   148 		delete [] wp->display;
   149 		delete wp;
   150 	}
   151 	g_Weapons.clear();
   152 	g_WeaponLookup.clear();
   153 }