Packet/Charlist.pm

filter: Statementstmt 97.3% 72 / 74 Branchbran 50.0% 14 / 28 Conditioncond 47.6% 10 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 73.3% 110 / 150 CC 23 SCAR 23.6
CC
23
Cov
78%
new
18.9
finalize
9.3
import
3.2
CRAP 10.5
« Packet/Login.pm ↑ Summary Packet.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package Game::Tibia::Packet::Charlist;
5  
6 # ABSTRACT: Character list packet support for the MMORPG Tibia
7 3 our $VERSION = '0.007'; # VERSION
8  
9 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Game::Tibia::Packet;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =pod
13  
14 =encoding utf8
15  
16 =head1 NAME
17  
18 Game::Tibia::Packet::Charlist - Character list packet support for the MMORPG Tibia
19  
20  
21 =head1 SYNOPSIS
22  
23 use Game::Tibia::Packet::Charlist;
24  
25 my $p = Game::Tibia::Packet::Charlist->new(
26 packet => $packet,
27 xtea => $xtea,
28 version => 830
29 );
30  
31 $p->{premium_days} = 0xff;
32 $sock->send($p->finalize);
33  
34  
35 =head1 DESCRIPTION
36  
37 Decodes Tibia Login packets into hashes and vice versa.
38  
39 =cut
40  
41 3 our %params;
42 2 ▶sub import {
Subroutine import: called CC=3 SCAR=11.7
43 2 (undef, %params) = (shift, %params, @_);
44 2 ▶ die "Malformed Tibia version\n" if exists $params{tibia} && $params{tibia} !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if exists $Game::Tibia::Packet::Charlist::params{"tibia"} and not $Game::Tibia::Packet::Charlist::params{"tibia"} =~ /^\d+$/u 0 2 2
Condition66%2/3
exists $Game::Tibia::Packet::Charlist::params{"tibia"} and not $Game::Tibia::Packet::Charlist::params{"tibia"} =~ /^\d+$/u
!ll&&!rl&&r
110
MC/DC0%0/2
exists $Game::Tibia::Packet::Charlist::params{"tibia"} and not $Game::Tibia::Packet::Charlist::params{"tibia"} =~ /^\d+$/u
exists $Game::Tibia::Packet::Charlist::params{"tibia"} not $Game::Tibia::Packet::Charlist::params{"tibia"} =~ /^\d+$/u
Truth table66%2/3
A and not(B)
A= exists $Game::Tibia::Packet::Charlist::params{"tibia"}B= not $Game::Tibia::Packet::Charlist::params{"tibia"} =~ /^\d+$/u
ABresult
0X0
100
111
45 }
46  
47 3 ▶use constant DLG_MOTD => 0x14;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶use constant DLG_INFO => 0x15;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 ▶use constant DLG_ERROR => 0x0a;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 3 ▶use constant DLG_CHARLIST => 0x64;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 =head1 METHODS AND ARGUMENTS
53  
54 =over 4
55  
56 =item new([packet => $packet, version => $version, xtea => $xtea])
57  
58 Constructs a new Game::Tibia::Packet::Charlist instance of version C<$version>. When C<packet> and C<xtea> are specified, the supplied packet is decrypted and is then retrievable with the C<payload> subroutine.
59  
60 =cut
61  
62 4 ▶sub new {
Subroutine new: called CC=14 SCAR=29.4
Pod documented
63 4 my $class = shift;
64  
65 4 my $self = {
66 packet => undef,
67 xtea => undef,
68  
69 @_
70 };
71  
72 4 ▶ $self->{version} //= $params{tibia};
Condition66%2/3
$self->{"version"} //= $Game::Tibia::Packet::Charlist::params{"tibia"}
l!l&&r!l&&!r
310
MC/DC0%0/2
$self->{"version"} //= $Game::Tibia::Packet::Charlist::params{"tibia"}
$self->{"version"} $Game::Tibia::Packet::Charlist::params{"tibia"}
Truth table66%2/3
A //= B
A= $self->{"version"}B= $Game::Tibia::Packet::Charlist::params{"tibia"}
ABresult
1X1
011
000
73 4 ▶ croak " 761 <= protocol version < 980 isn't satisfied" if !defined $self->{version} || ! (761 <= $self->{version} && $self->{version} < 980);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"version"} and (761 <= $self->{"version"} and $self->{"version"} < 980) 0 4 4
Condition33%2/6
defined $self->{"version"} and (761 <= $self->{"version"} and $self->{"version"} < 980)
!ll&&!rl&&r
004
761 <= $self->{"version"} and $self->{"version"} < 980
!ll&&!rl&&r
004
MC/DC0%0/3
defined $self->{"version"} and (761 <= $self->{"version"} and $self->{"version"} < 980)
defined $self->{"version"} 761 <= $self->{"version"} $self->{"version"} < 980
Truth table25%1/4
A and B and C
A= defined $self->{"version"}B= 761 <= $self->{"version"}C= $self->{"version"} < 980
ABCresult
0XX0
10X0
1100
1111
74 4 ▶ croak "Packet was specified without XTEA key" if defined $self->{packet} && !defined $self->{xtea};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"packet"} and not defined $self->{"xtea"} 0 4 4
Condition66%2/3
defined $self->{"packet"} and not defined $self->{"xtea"}
!ll&&!rl&&r
130
MC/DC0%0/2
defined $self->{"packet"} and not defined $self->{"xtea"}
defined $self->{"packet"} not defined $self->{"xtea"}
Truth table66%2/3
A and not(B)
A= defined $self->{"packet"}B= not defined $self->{"xtea"}
ABresult
0X0
100
111
75 4 ▶ $self->{versions}{client} = Game::Tibia::Packet::version $self->{version} unless ref $self->{version};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"version"} 4 0 4
76  
77 4 ▶ if (defined $self->{packet}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"packet"}) 3 1 4
78 my $packet = Game::Tibia::Packet->new(
79 packet => $self->{packet},
80 xtea => $self->{xtea},
81 version => $self->{version},
82 3 );
83  
84 3 my $payload = $packet->payload;
85 3 (my $type, $payload) = unpack 'Ca*', $payload;
86 3 ▶ if ($type eq DLG_MOTD) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 20) { } 3 0 3
elsif ($type eq 21) { } 0 0 0
elsif ($type eq 10) { } 0 0 0
87 3 ($self->{motd}, $payload) = unpack '(S/a)< a*', $payload;
88 } elsif ($type eq DLG_INFO) {
89 0 ($self->{info}, $payload) = unpack '(S/a)< a*', $payload;
90 } elsif ($type eq DLG_ERROR) {
91 0 ($self->{error}, $payload) = unpack '(S/a)< a*', $payload;
92 }
93 3 ($type, $payload) = unpack 'Ca*', $payload;
94 3 ▶ if ($type eq DLG_CHARLIST) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 100) 3 0 3
95 3 (my $count, $payload) = unpack 'Ca*', $payload;
96 3 $self->{characters} = undef;
97 3 my @chars;
98 3 ▶ while ($count--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count--) 6 3 9
99 6 my $char;
100 6 ($char->{name}, $char->{world}{name}, $char->{world}{ip}, $char->{world}{port}, $payload)
101 = unpack '(S/a S/a a4 S)< a*', $payload;
102 6 $char->{world}{ip} = join '.', unpack('C4', $char->{world}{ip});
103 6 push @chars, $char;
104 }
105 3 $self->{characters} = \@chars;
106 }
107 3 $self->{premium_days} = unpack 'S<', $payload;
108 }
109  
110 4 bless $self, $class;
111 4 return $self;
112 }
113  
114 =item finalize([$xtea]])
115  
116 Finalizes the packet. encrypts with XTEA and prepends header
117  
118 =cut
119  
120  
121 2 ▶sub finalize {
Subroutine finalize: called CC=8 SCAR=22.3
Pod documented
122 2 my $self = shift;
123 2 ▶ my $xtea = shift // $self->{xtea};
Condition33%1/3
shift() // $self->{"xtea"}
l!l&&r!l&&!r
020
MC/DC0%0/2
shift() // $self->{"xtea"}
shift() $self->{"xtea"}
Truth table33%1/3
A // B
A= shift()B= $self->{"xtea"}
ABresult
1X1
011
000
124  
125 2 my $packet = Game::Tibia::Packet->new(version => $self->{version});
126 2 ▶ $packet->payload .= pack '(C S/a)<', DLG_MOTD, $self->{motd} if defined $self->{motd};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"motd"} 2 0 2
127 2 ▶ $packet->payload .= pack '(C S/a)<', DLG_INFO, $self->{info} if defined $self->{info};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"info"} 0 2 2
128 2 ▶ $packet->payload .= pack '(C S/a)<', DLG_ERROR, $self->{error} if defined $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"error"} 0 2 2
129 2 ▶ if (defined $self->{characters} && @{$self->{characters}} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"characters"} and @{$self->{"characters"};} > 0) 2 0 2
Condition33%1/3
defined $self->{"characters"} and @{$self->{"characters"};} > 0
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self->{"characters"} and @{$self->{"characters"};} > 0
defined $self->{"characters"} @{$self->{"characters"};} > 0
Truth table33%1/3
A and B
A= defined $self->{"characters"}B= @{$self->{"characters"};} > 0
ABresult
0X0
100
111
130 2 $packet->payload .= pack 'C C', DLG_CHARLIST, scalar @{$self->{characters}};
131 2 foreach my $char (@{$self->{characters}}) {
132 $packet->payload .= pack '(S/a S/a a4 S)<',
133 $char->{name}, $char->{world}{name},
134 4 pack("C4", split('\.', $char->{world}{ip})), $char->{world}{port};
135 }
136 }
137 2 $packet->payload .= pack('S<', $self->{premium_days}); # pacc days
138 2 return $packet->finalize($xtea);
139 }
140  
141  
142 3 1;
143 __END__
« Packet/Login.pm ↑ Summary Packet.pm »