Raised This Month: $12 Target: $400
 3% 

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


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
gubka
Veteran Member
Join Date: Jan 2012
Location: Russia
Old 04-03-2019 , 08:56   [ANY] New way to store data into the char array
Reply With Quote #1

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
__________________

Last edited by gubka; 04-03-2019 at 16:54.
gubka is offline
Send a message via ICQ to gubka
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 04-03-2019 , 13:13   Re: [ANY] New way to store data into the char array
Reply With Quote #2

Seems interesting, but if I understood this right... then you can just use DataPacks for passing information?
TheDS1337 is offline
gubka
Veteran Member
Join Date: Jan 2012
Location: Russia
Old 04-03-2019 , 14:09   Re: [ANY] New way to store data into the char array
Reply With Quote #3

Quote:
Originally Posted by TheDS1337 View Post
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;

__________________

Last edited by gubka; 04-03-2019 at 16:43.
gubka is offline
Send a message via ICQ to gubka
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 04-03-2019 , 16:50   Re: [ANY] New way to store data into the char array
Reply With Quote #4

#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.
__________________
asherkin is offline
BAILOPAN
Join Date: Jan 2004
Old 04-03-2019 , 16:54   Re: [ANY] New way to store data into the char array
Reply With Quote #5

Yeah... can't believe I even need to say it, but don't do this. All pain and no gain.
__________________
egg
BAILOPAN is offline
gubka
Veteran Member
Join Date: Jan 2012
Location: Russia
Old 04-03-2019 , 17:07   Re: [ANY] New way to store data into the char array
Reply With Quote #6

Quote:
Originally Posted by BAILOPAN View Post
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 is offline
Send a message via ICQ to gubka
gubka
Veteran Member
Join Date: Jan 2012
Location: Russia
Old 04-03-2019 , 17:09   Re: [ANY] New way to store data into the char array
Reply With Quote #7

Quote:
Originally Posted by asherkin View Post
#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
__________________
gubka is offline
Send a message via ICQ to gubka
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 04-03-2019 , 17:45   Re: [ANY] New way to store data into the char array
Reply With Quote #8

Quote:
Originally Posted by gubka View Post
Here I will disagree with you, it will work, like other old compilers works with new ver of sm
__________________
Silvers is offline
gubka
Veteran Member
Join Date: Jan 2012
Location: Russia
Old 04-03-2019 , 17:54   Re: [ANY] New way to store data into the char array
Reply With Quote #9

Quote:
Originally Posted by Silvers View Post
Are they rewrite pawn vm every update?
__________________
gubka is offline
Send a message via ICQ to gubka
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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