2004-10-15 00:35:53 +00:00
|
|
|
//contains generic plugin code for dll/qvm
|
|
|
|
//it's this one or the engine...
|
|
|
|
#include "plugin.h"
|
2019-09-04 07:59:40 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
2016-07-12 00:40:13 +00:00
|
|
|
|
2004-10-15 00:35:53 +00:00
|
|
|
|
2019-09-04 07:59:40 +00:00
|
|
|
plugcorefuncs_t *plugfuncs;
|
|
|
|
plugcmdfuncs_t *cmdfuncs;
|
|
|
|
plugcvarfuncs_t *cvarfuncs;
|
|
|
|
//plugclientfuncs_t *clientfuncs;
|
2007-10-05 10:50:13 +00:00
|
|
|
|
2015-06-29 23:46:31 +00:00
|
|
|
|
2004-10-15 00:35:53 +00:00
|
|
|
|
2005-12-15 18:52:03 +00:00
|
|
|
|
2019-09-04 07:59:40 +00:00
|
|
|
/* An implementation of some 'standard' functions */
|
|
|
|
void Q_strlncpy(char *d, const char *s, int sizeofd, int lenofs)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
sizeofd--;
|
|
|
|
if (sizeofd < 0)
|
|
|
|
return; //this could be an error
|
2004-10-15 00:35:53 +00:00
|
|
|
|
2019-09-04 07:59:40 +00:00
|
|
|
for (i=0; lenofs-- > 0; i++)
|
|
|
|
{
|
|
|
|
if (i == sizeofd)
|
|
|
|
break;
|
|
|
|
*d++ = *s++;
|
|
|
|
}
|
|
|
|
*d='\0';
|
|
|
|
}
|
|
|
|
void Q_strlcpy(char *d, const char *s, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
n--;
|
|
|
|
if (n < 0)
|
|
|
|
return; //this could be an error
|
2005-11-27 01:12:16 +00:00
|
|
|
|
2019-09-04 07:59:40 +00:00
|
|
|
for (i=0; *s; i++)
|
|
|
|
{
|
|
|
|
if (i == n)
|
|
|
|
break;
|
|
|
|
*d++ = *s++;
|
|
|
|
}
|
|
|
|
*d='\0';
|
|
|
|
}
|
|
|
|
void Q_strlcat(char *d, const char *s, int n)
|
|
|
|
{
|
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
int dlen = strlen(d);
|
|
|
|
int slen = strlen(s)+1;
|
|
|
|
if (slen > (n-1)-dlen)
|
|
|
|
slen = (n-1)-dlen;
|
|
|
|
memcpy(d+dlen, s, slen);
|
|
|
|
d[n - 1] = 0;
|
|
|
|
}
|
|
|
|
}
|
2004-10-15 00:35:53 +00:00
|
|
|
|
2019-09-04 07:59:40 +00:00
|
|
|
char *Plug_Info_ValueForKey (const char *s, const char *key, char *out, size_t outsize)
|
|
|
|
{
|
|
|
|
int isvalue = 0;
|
|
|
|
const char *start;
|
|
|
|
char *oout = out;
|
|
|
|
*out = 0;
|
|
|
|
if (*s != '\\')
|
|
|
|
return out; //gah, get lost with your corrupt infostrings.
|
|
|
|
|
|
|
|
start = ++s;
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
while(s[0] == '\\' && s[1] == '\\')
|
|
|
|
s+=2;
|
|
|
|
if (s[0] != '\\' && *s)
|
|
|
|
{
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
2004-10-15 00:35:53 +00:00
|
|
|
|
2019-09-04 07:59:40 +00:00
|
|
|
//okay, it terminates here
|
|
|
|
isvalue = !isvalue;
|
|
|
|
if (isvalue)
|
|
|
|
{
|
|
|
|
if (strlen(key) == (size_t)(s - start) && !strncmp(start, key, s - start))
|
|
|
|
{
|
|
|
|
s++;
|
|
|
|
while (outsize --> 1)
|
|
|
|
{
|
|
|
|
if (s[0] == '\\' && s[1] == '\\')
|
|
|
|
s++;
|
|
|
|
else if (s[0] == '\\' || !s[0])
|
|
|
|
break;
|
|
|
|
*out++ = *s++;
|
|
|
|
}
|
|
|
|
*out++ = 0;
|
|
|
|
return oout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*s)
|
|
|
|
start = ++s;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return oout;
|
|
|
|
}
|
2005-11-30 00:48:29 +00:00
|
|
|
|
2021-08-05 16:49:25 +00:00
|
|
|
|
|
|
|
//returns true on truncation
|
|
|
|
qboolean VARGS Q_vsnprintfz (char *dest, size_t size, const char *fmt, va_list argptr)
|
2019-09-04 07:59:40 +00:00
|
|
|
{
|
2021-08-05 16:49:25 +00:00
|
|
|
size_t ret;
|
|
|
|
#ifdef _WIN32
|
|
|
|
//doesn't null terminate.
|
|
|
|
//returns -1 on truncation
|
|
|
|
ret = _vsnprintf (dest, size, fmt, argptr);
|
|
|
|
dest[size-1] = 0; //shitty paranoia
|
|
|
|
#else
|
|
|
|
//always null terminates.
|
|
|
|
//returns length regardless of truncation.
|
|
|
|
ret = vsnprintf (dest, size, fmt, argptr);
|
|
|
|
#endif
|
|
|
|
#ifdef _DEBUG
|
|
|
|
if (ret>=size)
|
|
|
|
plugfuncs->Error("Q_vsnprintfz: Truncation\n");
|
|
|
|
#endif
|
|
|
|
//if ret is -1 (windows oversize, or general error) then it'll be treated as unsigned so really long. this makes the following check quite simple.
|
2021-11-09 16:53:31 +00:00
|
|
|
return (ret>=size) ? qtrue : qfalse;
|
2019-09-04 07:59:40 +00:00
|
|
|
}
|
2021-08-05 16:49:25 +00:00
|
|
|
//windows/linux have inconsistant snprintf
|
|
|
|
//this is an attempt to get them consistant and safe
|
|
|
|
//size is the total size of the buffer
|
|
|
|
//returns true on overflow (will be truncated).
|
|
|
|
qboolean VARGS Q_snprintfz (char *dest, size_t size, const char *fmt, ...)
|
2019-09-04 07:59:40 +00:00
|
|
|
{
|
|
|
|
va_list argptr;
|
2021-08-05 16:49:25 +00:00
|
|
|
size_t ret;
|
2005-07-20 11:50:00 +00:00
|
|
|
|
2021-08-05 16:49:25 +00:00
|
|
|
va_start (argptr, fmt);
|
|
|
|
#ifdef _WIN32
|
|
|
|
//doesn't null terminate.
|
|
|
|
//returns -1 on truncation
|
|
|
|
ret = _vsnprintf (dest, size, fmt, argptr);
|
|
|
|
dest[size-1] = 0; //shitty paranoia
|
|
|
|
#else
|
|
|
|
//always null terminates.
|
|
|
|
//returns length regardless of truncation.
|
|
|
|
ret = vsnprintf (dest, size, fmt, argptr);
|
|
|
|
#endif
|
2019-09-04 07:59:40 +00:00
|
|
|
va_end (argptr);
|
2021-08-05 16:49:25 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
if (ret>=size)
|
|
|
|
plugfuncs->Error("Q_vsnprintfz: Truncation\n");
|
2005-07-20 11:50:00 +00:00
|
|
|
#endif
|
2021-08-05 16:49:25 +00:00
|
|
|
//if ret is -1 (windows oversize, or general error) then it'll be treated as unsigned so really long. this makes the following check quite simple.
|
2021-11-09 16:53:31 +00:00
|
|
|
return (ret>=size) ? qtrue : qfalse;
|
2021-08-05 16:49:25 +00:00
|
|
|
}
|
2005-07-20 11:50:00 +00:00
|
|
|
|
2013-11-29 14:36:47 +00:00
|
|
|
char *va(const char *format, ...) //Identical in function to the one in Quake, though I can assure you that I wrote it...
|
2004-10-15 00:35:53 +00:00
|
|
|
{ //It's not exactly hard, just easy to use, so gets duplicated lots.
|
|
|
|
va_list argptr;
|
|
|
|
static char string[1024];
|
|
|
|
|
|
|
|
va_start (argptr, format);
|
2021-08-05 16:49:25 +00:00
|
|
|
Q_vsnprintfz (string, sizeof(string), format,argptr);
|
2004-10-15 00:35:53 +00:00
|
|
|
va_end (argptr);
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2019-09-04 23:39:39 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// don't use these functions in MSVC8
|
|
|
|
#if (_MSC_VER < 1400)
|
|
|
|
int QDECL linuxlike_snprintf(char *buffer, int size, const char *format, ...)
|
|
|
|
{
|
|
|
|
#undef _vsnprintf
|
|
|
|
int ret;
|
|
|
|
va_list argptr;
|
|
|
|
|
|
|
|
if (size <= 0)
|
|
|
|
return 0;
|
|
|
|
size--;
|
|
|
|
|
|
|
|
va_start (argptr, format);
|
|
|
|
ret = _vsnprintf (buffer,size, format,argptr);
|
|
|
|
va_end (argptr);
|
|
|
|
|
|
|
|
buffer[size] = '\0';
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
int QDECL linuxlike_vsnprintf(char *buffer, int size, const char *format, va_list argptr)
|
|
|
|
{
|
|
|
|
#undef _vsnprintf
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (size <= 0)
|
|
|
|
return 0;
|
|
|
|
size--;
|
|
|
|
|
|
|
|
ret = _vsnprintf (buffer,size, format,argptr);
|
|
|
|
|
|
|
|
buffer[size] = '\0';
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2019-12-17 17:41:12 +00:00
|
|
|
#elif (_MSC_VER < 1900)
|
2019-09-04 23:39:39 +00:00
|
|
|
int VARGS linuxlike_snprintf_vc8(char *buffer, int size, const char *format, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list argptr;
|
|
|
|
|
|
|
|
va_start (argptr, format);
|
|
|
|
ret = vsnprintf_s (buffer,size, _TRUNCATE, format,argptr);
|
|
|
|
va_end (argptr);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-10-08 05:29:52 +00:00
|
|
|
void Con_Printf(const char *format, ...)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
static char string[1024];
|
|
|
|
|
|
|
|
va_start (argptr, format);
|
2021-08-05 16:49:25 +00:00
|
|
|
Q_vsnprintfz (string, sizeof(string), format,argptr);
|
2004-10-15 00:35:53 +00:00
|
|
|
va_end (argptr);
|
|
|
|
|
2019-09-04 07:59:40 +00:00
|
|
|
plugfuncs->Print(string);
|
2004-10-15 00:35:53 +00:00
|
|
|
}
|
2012-11-27 03:23:19 +00:00
|
|
|
void Con_DPrintf(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
static char string[1024];
|
|
|
|
|
2019-09-04 07:59:40 +00:00
|
|
|
if (!cvarfuncs->GetFloat("developer"))
|
2012-11-27 03:23:19 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
va_start (argptr, format);
|
2021-08-05 16:49:25 +00:00
|
|
|
Q_vsnprintfz (string, sizeof(string), format,argptr);
|
2012-11-27 03:23:19 +00:00
|
|
|
va_end (argptr);
|
|
|
|
|
2019-09-04 07:59:40 +00:00
|
|
|
plugfuncs->Print(string);
|
2012-11-27 03:23:19 +00:00
|
|
|
}
|
2012-10-08 05:29:52 +00:00
|
|
|
void Sys_Errorf(const char *format, ...)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
static char string[1024];
|
|
|
|
|
|
|
|
va_start (argptr, format);
|
2021-08-05 16:49:25 +00:00
|
|
|
Q_vsnprintfz (string, sizeof(string), format,argptr);
|
2004-10-15 00:35:53 +00:00
|
|
|
va_end (argptr);
|
|
|
|
|
2019-09-04 07:59:40 +00:00
|
|
|
plugfuncs->Error(string);
|
2004-10-15 00:35:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 05:31:34 +00:00
|
|
|
|
|
|
|
qboolean ZF_ReallocElements(void **ptr, size_t *elements, size_t newelements, size_t elementsize)
|
|
|
|
{
|
|
|
|
void *n;
|
|
|
|
size_t oldsize;
|
|
|
|
size_t newsize;
|
|
|
|
|
|
|
|
//protect against malicious overflows
|
|
|
|
if (newelements > SIZE_MAX / elementsize)
|
2023-05-29 09:17:44 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
return qfalse;
|
|
|
|
#else
|
2022-03-08 05:31:34 +00:00
|
|
|
return false;
|
2023-05-29 09:17:44 +00:00
|
|
|
#endif
|
2022-03-08 05:31:34 +00:00
|
|
|
|
|
|
|
oldsize = *elements * elementsize;
|
|
|
|
newsize = newelements * elementsize;
|
|
|
|
|
|
|
|
n = plugfuncs->Realloc(*ptr, newsize);
|
|
|
|
if (!n)
|
2023-05-29 09:17:44 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
return qfalse;
|
|
|
|
#else
|
2022-03-08 05:31:34 +00:00
|
|
|
return false;
|
2023-05-29 09:17:44 +00:00
|
|
|
#endif
|
2022-03-08 05:31:34 +00:00
|
|
|
if (newsize > oldsize)
|
|
|
|
memset((char*)n+oldsize, 0, newsize - oldsize);
|
|
|
|
*elements = newelements;
|
|
|
|
*ptr = n;
|
2023-05-29 09:17:44 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
return qtrue;
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
2022-03-08 05:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-01 22:32:06 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C"
|
|
|
|
#endif
|
2019-09-04 07:59:40 +00:00
|
|
|
qboolean NATIVEEXPORT FTEPlug_Init(plugcorefuncs_t *corefuncs)
|
2005-05-17 22:05:45 +00:00
|
|
|
{
|
2019-09-04 07:59:40 +00:00
|
|
|
plugfuncs = corefuncs;
|
2019-10-01 22:32:06 +00:00
|
|
|
cmdfuncs = (plugcmdfuncs_t*)plugfuncs->GetEngineInterface(plugcmdfuncs_name, sizeof(*cmdfuncs));
|
|
|
|
cvarfuncs = (plugcvarfuncs_t*)plugfuncs->GetEngineInterface(plugcvarfuncs_name, sizeof(*cvarfuncs));
|
2021-07-19 22:47:29 +00:00
|
|
|
if (!plugfuncs || !cmdfuncs || !cvarfuncs)
|
2021-07-22 08:40:55 +00:00
|
|
|
return qfalse; //erk
|
2004-10-15 00:35:53 +00:00
|
|
|
|
2019-09-04 07:59:40 +00:00
|
|
|
return Plug_Init();
|
2004-10-15 00:35:53 +00:00
|
|
|
}
|