but since AI 2008 will be rewritten for this part, it might be an interesting idea.
So, i was working on mail/auction support and got an idea to optimize some code.
My first idea was to make perl do less functions and thus increasing speed. (less unpack's, substr, code ...)
FROM (note: @mail is a global variable)
Code: Select all
my $j = 0;
for (my $i = 8; $i < $args->{RAW_MSG_SIZE}; $i+=73) {
$mail[$j]{mailID} = unpack("V1", substr($args->{RAW_MSG}, $i, 4));
$mail[$j]{title} = unpack("a40", substr($args->{RAW_MSG}, $i+4, 40));
$mail[$j]{read} = unpack("C1", substr($args->{RAW_MSG}, $i+44, 1));
$mail[$j]{sender} = unpack("Z24", substr($args->{RAW_MSG}, $i+45, 24));
$mail[$j]{timestamp} = unpack("V1", substr($args->{RAW_MSG}, $i+69, 4));
$j++;
}
Code: Select all
my @keys = qw(ID title read sender timestamp);
my $j = 0;
for (my $i = 8; $i < $args->{RAW_MSG_SIZE}; $i += 73) {
my @unpacked_data = unpack("V1 a40 C1 Z24 V1", substr($args->{RAW_MSG}, $i, 73);
foreach my $key (@keys) {
$mail[$j]{$key} = shift @unpacked_data;
}
$j++;
}
Then i started thinking, hmm why not do this for all variable lenght packets that contain sub-elements.
Due to that idea, the next idea popped up, why not make 1 function to do basically the same for all these packets?
But then another idea arose, why not integrate this straight into the parse function!
Code: Select all
...
if ($handler->[1]) {
my @unpacked_data = unpack("x2 $handler->[1]", $msg);
foreach my $key (@{$handler->[2]}) {
$args{$key} = shift @unpacked_data;
}
}
if($handler->[3]) {
my $j = 0;
undef @{$handler->[3]};
for (my $i = $handler->[6]; $i < $args{RAW_MSG_SIZE}; $i += $handler->[7]) {
my @unpacked_data = unpack($handler->[4], substr($msg, $i, $handler->[7]);
foreach my $key (@{$handler->[5]}) {
$handler->[3]->[$j]{$key} = shift @unpacked_data;
}
$j++;
}
}
...
Code: Select all
...
'0240' => ['mail_refreshinbox', 'V1', [qw(amount)], \@mail, 'V1 a40 C1 Z24 V1', [qw(ID title read sender timestamp)], 8, 73],
...
Code like this will not be necessary and we will not even have to make a sub for every packet, unless we want to display stuff or pass/adjust data.
Code: Select all
if ($switch eq '0123') {
$psize = 10;
} elsif ($switch eq '02E9') {
$psize = 22;
} else {
$psize = 18;
}
for (my $i = 4; $i < $msg_size; $i += $psize) {
Overriding a packet in $self{packet_list} is also more convenient than overriding a sub if a serverType differs from serverType0.
We will need a lot less code.
There's always room for improvement.
First thing i can think of is to not only allow for storing in a global var,
but also allow just passing a reference to the data.
So we can get information from our data and eventually pass the information to the environment.
(note: data = all, information = useful data)
Secondly, we can support decrypt straight into the parse function too and add a parameter for it in $self{packet_list}.
Packets becoming configurable per serverType, thats how i see it.