Unpack string helper [POST FROM OLD FORUMS]

Wrote new code? Fixed a bug? Want to discuss technical stuff? Feel free to post it here.

Moderator: Moderators

ponty
Moderators
Moderators
Posts: 130
Joined: 07 May 2008, 20:22
Noob?: No
Location: Canada

Unpack string helper [POST FROM OLD FORUMS]

#1 Post by ponty »

Migrating this post here since the link on the developer's wiki doc was mislinking to the old forums.

ORIGINAL POST FOUND HERE

Original Poster: Damokles
Posted: Sat Jul 02, 2005 3:08 pm
Post Subject: Unpack string helper
I wrote this little helper prog to keep track of positions for really long unpack strings.

It is designed for the packetParser so it will add a x2 before the string.

Feel free to modify, and post improved versions.

I haven't added all possible Templates only those we use most in openkore.

Perl:

Code: Select all

use strict;

my $unpackString = <STDIN>;
my @nameArray = split / /, <STDIN>;

my %size_lut = (
        'a'=>1,
        'A'=>1,
        'c'=>1,
        'C'=>1,
        's'=>2,
        'S'=>2,
        'v'=>2,
        'V'=>4,
        'n'=>2,
        'N'=>4,
        'l'=>4,
        'L'=>4,
        'x'=>1,
        'Z'=>1,

        );

$unpackString = 'x2 '.$unpackString;
my @unpackArray = split(' ',$unpackString);

my $pos = 0;
my $len = 0;
my $name;
foreach my $element (@unpackArray){
        $element =~ /(\w)(\d+)?/;
        $len = $size_lut{$1};
        $len *= $2 if $2;
  $name = (lc($1) ne 'x')? shift (@nameArray) : '';
 #($1.$2), $pos,($pos+$len-1),$len
format STDOUT =
@<<<< @>>>>-@<<<< length: @<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
($1.$2), $pos,($pos+$len-1),$len,$name
.
write();

  $pos += $len;
}
is parsed to

Code:

Code: Select all

x2        0-1     length: 2
a4        2-5     length: 4     ID
v1        6-7     length: 2     walk_speed
v1        8-9     length: 2     param1
v1       10-11    length: 2     param2
v1       12-13    length: 2     param3
v1       14-15    length: 2     type
C1       16-16    length: 1     pet
v1       17-18    length: 2     weapon
v1       19-20    length: 2     lowhead
v1       21-22    length: 2     shield
v1       23-24    length: 2     tophead
v1       25-26    length: 2     midhead
v1       27-28    length: 2     hair_color
v1       29-30    length: 2     head_dir
V1       31-34    length: 4     guildID
x10      35-44    length: 10
C1       45-45    length: 1     sex
a3       46-48    length: 3     coords
C1       49-49    length: 1     body_dir
x1       50-50    length: 1
C1       51-51    length: 1     act
v1       52-53    length: 2     lv
Follow The template in Asking Questions or Be Trashed

Openkore Documentation Editor/Writer
~Servus Servorum~
Technology
Super Moderators
Super Moderators
Posts: 801
Joined: 06 May 2008, 12:47
Noob?: No

Re: Unpack string helper [POST FROM OLD FORUMS]

#2 Post by Technology »

supports short notation like v3 instead of v v v: (not sure if we use shortnotations for other unpackers, add if you want)
but if we encounter something like v14 that could be nasty =/

Code: Select all

use strict;

my $unpackString = <STDIN>;
my @nameArray = split / /, <STDIN>;

my %size_lut = (
        'a'=>1,
        'A'=>1,
        'c'=>1,
        'C'=>1,
        's'=>2,
        'S'=>2,
        'v'=>2,
        'V'=>4,
        'n'=>2,
        'N'=>4,
        'l'=>4,
        'L'=>4,
        'x'=>1,
        'Z'=>1,
);

my %shortnot_lut = (
		'v'=>1,
		'V'=>1,
		'C'=>1,
		'c'=>1,
);

$unpackString = 'x2 '.$unpackString;
my @unpackArray = split(' ',$unpackString);

my $pos = 0;
my $len = 0;
my $name;
foreach my $element (@unpackArray){
        $element =~ /(\w)(\d+)?/;
        $len = $size_lut{$1};
        $len *= $2 if $2;
  $name = (lc($1) ne 'x') ? join(', ', splice(@nameArray, 0, (($shortnot_lut{$1} && $2) ? $2 : 1))) : '';
#($1.$2), $pos,($pos+$len-1),$len
format STDOUT =
@<<<< @>>>>-@<<<< length: @<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
($1.$2), $pos,($pos+$len-1),$len,$name
.
write();

  $pos += $len;
}
to use it, just run the perl script,

copy in:

Code: Select all

C a4 v3 V v5 V v5 a4 a2 v V C2 a5 x C2 v
enter

copy in:

Code: Select all

object_type ID walk_speed opt1 opt2 option type hair_style weapon shield lowhead tick tophead midhead hair_color clothes_color head_dir guildID emblemID manner opt3 karma sex coords unknown1 unknown2 lv
enter

done

ps, you can just copy paste this in your console too:

Code: Select all

damokles.pl
C a4 v3 V v5 V v5 a4 a2 v V C2 a5 x C2 v
object_type ID walk_speed opt1 opt2 option type hair_style weapon shield lowhead tick tophead midhead hair_color clothes_color head_dir guildID emblemID manner opt3 karma sex coords unknown1 unknown2 lv
which would then output something like this:

Code: Select all

C:\>damokles.pl
C a4 v3 V v5 V v5 a4 a2 v V C2 a5 x C2 v
object_type ID walk_speed opt1 opt2 option type hair_style weapon shield lowhead
 tick tophead midhead hair_color clothes_color head_dir guildID emblemID manner
opt3 karma sex coords unknown1 unknown2 lv
x2        0-1     length: 2
C         2-2     length: 1     object_type
a4        3-6     length: 4     ID
v3        7-12    length: 6     walk_speed, opt1, opt2
V        13-16    length: 4     option
v5       17-26    length: 10    type, hair_style, weapon, shield, lowhead
V        27-30    length: 4     tick
v5       31-40    length: 10    tophead, midhead, hair_color, clothes_color, hea
d_dir
a4       41-44    length: 4     guildID
a2       45-46    length: 2     emblemID
v        47-48    length: 2     manner
V        49-52    length: 4     opt3
C2       53-54    length: 2     karma, sex
a5       55-59    length: 5     coords
x        60-60    length: 1
C2       61-62    length: 2     unknown1, unknown2
v        63-64    length: 2     lv
One ST0 to rule them all? One PE viewer to find them!
One ST_kRO to bring them all and in the darkness bind them...

Mount Doom awaits us, fellowship of OpenKore!