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

Solved Set pack position on end not working


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hh2
AlliedModders Donor
Join Date: Sep 2010
Old 09-18-2017 , 20:36   Set pack position on end not working
Reply With Quote #1

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.

Last edited by hh2; 09-19-2017 at 16:09.
hh2 is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 09-18-2017 , 21:57   Re: Set pack position on end not working
Reply With Quote #2

Quote:
Originally Posted by Peace-Maker View Post
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.
headline is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 09-19-2017 , 03:15   Re: Set pack position on end not working
Reply With Quote #3

You could try using Dynamic (linked in my signature) or a plan Trie.
__________________
Neuro Toxin is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 09-19-2017 , 03:19   Re: Set pack position on end not working
Reply With Quote #4

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.
Fyren is offline
hh2
AlliedModders Donor
Join Date: Sep 2010
Old 09-19-2017 , 11:41   Re: Set pack position on end not working
Reply With Quote #5

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

Last edited by hh2; 09-19-2017 at 11:41.
hh2 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 09-19-2017 , 15:07   Re: Set pack position on end not working
Reply With Quote #6

Just do
Code:
pack.ReadCell();
__________________
asherkin is offline
hh2
AlliedModders Donor
Join Date: Sep 2010
Old 09-19-2017 , 16:08   Re: Set pack position on end not working
Reply With Quote #7

This is what i need, thanks for your help guys.
hh2 is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 09-19-2017 , 18:51   Re: Set pack position on end not working
Reply With Quote #8

In the latest dev snapshot, setting the position to the end is now allowed.
Fyren is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 02-21-2019 , 08:07   Re: Set pack position on end not working
Reply With Quote #9

Quote:
Originally Posted by Peace-Maker View Post
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 View Post
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);
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 02-21-2019 , 09:22   Re: Set pack position on end not working
Reply With Quote #10

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.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
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 11:19.


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