SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 10/04/2018, 12:22 AM   #1
BrunoBM23
Little Clucker
 
Join Date: Mar 2018
Posts: 37
Reputation: 32
Default Write & Read offsets - Pawn.RakNet

Write & Read offsets - Pawn.RakNet


I have seen people in doubt when it comes about write and read offsets in a bitstream, and creating a packet's structure array just to write/read a single message.

Before we can understand how they work and what the offsets of a bitstream parameter would be, we have to know how many bits each value type is equivalent to when inside the stream. Below, we have a table with such information:

TypeBitsBytes
FLOAT324
(U)INT32324
(U)INT16162
(U)INT881
BOOL10,125

Assuming you have understood what each type is equivalent to, let's take the information below into account:

To read/write into the offset of the parameter of your choice, you are gonna need the sum of the amount of bits of the parameters previous to the one we intend on reading/writing.

Let's carry on with an example:

PHP Code:
new BitStream:my_bs BS_New();

BS_WriteValue (
    
my_bs,
    
PR_UINT161,
    
PR_UINT322,
    
PR_UINT163,
    
PR_UINT84
); 
On the bitstream above, we have a total of 9 bytes, which are equivalent to 72 bits, because 8 bits = 1 byte.

Let's suppose we intend to read the value of the last parameter, which is 4 and the type is UINT8. There are two ways we can use to read this value, which would be: by using BS_SetReadOffset to set the read offset, or BS_IgnoreBits to ignore the other parameters' bits so we can read only the last 8.

So, knowing that with the sum of the previous parameters' bits we can reach the parameter we want's offsets, let's take into account the examples below:

Example 1 - BS_IgnoreBits
With the sum of the bits of the parameters previous to the last, we are gonna have 64 bits (16 + 32 + 16). We are going to ignore those 64 bits, using the function this way:

PHP Code:
BS_IgnoreBits(my_bs64); 
After ignoring these 64 bits we don't intend on reading, we can finally read the last parameter:

PHP Code:
new var;
    
BS_ReadValue (
    
my_bs,
    
PR_UINT8, var
); 
Then, only the last 8 bits are going to be read.

Example 2 - BS_SetReadOffset
The logic is pretty much the same here, we are going to sum the bits of the parameters previous to the last one, then, we are going to change the read offsets to that sum (64).

PHP Code:
BS_SetReadOffset(my_bs64); 
After changing the read offsets to 64, we can read the last parameter:

PHP Code:
new var;
    
BS_ReadValue (
    
my_bs,
    
PR_UINT8, var
); 

Just like in the first example, only the last 8 bits are going to be read.

Take into account that the same logic applies to write offsets. Just like we have the functions BS_SetReadOffset, BS_GetReadOffset and BS_ResetReadPointer for read offsets, we have BS_SetWriteOffset, BS_GetWriteOffset and BS_ResetWritePointer for write offsets. This is what these functions are for:

BS_SetWriteOffset - Sets the write offsets.
BS_GetWriteOffset - Gets the current write offset.
BS_ResetWritePointer - This function will reset the write offsets (0).

BS_SetReadOffset - Sets the read offsets.
BS_GetReadOffset - Gets the current read offset.
BS_ResetReadPointer - This function will reset the read offsets (0).

About writing/reading data
Some people think you are limited to writing/reading data only once by using BS_WriteValue/BS_ReadValue a single time, but that is not true. As explained previously, a bitstream has read & write offsets, and those are used to identify where you are reading/writing data on, and because of such this is possible:

PHP Code:
new BitStream:my_bs BS_New();

BS_WriteValue (
    
my_bs,
    
PR_UINT161
);

BS_WriteValue (
    
my_bs,
    
PR_UINT322
);

BS_WriteValue (
    
my_bs,
    
PR_UINT163
);

BS_WriteValue (
    
my_bs,
    
PR_UINT84
); 
On the example above, we wrote 72 bits in our bitstream. That way would be equivalent to:

PHP Code:
new BitStream:my_bs BS_New();

BS_WriteValue (
    
my_bs,
    
PR_UINT161,
    
PR_UINT322,
    
PR_UINT163,
    
PR_UINT84
); 

Some RPCs have pretty long structures, so being able to use BS_WriteValue/BS_ReadValue more than one time is benefitial, since functions are limited to 32 parameters in Pawn.

Last edited by BrunoBM23; 13/04/2018 at 11:30 PM.
BrunoBM23 is offline   Reply With Quote
Old 10/04/2018, 12:25 AM   #2
RogueDrifter
High-roller
 
RogueDrifter's Avatar
 
Join Date: Dec 2017
Location: SA-MP Drifting world.
Posts: 1,565
Reputation: 338
Default Re: Write & Read offsets - Pawn.RakNet

Great, thanks for sharing this! (why does this feel like a deja vu )
__________________
Code:
I'm available to script stuff for cash PM me here or on discord @ Andy[RDZ]#0040
[Tutorial]:Anti-cheat guide ||Mini-game 101||VC/LC maps install||
Array index out of bounds


[Github]:Link [Pastebin]:Link [Forum]:Link [Server]:Link [Discord]:Link


RogueDrifter is offline   Reply With Quote
Old 24/06/2018, 12:07 AM   #3
Freshncool
High-roller
 
Freshncool's Avatar
 
Join Date: Aug 2014
Location: Kingdom of Pakistan Reputation: 9999
Posts: 1,221
Reputation: 348
Default Re: Write & Read offsets - Pawn.RakNet

yes now lets do more includes that call one rpc for one player mashallah

EDIT: Got tricked... mashallah. Didn't notice the date, blasphemy, I blame you bruno!
__________________
🍔🍔🍔🍔🍔🍔🍔🍔🍔🍔🍔🍔
Freshncool is offline   Reply With Quote
Old 28/06/2018, 09:33 AM   #4
Sasino97
High-roller
 
Sasino97's Avatar
 
Join Date: Dec 2010
Location: Italy
Posts: 1,438
Reputation: 102
Thumbs up Re: Write & Read offsets - Pawn.RakNet

Thank you for sharing.
__________________
  • SA-MP Game Modes and Filter Scripts (PAWN, MySQL) (8 years experience)
  • Windows Applications (WPF, UWP)
  • Android Applications (Java Android SDK, Xamarin)
  • Web Design and Development (Wordpress, WooCommerce, PHP, MySQL)
  • Game Development (Unity3D, GameMaker Studio, LibGDX)
[email protected]
Use this e-mail - don't PM
Sasino97 is offline   Reply With Quote
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Plugin] Pawn.RakNet YourShadow Plugin Development 202 03/08/2018 07:29 PM
[Tutorial] Offsets de escrita/leitura - Pawn.RakNet BrunoBM23 Lançamentos/Releases 5 18/07/2018 04:26 PM
[Tutorial] RPC - Pawn.RakNet Jelly23 Tutorials 21 13/12/2017 09:51 PM
[Tutorial] Using y_xml to write/read xml files. [Another way to write CFG data but better] Ahmad45123 Tutorials 8 13/03/2015 02:41 AM


All times are GMT. The time now is 06:22 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.