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