Update fakemeta module project files and use AMTL.

This commit is contained in:
Arkshine 2014-08-09 20:50:45 +02:00
parent 889ad7c3eb
commit c76509fe41
16 changed files with 270 additions and 6782 deletions

View File

@ -3,8 +3,12 @@ import os.path
binary = AMXX.MetaModule(builder, 'fakemeta') binary = AMXX.MetaModule(builder, 'fakemeta')
binary.compiler.defines += [
'HAVE_STDINT_H',
]
binary.sources = [ binary.sources = [
'sdk/amxxmodule.cpp', '../../public/sdk/amxxmodule.cpp',
'dllfunc.cpp', 'dllfunc.cpp',
'engfunc.cpp', 'engfunc.cpp',
'fakemeta_amxx.cpp', 'fakemeta_amxx.cpp',

View File

@ -7,6 +7,7 @@
HLSDK = ../../../hlsdk HLSDK = ../../../hlsdk
MM_ROOT = ../../../metamod/metamod MM_ROOT = ../../../metamod/metamod
PUBLIC_ROOT = ../../public
##################################### #####################################
### EDIT BELOW FOR OTHER PROJECTS ### ### EDIT BELOW FOR OTHER PROJECTS ###
@ -14,7 +15,7 @@ MM_ROOT = ../../../metamod/metamod
PROJECT = fakemeta PROJECT = fakemeta
OBJECTS = sdk/amxxmodule.cpp dllfunc.cpp engfunc.cpp fakemeta_amxx.cpp pdata.cpp forward.cpp \ OBJECTS = amxxmodule.cpp dllfunc.cpp engfunc.cpp fakemeta_amxx.cpp pdata.cpp forward.cpp \
fm_tr.cpp pev.cpp glb.cpp fm_tr2.cpp misc.cpp fm_tr.cpp pev.cpp glb.cpp fm_tr2.cpp misc.cpp
############################################## ##############################################
@ -30,8 +31,9 @@ CPP_OSX = clang
LINK = LINK =
INCLUDE = -I. -I$(HLSDK) -I$(HLSDK)/common -I$(HLSDK)/dlls -I$(HLSDK)/engine -I$(HLSDK)/game_shared \ INCLUDE = -I. -I$(PUBLIC_ROOT) -I$(PUBLIC_ROOT)/sdk -I$(PUBLIC_ROOT)/amtl \
-I$(HLSDK)/pm_shared -I$(HLSDK)/public -I$(MM_ROOT) -Isdk -I$(HLSDK) -I$(HLSDK)/public -I$(HLSDK)/common -I$(HLSDK)/dlls -I$(HLSDK)/engine -I$(HLSDK)/game_shared -I$(HLSDK)/pm_shared \
-I$(MM_ROOT)
################################################ ################################################
### DO NOT EDIT BELOW HERE FOR MOST PROJECTS ### ### DO NOT EDIT BELOW HERE FOR MOST PROJECTS ###
@ -107,7 +109,7 @@ $(BIN_DIR)/%.o: %.cpp
all: all:
mkdir -p $(BIN_DIR) mkdir -p $(BIN_DIR)
mkdir -p $(BIN_DIR)/sdk ln -sf $(PUBLIC_ROOT)/sdk/amxxmodule.cpp
$(MAKE) -f $(MAKEFILE_NAME) $(PROJECT) $(MAKE) -f $(MAKEFILE_NAME) $(PROJECT)
$(PROJECT): $(OBJ_BIN) $(PROJECT): $(OBJ_BIN)
@ -120,6 +122,5 @@ default: all
clean: clean:
rm -rf $(BIN_DIR)/*.o rm -rf $(BIN_DIR)/*.o
rm -rf $(BIN_DIR)/sdk/*.o
rm -f $(BIN_DIR)/$(BINARY) rm -f $(BIN_DIR)/$(BINARY)

View File

@ -14,16 +14,17 @@
#ifndef _FAKEMETA_INCLUDE_H #ifndef _FAKEMETA_INCLUDE_H
#define _FAKEMETA_INCLUDE_H #define _FAKEMETA_INCLUDE_H
#include "sdk/amxxmodule.h" #include "amxxmodule.h"
#include <entity_state.h> #include <entity_state.h>
#include <usercmd.h> #include <usercmd.h>
#include "CVector.h"
#include "engfunc.h" #include "engfunc.h"
#include "dllfunc.h" #include "dllfunc.h"
#include "pev.h" #include "pev.h"
#include "forward.h" #include "forward.h"
#include "fm_tr.h" #include "fm_tr.h"
#include "glb.h" #include "glb.h"
#include <am-string.h>
#include <am-vector.h>
extern edict_t *g_player_edicts[33]; extern edict_t *g_player_edicts[33];

View File

@ -14,7 +14,7 @@
#ifndef _INCLUDE_TR_H #ifndef _INCLUDE_TR_H
#define _INCLUDE_TR_H #define _INCLUDE_TR_H
#include "CString.h" #include <am-string.h>
extern TraceResult *gfm_tr; extern TraceResult *gfm_tr;
@ -31,9 +31,9 @@ extern usercmd_t *g_uc_hook;
struct KVD_Wrapper struct KVD_Wrapper
{ {
KeyValueData *kvd; KeyValueData *kvd;
String cls; ke::AString cls;
String key; ke::AString key;
String val; ke::AString val;
}; };
extern KVD_Wrapper g_kvd_glb; extern KVD_Wrapper g_kvd_glb;

View File

@ -290,22 +290,22 @@ static cell AMX_NATIVE_CALL set_kvd(AMX *amx, cell *params)
} }
case KV_ClassName: case KV_ClassName:
{ {
kvdw->cls.assign(MF_GetAmxString(amx, params[3], 0, &len)); kvdw->cls = MF_GetAmxString(amx, params[3], 0, &len);
kvd->szClassName = const_cast<char *>(kvdw->cls.c_str()); kvd->szClassName = const_cast<char *>(kvdw->cls.chars());
return 1; return 1;
break; break;
} }
case KV_KeyName: case KV_KeyName:
{ {
kvdw->key.assign(MF_GetAmxString(amx, params[3], 0, &len)); kvdw->key = MF_GetAmxString(amx, params[3], 0, &len);
kvd->szKeyName = const_cast<char *>(kvdw->key.c_str()); kvd->szKeyName = const_cast<char *>(kvdw->key.chars());
return 1; return 1;
break; break;
} }
case KV_Value: case KV_Value:
{ {
kvdw->val.assign(MF_GetAmxString(amx, params[3], 0, &len)); kvdw->val = MF_GetAmxString(amx, params[3], 0, &len);
kvd->szValue = const_cast<char *>(kvdw->val.c_str()); kvd->szValue = const_cast<char *>(kvdw->val.chars());
return 1; return 1;
break; break;
} }

View File

@ -13,8 +13,8 @@
#include "fakemeta_amxx.h" #include "fakemeta_amxx.h"
CVector<int> Engine[ENGFUNC_NUM+10]; ke::Vector<int> Engine[ENGFUNC_NUM+10];
CVector<int> EnginePost[ENGFUNC_NUM+10]; ke::Vector<int> EnginePost[ENGFUNC_NUM + 10];
void *EngineAddrs[ENGFUNC_NUM+10]; void *EngineAddrs[ENGFUNC_NUM+10];
void *EngineAddrsPost[ENGFUNC_NUM+10]; void *EngineAddrsPost[ENGFUNC_NUM+10];
cell mCellResult; cell mCellResult;
@ -838,7 +838,7 @@ static cell AMX_NATIVE_CALL unregister_forward(AMX *amx, cell *params)
void *patchAddr = NULL; void *patchAddr = NULL;
CVector<int> *peng = NULL; ke::Vector<int> *peng = NULL;
if (post) if (post)
{ {
peng = &(EnginePost[func]); peng = &(EnginePost[func]);
@ -847,17 +847,12 @@ static cell AMX_NATIVE_CALL unregister_forward(AMX *amx, cell *params)
peng = &(Engine[func]); peng = &(Engine[func]);
patchAddr = EngineAddrs[func]; patchAddr = EngineAddrs[func];
} }
for (size_t i = 0; i < peng->length(); ++i)
CVector<int>::iterator begin, end=peng->end();
for (begin=peng->begin(); begin!=end; begin++)
{ {
if ((*begin) == func_id) if (peng->at(i) == func_id)
{ {
peng->erase(begin); peng->remove(i);
if (!peng->size() if (!peng->length() && patchAddr != NULL && func != FM_ServerDeactivate)
&& patchAddr != NULL
&& func != FM_ServerDeactivate)
{ {
/* Clear out this forward if we no longer need it */ /* Clear out this forward if we no longer need it */
*(void **)patchAddr = NULL; *(void **)patchAddr = NULL;
@ -1519,9 +1514,9 @@ static cell AMX_NATIVE_CALL register_forward(AMX *amx, cell *params)
if (post) if (post)
{ {
EnginePost[func].push_back(fId); EnginePost[func].append(fId);
} else { } else {
Engine[func].push_back(fId); Engine[func].append(fId);
} }
return fId; return fId;

View File

@ -14,6 +14,8 @@
#ifndef _INCLUDE_FORWARD_H #ifndef _INCLUDE_FORWARD_H
#define _INCLUDE_FORWARD_H #define _INCLUDE_FORWARD_H
#include <am-vector.h>
#define ENGFUNC_NUM FM_LAST_DONT_USE_ME // 131 #define ENGFUNC_NUM FM_LAST_DONT_USE_ME // 131
#define FMV_STRING 1 #define FMV_STRING 1
@ -181,8 +183,8 @@ enum {
FM_LAST_DONT_USE_ME FM_LAST_DONT_USE_ME
}; };
extern CVector<int> Engine[]; extern ke::Vector<int> Engine[];
extern CVector<int> EnginePost[]; extern ke::Vector<int> EnginePost[];
extern void *EngineAddrs[ENGFUNC_NUM+10]; extern void *EngineAddrs[ENGFUNC_NUM+10];
extern void *EngineAddrsPost[ENGFUNC_NUM+10]; extern void *EngineAddrsPost[ENGFUNC_NUM+10];
extern cell mCellResult; extern cell mCellResult;

View File

@ -924,7 +924,7 @@
register unsigned int i = 0; \ register unsigned int i = 0; \
clfm(); \ clfm(); \
int fmres = FMRES_IGNORED; \ int fmres = FMRES_IGNORED; \
for (i=0; i<Engine[pfnCall].size(); i++) \ for (i=0; i<Engine[pfnCall].length(); i++) \
{ \ { \
fmres = MF_ExecuteForward pfnArgs; \ fmres = MF_ExecuteForward pfnArgs; \
if (fmres >= lastFmRes) { \ if (fmres >= lastFmRes) { \
@ -941,7 +941,7 @@
register unsigned int i = 0; \ register unsigned int i = 0; \
clfm(); \ clfm(); \
int fmres = FMRES_IGNORED; \ int fmres = FMRES_IGNORED; \
for (i=0; i<EnginePost[pfnCall].size(); i++) \ for (i=0; i<EnginePost[pfnCall].length(); i++) \
{ \ { \
fmres = MF_ExecuteForward pfnArgs; \ fmres = MF_ExecuteForward pfnArgs; \
if (fmres >= lastFmRes) { \ if (fmres >= lastFmRes) { \

View File

View File

@ -52,8 +52,8 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>..\;..\sdk;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>..\;..\..\..\public;..\..\..\public\sdk; ..\..\..\public\amtl;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;FAKEMETA_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;FAKEMETA_EXPORTS;HAVE_STDINT_H;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MinimalRebuild>true</MinimalRebuild> <MinimalRebuild>true</MinimalRebuild>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks> <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
@ -73,8 +73,8 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile> <ClCompile>
<AdditionalIncludeDirectories>..\;..\sdk;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>..\;..\..\..\public;..\..\..\public\sdk; ..\..\..\public\amtl;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;FAKEMETA_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;FAKEMETA_EXPORTS;HAVE_STDINT_H;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary> <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<RuntimeTypeInfo>false</RuntimeTypeInfo> <RuntimeTypeInfo>false</RuntimeTypeInfo>
<PrecompiledHeader> <PrecompiledHeader>
@ -102,7 +102,7 @@
<ClCompile Include="..\pev.cpp" /> <ClCompile Include="..\pev.cpp" />
<ClCompile Include="..\forward.cpp" /> <ClCompile Include="..\forward.cpp" />
<ClCompile Include="..\glb.cpp" /> <ClCompile Include="..\glb.cpp" />
<ClCompile Include="..\sdk\amxxmodule.cpp" /> <ClCompile Include="..\..\..\public\sdk\amxxmodule.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\fakemeta_amxx.h" /> <ClInclude Include="..\fakemeta_amxx.h" />
@ -114,10 +114,10 @@
<ClInclude Include="..\forward.h" /> <ClInclude Include="..\forward.h" />
<ClInclude Include="..\forwardmacros.h" /> <ClInclude Include="..\forwardmacros.h" />
<ClInclude Include="..\glb.h" /> <ClInclude Include="..\glb.h" />
<ClInclude Include="..\sdk\moduleconfig.h" /> <ClInclude Include="..\moduleconfig.h" />
<ClInclude Include="..\sdk\CString.h" /> <ClInclude Include="..\sdk\CString.h" />
<ClInclude Include="..\sdk\CVector.h" /> <ClInclude Include="..\sdk\CVector.h" />
<ClInclude Include="..\sdk\amxxmodule.h" /> <ClInclude Include="..\..\..\public\sdk\amxxmodule.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="..\..\..\plugins\include\fakemeta.inc" /> <None Include="..\..\..\plugins\include\fakemeta.inc" />

View File

@ -65,7 +65,7 @@
<ClCompile Include="..\glb.cpp"> <ClCompile Include="..\glb.cpp">
<Filter>Globals</Filter> <Filter>Globals</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\sdk\amxxmodule.cpp"> <ClCompile Include="..\..\..\public\sdk\amxxmodule.cpp">
<Filter>Module SDK\SDK Base</Filter> <Filter>Module SDK\SDK Base</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
@ -94,16 +94,16 @@
<ClInclude Include="..\glb.h"> <ClInclude Include="..\glb.h">
<Filter>Globals</Filter> <Filter>Globals</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\sdk\moduleconfig.h">
<Filter>Module SDK</Filter>
</ClInclude>
<ClInclude Include="..\sdk\CString.h"> <ClInclude Include="..\sdk\CString.h">
<Filter>Module SDK\AMXX STL</Filter> <Filter>Module SDK\AMXX STL</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\sdk\CVector.h"> <ClInclude Include="..\sdk\CVector.h">
<Filter>Module SDK\AMXX STL</Filter> <Filter>Module SDK\AMXX STL</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\sdk\amxxmodule.h"> <ClInclude Include="..\moduleconfig.h">
<Filter>Module SDK</Filter>
</ClInclude>
<ClInclude Include="..\..\..\public\sdk\amxxmodule.h">
<Filter>Module SDK\SDK Base</Filter> <Filter>Module SDK\SDK Base</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>

View File

@ -1,391 +0,0 @@
// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license
#ifndef _INCLUDE_CSTRING_H
#define _INCLUDE_CSTRING_H
#include <string.h>
#include <stdio.h>
//by David "BAILOPAN" Anderson
class String
{
public:
String()
{
v = NULL;
a_size = 0;
//assign("");
}
~String()
{
if (v)
delete [] v;
}
String(const char *src)
{
v = NULL;
a_size = 0;
assign(src);
}
const char * _fread(FILE *fp)
{
Grow(512, false);
char *ret = fgets(v, 511, fp);
return ret;
}
String(const String &src)
{
v = NULL;
a_size = 0;
assign(src.c_str());
}
const char *c_str() { return v?v:""; }
const char *c_str() const { return v?v:""; }
void append(const char *t)
{
Grow(size() + strlen(t) + 1);
strcat(v, t);
}
void append(const char c)
{
size_t len = size();
Grow(len + 2);
v[len] = c;
v[len + 1] = '\0';
}
void append(String &d)
{
append(d.c_str());
}
void assign(const String &src)
{
assign(src.c_str());
}
void assign(const char *d)
{
if (!d)
{
clear();
} else {
size_t len = strlen(d);
Grow(len + 1, false);
memcpy(v, d, len);
v[len] = '\0';
}
}
void clear()
{
if (v)
v[0] = '\0';
}
int compare (const char *d) const
{
if (!v)
return strcmp("", d);
else
return strcmp(v, d);
}
//Added this for amxx inclusion
bool empty()
{
if (!v)
return true;
if (v[0] == '\0')
return true;
return false;
}
size_t size()
{
if (v)
return strlen(v);
else
return 0;
}
int find(const char c, int index = 0)
{
int len = static_cast<int>(size());
if (len < 1)
return npos;
if (index >= len || index < 0)
return npos;
int i = 0;
for (i=index; i<len; i++)
{
if (v[i] == c)
{
return i;
}
}
return npos;
}
bool is_space(int c)
{
if (c == '\f' || c == '\n' ||
c == '\t' || c == '\r' ||
c == '\v' || c == ' ')
{
return true;
}
return false;
}
void reparse_newlines()
{
size_t len = size();
int offs = 0;
char c;
if (!len)
return;
for (size_t i=0; i<len; i++)
{
c = v[i];
if (c == '^' && (i != len-1))
{
c = v[++i];
if (c == 'n')
c = '\n';
else if (c == 't')
c = '\t';
offs++;
}
v[i-offs] = c;
}
v[len-offs] = '\0';
}
void trim()
{
if (!v)
return;
unsigned int i = 0;
unsigned int j = 0;
size_t len = strlen(v);
if (len == 1)
{
if (is_space(v[i]))
{
clear();
return;
}
}
unsigned char c0 = v[0];
if (is_space(c0))
{
for (i=0; i<len; i++)
{
if (!is_space(v[i]) || (is_space(v[i]) && ((unsigned char)i==len-1)))
{
erase(0, i);
break;
}
}
}
len = strlen(v);
if (len < 1)
{
return;
}
if (is_space(v[len-1]))
{
for (i=len-1; i<len; i--)
{
if (!is_space(v[i])
|| (is_space(v[i]) && i==0))
{
erase(i+1, j);
break;
}
j++;
}
}
if (len == 1)
{
if (is_space(v[0]))
{
clear();
return;
}
}
}
void erase(unsigned int start, int num = npos)
{
if (!v)
return;
unsigned int i = 0;
size_t len = size();
//check for bounds
if (num == npos || start+num > len-start)
num = len - start;
//do the erasing
bool copyflag = false;
for (i=0; i<len; i++)
{
if (i>=start && i<start+num)
{
if (i+num < len)
{
v[i] = v[i+num];
} else {
v[i] = 0;
}
copyflag = true;
} else if (copyflag) {
if (i+num < len)
{
v[i] = v[i+num];
} else {
v[i] = 0;
}
}
}
len -= num;
v[len] = 0;
}
String substr(unsigned int index, int num = npos)
{
if (!v)
{
String b("");
return b;
}
String ns;
size_t len = size();
if (index >= len || !v)
return ns;
if (num == npos)
{
num = len - index;
} else if (index+num >= len) {
num = len - index;
}
unsigned int i = 0;
unsigned int nslen = num + 2;
ns.Grow(nslen);
for (i=index; i<index+num; i++)
ns.append(v[i]);
return ns;
}
void toLower()
{
if (!v)
return;
unsigned int i = 0;
size_t len = strlen(v);
for (i=0; i<len; i++)
{
if (v[i] >= 65 && v[i] <= 90)
v[i] &= ~(1<<5);
}
}
String & operator = (const String &src)
{
assign(src);
return *this;
}
String & operator = (const char *src)
{
assign(src);
return *this;
}
char operator [] (unsigned int index)
{
if (index > size() || !v)
{
return -1;
} else {
return v[index];
}
}
int at(int a)
{
if (a < 0 || a >= (int)size() || !v)
return -1;
return v[a];
}
bool at(int at, char c)
{
if (at < 0 || at >= (int)size() || !v)
return false;
v[at] = c;
return true;
}
private:
void Grow(unsigned int d, bool copy=true)
{
if (d <= a_size)
return;
char *n = new char[d + 1];
if (copy && v)
strcpy(n, v);
if (v)
delete [] v;
else
strcpy(n, "");
v = n;
a_size = d + 1;
}
char *v;
unsigned int a_size;
public:
static const int npos = -1;
};
#endif //_INCLUDE_CSTRING_H

View File

@ -1,480 +0,0 @@
// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license
#ifndef __CVECTOR_H__
#define __CVECTOR_H__
#include <assert.h>
// Vector
template <class T> class CVector
{
bool Grow(size_t amount)
{
// automatic grow
size_t newSize = m_Size * 2;
if (newSize == 0)
{
newSize = 8;
}
while (m_CurrentUsedSize + amount > newSize)
{
newSize *= 2;
}
T *newData = new T[newSize];
if (!newData)
return false;
if (m_Data)
{
for (size_t i=0; i<m_CurrentUsedSize; i++)
newData[i] = m_Data[i];
delete [] m_Data;
}
m_Data = newData;
m_Size = newSize;
return true;
}
bool GrowIfNeeded(size_t amount)
{
if (m_CurrentUsedSize + amount >= m_Size)
{
return Grow(amount);
}
else
{
return true;
}
}
bool ChangeSize(size_t size)
{
// change size
if (size == m_Size)
return true;
if (!size)
{
if (m_Data)
{
delete [] m_Data;
m_Data = NULL;
m_Size = 0;
}
return true;
}
T *newData = new T[size];
if (!newData)
return false;
if (m_Data)
{
size_t end = (m_CurrentUsedSize < size) ? (m_CurrentUsedSize) : size;
for (size_t i=0; i<end; i++)
newData[i] = m_Data[i];
delete [] m_Data;
}
m_Data = newData;
m_Size = size;
if (m_CurrentUsedSize > m_Size)
m_CurrentUsedSize = m_Size;
return true;
}
void FreeMemIfPossible()
{
if (!m_Data)
return;
if (!m_CurrentUsedSize)
{
ChangeSize(0);
return;
}
size_t newSize = m_Size;
while (m_CurrentUsedSize <= newSize / 2)
newSize /= 2;
if (newSize != m_Size)
ChangeSize(newSize);
}
protected:
T *m_Data;
size_t m_Size;
size_t m_CurrentUsedSize;
public:
class iterator
{
protected:
T *m_Ptr;
public:
// constructors / destructors
iterator()
{
m_Ptr = NULL;
}
iterator(T * ptr)
{
m_Ptr = ptr;
}
// member functions
T * base()
{
return m_Ptr;
}
const T * base() const
{
return m_Ptr;
}
// operators
T & operator*()
{
return *m_Ptr;
}
T * operator->()
{
return m_Ptr;
}
iterator & operator++() // preincrement
{
++m_Ptr;
return (*this);
}
iterator operator++(int) // postincrement
{
iterator tmp = *this;
++m_Ptr;
return tmp;
}
iterator & operator--() // predecrement
{
--m_Ptr;
return (*this);
}
iterator operator--(int) // postdecrememnt
{
iterator tmp = *this;
--m_Ptr;
return tmp;
}
bool operator==(T * right) const
{
return (m_Ptr == right);
}
bool operator==(const iterator & right) const
{
return (m_Ptr == right.m_Ptr);
}
bool operator!=(T * right) const
{
return (m_Ptr != right);
}
bool operator!=(const iterator & right) const
{
return (m_Ptr != right.m_Ptr);
}
iterator & operator+=(size_t offset)
{
m_Ptr += offset;
return (*this);
}
iterator & operator-=(size_t offset)
{
m_Ptr -= offset;
return (*this);
}
iterator operator+(size_t offset) const
{
iterator tmp(*this);
tmp.m_Ptr += offset;
return tmp;
}
iterator operator-(size_t offset) const
{
iterator tmp(*this);
tmp.m_Ptr -= offset;
return tmp;
}
T & operator[](size_t offset)
{
return (*(*this + offset));
}
const T & operator[](size_t offset) const
{
return (*(*this + offset));
}
bool operator<(const iterator & right) const
{
return m_Ptr < right.m_Ptr;
}
bool operator>(const iterator & right) const
{
return m_Ptr > right.m_Ptr;
}
bool operator<=(const iterator & right) const
{
return m_Ptr <= right.m_Ptr;
}
bool operator>=(const iterator & right) const
{
return m_Ptr >= right.m_Ptr;
}
size_t operator-(const iterator & right) const
{
return m_Ptr - right.m_Ptr;
}
};
// constructors / destructors
CVector<T>()
{
m_Size = 0;
m_CurrentUsedSize = 0;
m_Data = NULL;
}
CVector<T>(const CVector<T> & other)
{
// copy data
m_Data = new T [other.m_CurrentUsedSize];
m_Size = other.m_CurrentUsedSize;
m_CurrentUsedSize = other.m_CurrentUsedSize;
for (size_t i=0; i<other.m_CurrentUsedSize; i++)
m_Data[i] = other.m_Data[i];
}
~CVector<T>()
{
clear();
}
// interface
size_t size() const
{
return m_CurrentUsedSize;
}
size_t capacity() const
{
return m_Size;
}
iterator begin() const
{
return iterator(m_Data);
}
iterator end() const
{
return iterator(m_Data + m_CurrentUsedSize);
}
iterator iterAt(size_t pos)
{
if (pos > m_CurrentUsedSize)
assert(0);
return iterator(m_Data + pos);
}
bool reserve(size_t newSize)
{
if (newSize > m_Size)
return ChangeSize(newSize);
return true;
}
bool push_back(const T & elem)
{
if (!GrowIfNeeded(1))
{
return false;
}
m_Data[m_CurrentUsedSize++] = elem;
return true;
}
void pop_back()
{
--m_CurrentUsedSize;
if (m_CurrentUsedSize < 0)
m_CurrentUsedSize = 0;
FreeMemIfPossible();
}
bool resize(size_t newSize)
{
if (!ChangeSize(newSize))
return false;
m_CurrentUsedSize = newSize;
return true;
}
bool empty() const
{
return (m_CurrentUsedSize == 0);
}
T & at(size_t pos)
{
if (pos > m_CurrentUsedSize)
{
assert(0);
}
return m_Data[pos];
}
const T & at(size_t pos) const
{
if (pos > m_CurrentUsedSize)
{
assert(0);
}
return m_Data[pos];
}
T & operator[](size_t pos)
{
return at(pos);
}
const T & operator[](size_t pos) const
{
return at(pos);
}
T & front()
{
if (m_CurrentUsedSize < 1)
{
assert(0);
}
return m_Data[0];
}
const T & front() const
{
if (m_CurrentUsedSize < 1)
{
assert(0);
}
return m_Data[0];
}
T & back()
{
if (m_CurrentUsedSize < 1)
{
assert(0);
}
return m_Data[m_CurrentUsedSize - 1];
}
const T & back() const
{
if (m_CurrentUsedSize < 1)
{
assert(0);
}
return m_Data[m_CurrentUsedSize - 1];
}
iterator insert(iterator where, const T & value)
{
// validate iter
if (where < m_Data || where > (m_Data + m_CurrentUsedSize))
return iterator(0);
size_t ofs = where - begin();
if (!GrowIfNeeded(1))
{
return false;
}
++m_CurrentUsedSize;
where = begin() + ofs;
// Move subsequent entries
for (T *ptr = m_Data + m_CurrentUsedSize - 2; ptr >= where.base(); --ptr)
*(ptr + 1) = *ptr;
*where.base() = value;
return where;
}
iterator erase(iterator where)
{
// validate iter
if (where < m_Data || where >= (m_Data + m_CurrentUsedSize))
return iterator(0);
size_t ofs = where - begin();
if (m_CurrentUsedSize > 1)
{
// move
T *theend = m_Data + m_CurrentUsedSize;
for (T *ptr = where.base() + 1; ptr < theend; ++ptr)
*(ptr - 1) = *ptr;
}
--m_CurrentUsedSize;
FreeMemIfPossible();
return begin() + ofs;
}
void clear()
{
m_Size = 0;
m_CurrentUsedSize = 0;
if (m_Data)
{
delete [] m_Data;
m_Data = NULL;
}
}
};
#endif // __CVECTOR_H__

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,219 +1,219 @@
/* ======== SourceMM ======== /* ======== SourceMM ========
* Copyright (C) 2004-2005 Metamod:Source Development Team * Copyright (C) 2004-2005 Metamod:Source Development Team
* No warranties of any kind * No warranties of any kind
* *
* License: zlib/libpng * License: zlib/libpng
* *
* Author(s): Pavol "PM OnoTo" Marko * Author(s): Pavol "PM OnoTo" Marko
* ============================ * ============================
*/ */
#ifndef __SH_STACK_H__ #ifndef __SH_STACK_H__
#define __SH_STACK_H__ #define __SH_STACK_H__
#define SH_STACK_DEFAULT_SIZE 4 #define SH_STACK_DEFAULT_SIZE 4
//namespace SourceHook //namespace SourceHook
//{/ //{/
// Vector // Vector
template <class T> class CStack template <class T> class CStack
{ {
T *m_Elements; T *m_Elements;
size_t m_AllocatedSize; size_t m_AllocatedSize;
size_t m_UsedSize; size_t m_UsedSize;
public: public:
friend class iterator; friend class iterator;
class iterator class iterator
{ {
CStack<T> *m_pParent; CStack<T> *m_pParent;
size_t m_Index; size_t m_Index;
public: public:
iterator(CStack<T> *pParent, size_t id) : m_pParent(pParent), m_Index(id) iterator(CStack<T> *pParent, size_t id) : m_pParent(pParent), m_Index(id)
{ {
} }
iterator(CStack<T> *pParent) : m_pParent(pParent), m_Index(0) iterator(CStack<T> *pParent) : m_pParent(pParent), m_Index(0)
{ {
} }
iterator() : m_pParent(NULL), m_Index(0) iterator() : m_pParent(NULL), m_Index(0)
{ {
} }
T &operator *() T &operator *()
{ {
return m_pParent->m_Elements[m_Index]; return m_pParent->m_Elements[m_Index];
} }
const T &operator *() const const T &operator *() const
{ {
return m_pParent->m_Elements[m_Index]; return m_pParent->m_Elements[m_Index];
} }
T * operator->() T * operator->()
{ {
return m_pParent->m_Elements + m_Index; return m_pParent->m_Elements + m_Index;
} }
const T * operator->() const const T * operator->() const
{ {
return m_pParent->m_Elements + m_Index; return m_pParent->m_Elements + m_Index;
} }
iterator & operator++() // preincrement iterator & operator++() // preincrement
{ {
++m_Index; ++m_Index;
return (*this); return (*this);
} }
iterator operator++(int) // postincrement iterator operator++(int) // postincrement
{ {
iterator tmp = *this; iterator tmp = *this;
++m_Index; ++m_Index;
return tmp; return tmp;
} }
iterator & operator--() // predecrement iterator & operator--() // predecrement
{ {
--m_Index; --m_Index;
return (*this); return (*this);
} }
iterator operator--(int) // postdecrememnt iterator operator--(int) // postdecrememnt
{ {
iterator tmp = *this; iterator tmp = *this;
--m_Index; --m_Index;
return tmp; return tmp;
} }
bool operator==(const iterator & right) const bool operator==(const iterator & right) const
{ {
return (m_pParent == right.m_pParent && m_Index == right.m_Index); return (m_pParent == right.m_pParent && m_Index == right.m_Index);
} }
bool operator!=(const iterator & right) const bool operator!=(const iterator & right) const
{ {
return !(*this == right); return !(*this == right);
} }
}; };
CStack() : m_Elements(new T[SH_STACK_DEFAULT_SIZE]), CStack() : m_Elements(new T[SH_STACK_DEFAULT_SIZE]),
m_AllocatedSize(SH_STACK_DEFAULT_SIZE), m_AllocatedSize(SH_STACK_DEFAULT_SIZE),
m_UsedSize(0) m_UsedSize(0)
{ {
} }
CStack(size_t size) : m_Elements(new T[size]), CStack(size_t size) : m_Elements(new T[size]),
m_AllocatedSize(size), m_AllocatedSize(size),
m_UsedSize(0) m_UsedSize(0)
{ {
} }
CStack(const CStack &other) : m_Elements(NULL), CStack(const CStack &other) : m_Elements(NULL),
m_AllocatedSize(0), m_AllocatedSize(0),
m_UsedSize(0) m_UsedSize(0)
{ {
reserve(other.m_AllocatedSize); reserve(other.m_AllocatedSize);
m_UsedSize = other.m_UsedSize; m_UsedSize = other.m_UsedSize;
for (size_t i = 0; i < m_UsedSize; ++i) for (size_t i = 0; i < m_UsedSize; ++i)
m_Elements[i] = other.m_Elements[i]; m_Elements[i] = other.m_Elements[i];
} }
~CStack() ~CStack()
{ {
if (m_Elements) if (m_Elements)
delete [] m_Elements; delete [] m_Elements;
} }
void operator=(const CStack &other) void operator=(const CStack &other)
{ {
if (m_AllocatedSize < other.m_AllocatedSize) if (m_AllocatedSize < other.m_AllocatedSize)
{ {
if (m_Elements) if (m_Elements)
delete [] m_Elements; delete [] m_Elements;
m_Elements = new T[other.m_AllocatedSize]; m_Elements = new T[other.m_AllocatedSize];
m_AllocatedSize = other.m_AllocatedSize; m_AllocatedSize = other.m_AllocatedSize;
} }
m_UsedSize = other.m_UsedSize; m_UsedSize = other.m_UsedSize;
for (size_t i = 0; i < m_UsedSize; ++i) for (size_t i = 0; i < m_UsedSize; ++i)
m_Elements[i] = other.m_Elements[i]; m_Elements[i] = other.m_Elements[i];
} }
bool push(const T &val) bool push(const T &val)
{ {
if (m_UsedSize + 1 == m_AllocatedSize) if (m_UsedSize + 1 == m_AllocatedSize)
{ {
// zOHNOES! REALLOCATE! // zOHNOES! REALLOCATE!
m_AllocatedSize *= 2; m_AllocatedSize *= 2;
T *newElements = new T[m_AllocatedSize]; T *newElements = new T[m_AllocatedSize];
if (!newElements) if (!newElements)
{ {
m_AllocatedSize /= 2; m_AllocatedSize /= 2;
return false; return false;
} }
if (m_Elements) if (m_Elements)
{ {
for (size_t i = 0; i < m_UsedSize; ++i) for (size_t i = 0; i < m_UsedSize; ++i)
newElements[i] = m_Elements[i]; newElements[i] = m_Elements[i];
delete [] m_Elements; delete [] m_Elements;
} }
m_Elements = newElements; m_Elements = newElements;
} }
m_Elements[m_UsedSize++] = val; m_Elements[m_UsedSize++] = val;
return true; return true;
} }
void pop() void pop()
{ {
--m_UsedSize; --m_UsedSize;
} }
T &front() T &front()
{ {
return m_Elements[m_UsedSize - 1]; return m_Elements[m_UsedSize - 1];
} }
const T &front() const const T &front() const
{ {
return m_Elements[m_UsedSize - 1]; return m_Elements[m_UsedSize - 1];
} }
iterator begin() iterator begin()
{ {
return iterator(this, 0); return iterator(this, 0);
} }
iterator end() iterator end()
{ {
return iterator(this, m_UsedSize); return iterator(this, m_UsedSize);
} }
size_t size() size_t size()
{ {
return m_UsedSize; return m_UsedSize;
} }
size_t capacity() size_t capacity()
{ {
return m_AllocatedSize; return m_AllocatedSize;
} }
bool empty() bool empty()
{ {
return m_UsedSize == 0 ? true : false; return m_UsedSize == 0 ? true : false;
} }
bool reserve(size_t size) bool reserve(size_t size)
{ {
if (size > m_AllocatedSize) if (size > m_AllocatedSize)
{ {
T *newElements = new T[size]; T *newElements = new T[size];
if (!newElements) if (!newElements)
return false; return false;
if (m_Elements) if (m_Elements)
{ {
for (size_t i = 0; i < m_UsedSize; ++i) for (size_t i = 0; i < m_UsedSize; ++i)
newElements[i] = m_Elements[i]; newElements[i] = m_Elements[i];
delete [] m_Elements; delete [] m_Elements;
} }
m_Elements = newElements; m_Elements = newElements;
m_AllocatedSize = size; m_AllocatedSize = size;
} }
return true; return true;
} }
}; };
//}; //namespace SourceHook //}; //namespace SourceHook
#endif #endif