AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [ANY] New way to store data into the char array (https://forums.alliedmods.net/showthread.php?t=315349)

gubka 04-03-2019 08:56

[ANY] New way to store data into the char array
 
That code use the pawn emit which originally not exist in sm compiler
More info about #emit here
Regards to komashchenko for restoring some removed features to a compiler and for the code below,
which show a simple example of what scripters can do with #emit, myself found it very useful for menu info , when you need to store some data like "4 5.0 -1" and now I don't use FormatEx and ExploadString and just simply send data via parameters and after extracting them back. Remember that any formatting on that stream char will break it, so only use the two function below, I hope the original sm once will return some of that kind of features because they might be useful in some situation, for example when you need to get the number of arguments which use sent to (any ...)

Use can send data like int, float by the AnyToStream(sBuf, 5, 5.0, 3, 5, 7) and get back using StreamToAny(sBuf, iData)
where iData is the any[]
Minimum size of char buffer array should be not less then args * 4 + 1
PHP Code:

/**
 * @brief Converts any parameter into a stream.
 *
 * @param sStream           Input string buffer.
 */
void AnyToStream(char[] sStreamany ...)
{
    
// Initialize some variables
    
int iArgsint nArg;
    
#emit load.s.pri 8
    #emit stor.s.pri iArgs
    
sStream[iArgs 4] = 0;
    
iArgs 12 iArgs 4;
    
    
// i = byte index
    
for(int i 16iArgs+= 4nArg++)
    {
        
#emit addr.alt 0
        #emit load.s.pri i
        #emit add
        #emit load.i
        #emit load.i
        #emit push.pri
        #emit load.s.pri nArg
        #emit smul.c 5
        #emit load.s.alt sStream
        #emit add
        #emit move.alt
        #emit pop.pri
        #emit stor.i
        #emit move.pri
        #emit add.c 4
        #emit move.alt
        #emit const.pri 16
        #emit strb.i 0x1
        
        
for(int x 0nArg 5!= 4x++)
        {
            if(
sStream[x] == 0)
            {
                
sStream[4] |= (1<<x);
                
sStream[x] = 0xFF;
            }
        }
    }
}

/**
 * @brief Converts the stream in an any array.
 *
 * @param sStream           Input stream buffer.
 * @param iBuffer           Output data array.
 */
int StreamToAny(char[] sStreamany[] iBuffer)
{
    
// Initialize some variables
    
int iCount strlen(sStream) / 5nTempiMagic;
    
    
// i = byte index
    
for(int i 0iCounti++)
    {
        
#emit load.s.alt sStream
        #emit load.s.pri i
        #emit smul.c 5
        #emit add
        #emit lodb.i 4
        #emit addr.alt nTemp
        #emit stor.i
        
        
if((iMagic sStream[4]) != 16)
        {
            if(
iMagic & (<< 0)) nTemp &= 0xFFFFFF00;
            if(
iMagic & (<< 1)) nTemp &= 0xFFFF00FF;
            if(
iMagic & (<< 2)) nTemp &= 0xFF00FFFF;
            if(
iMagic & (<< 3)) nTemp &= 0x00FFFFFF;
        }
        
        
iBuffer[i] = nTemp;
    }
    
    
// Return amount 
    
return iCount;


Some other lower performed examples:
Spoiler

GitHub

TheDS1337 04-03-2019 13:13

Re: [ANY] New way to store data into the char array
 
Seems interesting, but if I understood this right... then you can just use DataPacks for passing information?

gubka 04-03-2019 14:09

Re: [ANY] New way to store data into the char array
 
Quote:

Originally Posted by TheDS1337 (Post 2646143)
Seems interesting, but if I understood this right... then you can just use DataPacks for passing information?

Yes but DataPacks work thought extension, but you also can convert Handle to char stream if you need. I also will attach the sourcecode of comp later a bit later tommorow

It will work with datapack is well
PHP Code:

char[] AnyToString(any Data)
{
    
char Buf[6];
    
#emit break
    #emit load.s.pri Data
    #emit stor.s.pri Buf
    
    
for(int i 0!= 4i++)
    {
        if(
Buf[i] == 0)
        {
            
Buf[4] |= (1<<i);
            
Buf[i] = 0xFF;
        }
    }
    
    return 
Buf;



asherkin 04-03-2019 16:50

Re: [ANY] New way to store data into the char array
 
#emit was removed from spcomp because it made it impossible to safely make many internal improvements, and it had never been used in any released plugin. Using a modified version of spcomp means your plugins will very likely not be compatible with future versions of SourceMod.

BAILOPAN 04-03-2019 16:54

Re: [ANY] New way to store data into the char array
 
Yeah... can't believe I even need to say it, but don't do this. All pain and no gain.

gubka 04-03-2019 17:07

Re: [ANY] New way to store data into the char array
 
Quote:

Originally Posted by BAILOPAN (Post 2646165)
Yeah... can't believe I even need to say it, but don't do this. All pain and no gain.

I fully agree with it, but that was just a small experiment ;)

gubka 04-03-2019 17:09

Re: [ANY] New way to store data into the char array
 
Quote:

Originally Posted by asherkin (Post 2646163)
#emit was removed from spcomp because it made it impossible to safely make many internal improvements, and it had never been used in any released plugin. Using a modified version of spcomp means your plugins will very likely not be compatible with future versions of SourceMod.

Here I will disagree with you, it will work, like other old compilers works with new ver of sm

Silvers 04-03-2019 17:45

Re: [ANY] New way to store data into the char array
 
Quote:

Originally Posted by gubka (Post 2646169)
Here I will disagree with you, it will work, like other old compilers works with new ver of sm

:nono::roll:

gubka 04-03-2019 17:54

Re: [ANY] New way to store data into the char array
 
Quote:

Originally Posted by Silvers (Post 2646172)
:nono::roll:

Are they rewrite pawn vm every update?


All times are GMT -4. The time now is 09:29.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.