blib/lib/Games/TF2/Demo.pm

filter: Statementstmt 43.9% 80 / 182 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 56.2% 27 / 48 Podpod n/a totaltotal 35.5% 107 / 301 CC 27 SCAR 28.0
CC
27
Cov
33%
_mask_of_kinds
90.0
game_state_at
20.0
_check_number
20.0
CRAP 16.5
↑ Summary blib/lib/Games/TF2/Demo/StringTable.pm »
1 package Games::TF2::Demo;
2  
3 11 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '0.01';
9  
10 11 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 XSLoader::load( __PACKAGE__, $VERSION );
14  
15 11 ▶class Games::TF2::Demo :does(Games::TF2::Demo::StateReader) {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶ use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶ use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 11 ▶ use Games::TF2::Demo::Chat;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶ use Games::TF2::Demo::StateReader;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶ use Games::TF2::Demo::Diagnostics;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 ▶ use Games::TF2::Demo::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 11 ▶ use Games::TF2::Demo::Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 11 ▶ use Games::TF2::Demo::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 11 ▶ use Games::TF2::Demo::Item::ConsoleCommand;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 ▶ use Games::TF2::Demo::Item::EntityChange;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 11 ▶ use Games::TF2::Demo::Item::GameEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 11 ▶ use Games::TF2::Demo::Item::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 11 ▶ use Games::TF2::Demo::Item::Sound;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 11 ▶ use Games::TF2::Demo::Item::StringTableChange;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 11 ▶ use Games::TF2::Demo::Item::TempEntity;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 11 ▶ use Games::TF2::Demo::Item::Tick;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 11 ▶ use Games::TF2::Demo::Item::UserMessage;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 11 ▶ use Games::TF2::Demo::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 11 ▶ use Games::TF2::Demo::Summary;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 11 ▶ use Games::TF2::Demo::Vector;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 # The decoding options of tfdemo_options, in the order the XS takes them.
39 Readonly my @OPTIONS => qw(entities temp_entities sounds summary);
40 Readonly my %IS_OPTION => map { $_ => 1 } @OPTIONS;
41  
42 # The handler methods by their bit in the XS's wiring mask, and the kinds
43 # of items (the names of their classes) by the method that gets them.
44 Readonly my @CALLBACK_METHODS => Games::TF2::Demo::_Parser->callback_methods;
45 Readonly my %BIT_OF_METHOD => map { $CALLBACK_METHODS[$_] => 1 << $_ } 0 .. $#CALLBACK_METHODS;
46 Readonly my $ALL_CALLBACKS => ( 1 << @CALLBACK_METHODS ) - 1;
47 Readonly my %METHOD_OF_KIND => (
48 Tick => 'on_tick',
49 GameEvent => 'on_game_event',
50 UserMessage => 'on_user_message',
51 EntityChange => 'on_entity',
52 TempEntity => 'on_temp_entity',
53 StringTableChange => 'on_string_table_changed',
54 ConsoleCommand => 'on_console_command',
55 Sound => 'on_sound',
56 Message => 'on_message',
57 );
58  
59 # A Games::TF2::Demo::_Parser: the C API's parser, freed with it.
60 field $parser;
61 0 ▶ field $header :reader;
Subroutine header: not called CC=1 SCAR=6.9
62 field $has_summary;
63  
64 # The iterator of items, weak: it is active until it is exhausted or gone.
65 field $iterator;
66  
67 ADJUST :params ( :$file = undef, :$data = undef, %options ) {
68 my @unknown = grep { !$IS_OPTION{$_} } sort keys %options;
69 _argument_error( 'new does not take ' . join( ', ', @unknown ) . ' (known: file, data, ' . join( ', ', @OPTIONS ) . ')' ) if @unknown;
70 _argument_error('new needs file or data, not both') if defined $file && defined $data;
71 _argument_error('new needs file or data') if !defined $file && !defined $data;
72  
73 my $defaults = Games::TF2::Demo::_Parser->default_options;
74 my %switches = map { $_ => ( ( $options{$_} // $defaults->{$_} ) ? 1 : 0 ) } @OPTIONS;
75 $has_summary = $switches{summary};
76 $parser
77 = defined $file
78 ? Games::TF2::Demo::_Parser->open_file( _path($file), @switches{@OPTIONS} )
79 : Games::TF2::Demo::_Parser->open_memory( _bytes($data), @switches{@OPTIONS} );
80 $header = Games::TF2::Demo::Header->new( $parser->header->%* );
81 }
82  
83 0 ▶ method step ($handler) {
Subroutine step: not called CC=2 SCAR=17.9
84 0 $self->_no_active_iterator('step');
85 0 ▶ return $parser->step( _handler($handler), $ALL_CALLBACKS ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$parser->step(_handler($handler), $ALL_CALLBACKS) ? : 0 0 0
86 }
87  
88 0 ▶ method run ($handler) {
Subroutine run: not called CC=1 SCAR=6.9
89 0 $self->_no_active_iterator('run');
90 0 $parser->run( _handler($handler), $ALL_CALLBACKS );
91 0 return $self;
92 }
93  
94 0 ▶ method items (%options) {
Subroutine items: not called CC=1 SCAR=6.9
95 0 $self->_no_active_iterator('items');
96 0 my $items = Games::TF2::Demo::Iterator->new( demo => $self, mask => _mask_of_kinds(%options) );
97 0 $iterator = $items;
98 0 weaken($iterator);
99 0 return $items;
100 }
101  
102 0 ▶ method each_item ( $code, %options ) {
Subroutine each_item: not called CC=3 SCAR=24.8
103 0 ▶ _argument_error('each_item needs a code reference') unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
104 0 my $items = $self->items(%options);
105 0 ▶ while ( defined( my $item = $items->next ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $item = $items->next)) 0 0 0
106 0 $code->($item);
107 }
108 0 return $self;
109 }
110  
111 # One step for the active iterator.
112 0 ▶ method _step_for_iterator ( $collector, $mask ) {
Subroutine _step_for_iterator: not called CC=1 SCAR=6.9
113 0 return $parser->step( $collector, $mask );
114 }
115  
116 0 ▶ method game_state_at ($tick) {
Subroutine game_state_at: not called CC=4 SCAR=30.0
117 0 _check_number( game_state_at => $tick );
118 0 $self->_no_active_iterator('game_state_at');
119 0 ▶ while ( $parser->step( undef, 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser->step(undef, 0)) 0 0 0
120 0 ▶ return $self->game_state if $self->last_frame_type eq 'Packet' && $self->last_frame_tick >= $tick;
Branch: 1 branch
BranchTrueFalseTotal
if $self->last_frame_type eq "Packet" and $self->last_frame_tick >= $tick 0 0 0
Condition0%0/3
$self->last_frame_type eq "Packet" and $self->last_frame_tick >= $tick
!ll&&!rl&&r
000
MC/DC0%0/2
$self->last_frame_type eq "Packet" and $self->last_frame_tick >= $tick
$self->last_frame_type eq "Packet" $self->last_frame_tick >= $tick
Truth table0%0/3
A and B
A= $self->last_frame_type eq "Packet"B= $self->last_frame_tick >= $tick
ABresult
0X0
100
111
121 }
122 0 return undef;
123 }
124  
125 0 ▶ method summary () {
Subroutine summary: not called CC=2 SCAR=17.9
126 0 ▶ Games::TF2::Demo::Error::State->throw( message => 'Games::TF2::Demo: summary needs the summary option' ) unless $has_summary;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_summary 0 0 0
127 0 return Games::TF2::Demo::Summary->from_fields( $parser->summary );
128 }
129  
130 0 ▶ method diagnostics () {
Subroutine diagnostics: not called CC=1 SCAR=6.9
131 0 return Games::TF2::Demo::Diagnostics->new( $parser->diagnostics->%* );
132 }
133  
134 0 ▶ method last_frame_type () {
Subroutine last_frame_type: not called CC=1 SCAR=6.9
135 0 return $parser->last_frame_type;
136 }
137  
138 0 ▶ method last_frame_tick () {
Subroutine last_frame_tick: not called CC=1 SCAR=6.9
139 0 return $parser->last_frame_tick;
140 }
141  
142 0 ▶ method _state_handle () { return $parser }
Subroutine _state_handle: not called CC=1 SCAR=6.9
143  
144 0 ▶ method _error_prefix () { return 'Games::TF2::Demo' }
Subroutine _error_prefix: not called CC=1 SCAR=6.9
145  
146 0 ▶ method _no_active_iterator ($what) {
Subroutine _no_active_iterator: not called CC=3 SCAR=24.8
147 0 ▶ return unless defined $iterator && !$iterator->exhausted;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $iterator and not $iterator->exhausted 0 0 0
Condition0%0/3
defined $iterator and not $iterator->exhausted
!ll&&!rl&&r
000
MC/DC0%0/2
defined $iterator and not $iterator->exhausted
defined $iterator not $iterator->exhausted
Truth table0%0/3
A and not(B)
A= defined $iteratorB= not $iterator->exhausted
ABresult
0X0
100
111
148 0 Games::TF2::Demo::Error::State->throw( message => "Games::TF2::Demo: $what while an iterator of items is active" );
149 }
150  
151 # A parser belongs to the thread that opened it.
152 0 ▶ sub CLONE_SKIP { return 1 }
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
153  
154 0 ▶ sub _argument_error ($message) {
Subroutine _argument_error: not called CC=1 SCAR=6.9
155 0 Games::TF2::Demo::Error::Argument->throw( message => "Games::TF2::Demo: $message" );
156 }
157  
158 0 ▶ sub _check_number ( $method, $number ) {
Subroutine _check_number: not called CC=4 SCAR=30.0
159 0 ▶ return if defined $number && !ref $number && $number =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $number and not ref $number and $number =~ /\A[0-9]+\z/u 0 0 0
Condition0%0/6
defined $number and not ref $number and $number =~ /\A[0-9]+\z/u
!ll&&!rl&&r
000
defined $number and not ref $number
!ll&&!rl&&r
000
MC/DC0%0/3
defined $number and not ref $number and $number =~ /\A[0-9]+\z/u
defined $number not ref $number $number =~ /\A[0-9]+\z/u
Truth table0%0/4
A and not(B) and C
A= defined $numberB= not ref $numberC= $number =~ /\A[0-9]+\z/u
ABCresult
0XX0
10X0
1100
1111
160 0 _argument_error("$method needs a non-negative integer");
161 }
162  
163 0 ▶ sub _path ($file) {
Subroutine _path: not called CC=3 SCAR=24.8
164 0 ▶ _argument_error('file must be a path') if ref $file || $file eq '';
Branch: 1 branch
BranchTrueFalseTotal
if ref $file or $file eq "" 0 0 0
Condition0%0/3
ref $file or $file eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $file or $file eq ""
ref $file $file eq ""
Truth table0%0/3
A or B
A= ref $fileB= $file eq ""
ABresult
1X1
011
000
165 0 return $file;
166 }
167  
168 0 ▶ sub _bytes ($data) {
Subroutine _bytes: not called CC=3 SCAR=24.8
169 0 ▶ _argument_error('data must be a byte string') if ref $data;
Branch: 1 branch
BranchTrueFalseTotal
if ref $data 0 0 0
170 0 my $bytes = $data;
171 0 ▶ utf8::downgrade( $bytes, 1 ) or _argument_error('data must be a byte string, it has characters above 255');
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($bytes, 1) or _argument_error("data must be a byte string, it has characters above 255") 0 0 0
172 0 return $bytes;
173 }
174  
175 0 ▶ sub _handler ($handler) {
Subroutine _handler: not called CC=3 SCAR=24.8
176 0 ▶ _argument_error('the handler must be an object that does Games::TF2::Demo::Handler') unless blessed($handler) && $handler->DOES('Games::TF2::Demo::Handler');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($handler) and $handler->DOES("Games::TF2::Demo::Handler") 0 0 0
Condition0%0/3
builtin::blessed($handler) and $handler->DOES("Games::TF2::Demo::Handler")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($handler) and $handler->DOES("Games::TF2::Demo::Handler")
builtin::blessed($handler) $handler->DOES("Games::TF2::Demo::Handler")
Truth table0%0/3
A and B
A= builtin::blessed($handler)B= $handler->DOES("Games::TF2::Demo::Handler")
ABresult
0X0
100
111
177 0 return $handler;
178 }
179  
180 # The wiring mask of the kinds of items asked for; all kinds by default.
181 0 ▶ sub _mask_of_kinds (%options) {
Subroutine _mask_of_kinds: not called CC=9 SCAR=45.0
182 0 my $kinds = delete $options{kinds};
183 0 ▶ _argument_error( 'items does not take ' . join( ', ', sort keys %options ) . ' (known: kinds)' ) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 0 0
184 0 ▶ return $ALL_CALLBACKS unless defined $kinds;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kinds 0 0 0
185 0 ▶ _argument_error('kinds must be an array reference of kinds') unless ref $kinds eq 'ARRAY' && $kinds->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $kinds eq "ARRAY" and @$kinds 0 0 0
Condition0%0/3
ref $kinds eq "ARRAY" and @$kinds
!ll&&!rl&&r
000
MC/DC0%0/2
ref $kinds eq "ARRAY" and @$kinds
ref $kinds eq "ARRAY" @$kinds
Truth table0%0/3
A and B
A= ref $kinds eq "ARRAY"B= @$kinds
ABresult
0X0
100
111
186 0 my $mask = 0;
187 0 foreach my $kind ( $kinds->@* ) {
188 0 ▶ my $method = defined $kind ? $METHOD_OF_KIND{$kind} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $kind ? : 0 0 0
189 0 ▶ _argument_error( 'no kind of item ' . ( $kind // 'undef' ) . ' (known: ' . join( ', ', sort keys %METHOD_OF_KIND ) . ')' ) unless defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 0 0
Condition0%0/2
$kind // "undef"
l!l
00
MC/DC0%0/1
$kind // "undef"
$kind
Truth table0%0/2
A
A= $kind
Aresult
11
00
190 0 $mask |= $BIT_OF_METHOD{$method};
191 }
192 0 return $mask;
193 }
194 }
195  
196 1;
197  
198 __END__
↑ Summary blib/lib/Games/TF2/Demo/StringTable.pm »