Skip to content

Instantly share code, notes, and snippets.

@IS4Code
Created June 13, 2016 22:20
Show Gist options
  • Save IS4Code/3fe94d8f4e9b22e3c434a14dbef13d8b to your computer and use it in GitHub Desktop.
Save IS4Code/3fe94d8f4e9b22e3c434a14dbef13d8b to your computer and use it in GitHub Desktop.
/*
The MIT License (MIT)
Copyright (c) 2016 IllidanS4
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//Documentation: http://forum.sa-mp.com/showthread.php?t=609548
//Version 1.0
/*
native IsPVarSet(playerid, const varname[]);
native SetPVarBool(playerid, const varname[], bool:bool_value)
native GetPVarBool(playerid, const varname[]);
native DecodePVarArrayName(varname[], &trim=0);
native ReformatPVarArrayName(varname[], size = sizeof(varname));
*/
#define PVAR:%0=%1; stock const PVAR_%0[]=%1;
#define PVARR:%0=%1; PVAR:ARR_%0=%1"[ ]";
#define PVAR_ARRAY<%0>[%1] (__ModifyPVarr(%1,PVAR_ARR_%0,sizeof(PVAR_ARR_%0)),PVAR_ARR_%0)
static PVAR_Loader()
{
new null[] = "";
SetPVarInt(0, null, 0);
GetPVarInt(0, null);
SetPVarFloat(0, null, 0.0);
GetPVarFloat(0, null);
SetPVarString(0, null, null);
GetPVarString(0, null, null, 0);
DeletePVar(0, null);
GetPVarType(0, null);
PVAR_Loader();
}
stock __ModifyPVarr(value, const arr[], size = sizeof(arr))
{
#pragma unused arr
setarg(1, size-3, EncodePVarrChar(value%229));
value /= 229;
setarg(1, size-4, EncodePVarrChar(value%229));
value /= 229;
setarg(1, size-5, EncodePVarrChar(value%229));
value /= 229;
setarg(1, size-6, EncodePVarrChar(value%229));
}
static stock EncodePVarrChar(c)
{
c += 1;
if(c >= 'a')
{
return c + ('z'-'a'+1);
}
return c;
}
static stock DecodePVarrChar(c)
{
if(c < 0)
{
return DecodePVarrChar(256+c);
}else if(c > 'z')
{
return c-('z'-'a'+2);
}
return c - 1;
}
stock ReformatPVarArrayName(varname[], size = sizeof(varname))
{
new trim, idx = DecodePVarArrayName(varname, trim);
if(idx != -1)
{
varname[trim] = '\0';
format(varname, size, "%s[%d]", varname, idx);
}
}
stock DecodePVarArrayName(varname[], &trim=0)
{
new len = strlen(varname);
if(len >= 6 && varname[len-1] == ']' && varname[len-6] == '[')
{
trim = len-6;
return DecodePVarArrayChars(varname[len-5]);
}
return -1;
}
static stock DecodePVarArrayChars(chars[])
{
return DecodePVarrChar(chars[3]) + DecodePVarrChar(chars[2])*229 + DecodePVarrChar(chars[1])*(229*229) + DecodePVarrChar(chars[0])*(229*229*229);
}
stock CONST_SetPVarInt(playerid, const varname[], int_value)
{
new ret;
#emit PUSH.S int_value
#emit PUSH.S varname
#emit PUSH.S playerid
#emit PUSH.C 12
#emit SYSREQ.C SetPVarInt
#emit STACK 16
#emit STOR.S.pri ret
return ret;
}
#define SetPVarInt CONST_SetPVarInt
stock CONST_GetPVarInt(playerid, const varname[])
{
new ret;
#emit PUSH.S varname
#emit PUSH.S playerid
#emit PUSH.C 8
#emit SYSREQ.C GetPVarInt
#emit STACK 12
#emit STOR.S.pri ret
return ret;
}
#define GetPVarInt CONST_GetPVarInt
stock CONST_SetPVarFloat(playerid, const varname[], Float:float_value)
{
new ret;
#emit PUSH.S float_value
#emit PUSH.S varname
#emit PUSH.S playerid
#emit PUSH.C 12
#emit SYSREQ.C SetPVarFloat
#emit STACK 16
#emit STOR.S.pri ret
return ret;
}
#define SetPVarFloat CONST_SetPVarFloat
stock Float:CONST_GetPVarFloat(playerid, const varname[])
{
new Float:ret;
#emit PUSH.S varname
#emit PUSH.S playerid
#emit PUSH.C 8
#emit SYSREQ.C GetPVarFloat
#emit STACK 12
#emit STOR.S.pri ret
return ret;
}
#define GetPVarFloat CONST_GetPVarFloat
stock CONST_SetPVarString(playerid, const varname[], const string_value[])
{
new ret;
#emit PUSH.S string_value
#emit PUSH.S varname
#emit PUSH.S playerid
#emit PUSH.C 12
#emit SYSREQ.C SetPVarString
#emit STACK 16
#emit STOR.S.pri ret
return ret;
}
#define SetPVarString CONST_SetPVarString
stock CONST_GetPVarString(playerid, const varname[], string_return[], len=sizeof string_return)
{
new ret;
#emit PUSH.S len
#emit PUSH.S string_return
#emit PUSH.S varname
#emit PUSH.S playerid
#emit PUSH.C 16
#emit SYSREQ.C GetPVarString
#emit STACK 20
#emit STOR.S.pri ret
return ret;
}
#define GetPVarString CONST_GetPVarString
stock CONST_DeletePVar(playerid, const varname[])
{
new ret;
#emit PUSH.S varname
#emit PUSH.S playerid
#emit PUSH.C 8
#emit SYSREQ.C DeletePVar
#emit STACK 12
#emit STOR.S.pri ret
return ret;
}
#define DeletePVar CONST_DeletePVar
stock CONST_GetPVarType(playerid, const varname[])
{
new ret;
#emit PUSH.S varname
#emit PUSH.S playerid
#emit PUSH.C 8
#emit SYSREQ.C GetPVarType
#emit STACK 12
#emit STOR.S.pri ret
return ret;
}
#define GetPVarType CONST_GetPVarType
#define IsPVarSet(%0,%1) (GetPVarType(%0,%1)!=0)
stock SetPVarBool(playerid, const varname[], bool:bool_value)
if(bool_value) SetPVarInt(playerid,varname,true);
else DeletePVar(playerid,varname);
#define GetPVarBool(%0,%1) IsPVarSet(%0,%1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment