Changed from cell->pointer casts to reference handles.

Fixed a memory leak.
This commit is contained in:
David Anderson 2004-06-24 07:02:29 +00:00
parent 553f1872a5
commit 37d144abe5
3 changed files with 1705 additions and 1658 deletions

View File

@ -33,51 +33,9 @@
#include <time.h> #include <time.h>
#include "amxmodx.h" #include "amxmodx.h"
static cell AMX_NATIVE_CALL get_xvar_id(AMX *amx, cell *params) int emit_sound(int iEdict, int channel, const char *szSample, REAL vol, REAL att, int flags, int pitch)
{ {
int len; if (iEdict == 0) {
char* sName = get_amxstring(amx,params[1],0,len);
cell ptr;
for ( CPluginMngr::iterator a = g_plugins.begin(); a ; ++a )
{
if ( (*a).isValid() && amx_FindPubVar( (*a).getAMX() , sName , &ptr ) == AMX_ERR_NONE )
return g_xvars.put( (*a).getAMX() , get_amxaddr( (*a).getAMX() , ptr ) );
}
return -1;
}
static cell AMX_NATIVE_CALL get_xvar_num(AMX *amx, cell *params)
{
return g_xvars.getValue(params[1]);
}
static cell AMX_NATIVE_CALL set_xvar_num(AMX *amx, cell *params)
{
if ( g_xvars.setValue( params[1] , params[2] ) ){
amx_RaiseError(amx,AMX_ERR_NATIVE);
return 0;
}
return 1;
}
static cell AMX_NATIVE_CALL xvar_exists(AMX *amx, cell *params)
{
return (get_xvar_id( amx , params ) != -1) ? 1 : 0;
}
static cell AMX_NATIVE_CALL emit_sound(AMX *amx, cell *params) /* 7 param */
{
int len;
char* szSample = get_amxstring(amx,params[3],0,len);
float vol = *(REAL *)((void *)&params[4]);
float att = *(REAL *)((void *)&params[5]);
int channel = params[2];
int pitch = params[7];
int flags = params[6];
if (params[1] == 0) {
for(int i = 1; i <= gpGlobals->maxClients ; ++i){ for(int i = 1; i <= gpGlobals->maxClients ; ++i){
CPlayer* pPlayer = GET_PLAYER_POINTER_I(i); CPlayer* pPlayer = GET_PLAYER_POINTER_I(i);
if (pPlayer->ingame) if (pPlayer->ingame)
@ -86,7 +44,7 @@ static cell AMX_NATIVE_CALL emit_sound(AMX *amx, cell *params) /* 7 param */
} }
else else
{ {
edict_t* pEdict = INDEXENT(params[1]); edict_t* pEdict = INDEXENT(iEdict);
if (!FNullEnt(pEdict)) if (!FNullEnt(pEdict))
EMIT_SOUND_DYN2(pEdict,channel , szSample, vol, att, flags, pitch); EMIT_SOUND_DYN2(pEdict,channel , szSample, vol, att, flags, pitch);
} }
@ -94,24 +52,22 @@ static cell AMX_NATIVE_CALL emit_sound(AMX *amx, cell *params) /* 7 param */
return 1; return 1;
} }
static cell AMX_NATIVE_CALL server_print(AMX *amx, cell *params) /* 1 param */ int server_print(char *message) /* 1 param */
{ {
int len; int len = strlen(message);
char* message = format_amxstring(amx,params,1,len);
if ( len > 254 ) len = 254; if ( len > 254 ) len = 254;
message[len++]='\n'; message[len++]='\n';
message[len]=0; message[len]=0;
SERVER_PRINT( message ); SERVER_PRINT(message);
return len; return len;
} }
static cell AMX_NATIVE_CALL engclient_print(AMX *amx, cell *params) /* 3 param */ int engclient_print(int iPlayer, int iPrintType, const char *message) /* 3 param */
{ {
int len; int len = strlen(message);
char* message = format_amxstring(amx,params,3,len);
message[len++]='\n'; message[len++]='\n';
message[len]=0; message[len]=0;
if (params[1] == 0) { if (iPlayer == 0) {
for(int i = 1; i <= gpGlobals->maxClients ; ++i){ for(int i = 1; i <= gpGlobals->maxClients ; ++i){
CPlayer* pPlayer = GET_PLAYER_POINTER_I(i); CPlayer* pPlayer = GET_PLAYER_POINTER_I(i);
if (pPlayer->ingame) if (pPlayer->ingame)

View File

@ -55,6 +55,7 @@
#include <meta_api.h> #include <meta_api.h>
#include "amxmodx.h" #include "amxmodx.h"
CVector<FILE *> FileList;
static cell AMX_NATIVE_CALL read_dir(AMX *amx, cell *params) static cell AMX_NATIVE_CALL read_dir(AMX *amx, cell *params)
{ {
@ -265,20 +266,40 @@ static cell AMX_NATIVE_CALL file_size(AMX *amx, cell *params) /* 1 param */
} }
//ported from Sanji's file access module by BAILOPAN //ported from Sanji's file access module by BAILOPAN
// Important update - now uses new handles
static cell AMX_NATIVE_CALL amx_fopen(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fopen(AMX *amx, cell *params)
{ {
int len; int len, i, j=-1;
char *file = build_pathname("%s", get_amxstring(amx, params[1], 1, len)); char *file = build_pathname("%s", get_amxstring(amx, params[1], 1, len));
char *flags = get_amxstring(amx, params[2], 0, len); char *flags = get_amxstring(amx, params[2], 0, len);
FILE *fp = fopen(file, flags); FILE *fp = fopen(file, flags);
return (int)file; for (i=0; i<FileList.size(); i++)
{
if (FileList.at(i) == NULL)
{
j = i;
break;
}
}
if (j==-1)
{
FileList.push_back(fp);
j = FileList.size() - 1;
} else {
FileList.at(j) = fp;
}
return j+1;
} }
static cell AMX_NATIVE_CALL amx_fclose(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fclose(AMX *amx, cell *params)
{ {
FILE *file = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (file) { if (id >= FileList.size() || FileList.at(id) == NULL)
return fclose(file); return 0;
FILE *fp = FileList.at(id);
if (fp) {
return fclose(fp);
} else { } else {
return -1; return -1;
} }
@ -286,7 +307,10 @@ static cell AMX_NATIVE_CALL amx_fclose(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fgetc(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fgetc(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
if (fp) { if (fp) {
return fgetc(fp); return fgetc(fp);
} else { } else {
@ -296,20 +320,27 @@ static cell AMX_NATIVE_CALL amx_fgetc(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fread(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fread(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
char *buffer; char *buffer;
if (fp) { if (fp) {
buffer = new char[params[3]]; // SLOW!!! :TODO: Find a better way (auto pointers?) buffer = new char[params[3]]; // SLOW!!! :TODO: Find a better way (auto pointers?)
fread(buffer, sizeof(char), params[3], fp); fread(buffer, sizeof(char), params[3], fp);
return set_amxstring(amx, params[2], buffer, params[3]); set_amxstring(amx, params[2], buffer, params[3]);
delete [] buffer; delete [] buffer;
return 1;
} }
return -1; return -1;
} }
static cell AMX_NATIVE_CALL amx_fwrite(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fwrite(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
char *buf; char *buf;
int len; int len;
if (fp) { if (fp) {
@ -321,7 +352,10 @@ static cell AMX_NATIVE_CALL amx_fwrite(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_feof(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_feof(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
if (fp) { if (fp) {
if (feof(fp)) { if (feof(fp)) {
return 1; return 1;
@ -333,7 +367,11 @@ static cell AMX_NATIVE_CALL amx_feof(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fseek(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fseek(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
if (fp) { if (fp) {
return fseek(fp, (long)params[2], params[3]); return fseek(fp, (long)params[2], params[3]);
} }
@ -342,7 +380,11 @@ static cell AMX_NATIVE_CALL amx_fseek(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fputc(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fputc(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
if (fp) { if (fp) {
return fputc(params[2], fp); return fputc(params[2], fp);
} }
@ -351,7 +393,11 @@ static cell AMX_NATIVE_CALL amx_fputc(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_rewind(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_rewind(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
if (fp) { if (fp) {
rewind(fp); rewind(fp);
return 1; return 1;
@ -361,7 +407,11 @@ static cell AMX_NATIVE_CALL amx_rewind(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fflush(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fflush(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
if (fp) { if (fp) {
return fflush(fp); return fflush(fp);
} }
@ -370,7 +420,11 @@ static cell AMX_NATIVE_CALL amx_fflush(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fscanf(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fscanf(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
char *buf; char *buf;
int len; int len;
buf = format_amxstring(amx, params, 2, len); buf = format_amxstring(amx, params, 2, len);
@ -383,7 +437,11 @@ static cell AMX_NATIVE_CALL amx_fscanf(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_ftell(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_ftell(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
if (fp) { if (fp) {
return ftell(fp); return ftell(fp);
} }
@ -406,7 +464,11 @@ static cell AMX_NATIVE_CALL amx_filesize(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fgetl(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fgetl(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
long t; long t;
if (fp) { if (fp) {
fread(&t, sizeof(long), 1, fp); fread(&t, sizeof(long), 1, fp);
@ -417,7 +479,11 @@ static cell AMX_NATIVE_CALL amx_fgetl(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fgeti(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fgeti(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
int t; int t;
if (fp) { if (fp) {
fread(&t, sizeof(int), 1, fp); fread(&t, sizeof(int), 1, fp);
@ -428,7 +494,11 @@ static cell AMX_NATIVE_CALL amx_fgeti(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fgets(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fgets(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
short t; short t;
if (fp) { if (fp) {
fread(&t, sizeof(short), 1, fp); fread(&t, sizeof(short), 1, fp);
@ -439,7 +509,11 @@ static cell AMX_NATIVE_CALL amx_fgets(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fputs(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fputs(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
short size = params[2]; short size = params[2];
if (fp) { if (fp) {
return fwrite(&size, sizeof(short), 1, fp); return fwrite(&size, sizeof(short), 1, fp);
@ -449,7 +523,11 @@ static cell AMX_NATIVE_CALL amx_fputs(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fputl(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fputl(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
long size = params[2]; long size = params[2];
if (fp) { if (fp) {
return fwrite(&size, sizeof(long), 1, fp); return fwrite(&size, sizeof(long), 1, fp);
@ -459,7 +537,11 @@ static cell AMX_NATIVE_CALL amx_fputl(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fputi(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fputi(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
int size = params[2]; int size = params[2];
if (fp) { if (fp) {
return fwrite(&size, sizeof(int), 1, fp); return fwrite(&size, sizeof(int), 1, fp);
@ -469,7 +551,11 @@ static cell AMX_NATIVE_CALL amx_fputi(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fgetf(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fgetf(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
float t; float t;
if (fp) { if (fp) {
fread(&t, sizeof(float), 1, fp); fread(&t, sizeof(float), 1, fp);
@ -480,7 +566,11 @@ static cell AMX_NATIVE_CALL amx_fgetf(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL amx_fputf(AMX *amx, cell *params) static cell AMX_NATIVE_CALL amx_fputf(AMX *amx, cell *params)
{ {
FILE *fp = (FILE *)params[1]; unsigned int id = params[1] - 1;
if (id >= FileList.size() || FileList.at(id) == NULL)
return 0;
FILE *fp = FileList.at(id);
float size = *(float *)((void *)&params[2]); float size = *(float *)((void *)&params[2]);
if (fp) { if (fp) {
return fwrite(&size, sizeof(float), 1, fp); return fwrite(&size, sizeof(float), 1, fp);

View File

@ -16,6 +16,7 @@ Global
{7A2555A6-CBA6-4BD0-96AE-72DA57B995DF}.Debug.ActiveCfg = Debug|Win32 {7A2555A6-CBA6-4BD0-96AE-72DA57B995DF}.Debug.ActiveCfg = Debug|Win32
{7A2555A6-CBA6-4BD0-96AE-72DA57B995DF}.Debug.Build.0 = Debug|Win32 {7A2555A6-CBA6-4BD0-96AE-72DA57B995DF}.Debug.Build.0 = Debug|Win32
{7A2555A6-CBA6-4BD0-96AE-72DA57B995DF}.JITDebug.ActiveCfg = JITDebug|Win32 {7A2555A6-CBA6-4BD0-96AE-72DA57B995DF}.JITDebug.ActiveCfg = JITDebug|Win32
{7A2555A6-CBA6-4BD0-96AE-72DA57B995DF}.JITDebug.Build.0 = JITDebug|Win32
{7A2555A6-CBA6-4BD0-96AE-72DA57B995DF}.JITRelease.ActiveCfg = JITRelease|Win32 {7A2555A6-CBA6-4BD0-96AE-72DA57B995DF}.JITRelease.ActiveCfg = JITRelease|Win32
{7A2555A6-CBA6-4BD0-96AE-72DA57B995DF}.MemtestDebug.ActiveCfg = MemtestDebug|Win32 {7A2555A6-CBA6-4BD0-96AE-72DA57B995DF}.MemtestDebug.ActiveCfg = MemtestDebug|Win32
{7A2555A6-CBA6-4BD0-96AE-72DA57B995DF}.MemtestDebug.Build.0 = MemtestDebug|Win32 {7A2555A6-CBA6-4BD0-96AE-72DA57B995DF}.MemtestDebug.Build.0 = MemtestDebug|Win32