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