AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved Set pack position on end not working (https://forums.alliedmods.net/showthread.php?t=301390)

hh2 09-18-2017 20:36

Set pack position on end not working
 
What is wrong in this code?
PHP Code:

    // Create DataPack
    
DataPack pack CreateDataPack();
    
    
// Write data to DataPack
    
pack.WriteCell(client);
    
pack.WriteString("test");
    
pack.WriteCell(5);
    
    
// Get DataPack end position for later
    
DataPackPos pos pack.Position;
    
    
// Reset DataPack
    
pack.Reset();
    
    
// Read only client id
    
int client2 pack.ReadCell();
    
    
// Set position to end 
    
pack.Position pos;       // Error here
    
    // Write new data to the end
    
pack.WriteCell(6); 

Logs:
Code:

[SM] Exception reported: Invalid DataPack position, 28 is out of bounds
[SM] Call stack trace:
[SM] [0] DataPack.Position.set

I'm trying also something like (pos - view_as<DataPackPos>(1)), but this gave me incorrect results.

headline 09-18-2017 21:57

Re: Set pack position on end not working
 
Quote:

Originally Posted by Peace-Maker (Post 2144341)
You shouldn't be using SetPackPosition. It's setting the position of the internal char* stream to some offset. You'd need to calculate the offset yourself which will likely break easily.

WritePackCell first "pushes" the size of a cell (4 bytes) on the stream then the cell itself (4 bytes again). So you'd need to set the pack position to 5*8=40. It gets trickier with strings in the pack.. You'd be better off with an adt_trie. SetPackPosition should be deprecated as it's of no practical use.


Neuro Toxin 09-19-2017 03:15

Re: Set pack position on end not working
 
You could try using Dynamic (linked in my signature) or a plan Trie.

Fyren 09-19-2017 03:19

Re: Set pack position on end not working
 
The quote from PM above is probably talking about is arbitrarily setting the position (e.g. writing four cells then setting the position to 2, which is wrong).

The short answer is you cannot set the pack position to the end. I think the intented way to use datapacks is create them, write all the data, then reset them and send them off to be read.

The longer answer is I guess you can save the position of the last thing you wrote, then later set the position to that, then read that again to end up with the position being at the end. It also looks like SM's tracking of the datapack size will be wrong if you rewrite earlier portions of the datapack and not at the very end.

hh2 09-19-2017 11:41

Re: Set pack position on end not working
 
Quote:

Originally Posted by Neuro Toxin (Post 2549622)
You could try using Dynamic (linked in my signature) or a plan Trie.

This library looking good, but i dont wanna use it only for 1 callback.

Quote:

Originally Posted by Fyren (Post 2549624)
The longer answer is I guess you can save the position of the last thing you wrote, then later set the position to that, then read that again to end up with the position being at the end. It also looks like SM's tracking of the datapack size will be wrong if you rewrite earlier portions of the datapack and not at the very end.

Probably use this method, but is there way to get rid of that warning: warning 204: symbol is assigned a value that is never used: "five"

PHP Code:

    // Create DataPack
    
DataPack pack CreateDataPack();
    
    
// Write data to DataPack
    
pack.WriteCell(client);
    
pack.WriteString("test");
    
DataPackPos pos pack.Position;
    
pack.WriteCell(5);
    
    
// Reset DataPack
    
pack.Reset();
    
    
// Read only client id
    
int client2 pack.ReadCell();
    
    
// Set position to end
    
pack.Position pos;
    
int five pack.ReadCell();
    
    
// Write new data to the end
    
pack.WriteCell(6); 


asherkin 09-19-2017 15:07

Re: Set pack position on end not working
 
Just do
Code:

pack.ReadCell();

hh2 09-19-2017 16:08

Re: Set pack position on end not working
 
This is what i need, thanks for your help guys.

Fyren 09-19-2017 18:51

Re: Set pack position on end not working
 
In the latest dev snapshot, setting the position to the end is now allowed.

Dragokas 02-21-2019 08:07

Re: Set pack position on end not working
 
Quote:

Originally Posted by Peace-Maker (Post 2144341)
WritePackCell first "pushes" the size of a cell (4 bytes) on the stream then the cell itself (4 bytes again).

in such case, why:
Code:

    DataPack hPack = new DataPack();
    PrintToServer("Position: %i", hPack.Position);
    hPack.WriteCell(5);
    PrintToServer("Position: %i", hPack.Position);

Quote:

Position: 0
Position: 9
0 ... 8 == 9 bytes, not 8 (4 + 4).

Quote:

Originally Posted by Fyren (Post 2549624)
It also looks like SM's tracking of the datapack size will be wrong if you rewrite earlier portions of the datapack and not at the very end.

So, this code will be unsafe?
Code:

        DataPack hPack = new DataPack();
        hPack.WriteCell(5);
        hPack.WriteCell(6);
       
        // ...... some actions
       
        // overwrite first cell
        hPack.Reset();
        hPack.WriteCell(4);


Dragokas 02-21-2019 09:22

Re: Set pack position on end not working
 
off.
One more question, please:

Can I withdraw the TIMER_DATA_HNDL_CLOSE flag in timer callback?
Or it is safe to use CloneHandle there:

Spoiler

thanks.


All times are GMT -4. The time now is 14:07.

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