lib/Games/TF2/Demo.xs

filter: Statementstmt 0.0% 0 / 1067 Branchbran 0.0% 0 / 550 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 1617 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Games/TF2/Demo/Item/ConsoleCommand.pm ↑ Summary blib/lib/Games/TF2/Demo/Handler.pm »
1 /*
2 * Demo.xs - the XS surface of Games::TF2::Demo and Games::TF2::Server over
3 * libtfdemo's C API.
4 *
5 * Copyright 2026 davenonymous
6 * SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
7 *
8 * Only tfdemo.h and tfnet.h are used, never the C++ API. A demo's handle is
9 * a Games::TF2::Demo::_Parser, a connection's a Games::TF2::Server::_Connection
10 * (a subclass, for the state readers): a blessed reference to the address
11 * of a demo_parser. Everything a callback or a poll gets is copied into
12 * Perl values at once: the C API's pointers are valid only during the
13 * callback or until the next poll. What the reading functions between
14 * steps or polls return (game state, summary, players, string tables,
15 * server info) is copied into plain hashes and arrays, which the Perl
16 * classes wrap.
17 *
18 * The callbacks run inside libtfdemo's C++ frames, which a Perl die must
19 * never unwind: every Perl call made there runs under G_EVAL, and the
20 * first exception of a frame is kept until tfdemo_step returns. From then
21 * on the frame's callbacks return at once, and the exception is thrown
22 * again once the frame is done.
23 */
24 #define PERL_NO_GET_CONTEXT
25 #include "EXTERN.h"
26 #include "perl.h"
27 #include "XSUB.h"
28  
29 #include "tfdemo.h"
30 #include "tfnet.h"
31  
32 #define PARSER_CLASS "Games::TF2::Demo::_Parser"
33 #define CONNECTION_CLASS "Games::TF2::Server::_Connection"
34 #define ERROR_CLASS "Games::TF2::Demo::Error"
35 #define VECTOR_CLASS "Games::TF2::Demo::Vector"
36 #define CHAT_CLASS "Games::TF2::Demo::Chat"
37 #define ITEM_CLASS(name) "Games::TF2::Demo::Item::" name
38  
39 /* The most constructor arguments an item takes, as key and value. */
40 #define MAX_ITEM_ARGUMENTS 32
41  
42 typedef struct {
43 /* A demo's parser, NULL for a connection. */
44 tfdemo_parser *parser;
45 /* A connection, NULL for a demo. */
46 tfnet_connection *connection;
47 /* What the state readers read: the parser, or the connection's view. */
48 const tfdemo_parser *state;
49 /* Per class id: [ the class name as bytes, the props hash key of each flat property ]. */
50 AV *class_keys;
51 /* True while step or run handles frames: a handler must not call them. */
52 int stepping;
53 } demo_parser;
54  
55 /* What the callbacks of a step share. */
56 typedef struct {
57 demo_parser *demo;
58 SV *handler;
59 /* The first exception of the frame, NULL while there is none. */
60 SV *error;
61 } frame_context;
62  
63 static const char *const CHANGE_KINDS[] = {"created", "entered", "updated", "left", "deleted"};
64  
65 /* ---------------------------------------------------------------- strings */
66  
67 /*
68 * The length of the well-formed UTF-8 sequence at `at`, 0 for none: the
69 * rule of the tfdemo tool's JsonWriter::value (tools/tfdemo/json.cpp), so
70 * that a string reads the same in Perl as in the tool's JSON.
71 */
72 0 static STRLEN utf8_sequence_length(const U8 *text, STRLEN size, STRLEN at) {
73 static const U32 minimum[5] = {0, 0, 0x80, 0x800, 0x10000};
74 0 const U8 lead = text[at];
75 STRLEN length;
76 STRLEN i;
77 U32 code;
78  
79 0 ▶ if (lead < 0x80) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (lead < 0x80) return 1; 0 0 0
80 0 ▶ if ((lead & 0xE0) == 0xC0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((lead & 0xE0) == 0xC0) { 0 0 0
81 0 length = 2;
82 0 code = lead & 0x1F;
83 0 ▶ } else if ((lead & 0xF0) == 0xE0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ((lead & 0xF0) == 0xE0) { 0 0 0
84 0 length = 3;
85 0 code = lead & 0x0F;
86 0 ▶ } else if ((lead & 0xF8) == 0xF0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ((lead & 0xF8) == 0xF0) { 0 0 0
87 0 length = 4;
88 0 code = lead & 0x07;
89 } else {
90 0 return 0;
91 }
92 0 ▶ if (at + length > size) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (at + length > size) return 0; 0 0 0
93 0 ▶ for (i = 1; i < length; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < length; ++i) { 0 0 0
94 0 ▶ if ((text[at + i] & 0xC0) != 0x80) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((text[at + i] & 0xC0) != 0x80) return 0; 0 0 0
95 0 code = (code << 6) | (text[at + i] & 0x3F);
96 }
97 0 ▶ if (code < minimum[length] || code > 0x10FFFF || (code >= 0xD800 && code <= 0xDFFF)) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (code < minimum[length] || code > 0x10FFFF || (code >= 0xD800 && code <= 0xDFFF)) return 0; 0 0 0
if (code < minimum[length] || code > 0x10FFFF || (code >= 0xD800 && code <= 0xDFFF)) return 0; 0 0 0
if (code < minimum[length] || code > 0x10FFFF || (code >= 0xD800 && code <= 0xDFFF)) return 0; 0 0 0
if (code < minimum[length] || code > 0x10FFFF || (code >= 0xD800 && code <= 0xDFFF)) return 0; 0 0 0
98 0 return length;
99 }
100  
101 /* A string of a demo as a Perl character string: UTF-8 decoded, each byte
102 * that starts no well-formed sequence replaced by U+FFFD. NULL is undef. */
103 0 static SV *new_demo_string(pTHX_ const char *text, STRLEN size) {
104 static const char replacement[] = "\xEF\xBF\xBD";
105 0 const U8 *bytes = (const U8 *)text;
106 SV *string;
107 0 STRLEN at = 0;
108  
109 0 ▶ if (!text) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!text) return newSV(0); 0 0 0
110 0 string = newSV(size + 1);
111 0 sv_setpvs(string, "");
112 0 ▶ while (at < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (at < size) { 0 0 0
113 0 STRLEN length = utf8_sequence_length(bytes, size, at);
114 0 ▶ if (length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length == 0) { 0 0 0
115 0 sv_catpvn(string, replacement, sizeof(replacement) - 1);
116 0 ++at;
117 0 continue;
118 }
119 0 sv_catpvn(string, text + at, length);
120 0 at += length;
121 }
122 0 SvUTF8_on(string);
123 0 return string;
124 }
125  
126 0 static SV *new_demo_cstring(pTHX_ const char *text) {
127 0 ▶ return new_demo_string(aTHX_ text, text ? strlen(text) : 0);
Branch: 1 branch
BranchTrueFalseTotal
return new_demo_string(aTHX_ text, text ? strlen(text) : 0); 0 0 0
128 }
129  
130 0 static SV *new_int64(pTHX_ int64_t value) {
131 #if IVSIZE >= 8
132 0 return newSViv((IV)value);
133 #else
134 if (value >= IV_MIN && value <= IV_MAX) return newSViv((IV)value);
135 return newSVnv((NV)value);
136 #endif
137 }
138  
139 /* ------------------------------------------------------------------ errors */
140  
141 /* Pushes Games::TF2::Demo::Error, the status and a message (mortal). */
142 0 static void push_status(pTHX_ tfdemo_status status, SV *message) {
143 0 dSP;
144 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
145 0 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 0 0
146 0 PUSHs(sv_2mortal(newSVpvs(ERROR_CLASS)));
147 0 PUSHs(sv_2mortal(newSViv((IV)status)));
148 0 PUSHs(message);
149 0 PUTBACK;
150 0 }
151  
152 /* Throws the Games::TF2::Demo::Error of a status with a message (mortal);
153 * outside callbacks only. */
154 0 static void throw_status_message(pTHX_ tfdemo_status status, SV *message) {
155 0 push_status(aTHX_ status, message);
156 0 call_method("throw_status", G_DISCARD | G_VOID);
157 0 croak("%s->throw_status returned", ERROR_CLASS);
158 }
159  
160 /* Throws the Games::TF2::Demo::Error of a failed call; outside callbacks only. */
161 0 static void throw_status(pTHX_ tfdemo_status status) {
162 0 throw_status_message(aTHX_ status, sv_2mortal(new_demo_cstring(aTHX_ tfdemo_last_error())));
163 0 }
164  
165 /* Throws a Games::TF2::Demo::Error of this binding's own; outside callbacks only. */
166 0 static void throw_message(pTHX_ tfdemo_status status, const char *message) {
167 0 throw_status_message(aTHX_ status, sv_2mortal(newSVpv(message, 0)));
168 0 }
169  
170 /* Keeps $@ as the frame's exception unless an earlier one is kept. */
171 0 static void keep_exception(pTHX_ frame_context *frame) {
172 0 ▶ if (!frame->error) frame->error = newSVsv(ERRSV);
Branch: 2 branches
BranchTrueFalseTotal
if (!frame->error) frame->error = newSVsv(ERRSV); 0 0 0
if (!frame->error) frame->error = newSVsv(ERRSV); 0 0 0
173 0 }
174  
175 /* Keeps the Games::TF2::Demo::Error of a failed call as the frame's exception. */
176 0 static void keep_status(pTHX_ frame_context *frame, tfdemo_status status) {
177 0 ▶ if (frame->error) return;
Branch: 1 branch
BranchTrueFalseTotal
if (frame->error) return; 0 0 0
178 0 ENTER;
179 0 SAVETMPS;
180 0 push_status(aTHX_ status, sv_2mortal(new_demo_cstring(aTHX_ tfdemo_last_error())));
181 0 call_method("throw_status", G_DISCARD | G_VOID | G_EVAL);
182 0 keep_exception(aTHX_ frame);
183 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
184 0 LEAVE;
185 }
186  
187 /* ----------------------------------------------------------------- objects */
188  
189 /*
190 * CLASS->new(arguments), each argument a new SV this function owns. NULL,
191 * with the exception kept, when the constructor dies or an exception was
192 * kept before.
193 */
194 0 static SV *construct(pTHX_ frame_context *frame, const char *class_name, SV **arguments, int count) {
195 0 SV *object = NULL;
196 SV *result;
197 int i;
198  
199 0 ▶ if (frame->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->error) { 0 0 0
200 0 ▶ for (i = 0; i < count; ++i) SvREFCNT_dec(arguments[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) SvREFCNT_dec(arguments[i]); 0 0 0
201 0 return NULL;
202 }
203 {
204 0 dSP;
205 0 ENTER;
206 0 SAVETMPS;
207 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
208 0 ▶ EXTEND(SP, count + 1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, count + 1); 0 0 0
EXTEND(SP, count + 1); 0 0 0
209 0 PUSHs(sv_2mortal(newSVpv(class_name, 0)));
210 0 ▶ for (i = 0; i < count; ++i) PUSHs(sv_2mortal(arguments[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) PUSHs(sv_2mortal(arguments[i])); 0 0 0
211 0 PUTBACK;
212 0 call_method("new", G_SCALAR | G_EVAL);
213 0 SPAGAIN;
214 0 result = POPs;
215 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
216 0 keep_exception(aTHX_ frame);
217 } else {
218 0 object = newSVsv(result);
219 }
220 0 PUTBACK;
221 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
222 0 LEAVE;
223 }
224 0 return object;
225 }
226  
227 /* A Games::TF2::Demo::Vector; undef when it cannot be made. */
228 0 static SV *new_vector(pTHX_ frame_context *frame, float x, float y, float z) {
229 SV *arguments[6];
230 SV *vector;
231  
232 0 arguments[0] = newSVpvs("x");
233 0 arguments[1] = newSVnv((NV)x);
234 0 arguments[2] = newSVpvs("y");
235 0 arguments[3] = newSVnv((NV)y);
236 0 arguments[4] = newSVpvs("z");
237 0 arguments[5] = newSVnv((NV)z);
238 0 vector = construct(aTHX_ frame, VECTOR_CLASS, arguments, 6);
239 0 ▶ return vector ? vector : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
return vector ? vector : newSV(0); 0 0 0
240 }
241  
242 /* ----------------------------------------------------------- property values */
243  
244 /* The Perl value of a property that is no array: a scalar or a Vector. A
245 * VECTOR_XY property becomes a Vector with z 0. */
246 0 static SV *new_scalar_value(pTHX_ frame_context *frame, const tfdemo_prop_value *value) {
247 0 switch (value->type) {
248 0 case TFDEMO_PROP_INT: return new_int64(aTHX_ value->int_value);
249 0 case TFDEMO_PROP_FLOAT: return newSVnv((NV)value->float_value);
250 0 case TFDEMO_PROP_VECTOR:
251 0 return new_vector(aTHX_ frame, value->vector[0], value->vector[1], value->vector[2]);
252 0 case TFDEMO_PROP_VECTOR_XY: return new_vector(aTHX_ frame, value->vector[0], value->vector[1], 0.0f);
253 0 case TFDEMO_PROP_STRING: return new_demo_string(aTHX_ value->string_value, value->string_length);
254 0 case TFDEMO_PROP_UNSET:
255 case TFDEMO_PROP_ARRAY:
256 0 default: return newSV(0);
257 }
258 }
259  
260 /* Reads an element of an array property of an entity or a temp entity. */
261 typedef tfdemo_status (*element_reader)(const void *owner, int32_t prop, size_t element, tfdemo_prop_value *out);
262  
263 0 static tfdemo_status read_entity_element(const void *owner, int32_t prop, size_t element, tfdemo_prop_value *out) {
264 0 return tfdemo_entity_prop_element((const tfdemo_entity *)owner, prop, element, out);
265 }
266  
267 0 static tfdemo_status read_temp_entity_element(const void *owner, int32_t prop, size_t element, tfdemo_prop_value *out) {
268 0 return tfdemo_temp_entity_prop_element((const tfdemo_temp_entity *)owner, prop, element, out);
269 }
270  
271 /* The Perl value of a property: an array becomes an array reference of
272 * its elements' values. */
273 0 static SV *new_prop_value(pTHX_ frame_context *frame, const void *owner, element_reader read_element, int32_t prop,
274 const tfdemo_prop_value *value) {
275 AV *elements;
276 size_t element;
277  
278 0 ▶ if (value->type != TFDEMO_PROP_ARRAY) return new_scalar_value(aTHX_ frame, value);
Branch: 1 branch
BranchTrueFalseTotal
if (value->type != TFDEMO_PROP_ARRAY) return new_scalar_value(aTHX_ frame, value); 0 0 0
279 0 elements = newAV();
280 0 ▶ if (value->array_length > 0) av_extend(elements, (SSize_t)value->array_length - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (value->array_length > 0) av_extend(elements, (SSize_t)value->array_length - 1); 0 0 0
281 0 ▶ for (element = 0; element < value->array_length; ++element) {
Branch: 1 branch
BranchTrueFalseTotal
for (element = 0; element < value->array_length; ++element) { 0 0 0
282 tfdemo_prop_value element_value;
283 0 const tfdemo_status status = read_element(owner, prop, element, &element_value);
284 0 ▶ if (status != TFDEMO_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) { 0 0 0
285 0 keep_status(aTHX_ frame, status);
286 0 break;
287 }
288 0 av_push(elements, new_scalar_value(aTHX_ frame, &element_value));
289 }
290 0 return newRV_noinc((SV *)elements);
291 }
292  
293 /* A key that is unique among those `seen` so far: the first of a name is
294 * the name, the later ones get "#2", "#3" and so on; decoded. */
295 0 static SV *new_unique_key(pTHX_ HV *seen, const char *name, STRLEN name_length) {
296 0 SV **occurrences = hv_fetch(seen, name, (I32)name_length, 1);
297 0 ▶ const IV occurrence = (SvOK(*occurrences) ? SvIV(*occurrences) : 0) + 1;
Branch: 1 branch
BranchTrueFalseTotal
const IV occurrence = (SvOK(*occurrences) ? SvIV(*occurrences) : 0) + 1; 0 0 0
298 SV *bytes;
299 SV *key;
300 STRLEN length;
301 const char *text;
302  
303 0 sv_setiv(*occurrences, occurrence);
304 0 ▶ if (occurrence == 1) return new_demo_string(aTHX_ name, name_length);
Branch: 1 branch
BranchTrueFalseTotal
if (occurrence == 1) return new_demo_string(aTHX_ name, name_length); 0 0 0
305 0 bytes = newSVpvn(name, name_length);
306 0 sv_catpvf(bytes, "#%" IVdf, occurrence);
307 0 text = SvPV(bytes, length);
308 0 key = new_demo_string(aTHX_ text, length);
309 0 SvREFCNT_dec(bytes);
310 0 return key;
311 }
312  
313 /*
314 * The props hash keys of a class, one per flat property: "table.prop",
315 * decoded. A key that repeats within the class (animation layers, the
316 * match and round scoreboard values) keeps it for its first property;
317 * the later ones get "#2", "#3" and so on, in flat property order. Built
318 * once per class and kept until the class at that id has another name or
319 * number of properties (new data tables).
320 */
321 0 static AV *class_keys(pTHX_ demo_parser *demo, int32_t class_id) {
322 0 const char *class_name = tfdemo_class_name(demo->state, class_id);
323 0 const int32_t prop_count = tfdemo_class_prop_count(demo->state, class_id);
324 0 ▶ SV **cached = class_id >= 0 ? av_fetch(demo->class_keys, class_id, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **cached = class_id >= 0 ? av_fetch(demo->class_keys, class_id, 0) : NULL; 0 0 0
325 HV *seen;
326 AV *keys;
327 int32_t prop;
328  
329 0 ▶ if (class_id < 0 || !class_name) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (class_id < 0 || !class_name) return NULL; 0 0 0
if (class_id < 0 || !class_name) return NULL; 0 0 0
330 0 ▶ if (cached && SvROK(*cached)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cached && SvROK(*cached)) { 0 0 0
if (cached && SvROK(*cached)) { 0 0 0
331 0 AV *entry = (AV *)SvRV(*cached);
332 0 SV **name = av_fetch(entry, 0, 0);
333 0 ▶ if (av_len(entry) == prop_count && name && strEQ(SvPV_nolen(*name), class_name)) return entry;
Branch: 3 branches
BranchTrueFalseTotal
if (av_len(entry) == prop_count && name && strEQ(SvPV_nolen(*name), class_name)) return entry; 0 0 0
if (av_len(entry) == prop_count && name && strEQ(SvPV_nolen(*name), class_name)) return entry; 0 0 0
if (av_len(entry) == prop_count && name && strEQ(SvPV_nolen(*name), class_name)) return entry; 0 0 0
334 }
335 0 keys = newAV();
336 0 av_extend(keys, prop_count);
337 0 av_push(keys, newSVpv(class_name, 0));
338 0 seen = newHV();
339 0 ▶ for (prop = 0; prop < prop_count; ++prop) {
Branch: 1 branch
BranchTrueFalseTotal
for (prop = 0; prop < prop_count; ++prop) { 0 0 0
340 0 const char *table = tfdemo_class_prop_table(demo->state, class_id, prop);
341 0 const char *name = tfdemo_class_prop_name(demo->state, class_id, prop);
342 0 ▶ SV *table_and_name = newSVpvf("%s.%s", table ? table : "", name ? name : "");
Branch: 2 branches
BranchTrueFalseTotal
SV *table_and_name = newSVpvf("%s.%s", table ? table : "", name ? name : ""); 0 0 0
SV *table_and_name = newSVpvf("%s.%s", table ? table : "", name ? name : ""); 0 0 0
343 STRLEN length;
344 0 const char *bytes = SvPV(table_and_name, length);
345  
346 0 av_push(keys, new_unique_key(aTHX_ seen, bytes, length));
347 0 SvREFCNT_dec(table_and_name);
348 }
349 0 SvREFCNT_dec((SV *)seen);
350 0 av_store(demo->class_keys, class_id, newRV_noinc((SV *)keys));
351 0 return keys;
352 }
353  
354 0 static SV *prop_key(pTHX_ AV *keys, int32_t prop) {
355 0 ▶ SV **key = keys && prop >= 0 ? av_fetch(keys, prop + 1, 0) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **key = keys && prop >= 0 ? av_fetch(keys, prop + 1, 0) : NULL; 0 0 0
SV **key = keys && prop >= 0 ? av_fetch(keys, prop + 1, 0) : NULL; 0 0 0
356 0 ▶ return key ? *key : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return key ? *key : NULL; 0 0 0
357 }
358  
359 /* Stores an entity's property under its key; one never sent is left out. */
360 0 static void store_entity_prop(pTHX_ frame_context *frame, HV *props, AV *keys, const tfdemo_entity *entity, int32_t prop) {
361 0 SV *key = prop_key(aTHX_ keys, prop);
362 tfdemo_prop_value value;
363 0 const tfdemo_status status = tfdemo_entity_prop(entity, prop, &value);
364  
365 0 ▶ if (status != TFDEMO_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) { 0 0 0
366 0 keep_status(aTHX_ frame, status);
367 0 return;
368 }
369 0 ▶ if (value.type == TFDEMO_PROP_UNSET || !key) return;
Branch: 2 branches
BranchTrueFalseTotal
if (value.type == TFDEMO_PROP_UNSET || !key) return; 0 0 0
if (value.type == TFDEMO_PROP_UNSET || !key) return; 0 0 0
370 0 (void)hv_store_ent(props, key, new_prop_value(aTHX_ frame, entity, read_entity_element, prop, &value), 0);
371 }
372  
373 /* The properties an entity change sent, as a hash reference. */
374 0 static SV *new_changed_props(
375 pTHX_ frame_context *frame, const tfdemo_entity *entity, const int32_t *changed, size_t changed_count) {
376 0 AV *keys = class_keys(aTHX_ frame->demo, tfdemo_entity_class(entity));
377 0 HV *props = newHV();
378 size_t i;
379  
380 0 ▶ for (i = 0; i < changed_count && !frame->error; ++i) store_entity_prop(aTHX_ frame, props, keys, entity, changed[i]);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < changed_count && !frame->error; ++i) store_entity_prop(aTHX_ frame, props, keys, entity, changed[i]); 0 0 0
for (i = 0; i < changed_count && !frame->error; ++i) store_entity_prop(aTHX_ frame, props, keys, entity, changed[i]); 0 0 0
381 0 return newRV_noinc((SV *)props);
382 }
383  
384 /* Every property of an entity, as a hash reference. */
385 0 static SV *new_all_props(pTHX_ frame_context *frame, const tfdemo_entity *entity) {
386 0 const int32_t class_id = tfdemo_entity_class(entity);
387 0 const int32_t count = tfdemo_class_prop_count(frame->demo->state, class_id);
388 0 AV *keys = class_keys(aTHX_ frame->demo, class_id);
389 0 HV *props = newHV();
390 int32_t prop;
391  
392 0 ▶ for (prop = 0; prop < count && !frame->error; ++prop) store_entity_prop(aTHX_ frame, props, keys, entity, prop);
Branch: 2 branches
BranchTrueFalseTotal
for (prop = 0; prop < count && !frame->error; ++prop) store_entity_prop(aTHX_ frame, props, keys, entity, prop); 0 0 0
for (prop = 0; prop < count && !frame->error; ++prop) store_entity_prop(aTHX_ frame, props, keys, entity, prop); 0 0 0
393 0 return newRV_noinc((SV *)props);
394 }
395  
396 /* The props of a temp entity; one never sent is left out. */
397 0 static SV *new_temp_entity_props(pTHX_ frame_context *frame, const tfdemo_temp_entity *temp_entity) {
398 0 const int32_t class_id = tfdemo_temp_entity_class(temp_entity);
399 0 const int32_t count = tfdemo_class_prop_count(frame->demo->state, class_id);
400 0 AV *keys = class_keys(aTHX_ frame->demo, class_id);
401 0 HV *props = newHV();
402 int32_t prop;
403  
404 0 ▶ for (prop = 0; prop < count && !frame->error; ++prop) {
Branch: 2 branches
BranchTrueFalseTotal
for (prop = 0; prop < count && !frame->error; ++prop) { 0 0 0
for (prop = 0; prop < count && !frame->error; ++prop) { 0 0 0
405 0 SV *key = prop_key(aTHX_ keys, prop);
406 tfdemo_prop_value value;
407 0 const tfdemo_status status = tfdemo_temp_entity_prop(temp_entity, prop, &value);
408  
409 0 ▶ if (status != TFDEMO_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) { 0 0 0
410 0 keep_status(aTHX_ frame, status);
411 0 break;
412 }
413 0 ▶ if (value.type == TFDEMO_PROP_UNSET || !key) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (value.type == TFDEMO_PROP_UNSET || !key) continue; 0 0 0
if (value.type == TFDEMO_PROP_UNSET || !key) continue; 0 0 0
414 0 (void)hv_store_ent(props, key, new_prop_value(aTHX_ frame, temp_entity, read_temp_entity_element, prop, &value), 0);
415 }
416 0 return newRV_noinc((SV *)props);
417 }
418  
419 /* --------------------------------------------------------------- callbacks */
420  
421 /* The constructor arguments of an item, as key and value. */
422 typedef struct {
423 SV *values[MAX_ITEM_ARGUMENTS];
424 int count;
425 } item_arguments;
426  
427 /* MAX_ITEM_ARGUMENTS holds the largest item (a Sound: 13 keys and values).
428 * Callbacks must not croak, so an argument beyond it is dropped. */
429 0 static void add_argument(pTHX_ item_arguments *arguments, const char *key, SV *value) {
430 0 ▶ if (arguments->count + 2 > MAX_ITEM_ARGUMENTS) {
Branch: 1 branch
BranchTrueFalseTotal
if (arguments->count + 2 > MAX_ITEM_ARGUMENTS) { 0 0 0
431 0 SvREFCNT_dec(value);
432 0 return;
433 }
434 0 arguments->values[arguments->count++] = newSVpv(key, 0);
435 0 arguments->values[arguments->count++] = value;
436 }
437  
438 0 static void add_ticks(pTHX_ item_arguments *arguments, const tfdemo_parser *parser) {
439 0 add_argument(aTHX_ arguments, "demo_tick", newSViv((IV)tfdemo_demo_tick(parser)));
440 0 add_argument(aTHX_ arguments, "server_tick", newSViv((IV)tfdemo_server_tick(parser)));
441 0 }
442  
443 /* Makes the item and hands it to the handler's method. */
444 0 static void deliver(pTHX_ frame_context *frame, const char *method, const char *item_class, item_arguments *arguments) {
445 0 SV *item = construct(aTHX_ frame, item_class, arguments->values, arguments->count);
446 0 dSP;
447  
448 0 ▶ if (!item) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!item) return; 0 0 0
449 0 ENTER;
450 0 SAVETMPS;
451 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
452 0 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 0 0
453 0 PUSHs(frame->handler);
454 0 PUSHs(sv_2mortal(item));
455 0 PUTBACK;
456 0 call_method(method, G_DISCARD | G_VOID | G_EVAL);
457 0 ▶ if (SvTRUE(ERRSV)) keep_exception(aTHX_ frame);
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) keep_exception(aTHX_ frame); 0 0 0
if (SvTRUE(ERRSV)) keep_exception(aTHX_ frame); 0 0 0
458 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
459 0 LEAVE;
460 }
461  
462 0 static void on_tick(void *user_data, const tfdemo_parser *parser) {
463 dTHX;
464 0 frame_context *frame = (frame_context *)user_data;
465 0 item_arguments arguments = {{0}, 0};
466  
467 0 ▶ if (frame->error) return;
Branch: 1 branch
BranchTrueFalseTotal
if (frame->error) return; 0 0 0
468 0 add_ticks(aTHX_ &arguments, parser);
469 0 deliver(aTHX_ frame, "on_tick", ITEM_CLASS("Tick"), &arguments);
470 }
471  
472 0 static void on_game_event(void *user_data, const tfdemo_parser *parser, const tfdemo_game_event *event) {
473 dTHX;
474 0 frame_context *frame = (frame_context *)user_data;
475 0 item_arguments arguments = {{0}, 0};
476 HV *values;
477 HV *seen;
478 size_t key;
479  
480 0 ▶ if (frame->error) return;
Branch: 1 branch
BranchTrueFalseTotal
if (frame->error) return; 0 0 0
481 0 values = newHV();
482 0 seen = newHV();
483 0 ▶ for (key = 0; key < tfdemo_event_key_count(event); ++key) {
Branch: 1 branch
BranchTrueFalseTotal
for (key = 0; key < tfdemo_event_key_count(event); ++key) { 0 0 0
484 0 const char *name = tfdemo_event_key_name(event, key);
485 0 ▶ SV *key_sv = new_unique_key(aTHX_ seen, name ? name : "", name ? strlen(name) : 0);
Branch: 2 branches
BranchTrueFalseTotal
SV *key_sv = new_unique_key(aTHX_ seen, name ? name : "", name ? strlen(name) : 0); 0 0 0
SV *key_sv = new_unique_key(aTHX_ seen, name ? name : "", name ? strlen(name) : 0); 0 0 0
486 SV *value;
487 0 switch (tfdemo_event_key_type(event, key)) {
488 0 case TFDEMO_EVENT_STRING: value = new_demo_cstring(aTHX_ tfdemo_event_string_at(event, key)); break;
489 0 case TFDEMO_EVENT_FLOAT: value = newSVnv((NV)tfdemo_event_float_at(event, key, 0.0f)); break;
490 0 default: value = newSViv((IV)tfdemo_event_int_at(event, key, 0)); break;
491 }
492 0 (void)hv_store_ent(values, key_sv, value, 0);
493 0 SvREFCNT_dec(key_sv);
494 }
495 0 SvREFCNT_dec((SV *)seen);
496 0 add_ticks(aTHX_ &arguments, parser);
497 0 add_argument(aTHX_ &arguments, "name", new_demo_cstring(aTHX_ tfdemo_event_name(event)));
498 0 add_argument(aTHX_ &arguments, "id", newSViv((IV)tfdemo_event_id(event)));
499 0 add_argument(aTHX_ &arguments, "values", newRV_noinc((SV *)values));
500 0 deliver(aTHX_ frame, "on_game_event", ITEM_CLASS("GameEvent"), &arguments);
501 }
502  
503 /* The chat line of a user message as a Games::TF2::Demo::Chat, undef for
504 * any other message. */
505 0 static SV *new_chat(pTHX_ frame_context *frame, const tfdemo_user_message *message) {
506 tfdemo_chat chat;
507 SV *arguments[8];
508 SV *object;
509  
510 0 ▶ if (tfdemo_user_message_chat(message, &chat) != TFDEMO_OK) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (tfdemo_user_message_chat(message, &chat) != TFDEMO_OK) return newSV(0); 0 0 0
511 0 arguments[0] = newSVpvs("kind");
512 0 arguments[1] = new_demo_cstring(aTHX_ chat.kind);
513 0 arguments[2] = newSVpvs("client");
514 0 arguments[3] = newSViv((IV)chat.client);
515 0 arguments[4] = newSVpvs("from");
516 0 arguments[5] = new_demo_cstring(aTHX_ chat.sender);
517 0 arguments[6] = newSVpvs("text");
518 0 arguments[7] = new_demo_cstring(aTHX_ chat.text);
519 0 object = construct(aTHX_ frame, CHAT_CLASS, arguments, 8);
520 0 ▶ return object ? object : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
return object ? object : newSV(0); 0 0 0
521 }
522  
523 0 static void on_user_message(void *user_data, const tfdemo_parser *parser, const tfdemo_user_message *message) {
524 dTHX;
525 0 frame_context *frame = (frame_context *)user_data;
526 0 item_arguments arguments = {{0}, 0};
527 0 size_t bytes = 0;
528 0 size_t bits = 0;
529 const uint8_t *data;
530  
531 0 ▶ if (frame->error) return;
Branch: 1 branch
BranchTrueFalseTotal
if (frame->error) return; 0 0 0
532 0 data = tfdemo_user_message_data(message, &bytes, &bits);
533 0 add_ticks(aTHX_ &arguments, parser);
534 0 add_argument(aTHX_ &arguments, "type", newSViv((IV)tfdemo_user_message_type(message)));
535 0 add_argument(aTHX_ &arguments, "name", new_demo_cstring(aTHX_ tfdemo_user_message_name(message)));
536 0 ▶ add_argument(aTHX_ &arguments, "data", data ? newSVpvn((const char *)data, bytes) : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
add_argument(aTHX_ &arguments, "data", data ? newSVpvn((const char *)data, bytes) : newSVpvs("")); 0 0 0
537 0 add_argument(aTHX_ &arguments, "bits", newSVuv((UV)bits));
538 0 add_argument(aTHX_ &arguments, "chat", new_chat(aTHX_ frame, message));
539 0 deliver(aTHX_ frame, "on_user_message", ITEM_CLASS("UserMessage"), &arguments);
540 }
541  
542 0 static void on_entity(void *user_data, const tfdemo_parser *parser, const tfdemo_entity *entity,
543 tfdemo_entity_change change, const int32_t *changed, size_t changed_count) {
544 dTHX;
545 0 frame_context *frame = (frame_context *)user_data;
546 0 item_arguments arguments = {{0}, 0};
547 0 ▶ const char *kind = (unsigned)change < sizeof(CHANGE_KINDS) / sizeof(CHANGE_KINDS[0]) ? CHANGE_KINDS[change] : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *kind = (unsigned)change < sizeof(CHANGE_KINDS) / sizeof(CHANGE_KINDS[0]) ? CHANGE_KINDS[change] : ""; 0 0 0
548  
549 0 ▶ if (frame->error) return;
Branch: 1 branch
BranchTrueFalseTotal
if (frame->error) return; 0 0 0
550 0 add_ticks(aTHX_ &arguments, parser);
551 0 add_argument(aTHX_ &arguments, "index", newSViv((IV)tfdemo_entity_index(entity)));
552 0 add_argument(aTHX_ &arguments, "serial", newSViv((IV)tfdemo_entity_serial(entity)));
553 0 add_argument(aTHX_ &arguments, "class_name",
554 new_demo_cstring(aTHX_ tfdemo_class_name(parser, tfdemo_entity_class(entity))));
555 0 add_argument(aTHX_ &arguments, "change", newSVpv(kind, 0));
556 0 add_argument(aTHX_ &arguments, "props", new_changed_props(aTHX_ frame, entity, changed, changed_count));
557 0 deliver(aTHX_ frame, "on_entity", ITEM_CLASS("EntityChange"), &arguments);
558 }
559  
560 0 static void on_temp_entity(void *user_data, const tfdemo_parser *parser, const tfdemo_temp_entity *temp_entity) {
561 dTHX;
562 0 frame_context *frame = (frame_context *)user_data;
563 0 item_arguments arguments = {{0}, 0};
564  
565 0 ▶ if (frame->error) return;
Branch: 1 branch
BranchTrueFalseTotal
if (frame->error) return; 0 0 0
566 0 add_ticks(aTHX_ &arguments, parser);
567 0 add_argument(aTHX_ &arguments, "class_name",
568 new_demo_cstring(aTHX_ tfdemo_class_name(parser, tfdemo_temp_entity_class(temp_entity))));
569 0 add_argument(aTHX_ &arguments, "delay", newSVnv((NV)tfdemo_temp_entity_delay(temp_entity)));
570 0 add_argument(aTHX_ &arguments, "props", new_temp_entity_props(aTHX_ frame, temp_entity));
571 0 deliver(aTHX_ frame, "on_temp_entity", ITEM_CLASS("TempEntity"), &arguments);
572 }
573  
574 0 static void on_string_table_changed(
575 void *user_data, const tfdemo_parser *parser, int32_t table_id, int32_t index, int added) {
576 dTHX;
577 0 frame_context *frame = (frame_context *)user_data;
578 0 item_arguments arguments = {{0}, 0};
579  
580 0 ▶ if (frame->error) return;
Branch: 1 branch
BranchTrueFalseTotal
if (frame->error) return; 0 0 0
581 0 add_ticks(aTHX_ &arguments, parser);
582 0 add_argument(aTHX_ &arguments, "table_id", newSViv((IV)table_id));
583 0 add_argument(aTHX_ &arguments, "table", new_demo_cstring(aTHX_ tfdemo_string_table_name(parser, table_id)));
584 0 add_argument(aTHX_ &arguments, "index", newSViv((IV)index));
585 0 add_argument(aTHX_ &arguments, "added", newSViv(added ? 1 : 0));
586 0 deliver(aTHX_ frame, "on_string_table_changed", ITEM_CLASS("StringTableChange"), &arguments);
587 }
588  
589 0 static void on_console_command(void *user_data, const tfdemo_parser *parser, const char *command) {
590 dTHX;
591 0 frame_context *frame = (frame_context *)user_data;
592 0 item_arguments arguments = {{0}, 0};
593  
594 0 ▶ if (frame->error) return;
Branch: 1 branch
BranchTrueFalseTotal
if (frame->error) return; 0 0 0
595 0 add_ticks(aTHX_ &arguments, parser);
596 0 add_argument(aTHX_ &arguments, "command", new_demo_cstring(aTHX_ command));
597 0 deliver(aTHX_ frame, "on_console_command", ITEM_CLASS("ConsoleCommand"), &arguments);
598 }
599  
600 0 static void on_sound(void *user_data, const tfdemo_parser *parser, const tfdemo_sound *sound) {
601 dTHX;
602 0 frame_context *frame = (frame_context *)user_data;
603 0 item_arguments arguments = {{0}, 0};
604  
605 0 ▶ if (frame->error) return;
Branch: 1 branch
BranchTrueFalseTotal
if (frame->error) return; 0 0 0
606 0 add_ticks(aTHX_ &arguments, parser);
607 0 add_argument(aTHX_ &arguments, "entity_index", newSViv((IV)sound->entity_index));
608 0 add_argument(aTHX_ &arguments, "sound_index", newSViv((IV)sound->sound_index));
609 0 add_argument(aTHX_ &arguments, "name", new_demo_cstring(aTHX_ sound->name));
610 0 add_argument(aTHX_ &arguments, "flags", newSVuv((UV)sound->flags));
611 0 add_argument(aTHX_ &arguments, "channel", newSViv((IV)sound->channel));
612 0 add_argument(aTHX_ &arguments, "volume", newSVnv((NV)sound->volume));
613 0 add_argument(aTHX_ &arguments, "sound_level", newSViv((IV)sound->sound_level));
614 0 add_argument(aTHX_ &arguments, "pitch", newSViv((IV)sound->pitch));
615 0 add_argument(aTHX_ &arguments, "delay", newSVnv((NV)sound->delay));
616 0 add_argument(aTHX_ &arguments, "origin", new_vector(aTHX_ frame, sound->origin[0], sound->origin[1], sound->origin[2]));
617 0 add_argument(aTHX_ &arguments, "speaker_entity", newSViv((IV)sound->speaker_entity));
618 0 deliver(aTHX_ frame, "on_sound", ITEM_CLASS("Sound"), &arguments);
619 }
620  
621 0 static void on_message(void *user_data, const tfdemo_parser *parser, int32_t type, size_t bit_offset, size_t bit_length) {
622 dTHX;
623 0 frame_context *frame = (frame_context *)user_data;
624 0 item_arguments arguments = {{0}, 0};
625  
626 0 ▶ if (frame->error) return;
Branch: 1 branch
BranchTrueFalseTotal
if (frame->error) return; 0 0 0
627 0 add_ticks(aTHX_ &arguments, parser);
628 0 add_argument(aTHX_ &arguments, "type", newSViv((IV)type));
629 0 add_argument(aTHX_ &arguments, "name", new_demo_cstring(aTHX_ tfdemo_message_type_name(type)));
630 0 add_argument(aTHX_ &arguments, "bit_offset", newSVuv((UV)bit_offset));
631 0 add_argument(aTHX_ &arguments, "bit_length", newSVuv((UV)bit_length));
632 0 deliver(aTHX_ frame, "on_message", ITEM_CLASS("Message"), &arguments);
633 }
634  
635 /* True when the handler's class has the method (AUTOLOAD not counted). */
636 0 static int implements(pTHX_ HV *stash, const char *method) {
637 0 return gv_fetchmethod_autoload(stash, method, FALSE) != NULL;
638 }
639  
640 /* The handler methods in the order of their bits in a wiring mask; on_message
641 * is set on the parser (tfdemo_set_on_message), the others in tfdemo_callbacks. */
642 static const char *const CALLBACK_METHODS[] = {"on_tick", "on_game_event", "on_user_message", "on_entity",
643 "on_temp_entity", "on_string_table_changed", "on_console_command", "on_sound", "on_message"};
644 #define CALLBACK_COUNT (sizeof(CALLBACK_METHODS) / sizeof(CALLBACK_METHODS[0]))
645 #define ON_MESSAGE_BIT (1u << 8)
646  
647 /* The bits of the methods in `mask` the handler implements; a callback
648 * without a method stays unwired and costs nothing. */
649 0 static unsigned wired_mask(pTHX_ SV *handler, unsigned mask) {
650 HV *stash;
651 0 unsigned wired = 0;
652 size_t i;
653  
654 0 ▶ if (!mask) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!mask) return 0; 0 0 0
655 0 stash = SvSTASH(SvRV(handler));
656 0 ▶ for (i = 0; i < CALLBACK_COUNT; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < CALLBACK_COUNT; ++i) { 0 0 0
657 0 ▶ if ((mask & (1u << i)) && implements(aTHX_ stash, CALLBACK_METHODS[i])) wired |= 1u << i;
Branch: 2 branches
BranchTrueFalseTotal
if ((mask & (1u << i)) && implements(aTHX_ stash, CALLBACK_METHODS[i])) wired |= 1u << i; 0 0 0
if ((mask & (1u << i)) && implements(aTHX_ stash, CALLBACK_METHODS[i])) wired |= 1u << i; 0 0 0
658 }
659 0 return wired;
660 }
661  
662 0 static tfdemo_callbacks wired_callbacks(frame_context *frame, unsigned wired) {
663 tfdemo_callbacks callbacks;
664  
665 0 Zero(&callbacks, 1, tfdemo_callbacks);
666 0 callbacks.user_data = frame;
667 0 ▶ if (wired & (1u << 0)) callbacks.on_tick = on_tick;
Branch: 1 branch
BranchTrueFalseTotal
if (wired & (1u << 0)) callbacks.on_tick = on_tick; 0 0 0
668 0 ▶ if (wired & (1u << 1)) callbacks.on_game_event = on_game_event;
Branch: 1 branch
BranchTrueFalseTotal
if (wired & (1u << 1)) callbacks.on_game_event = on_game_event; 0 0 0
669 0 ▶ if (wired & (1u << 2)) callbacks.on_user_message = on_user_message;
Branch: 1 branch
BranchTrueFalseTotal
if (wired & (1u << 2)) callbacks.on_user_message = on_user_message; 0 0 0
670 0 ▶ if (wired & (1u << 3)) callbacks.on_entity = on_entity;
Branch: 1 branch
BranchTrueFalseTotal
if (wired & (1u << 3)) callbacks.on_entity = on_entity; 0 0 0
671 0 ▶ if (wired & (1u << 4)) callbacks.on_temp_entity = on_temp_entity;
Branch: 1 branch
BranchTrueFalseTotal
if (wired & (1u << 4)) callbacks.on_temp_entity = on_temp_entity; 0 0 0
672 0 ▶ if (wired & (1u << 5)) callbacks.on_string_table_changed = on_string_table_changed;
Branch: 1 branch
BranchTrueFalseTotal
if (wired & (1u << 5)) callbacks.on_string_table_changed = on_string_table_changed; 0 0 0
673 0 ▶ if (wired & (1u << 6)) callbacks.on_console_command = on_console_command;
Branch: 1 branch
BranchTrueFalseTotal
if (wired & (1u << 6)) callbacks.on_console_command = on_console_command; 0 0 0
674 0 ▶ if (wired & (1u << 7)) callbacks.on_sound = on_sound;
Branch: 1 branch
BranchTrueFalseTotal
if (wired & (1u << 7)) callbacks.on_sound = on_sound; 0 0 0
675 0 return callbacks;
676 }
677  
678 /*
679 * Handles one frame, or every frame up to the end, calling the handler's
680 * methods of `mask` it implements (no handler: mask 0). A handler's
681 * exception ends the frame's callbacks and is thrown once the frame is
682 * done, before a failure of the library in the same frame. Returns true
683 * while frames follow.
684 */
685 0 static int handle_frames(pTHX_ demo_parser *demo, SV *handler, unsigned mask, int to_the_end) {
686 frame_context frame;
687 tfdemo_callbacks callbacks;
688 tfdemo_status status;
689 unsigned wired;
690 0 int more = 0;
691  
692 0 ▶ if (!demo->parser) throw_message(aTHX_ TFDEMO_ERROR_STATE, "Games::TF2::Demo: a connection has no frames to step through");
Branch: 1 branch
BranchTrueFalseTotal
if (!demo->parser) throw_message(aTHX_ TFDEMO_ERROR_STATE, "Games::TF2::Demo: a connection has no frames to step through"); 0 0 0
693 0 ▶ if (demo->stepping) throw_message(aTHX_ TFDEMO_ERROR_STATE, "Games::TF2::Demo: step and run cannot be called from a handler");
Branch: 1 branch
BranchTrueFalseTotal
if (demo->stepping) throw_message(aTHX_ TFDEMO_ERROR_STATE, "Games::TF2::Demo: step and run cannot be called from a handler"); 0 0 0
694 0 ▶ if (mask && (!SvROK(handler) || !SvOBJECT(SvRV(handler)))) throw_message(aTHX_ TFDEMO_ERROR_ARGUMENT, "Games::TF2::Demo: the handler must be an object");
Branch: 3 branches
BranchTrueFalseTotal
if (mask && (!SvROK(handler) || !SvOBJECT(SvRV(handler)))) throw_message(aTHX_ TFDEMO_ERROR_ARGUMENT, "Games::TF2::Demo: the handler must be an object"); 0 0 0
if (mask && (!SvROK(handler) || !SvOBJECT(SvRV(handler)))) throw_message(aTHX_ TFDEMO_ERROR_ARGUMENT, "Games::TF2::Demo: the handler must be an object"); 0 0 0
if (mask && (!SvROK(handler) || !SvOBJECT(SvRV(handler)))) throw_message(aTHX_ TFDEMO_ERROR_ARGUMENT, "Games::TF2::Demo: the handler must be an object"); 0 0 0
695 0 frame.demo = demo;
696 0 frame.handler = handler;
697 0 frame.error = NULL;
698 0 wired = wired_mask(aTHX_ handler, mask);
699 0 callbacks = wired_callbacks(&frame, wired);
700 0 ▶ if (wired & ON_MESSAGE_BIT) (void)tfdemo_set_on_message(demo->parser, on_message, &frame);
Branch: 1 branch
BranchTrueFalseTotal
if (wired & ON_MESSAGE_BIT) (void)tfdemo_set_on_message(demo->parser, on_message, &frame); 0 0 0
701 0 demo->stepping = 1;
702 do {
703 0 status = tfdemo_step(demo->parser, &callbacks, &more);
704 0 ▶ } while (to_the_end && status == TFDEMO_OK && more && !frame.error);
Branch: 4 branches
BranchTrueFalseTotal
} while (to_the_end && status == TFDEMO_OK && more && !frame.error); 0 0 0
} while (to_the_end && status == TFDEMO_OK && more && !frame.error); 0 0 0
} while (to_the_end && status == TFDEMO_OK && more && !frame.error); 0 0 0
} while (to_the_end && status == TFDEMO_OK && more && !frame.error); 0 0 0
705 0 demo->stepping = 0;
706 0 ▶ if (wired & ON_MESSAGE_BIT) {
Branch: 1 branch
BranchTrueFalseTotal
if (wired & ON_MESSAGE_BIT) { 0 0 0
707 /* Kept before tfdemo_set_on_message clears tfdemo_last_error. */
708 0 ▶ SV *message = status == TFDEMO_OK ? NULL : sv_2mortal(new_demo_cstring(aTHX_ tfdemo_last_error()));
Branch: 1 branch
BranchTrueFalseTotal
SV *message = status == TFDEMO_OK ? NULL : sv_2mortal(new_demo_cstring(aTHX_ tfdemo_last_error())); 0 0 0
709 0 (void)tfdemo_set_on_message(demo->parser, NULL, NULL);
710 0 ▶ if (frame.error) croak_sv(sv_2mortal(frame.error));
Branch: 1 branch
BranchTrueFalseTotal
if (frame.error) croak_sv(sv_2mortal(frame.error)); 0 0 0
711 0 ▶ if (message) throw_status_message(aTHX_ status, message);
Branch: 1 branch
BranchTrueFalseTotal
if (message) throw_status_message(aTHX_ status, message); 0 0 0
712 }
713 0 ▶ if (frame.error) croak_sv(sv_2mortal(frame.error));
Branch: 1 branch
BranchTrueFalseTotal
if (frame.error) croak_sv(sv_2mortal(frame.error)); 0 0 0
714 0 ▶ if (status != TFDEMO_OK) throw_status(aTHX_ status);
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) throw_status(aTHX_ status); 0 0 0
715 0 return more;
716 }
717  
718 /* ------------------------------------------------------------------ handles */
719  
720 0 static demo_parser *demo_of(pTHX_ SV *self) {
721 0 ▶ if (!sv_isobject(self) || !sv_derived_from(self, PARSER_CLASS)) croak("Games::TF2::Demo: not a " PARSER_CLASS);
Branch: 2 branches
BranchTrueFalseTotal
if (!sv_isobject(self) || !sv_derived_from(self, PARSER_CLASS)) croak("Games::TF2::Demo: not a " PARSER_CLASS); 0 0 0
if (!sv_isobject(self) || !sv_derived_from(self, PARSER_CLASS)) croak("Games::TF2::Demo: not a " PARSER_CLASS); 0 0 0
722 0 return INT2PTR(demo_parser *, SvIV(SvRV(self)));
723 }
724  
725 /* A handle of a demo's parser or of a connection (the other NULL). */
726 0 static SV *new_handle(pTHX_ const char *class_name, tfdemo_parser *parser, tfnet_connection *connection) {
727 demo_parser *demo;
728 SV *handle;
729  
730 0 Newxz(demo, 1, demo_parser);
731 0 demo->parser = parser;
732 0 demo->connection = connection;
733 0 ▶ demo->state = parser ? parser : tfnet_state(connection);
Branch: 1 branch
BranchTrueFalseTotal
demo->state = parser ? parser : tfnet_state(connection); 0 0 0
734 0 demo->class_keys = newAV();
735 0 handle = newSV(0);
736 0 sv_setref_pv(handle, class_name, (void *)demo);
737 0 return handle;
738 }
739  
740 0 static tfdemo_options options_of(int entities, int temp_entities, int sounds, int summary) {
741 0 tfdemo_options options = tfdemo_default_options();
742 0 options.entities = entities ? 1 : 0;
743 0 options.temp_entities = temp_entities ? 1 : 0;
744 0 options.sounds = sounds ? 1 : 0;
745 0 options.summary = summary ? 1 : 0;
746 0 return options;
747 }
748  
749 0 static void store(pTHX_ HV *hash, const char *key, SV *value) {
750 0 (void)hv_store(hash, key, (I32)strlen(key), value, 0);
751 0 }
752  
753 /* ---------------------------------------------------------------- snapshots */
754  
755 #define STORE_IV(hash, object, field) store(aTHX_ (hash), #field, newSViv((IV)(object)->field))
756 #define STORE_UV(hash, object, field) store(aTHX_ (hash), #field, newSVuv((UV)(object)->field))
757 #define STORE_NV(hash, object, field) store(aTHX_ (hash), #field, newSVnv((NV)(object)->field))
758 #define STORE_BOOL(hash, object, field) store(aTHX_ (hash), #field, newSViv((object)->field ? 1 : 0))
759 #define STORE_STRING(hash, object, field) store(aTHX_ (hash), #field, new_demo_cstring(aTHX_ (object)->field))
760 #define STORE_VECTOR(frame, hash, object, field) \
761 store(aTHX_ (hash), #field, new_vector(aTHX_ (frame), (object)->field[0], (object)->field[1], (object)->field[2]))
762  
763 0 static SV *hash_ref(pTHX_ HV *hash) {
764 0 return newRV_noinc((SV *)hash);
765 }
766  
767 0 static SV *new_player_info(pTHX_ const tfdemo_player_info *player) {
768 0 HV *fields = newHV();
769 0 STORE_IV(fields, player, entity_index);
770 0 STORE_IV(fields, player, user_id);
771 0 STORE_STRING(fields, player, name);
772 0 STORE_STRING(fields, player, steam_id);
773 0 STORE_BOOL(fields, player, is_bot);
774 0 STORE_BOOL(fields, player, is_hltv);
775 0 return hash_ref(aTHX_ fields);
776 }
777  
778 /* A player lookup: the player's fields, undef for an unknown player
779 * (TFDEMO_ERROR_STATE); another failure is thrown. */
780 0 static SV *player_or_undef(pTHX_ tfdemo_status status, const tfdemo_player_info *player) {
781 0 ▶ if (status == TFDEMO_ERROR_STATE) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (status == TFDEMO_ERROR_STATE) return newSV(0); 0 0 0
782 0 ▶ if (status != TFDEMO_OK) throw_status(aTHX_ status);
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) throw_status(aTHX_ status); 0 0 0
783 0 return new_player_info(aTHX_ player);
784 }
785  
786 0 static SV *new_player_state(pTHX_ frame_context *frame, const tfdemo_player_state *player) {
787 0 HV *fields = newHV();
788 0 AV *conditions = newAV();
789 size_t i;
790  
791 0 ▶ for (i = 0; i < sizeof(player->conditions) / sizeof(player->conditions[0]); ++i) av_push(conditions, newSVuv((UV)player->conditions[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof(player->conditions) / sizeof(player->conditions[0]); ++i) av_push(conditions, newSVuv((UV)player->conditions[i])); 0 0 0
792 0 STORE_IV(fields, player, entity_index);
793 0 STORE_IV(fields, player, user_id);
794 0 STORE_STRING(fields, player, name);
795 0 STORE_STRING(fields, player, steam_id);
796 0 STORE_IV(fields, player, team);
797 0 STORE_IV(fields, player, player_class);
798 0 STORE_BOOL(fields, player, alive);
799 0 STORE_BOOL(fields, player, in_pvs);
800 0 STORE_IV(fields, player, health);
801 0 STORE_IV(fields, player, max_health);
802 0 STORE_VECTOR(frame, fields, player, origin);
803 0 STORE_NV(fields, player, pitch);
804 0 STORE_NV(fields, player, yaw);
805 0 STORE_NV(fields, player, ubercharge);
806 0 STORE_NV(fields, player, cloak);
807 0 store(aTHX_ fields, "conditions", newRV_noinc((SV *)conditions));
808 0 STORE_IV(fields, player, ping);
809 0 STORE_IV(fields, player, score);
810 0 STORE_IV(fields, player, deaths);
811 0 STORE_IV(fields, player, damage);
812 0 return hash_ref(aTHX_ fields);
813 }
814  
815 0 static SV *new_building_state(pTHX_ frame_context *frame, const tfdemo_building_state *building) {
816 0 HV *fields = newHV();
817 0 STORE_IV(fields, building, entity_index);
818 0 STORE_IV(fields, building, kind);
819 0 STORE_IV(fields, building, team);
820 0 STORE_IV(fields, building, builder);
821 0 STORE_IV(fields, building, level);
822 0 STORE_IV(fields, building, health);
823 0 STORE_IV(fields, building, max_health);
824 0 STORE_VECTOR(frame, fields, building, origin);
825 0 STORE_NV(fields, building, yaw);
826 0 STORE_BOOL(fields, building, under_construction);
827 0 STORE_NV(fields, building, construction);
828 0 STORE_BOOL(fields, building, sapped);
829 0 STORE_BOOL(fields, building, mini);
830 0 return hash_ref(aTHX_ fields);
831 }
832  
833 0 static SV *new_projectile_state(pTHX_ frame_context *frame, const tfdemo_projectile_state *projectile) {
834 0 HV *fields = newHV();
835 0 STORE_IV(fields, projectile, entity_index);
836 0 STORE_STRING(fields, projectile, class_name);
837 0 STORE_IV(fields, projectile, kind);
838 0 STORE_IV(fields, projectile, team);
839 0 STORE_VECTOR(frame, fields, projectile, origin);
840 0 STORE_VECTOR(frame, fields, projectile, angles);
841 0 STORE_VECTOR(frame, fields, projectile, initial_velocity);
842 0 STORE_BOOL(fields, projectile, critical);
843 0 STORE_IV(fields, projectile, deflected);
844 0 STORE_IV(fields, projectile, launcher);
845 0 STORE_IV(fields, projectile, owner);
846 0 return hash_ref(aTHX_ fields);
847 }
848  
849 0 static SV *new_control_point_state(pTHX_ frame_context *frame, const tfdemo_control_point_state *point) {
850 0 HV *fields = newHV();
851 0 STORE_IV(fields, point, index);
852 0 STORE_IV(fields, point, owner);
853 0 STORE_IV(fields, point, capping_team);
854 0 STORE_NV(fields, point, progress);
855 0 STORE_BOOL(fields, point, locked);
856 0 STORE_BOOL(fields, point, visible);
857 0 STORE_IV(fields, point, cappers);
858 0 STORE_VECTOR(frame, fields, point, position);
859 0 return hash_ref(aTHX_ fields);
860 }
861  
862 0 static SV *new_cart_state(pTHX_ frame_context *frame, const tfdemo_cart_state *cart) {
863 0 HV *fields = newHV();
864 0 STORE_IV(fields, cart, entity_index);
865 0 STORE_IV(fields, cart, team);
866 0 STORE_NV(fields, cart, progress);
867 0 STORE_IV(fields, cart, speed_level);
868 0 STORE_IV(fields, cart, cappers);
869 0 STORE_NV(fields, cart, recede_time);
870 0 STORE_VECTOR(frame, fields, cart, origin);
871 0 return hash_ref(aTHX_ fields);
872 }
873  
874 /* Each list of a game state: its count, its getter and the hash of an element. */
875 #define GAME_STATE_LIST(fields, name, state, frame, kind, count_of, get, make) \
876 do { \
877 AV *list = newAV(); \
878 size_t index; \
879 store(aTHX_ (fields), (name), newRV_noinc((SV *)list)); \
880 for (index = 0; index < count_of(state) && status == TFDEMO_OK; ++index) { \
881 kind element; \
882 status = get((state), index, &element); \
883 if (status == TFDEMO_OK) av_push(list, make(aTHX_ (frame), &element)); \
884 } \
885 } while (0)
886  
887 /* The game state now, as a hash reference of plain values and Vectors. */
888 0 static SV *new_game_state(pTHX_ demo_parser *demo) {
889 0 frame_context frame = {demo, NULL, NULL};
890 0 tfdemo_game_state *state = NULL;
891 0 tfdemo_status status = tfdemo_read_game_state(demo->state, &state);
892 HV *fields;
893 float min[3];
894 float max[3];
895  
896 0 ▶ if (status != TFDEMO_OK) throw_status(aTHX_ status);
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) throw_status(aTHX_ status); 0 0 0
897 0 fields = (HV *)sv_2mortal((SV *)newHV());
898 0 status = tfdemo_game_state_world(state, min, max);
899 0 store(aTHX_ fields, "demo_tick", newSViv((IV)tfdemo_demo_tick(demo->state)));
900 0 store(aTHX_ fields, "server_tick", newSViv((IV)tfdemo_server_tick(demo->state)));
901 0 store(aTHX_ fields, "world_min", new_vector(aTHX_ &frame, min[0], min[1], min[2]));
902 0 store(aTHX_ fields, "world_max", new_vector(aTHX_ &frame, max[0], max[1], max[2]));
903 0 ▶ GAME_STATE_LIST(fields, "players", state, &frame, tfdemo_player_state, tfdemo_game_state_player_count,
Branch: 3 branches
BranchTrueFalseTotal
GAME_STATE_LIST(fields, "players", state, &frame, tfdemo_player_state, tfdemo_game_state_player_count, 0 0 0
GAME_STATE_LIST(fields, "players", state, &frame, tfdemo_player_state, tfdemo_game_state_player_count, 0 0 0
GAME_STATE_LIST(fields, "players", state, &frame, tfdemo_player_state, tfdemo_game_state_player_count, 0 0 0
904 tfdemo_game_state_player, new_player_state);
905 0 ▶ GAME_STATE_LIST(fields, "buildings", state, &frame, tfdemo_building_state, tfdemo_game_state_building_count,
Branch: 3 branches
BranchTrueFalseTotal
GAME_STATE_LIST(fields, "buildings", state, &frame, tfdemo_building_state, tfdemo_game_state_building_count, 0 0 0
GAME_STATE_LIST(fields, "buildings", state, &frame, tfdemo_building_state, tfdemo_game_state_building_count, 0 0 0
GAME_STATE_LIST(fields, "buildings", state, &frame, tfdemo_building_state, tfdemo_game_state_building_count, 0 0 0
906 tfdemo_game_state_building, new_building_state);
907 0 ▶ GAME_STATE_LIST(fields, "projectiles", state, &frame, tfdemo_projectile_state,
Branch: 3 branches
BranchTrueFalseTotal
GAME_STATE_LIST(fields, "projectiles", state, &frame, tfdemo_projectile_state, 0 0 0
GAME_STATE_LIST(fields, "projectiles", state, &frame, tfdemo_projectile_state, 0 0 0
GAME_STATE_LIST(fields, "projectiles", state, &frame, tfdemo_projectile_state, 0 0 0
908 tfdemo_game_state_projectile_count, tfdemo_game_state_projectile, new_projectile_state);
909 0 ▶ GAME_STATE_LIST(fields, "control_points", state, &frame, tfdemo_control_point_state,
Branch: 3 branches
BranchTrueFalseTotal
GAME_STATE_LIST(fields, "control_points", state, &frame, tfdemo_control_point_state, 0 0 0
GAME_STATE_LIST(fields, "control_points", state, &frame, tfdemo_control_point_state, 0 0 0
GAME_STATE_LIST(fields, "control_points", state, &frame, tfdemo_control_point_state, 0 0 0
910 tfdemo_game_state_control_point_count, tfdemo_game_state_control_point, new_control_point_state);
911 0 ▶ GAME_STATE_LIST(fields, "carts", state, &frame, tfdemo_cart_state, tfdemo_game_state_cart_count,
Branch: 3 branches
BranchTrueFalseTotal
GAME_STATE_LIST(fields, "carts", state, &frame, tfdemo_cart_state, tfdemo_game_state_cart_count, 0 0 0
GAME_STATE_LIST(fields, "carts", state, &frame, tfdemo_cart_state, tfdemo_game_state_cart_count, 0 0 0
GAME_STATE_LIST(fields, "carts", state, &frame, tfdemo_cart_state, tfdemo_game_state_cart_count, 0 0 0
912 tfdemo_game_state_cart, new_cart_state);
913 0 ▶ if (status != TFDEMO_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) { 0 0 0
914 0 SV *message = sv_2mortal(new_demo_cstring(aTHX_ tfdemo_last_error()));
915 0 tfdemo_game_state_free(state);
916 0 throw_status_message(aTHX_ status, message);
917 }
918 0 tfdemo_game_state_free(state);
919 0 ▶ if (frame.error) croak_sv(sv_2mortal(frame.error));
Branch: 1 branch
BranchTrueFalseTotal
if (frame.error) croak_sv(sv_2mortal(frame.error)); 0 0 0
920 0 return newRV_inc((SV *)fields);
921 }
922  
923 0 static SV *new_player_scores(pTHX_ const tfdemo_player_scores *scores) {
924 0 HV *fields = newHV();
925 0 STORE_IV(fields, scores, points);
926 0 STORE_IV(fields, scores, kills);
927 0 STORE_IV(fields, scores, deaths);
928 0 STORE_IV(fields, scores, damage);
929 0 STORE_IV(fields, scores, damage_assist);
930 0 STORE_IV(fields, scores, healing);
931 0 STORE_IV(fields, scores, healing_assist);
932 0 STORE_IV(fields, scores, bonus_points);
933 0 STORE_IV(fields, scores, damage_blocked);
934 0 STORE_IV(fields, scores, support);
935 0 return hash_ref(aTHX_ fields);
936 }
937  
938 0 static SV *new_score_data(pTHX_ const tfdemo_score_data *scores) {
939 0 HV *fields = newHV();
940 0 STORE_IV(fields, scores, captures);
941 0 STORE_IV(fields, scores, defenses);
942 0 STORE_IV(fields, scores, kills);
943 0 STORE_IV(fields, scores, deaths);
944 0 STORE_IV(fields, scores, suicides);
945 0 STORE_IV(fields, scores, dominations);
946 0 STORE_IV(fields, scores, revenges);
947 0 STORE_IV(fields, scores, buildings_built);
948 0 STORE_IV(fields, scores, buildings_destroyed);
949 0 STORE_IV(fields, scores, headshots);
950 0 STORE_IV(fields, scores, backstabs);
951 0 STORE_IV(fields, scores, healing);
952 0 STORE_IV(fields, scores, ubercharges);
953 0 STORE_IV(fields, scores, teleports);
954 0 STORE_IV(fields, scores, damage);
955 0 STORE_IV(fields, scores, crits);
956 0 STORE_IV(fields, scores, resupplies);
957 0 STORE_IV(fields, scores, assists);
958 0 STORE_IV(fields, scores, bonus_points);
959 0 STORE_IV(fields, scores, points);
960 0 return hash_ref(aTHX_ fields);
961 }
962  
963 0 static SV *new_summary_user(pTHX_ const tfdemo_summary_user_info *user, const int32_t spawns[10]) {
964 0 HV *fields = newHV();
965 0 AV *classes = newAV();
966 int i;
967  
968 0 ▶ for (i = 0; i < 10; ++i) av_push(classes, newSViv((IV)spawns[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 10; ++i) av_push(classes, newSViv((IV)spawns[i])); 0 0 0
969 0 STORE_IV(fields, user, user_id);
970 0 STORE_IV(fields, user, entity_index);
971 0 STORE_STRING(fields, user, name);
972 0 STORE_STRING(fields, user, steam_id);
973 0 STORE_BOOL(fields, user, is_bot);
974 0 STORE_IV(fields, user, team);
975 0 store(aTHX_ fields, "spawns", newRV_noinc((SV *)classes));
976 0 store(aTHX_ fields, "scores", new_player_scores(aTHX_ &user->scores));
977 0 store(aTHX_ fields, "match_scores", new_score_data(aTHX_ &user->match_scores));
978 0 store(aTHX_ fields, "round_scores", new_score_data(aTHX_ &user->round_scores));
979 0 STORE_BOOL(fields, user, scoreboard_sent);
980 0 return hash_ref(aTHX_ fields);
981 }
982  
983 0 static SV *new_chat_line(pTHX_ const tfdemo_chat *chat, int32_t tick) {
984 0 HV *fields = newHV();
985 0 store(aTHX_ fields, "tick", newSViv((IV)tick));
986 0 STORE_STRING(fields, chat, kind);
987 0 STORE_IV(fields, chat, client);
988 0 store(aTHX_ fields, "from", new_demo_cstring(aTHX_ chat->sender));
989 0 STORE_STRING(fields, chat, text);
990 0 return hash_ref(aTHX_ fields);
991 }
992  
993 0 static SV *new_death(pTHX_ const tfdemo_death_info *death) {
994 0 HV *fields = newHV();
995 0 STORE_IV(fields, death, tick);
996 0 STORE_IV(fields, death, victim);
997 0 STORE_IV(fields, death, attacker);
998 0 STORE_BOOL(fields, death, has_assister);
999 0 STORE_IV(fields, death, assister);
1000 0 STORE_STRING(fields, death, weapon);
1001 0 STORE_IV(fields, death, custom_kill);
1002 0 STORE_IV(fields, death, death_flags);
1003 0 STORE_BOOL(fields, death, feign_death);
1004 0 return hash_ref(aTHX_ fields);
1005 }
1006  
1007 0 static SV *new_round(pTHX_ const tfdemo_round_info *round) {
1008 0 HV *fields = newHV();
1009 0 STORE_IV(fields, round, end_tick);
1010 0 STORE_IV(fields, round, winner);
1011 0 STORE_IV(fields, round, win_reason);
1012 0 STORE_NV(fields, round, length);
1013 0 return hash_ref(aTHX_ fields);
1014 }
1015  
1016 0 static SV *new_pause(pTHX_ const tfdemo_pause_info *pause) {
1017 0 HV *fields = newHV();
1018 0 STORE_IV(fields, pause, from);
1019 0 STORE_IV(fields, pause, to);
1020 0 return hash_ref(aTHX_ fields);
1021 }
1022  
1023 0 static SV *new_direct_hit(pTHX_ const tfdemo_direct_hit_info *hit) {
1024 0 HV *fields = newHV();
1025 0 STORE_IV(fields, hit, tick);
1026 0 STORE_IV(fields, hit, victim);
1027 0 STORE_IV(fields, hit, attacker);
1028 0 STORE_IV(fields, hit, victim_user_id);
1029 0 STORE_IV(fields, hit, attacker_user_id);
1030 0 STORE_IV(fields, hit, kind);
1031 0 STORE_BOOL(fields, hit, critical);
1032 0 STORE_IV(fields, hit, damage);
1033 0 return hash_ref(aTHX_ fields);
1034 }
1035  
1036 /* Appends a hash per element of a summary list; throws when a getter fails. */
1037 #define SUMMARY_LIST(fields, name, parser, kind, count_of, get, make) \
1038 do { \
1039 AV *list = newAV(); \
1040 size_t index; \
1041 store(aTHX_ (fields), (name), newRV_noinc((SV *)list)); \
1042 for (index = 0; index < count_of(parser); ++index) { \
1043 kind element; \
1044 const tfdemo_status status = get((parser), index, &element); \
1045 if (status != TFDEMO_OK) throw_status(aTHX_ status); \
1046 av_push(list, make(aTHX_ &element)); \
1047 } \
1048 } while (0)
1049  
1050 /* The match summary so far, as a hash reference of plain values. */
1051 0 static SV *new_summary(pTHX_ demo_parser *demo) {
1052 0 const tfdemo_parser *parser = demo->state;
1053 0 HV *fields = (HV *)sv_2mortal((SV *)newHV());
1054 0 AV *users = newAV();
1055 0 AV *chat = newAV();
1056 size_t index;
1057  
1058 0 store(aTHX_ fields, "users", newRV_noinc((SV *)users));
1059 0 ▶ for (index = 0; index < tfdemo_summary_user_count(parser); ++index) {
Branch: 1 branch
BranchTrueFalseTotal
for (index = 0; index < tfdemo_summary_user_count(parser); ++index) { 0 0 0
1060 tfdemo_summary_user_info user;
1061 int32_t spawns[10];
1062 0 tfdemo_status status = tfdemo_summary_user(parser, index, &user);
1063 0 ▶ if (status == TFDEMO_OK) status = tfdemo_summary_user_spawns(parser, index, spawns);
Branch: 1 branch
BranchTrueFalseTotal
if (status == TFDEMO_OK) status = tfdemo_summary_user_spawns(parser, index, spawns); 0 0 0
1064 0 ▶ if (status != TFDEMO_OK) throw_status(aTHX_ status);
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) throw_status(aTHX_ status); 0 0 0
1065 0 av_push(users, new_summary_user(aTHX_ &user, spawns));
1066 }
1067 0 store(aTHX_ fields, "chat", newRV_noinc((SV *)chat));
1068 0 ▶ for (index = 0; index < tfdemo_summary_chat_count(parser); ++index) {
Branch: 1 branch
BranchTrueFalseTotal
for (index = 0; index < tfdemo_summary_chat_count(parser); ++index) { 0 0 0
1069 tfdemo_chat line;
1070 0 int32_t tick = 0;
1071 0 const tfdemo_status status = tfdemo_summary_chat(parser, index, &line, &tick);
1072 0 ▶ if (status != TFDEMO_OK) throw_status(aTHX_ status);
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) throw_status(aTHX_ status); 0 0 0
1073 0 av_push(chat, new_chat_line(aTHX_ &line, tick));
1074 }
1075 0 ▶ SUMMARY_LIST(fields, "deaths", parser, tfdemo_death_info, tfdemo_summary_death_count, tfdemo_summary_death, new_death);
Branch: 2 branches
BranchTrueFalseTotal
SUMMARY_LIST(fields, "deaths", parser, tfdemo_death_info, tfdemo_summary_death_count, tfdemo_summary_death, new_death); 0 0 0
SUMMARY_LIST(fields, "deaths", parser, tfdemo_death_info, tfdemo_summary_death_count, tfdemo_summary_death, new_death); 0 0 0
1076 0 ▶ SUMMARY_LIST(fields, "rounds", parser, tfdemo_round_info, tfdemo_summary_round_count, tfdemo_summary_round, new_round);
Branch: 2 branches
BranchTrueFalseTotal
SUMMARY_LIST(fields, "rounds", parser, tfdemo_round_info, tfdemo_summary_round_count, tfdemo_summary_round, new_round); 0 0 0
SUMMARY_LIST(fields, "rounds", parser, tfdemo_round_info, tfdemo_summary_round_count, tfdemo_summary_round, new_round); 0 0 0
1077 0 ▶ SUMMARY_LIST(fields, "pauses", parser, tfdemo_pause_info, tfdemo_summary_pause_count, tfdemo_summary_pause, new_pause);
Branch: 2 branches
BranchTrueFalseTotal
SUMMARY_LIST(fields, "pauses", parser, tfdemo_pause_info, tfdemo_summary_pause_count, tfdemo_summary_pause, new_pause); 0 0 0
SUMMARY_LIST(fields, "pauses", parser, tfdemo_pause_info, tfdemo_summary_pause_count, tfdemo_summary_pause, new_pause); 0 0 0
1078 0 ▶ SUMMARY_LIST(fields, "direct_hits", parser, tfdemo_direct_hit_info, tfdemo_direct_hit_count, tfdemo_direct_hit, new_direct_hit);
Branch: 2 branches
BranchTrueFalseTotal
SUMMARY_LIST(fields, "direct_hits", parser, tfdemo_direct_hit_info, tfdemo_direct_hit_count, tfdemo_direct_hit, new_direct_hit); 0 0 0
SUMMARY_LIST(fields, "direct_hits", parser, tfdemo_direct_hit_info, tfdemo_direct_hit_count, tfdemo_direct_hit, new_direct_hit); 0 0 0
1079 0 store(aTHX_ fields, "start_tick", newSViv((IV)tfdemo_summary_start_tick(parser)));
1080 0 store(aTHX_ fields, "tick_interval", newSVnv((NV)tfdemo_tick_interval(parser)));
1081 0 return newRV_inc((SV *)fields);
1082 }
1083  
1084 /* A copy of a string table: its name and each entry's text and user data. */
1085 0 static SV *new_string_table(pTHX_ const tfdemo_parser *parser, int32_t table_id) {
1086 0 HV *fields = newHV();
1087 0 AV *texts = newAV();
1088 0 AV *user_data = newAV();
1089 0 const int32_t size = tfdemo_string_table_size(parser, table_id);
1090 int32_t index;
1091  
1092 0 ▶ for (index = 0; index < size; ++index) {
Branch: 1 branch
BranchTrueFalseTotal
for (index = 0; index < size; ++index) { 0 0 0
1093 0 size_t length = 0;
1094 0 const uint8_t *data = tfdemo_string_table_user_data(parser, table_id, index, &length);
1095 0 av_push(texts, new_demo_cstring(aTHX_ tfdemo_string_table_text(parser, table_id, index)));
1096 0 ▶ av_push(user_data, data ? newSVpvn((const char *)data, length) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(user_data, data ? newSVpvn((const char *)data, length) : newSV(0)); 0 0 0
1097 }
1098 0 store(aTHX_ fields, "name", new_demo_cstring(aTHX_ tfdemo_string_table_name(parser, table_id)));
1099 0 store(aTHX_ fields, "texts", newRV_noinc((SV *)texts));
1100 0 store(aTHX_ fields, "user_data", newRV_noinc((SV *)user_data));
1101 0 return hash_ref(aTHX_ fields);
1102 }
1103  
1104 0 static SV *new_server_info(pTHX_ const tfdemo_server_info *info) {
1105 0 HV *fields = newHV();
1106 0 const char os[2] = {info->os, 0};
1107 0 STORE_IV(fields, info, protocol);
1108 0 STORE_BOOL(fields, info, is_hltv);
1109 0 STORE_BOOL(fields, info, is_dedicated);
1110 0 STORE_IV(fields, info, max_classes);
1111 0 STORE_IV(fields, info, player_slot);
1112 0 STORE_IV(fields, info, max_clients);
1113 0 STORE_NV(fields, info, tick_interval);
1114 0 store(aTHX_ fields, "os", newSVpv(os, 0));
1115 0 STORE_STRING(fields, info, game_dir);
1116 0 STORE_STRING(fields, info, map_name);
1117 0 STORE_STRING(fields, info, sky_name);
1118 0 STORE_STRING(fields, info, host_name);
1119 0 STORE_BOOL(fields, info, is_replay);
1120 0 return hash_ref(aTHX_ fields);
1121 }
1122  
1123 0 static SV *new_diagnostics(pTHX_ const tfdemo_diagnostics *diagnostics) {
1124 0 HV *fields = newHV();
1125 0 STORE_IV(fields, diagnostics, older_frame_deltas);
1126 0 STORE_IV(fields, diagnostics, unknown_snapshot_tables);
1127 0 STORE_IV(fields, diagnostics, undecodable_user_messages);
1128 0 STORE_IV(fields, diagnostics, dropped_sound_messages);
1129 0 return hash_ref(aTHX_ fields);
1130 }
1131  
1132 /* ------------------------------------------------------------- connections */
1133  
1134 /* The names of tfnet.h's enums as Games::TF2::Server takes and gives them,
1135 * each at its enum's value. */
1136 static const char *const PHASE_NAMES[] = {"signon", "in_game", "disconnected"};
1137 static const char *const CAUSE_NAMES[] = {"server", "timeout", "error", "local"};
1138 static const char *const CVAR_STATUS_NAMES[] = {"value", "not_found", "not_valid", "protected", "unanswered"};
1139 static const char *const SOURCE_NAMES[] = {"user_messages", "chat", "menus", "convars", "prints", "cvar_queries"};
1140 static const char *const EVENT_TYPE_NAMES[] = {"ServerInfo", "InGame", "LevelChange", "Disconnected", "EntityFrame",
1141 "GameEvent", "UserMessage", "Chat", "Menu", "Convars", "Print", "Stats", "CvarQuery"};
1142  
1143 #define NAME_COUNT(names) (sizeof(names) / sizeof((names)[0]))
1144 #define NAME_OF(names, value) ((size_t)(value) < NAME_COUNT(names) ? (names)[(size_t)(value)] : "")
1145  
1146 /* The value of a name in `names`; throws Argument for an unknown one. */
1147 0 static int value_of_name(pTHX_ const char *const *names, size_t count, const char *what, SV *name) {
1148 0 ▶ const char *text = SvOK(name) ? SvPV_nolen(name) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *text = SvOK(name) ? SvPV_nolen(name) : ""; 0 0 0
1149 size_t i;
1150  
1151 0 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 0 0 0
1152 0 ▶ if (strEQ(names[i], text)) return (int)i;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(names[i], text)) return (int)i; 0 0 0
1153 }
1154 0 throw_status_message(aTHX_ TFDEMO_ERROR_ARGUMENT, sv_2mortal(newSVpvf("Games::TF2::Server: no %s %s", what, text)));
1155 0 return -1;
1156 }
1157  
1158 0 static void check(pTHX_ tfdemo_status status) {
1159 0 ▶ if (status != TFDEMO_OK) throw_status(aTHX_ status);
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) throw_status(aTHX_ status); 0 0 0
1160 0 }
1161  
1162 0 static SV *new_uint64(pTHX_ uint64_t value) {
1163 #if UVSIZE >= 8
1164 0 return newSVuv((UV)value);
1165 #else
1166 if (value <= UV_MAX) return newSVuv((UV)value);
1167 return newSVnv((NV)value);
1168 #endif
1169 }
1170  
1171 0 static tfnet_connection *connection_of(pTHX_ SV *self) {
1172 0 demo_parser *demo = demo_of(aTHX_ self);
1173 0 ▶ if (!demo->connection) croak("Games::TF2::Server: not a " CONNECTION_CLASS);
Branch: 1 branch
BranchTrueFalseTotal
if (!demo->connection) croak("Games::TF2::Server: not a " CONNECTION_CLASS); 0 0 0
1174 0 return demo->connection;
1175 }
1176  
1177 /* Text a connection sends: the UTF-8 bytes of a Perl string. */
1178 0 static const char *sent_text(pTHX_ SV *text) {
1179 0 ▶ return SvOK(text) ? SvPVutf8_nolen(text) : "";
Branch: 1 branch
BranchTrueFalseTotal
return SvOK(text) ? SvPVutf8_nolen(text) : ""; 0 0 0
1180 }
1181  
1182 /* Applies the connection options of a hash; a failure is kept in `status`. */
1183 0 static tfdemo_status apply_options(pTHX_ tfnet_options *options, HV *fields) {
1184 SV **value;
1185 0 tfdemo_status status = TFDEMO_OK;
1186  
1187 0 ▶ if ((value = hv_fetchs(fields, "tables", 0)) && SvOK(*value)) status = tfnet_options_set_tables(options, SvPV_nolen(*value));
Branch: 2 branches
BranchTrueFalseTotal
if ((value = hv_fetchs(fields, "tables", 0)) && SvOK(*value)) status = tfnet_options_set_tables(options, SvPV_nolen(*value)); 0 0 0
if ((value = hv_fetchs(fields, "tables", 0)) && SvOK(*value)) status = tfnet_options_set_tables(options, SvPV_nolen(*value)); 0 0 0
1188 0 ▶ if (status == TFDEMO_OK && (value = hv_fetchs(fields, "name", 0)) && SvOK(*value))
Branch: 3 branches
BranchTrueFalseTotal
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "name", 0)) && SvOK(*value)) 0 0 0
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "name", 0)) && SvOK(*value)) 0 0 0
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "name", 0)) && SvOK(*value)) 0 0 0
1189 0 status = tfnet_options_set_name(options, sent_text(aTHX_ *value));
1190 0 ▶ if (status == TFDEMO_OK && (value = hv_fetchs(fields, "version", 0)) && SvOK(*value))
Branch: 3 branches
BranchTrueFalseTotal
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "version", 0)) && SvOK(*value)) 0 0 0
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "version", 0)) && SvOK(*value)) 0 0 0
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "version", 0)) && SvOK(*value)) 0 0 0
1191 0 status = tfnet_options_set_version(options, sent_text(aTHX_ *value));
1192 0 ▶ if (status == TFDEMO_OK && (value = hv_fetchs(fields, "password", 0)) && SvOK(*value))
Branch: 3 branches
BranchTrueFalseTotal
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "password", 0)) && SvOK(*value)) 0 0 0
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "password", 0)) && SvOK(*value)) 0 0 0
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "password", 0)) && SvOK(*value)) 0 0 0
1193 0 status = tfnet_options_set_password(options, sent_text(aTHX_ *value));
1194 0 ▶ if (status == TFDEMO_OK && (value = hv_fetchs(fields, "steam_id", 0)) && SvOK(*value))
Branch: 3 branches
BranchTrueFalseTotal
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "steam_id", 0)) && SvOK(*value)) 0 0 0
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "steam_id", 0)) && SvOK(*value)) 0 0 0
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "steam_id", 0)) && SvOK(*value)) 0 0 0
1195 0 status = tfnet_options_set_steam_id(options, (uint64_t)strtoull(SvPV_nolen(*value), NULL, 10));
1196 0 ▶ if (status == TFDEMO_OK && (value = hv_fetchs(fields, "user_info", 0)) && SvROK(*value)) {
Branch: 3 branches
BranchTrueFalseTotal
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "user_info", 0)) && SvROK(*value)) { 0 0 0
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "user_info", 0)) && SvROK(*value)) { 0 0 0
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "user_info", 0)) && SvROK(*value)) { 0 0 0
1197 0 HV *user_info = (HV *)SvRV(*value);
1198 HE *entry;
1199 0 hv_iterinit(user_info);
1200 0 ▶ while (status == TFDEMO_OK && (entry = hv_iternext(user_info))) {
Branch: 2 branches
BranchTrueFalseTotal
while (status == TFDEMO_OK && (entry = hv_iternext(user_info))) { 0 0 0
while (status == TFDEMO_OK && (entry = hv_iternext(user_info))) { 0 0 0
1201 0 status = tfnet_options_set_user_info(
1202 options, sent_text(aTHX_ hv_iterkeysv(entry)), sent_text(aTHX_ hv_iterval(user_info, entry)));
1203 }
1204 }
1205 0 ▶ if (status == TFDEMO_OK && (value = hv_fetchs(fields, "cvar_answers", 0)) && SvROK(*value)) {
Branch: 3 branches
BranchTrueFalseTotal
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "cvar_answers", 0)) && SvROK(*value)) { 0 0 0
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "cvar_answers", 0)) && SvROK(*value)) { 0 0 0
if (status == TFDEMO_OK && (value = hv_fetchs(fields, "cvar_answers", 0)) && SvROK(*value)) { 0 0 0
1206 0 HV *answers = (HV *)SvRV(*value);
1207 HE *entry;
1208 0 hv_iterinit(answers);
1209 0 ▶ while (status == TFDEMO_OK && (entry = hv_iternext(answers))) {
Branch: 2 branches
BranchTrueFalseTotal
while (status == TFDEMO_OK && (entry = hv_iternext(answers))) { 0 0 0
while (status == TFDEMO_OK && (entry = hv_iternext(answers))) { 0 0 0
1210 0 AV *answer = (AV *)SvRV(hv_iterval(answers, entry));
1211 0 SV **answer_status = av_fetch(answer, 0, 0);
1212 0 SV **answer_value = av_fetch(answer, 1, 0);
1213 0 ▶ const int cvar_status = value_of_name(
Branch: 1 branch
BranchTrueFalseTotal
const int cvar_status = value_of_name( 0 0 0
1214 aTHX_ CVAR_STATUS_NAMES, NAME_COUNT(CVAR_STATUS_NAMES), "cvar status", answer_status ? *answer_status : &PL_sv_undef);
1215 0 ▶ status = tfnet_options_set_cvar_answer(options, sent_text(aTHX_ hv_iterkeysv(entry)), (tfnet_cvar_status)cvar_status,
Branch: 1 branch
BranchTrueFalseTotal
status = tfnet_options_set_cvar_answer(options, sent_text(aTHX_ hv_iterkeysv(entry)), (tfnet_cvar_status)cvar_status, 0 0 0
1216 0 ▶ answer_value && SvOK(*answer_value) ? sent_text(aTHX_ *answer_value) : NULL);
Branch: 1 branch
BranchTrueFalseTotal
answer_value && SvOK(*answer_value) ? sent_text(aTHX_ *answer_value) : NULL); 0 0 0
1217 }
1218 }
1219 0 ▶ if (status == TFDEMO_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == TFDEMO_OK) { 0 0 0
1220 0 SV **connect_ms = hv_fetchs(fields, "connect_timeout", 0);
1221 0 SV **silence_ms = hv_fetchs(fields, "silence_timeout", 0);
1222 0 ▶ if (connect_ms && silence_ms) status = tfnet_options_set_timeouts(options, (int32_t)SvIV(*connect_ms), (int32_t)SvIV(*silence_ms));
Branch: 2 branches
BranchTrueFalseTotal
if (connect_ms && silence_ms) status = tfnet_options_set_timeouts(options, (int32_t)SvIV(*connect_ms), (int32_t)SvIV(*silence_ms)); 0 0 0
if (connect_ms && silence_ms) status = tfnet_options_set_timeouts(options, (int32_t)SvIV(*connect_ms), (int32_t)SvIV(*silence_ms)); 0 0 0
1223 }
1224 0 ▶ if (status == TFDEMO_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == TFDEMO_OK) { 0 0 0
1225 0 SV **entities = hv_fetchs(fields, "entities", 0);
1226 0 SV **temp_entities = hv_fetchs(fields, "temp_entities", 0);
1227 0 SV **sounds = hv_fetchs(fields, "sounds", 0);
1228 0 tfdemo_options parse = tfdemo_default_options();
1229 0 ▶ if (entities) parse.entities = SvTRUE(*entities) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
if (entities) parse.entities = SvTRUE(*entities) ? 1 : 0; 0 0 0
1230 0 ▶ if (temp_entities) parse.temp_entities = SvTRUE(*temp_entities) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
if (temp_entities) parse.temp_entities = SvTRUE(*temp_entities) ? 1 : 0; 0 0 0
1231 0 ▶ if (sounds) parse.sounds = SvTRUE(*sounds) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sounds) parse.sounds = SvTRUE(*sounds) ? 1 : 0; 0 0 0
1232 0 status = tfnet_options_set_parse(options, &parse);
1233 }
1234 0 return status;
1235 }
1236  
1237 /* Whether a subscribed property name ("name" or "table.name") names the
1238 * flat property `prop` of a class. */
1239 0 static int names_prop(const tfdemo_parser *state, int32_t class_id, int32_t prop, const char *subscribed) {
1240 0 const char *table = tfdemo_class_prop_table(state, class_id, prop);
1241 0 const char *name = tfdemo_class_prop_name(state, class_id, prop);
1242 size_t table_length;
1243  
1244 0 ▶ if (!name) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return 0; 0 0 0
1245 0 ▶ if (strEQ(subscribed, name)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(subscribed, name)) return 1; 0 0 0
1246 0 ▶ if (!table) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!table) return 0; 0 0 0
1247 0 table_length = strlen(table);
1248 0 ▶ return strnEQ(subscribed, table, table_length) && subscribed[table_length] == '.' && strEQ(subscribed + table_length + 1, name);
Branch: 3 branches
BranchTrueFalseTotal
return strnEQ(subscribed, table, table_length) && subscribed[table_length] == '.' && strEQ(subscribed + table_length + 1, name); 0 0 0
return strnEQ(subscribed, table, table_length) && subscribed[table_length] == '.' && strEQ(subscribed + table_length + 1, name); 0 0 0
return strnEQ(subscribed, table, table_length) && subscribed[table_length] == '.' && strEQ(subscribed + table_length + 1, name); 0 0 0
1249 }
1250  
1251 /*
1252 * The elements of an array property of an entity frame, which tfnet.h
1253 * does not hand out: read from the entity of the connection's state, the
1254 * one property its class has under that name. undef when the entity is
1255 * gone or the name is not one property's.
1256 */
1257 0 static SV *new_frame_array(pTHX_ frame_context *frame, int32_t index, const char *subscribed) {
1258 0 const tfdemo_parser *state = frame->demo->state;
1259 0 const tfdemo_entity *entity = tfdemo_get_entity(state, index);
1260 int32_t class_id;
1261 int32_t count;
1262 int32_t prop;
1263 0 int32_t found = -1;
1264 tfdemo_prop_value value;
1265 tfdemo_status status;
1266  
1267 0 ▶ if (!entity || !subscribed) return newSV(0);
Branch: 2 branches
BranchTrueFalseTotal
if (!entity || !subscribed) return newSV(0); 0 0 0
if (!entity || !subscribed) return newSV(0); 0 0 0
1268 0 class_id = tfdemo_entity_class(entity);
1269 0 count = tfdemo_class_prop_count(state, class_id);
1270 0 ▶ for (prop = 0; prop < count; ++prop) {
Branch: 1 branch
BranchTrueFalseTotal
for (prop = 0; prop < count; ++prop) { 0 0 0
1271 0 ▶ if (!names_prop(state, class_id, prop, subscribed)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!names_prop(state, class_id, prop, subscribed)) continue; 0 0 0
1272 0 ▶ if (found >= 0) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (found >= 0) return newSV(0); 0 0 0
1273 0 found = prop;
1274 }
1275 0 ▶ if (found < 0) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (found < 0) return newSV(0); 0 0 0
1276 0 status = tfdemo_entity_prop(entity, found, &value);
1277 0 ▶ if (status != TFDEMO_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) { 0 0 0
1278 0 keep_status(aTHX_ frame, status);
1279 0 return newSV(0);
1280 }
1281 0 ▶ if (value.type != TFDEMO_PROP_ARRAY) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (value.type != TFDEMO_PROP_ARRAY) return newSV(0); 0 0 0
1282 0 return new_prop_value(aTHX_ frame, entity, read_entity_element, found, &value);
1283 }
1284  
1285 0 static SV *new_frame_entity(pTHX_ frame_context *frame, size_t event, size_t entity_number) {
1286 0 const tfnet_connection *connection = frame->demo->connection;
1287 tfnet_frame_entity entity;
1288 HV *fields;
1289 HV *props;
1290 size_t prop;
1291 0 tfdemo_status status = tfnet_frame_entity_at(connection, event, entity_number, &entity);
1292  
1293 0 ▶ if (status != TFDEMO_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) { 0 0 0
1294 0 keep_status(aTHX_ frame, status);
1295 0 return newSV(0);
1296 }
1297 0 fields = newHV();
1298 0 props = newHV();
1299 0 STORE_IV(fields, &entity, index);
1300 0 STORE_BOOL(fields, &entity, present);
1301 0 STORE_STRING(fields, &entity, class_name);
1302 0 STORE_IV(fields, &entity, serial);
1303 0 ▶ store(aTHX_ fields, "simulation_tick", entity.has_simulation_tick ? newSViv((IV)entity.simulation_tick) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
store(aTHX_ fields, "simulation_tick", entity.has_simulation_tick ? newSViv((IV)entity.simulation_tick) : newSV(0)); 0 0 0
1304 0 ▶ for (prop = 0; prop < entity.prop_count && !frame->error; ++prop) {
Branch: 2 branches
BranchTrueFalseTotal
for (prop = 0; prop < entity.prop_count && !frame->error; ++prop) { 0 0 0
for (prop = 0; prop < entity.prop_count && !frame->error; ++prop) { 0 0 0
1305 0 const char *name = NULL;
1306 tfdemo_prop_value value;
1307 SV *key;
1308 0 status = tfnet_frame_entity_prop(connection, event, entity_number, prop, &name, &value);
1309 0 ▶ if (status != TFDEMO_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) { 0 0 0
1310 0 keep_status(aTHX_ frame, status);
1311 0 break;
1312 }
1313 0 ▶ if (value.type == TFDEMO_PROP_UNSET) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (value.type == TFDEMO_PROP_UNSET) continue; 0 0 0
1314 0 key = new_demo_cstring(aTHX_ name);
1315 0 ▶ (void)hv_store_ent(props, key,
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store_ent(props, key, 0 0 0
1316 value.type == TFDEMO_PROP_ARRAY ? new_frame_array(aTHX_ frame, entity.index, name) : new_scalar_value(aTHX_ frame, &value), 0);
1317 0 SvREFCNT_dec(key);
1318 }
1319 0 store(aTHX_ fields, "props", newRV_noinc((SV *)props));
1320 0 return hash_ref(aTHX_ fields);
1321 }
1322  
1323 0 static SV *new_net_game_event_values(pTHX_ frame_context *frame, size_t event) {
1324 0 const tfnet_connection *connection = frame->demo->connection;
1325 0 const size_t count = tfnet_game_event_key_count(connection, event);
1326 0 HV *values = newHV();
1327 0 HV *seen = newHV();
1328 size_t key;
1329  
1330 0 ▶ for (key = 0; key < count; ++key) {
Branch: 1 branch
BranchTrueFalseTotal
for (key = 0; key < count; ++key) { 0 0 0
1331 0 const char *name = NULL;
1332 tfnet_value value;
1333 SV *key_sv;
1334 SV *value_sv;
1335 0 const tfdemo_status status = tfnet_game_event_key(connection, event, key, &name, &value);
1336 0 ▶ if (status != TFDEMO_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) { 0 0 0
1337 0 keep_status(aTHX_ frame, status);
1338 0 break;
1339 }
1340 0 switch (value.type) {
1341 0 case TFNET_VALUE_STRING: value_sv = new_demo_cstring(aTHX_ value.string_value); break;
1342 0 case TFNET_VALUE_FLOAT: value_sv = newSVnv((NV)value.float_value); break;
1343 0 default: value_sv = newSViv((IV)value.int_value); break;
1344 }
1345 0 ▶ key_sv = new_unique_key(aTHX_ seen, name ? name : "", name ? strlen(name) : 0);
Branch: 2 branches
BranchTrueFalseTotal
key_sv = new_unique_key(aTHX_ seen, name ? name : "", name ? strlen(name) : 0); 0 0 0
key_sv = new_unique_key(aTHX_ seen, name ? name : "", name ? strlen(name) : 0); 0 0 0
1346 0 (void)hv_store_ent(values, key_sv, value_sv, 0);
1347 0 SvREFCNT_dec(key_sv);
1348 }
1349 0 SvREFCNT_dec((SV *)seen);
1350 0 return newRV_noinc((SV *)values);
1351 }
1352  
1353 0 static SV *new_stats(pTHX_ const tfnet_stats *stats) {
1354 0 HV *fields = newHV();
1355 0 store(aTHX_ fields, "packets_received", new_uint64(aTHX_ stats->packets_received));
1356 0 store(aTHX_ fields, "bytes_received", new_uint64(aTHX_ stats->bytes_received));
1357 0 store(aTHX_ fields, "packets_sent", new_uint64(aTHX_ stats->packets_sent));
1358 0 store(aTHX_ fields, "bytes_sent", new_uint64(aTHX_ stats->bytes_sent));
1359 0 store(aTHX_ fields, "packets_lost", new_uint64(aTHX_ stats->packets_lost));
1360 0 store(aTHX_ fields, "packets_dropped", new_uint64(aTHX_ stats->packets_dropped));
1361 0 store(aTHX_ fields, "reliable_transfers", new_uint64(aTHX_ stats->reliable_transfers));
1362 0 store(aTHX_ fields, "entity_frames", new_uint64(aTHX_ stats->entity_frames));
1363 0 store(aTHX_ fields, "level_changes", new_uint64(aTHX_ stats->level_changes));
1364 0 store(aTHX_ fields, "connected_milliseconds", new_int64(aTHX_ stats->connected_milliseconds));
1365 0 return hash_ref(aTHX_ fields);
1366 }
1367  
1368 /* Stores the fields of an event of the last poll by its type; a failure
1369 * of the library is kept as the frame's exception. */
1370 0 static void store_event_fields(pTHX_ frame_context *frame, HV *fields, size_t event, int32_t type) {
1371 0 const tfnet_connection *connection = frame->demo->connection;
1372 0 tfdemo_status status = TFDEMO_OK;
1373  
1374 0 switch (type) {
1375 0 case TFNET_EVENT_SERVER_INFO: {
1376 0 const tfdemo_server_info info = tfdemo_get_server_info(frame->demo->state);
1377 0 ▶ store(aTHX_ fields, "server_info", info.present ? new_server_info(aTHX_ &info) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
store(aTHX_ fields, "server_info", info.present ? new_server_info(aTHX_ &info) : newSV(0)); 0 0 0
1378 0 break;
1379 }
1380 0 case TFNET_EVENT_DISCONNECTED: {
1381 0 tfnet_disconnect_cause cause = TFNET_CAUSE_ERROR;
1382 0 const char *reason = NULL;
1383 0 status = tfnet_event_disconnected(connection, event, &cause, &reason);
1384 0 ▶ store(aTHX_ fields, "cause", newSVpv(NAME_OF(CAUSE_NAMES, cause), 0));
Branch: 1 branch
BranchTrueFalseTotal
store(aTHX_ fields, "cause", newSVpv(NAME_OF(CAUSE_NAMES, cause), 0)); 0 0 0
1385 0 ▶ store(aTHX_ fields, "reason", new_demo_cstring(aTHX_ reason ? reason : ""));
Branch: 1 branch
BranchTrueFalseTotal
store(aTHX_ fields, "reason", new_demo_cstring(aTHX_ reason ? reason : "")); 0 0 0
1386 0 break;
1387 }
1388 0 case TFNET_EVENT_ENTITY_FRAME: {
1389 0 const size_t count = tfnet_frame_entity_count(connection, event);
1390 0 AV *entities = newAV();
1391 size_t entity;
1392 0 ▶ for (entity = 0; entity < count && !frame->error; ++entity) av_push(entities, new_frame_entity(aTHX_ frame, event, entity));
Branch: 2 branches
BranchTrueFalseTotal
for (entity = 0; entity < count && !frame->error; ++entity) av_push(entities, new_frame_entity(aTHX_ frame, event, entity)); 0 0 0
for (entity = 0; entity < count && !frame->error; ++entity) av_push(entities, new_frame_entity(aTHX_ frame, event, entity)); 0 0 0
1393 0 store(aTHX_ fields, "entities", newRV_noinc((SV *)entities));
1394 0 break;
1395 }
1396 0 case TFNET_EVENT_GAME_EVENT:
1397 0 store(aTHX_ fields, "name", new_demo_cstring(aTHX_ tfnet_game_event_name(connection, event)));
1398 0 store(aTHX_ fields, "values", new_net_game_event_values(aTHX_ frame, event));
1399 0 break;
1400 0 case TFNET_EVENT_USER_MESSAGE: {
1401 0 const tfdemo_user_message *message = tfnet_user_message(connection, event);
1402 0 size_t bytes = 0;
1403 0 size_t bits = 0;
1404 0 ▶ const uint8_t *data = message ? tfdemo_user_message_data(message, &bytes, &bits) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const uint8_t *data = message ? tfdemo_user_message_data(message, &bytes, &bits) : NULL; 0 0 0
1405 0 ▶ if (!message) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!message) break; 0 0 0
1406 0 store(aTHX_ fields, "type", newSViv((IV)tfdemo_user_message_type(message)));
1407 0 store(aTHX_ fields, "name", new_demo_cstring(aTHX_ tfdemo_user_message_name(message)));
1408 0 ▶ store(aTHX_ fields, "data", data ? newSVpvn((const char *)data, bytes) : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
store(aTHX_ fields, "data", data ? newSVpvn((const char *)data, bytes) : newSVpvs("")); 0 0 0
1409 0 store(aTHX_ fields, "bits", newSVuv((UV)bits));
1410 0 store(aTHX_ fields, "chat", new_chat(aTHX_ frame, message));
1411 0 break;
1412 }
1413 0 case TFNET_EVENT_CHAT: {
1414 tfdemo_chat chat;
1415 0 status = tfnet_chat(connection, event, &chat);
1416 0 ▶ if (status != TFDEMO_OK) break;
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) break; 0 0 0
1417 0 STORE_STRING(fields, &chat, kind);
1418 0 STORE_IV(fields, &chat, client);
1419 0 store(aTHX_ fields, "from", new_demo_cstring(aTHX_ chat.sender));
1420 0 STORE_STRING(fields, &chat, text);
1421 0 break;
1422 }
1423 0 case TFNET_EVENT_MENU: {
1424 tfnet_menu menu;
1425 0 AV *items = newAV();
1426 0 const size_t count = tfnet_menu_item_count(connection, event);
1427 size_t item;
1428 0 store(aTHX_ fields, "items", newRV_noinc((SV *)items));
1429 0 status = tfnet_menu_of(connection, event, &menu);
1430 0 ▶ if (status != TFDEMO_OK) break;
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) break; 0 0 0
1431 0 STORE_STRING(fields, &menu, text);
1432 0 STORE_UV(fields, &menu, keys);
1433 0 STORE_IV(fields, &menu, display_seconds);
1434 0 ▶ for (item = 0; item < count && status == TFDEMO_OK; ++item) {
Branch: 2 branches
BranchTrueFalseTotal
for (item = 0; item < count && status == TFDEMO_OK; ++item) { 0 0 0
for (item = 0; item < count && status == TFDEMO_OK; ++item) { 0 0 0
1435 tfnet_menu_item menu_item;
1436 HV *item_fields;
1437 0 status = tfnet_menu_item_at(connection, event, item, &menu_item);
1438 0 ▶ if (status != TFDEMO_OK) break;
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) break; 0 0 0
1439 0 item_fields = newHV();
1440 0 STORE_IV(item_fields, &menu_item, key);
1441 0 STORE_STRING(item_fields, &menu_item, text);
1442 0 STORE_BOOL(item_fields, &menu_item, selectable);
1443 0 av_push(items, hash_ref(aTHX_ item_fields));
1444 }
1445 0 break;
1446 }
1447 0 case TFNET_EVENT_CONVARS: {
1448 0 HV *convars = newHV();
1449 0 const size_t count = tfnet_convar_count(connection, event);
1450 size_t convar;
1451 0 store(aTHX_ fields, "convars", newRV_noinc((SV *)convars));
1452 0 ▶ for (convar = 0; convar < count && status == TFDEMO_OK; ++convar) {
Branch: 2 branches
BranchTrueFalseTotal
for (convar = 0; convar < count && status == TFDEMO_OK; ++convar) { 0 0 0
for (convar = 0; convar < count && status == TFDEMO_OK; ++convar) { 0 0 0
1453 0 const char *name = NULL;
1454 0 const char *value = NULL;
1455 SV *key;
1456 0 status = tfnet_convar(connection, event, convar, &name, &value);
1457 0 ▶ if (status != TFDEMO_OK) break;
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) break; 0 0 0
1458 0 key = new_demo_cstring(aTHX_ name);
1459 0 (void)hv_store_ent(convars, key, new_demo_cstring(aTHX_ value), 0);
1460 0 SvREFCNT_dec(key);
1461 }
1462 0 break;
1463 }
1464 0 case TFNET_EVENT_PRINT: store(aTHX_ fields, "text", new_demo_cstring(aTHX_ tfnet_print_text(connection, event))); break;
1465 0 case TFNET_EVENT_STATS: {
1466 tfnet_stats stats;
1467 0 status = tfnet_event_stats(connection, event, &stats);
1468 0 ▶ if (status == TFDEMO_OK) store(aTHX_ fields, "stats", new_stats(aTHX_ &stats));
Branch: 1 branch
BranchTrueFalseTotal
if (status == TFDEMO_OK) store(aTHX_ fields, "stats", new_stats(aTHX_ &stats)); 0 0 0
1469 0 break;
1470 }
1471 0 case TFNET_EVENT_CVAR_QUERY: {
1472 tfnet_cvar_query query;
1473 0 status = tfnet_cvar_query_of(connection, event, &query);
1474 0 ▶ if (status != TFDEMO_OK) break;
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) break; 0 0 0
1475 0 STORE_STRING(fields, &query, name);
1476 0 STORE_IV(fields, &query, cookie);
1477 0 ▶ store(aTHX_ fields, "status", newSVpv(NAME_OF(CVAR_STATUS_NAMES, query.status), 0));
Branch: 1 branch
BranchTrueFalseTotal
store(aTHX_ fields, "status", newSVpv(NAME_OF(CVAR_STATUS_NAMES, query.status), 0)); 0 0 0
1478 0 STORE_STRING(fields, &query, value);
1479 0 break;
1480 }
1481 0 default: break;
1482 }
1483 0 ▶ if (status != TFDEMO_OK) keep_status(aTHX_ frame, status);
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) keep_status(aTHX_ frame, status); 0 0 0
1484 0 }
1485  
1486 /* An event of the last poll as a hash: its type (the name of its class),
1487 * its tick and its fields. */
1488 0 static SV *new_event(pTHX_ frame_context *frame, size_t event) {
1489 0 const int32_t type = tfnet_event_type_of(frame->demo->connection, event);
1490 0 HV *fields = newHV();
1491  
1492 0 ▶ store(aTHX_ fields, "type", newSVpv(NAME_OF(EVENT_TYPE_NAMES, type), 0));
Branch: 1 branch
BranchTrueFalseTotal
store(aTHX_ fields, "type", newSVpv(NAME_OF(EVENT_TYPE_NAMES, type), 0)); 0 0 0
1493 0 store(aTHX_ fields, "tick", newSViv((IV)tfnet_event_tick(frame->demo->connection, event)));
1494 0 store_event_fields(aTHX_ frame, fields, event, type);
1495 0 return hash_ref(aTHX_ fields);
1496 }
1497  
1498 MODULE = Games::TF2::Demo PACKAGE = Games::TF2::Demo::_Parser
1499  
1500 PROTOTYPES: DISABLE
1501  
1502 SV *
1503 default_options(class_name)
1504 const char *class_name
1505 PREINIT:
1506 tfdemo_options options;
1507 HV *defaults;
1508 CODE:
1509 PERL_UNUSED_VAR(class_name);
1510 0 options = tfdemo_default_options();
1511 0 defaults = newHV();
1512 0 store(aTHX_ defaults, "entities", newSViv(options.entities));
1513 0 store(aTHX_ defaults, "temp_entities", newSViv(options.temp_entities));
1514 0 store(aTHX_ defaults, "sounds", newSViv(options.sounds));
1515 0 store(aTHX_ defaults, "summary", newSViv(options.summary));
1516 0 RETVAL = newRV_noinc((SV *)defaults);
1517 OUTPUT:
1518 RETVAL
1519  
1520 SV *
1521 open_file(class_name, path, entities, temp_entities, sounds, summary)
1522 const char *class_name
1523 const char *path
1524 int entities
1525 int temp_entities
1526 int sounds
1527 int summary
1528 PREINIT:
1529 tfdemo_options options;
1530 0 tfdemo_parser *parser = NULL;
1531 tfdemo_status status;
1532 CODE:
1533 0 options = options_of(entities, temp_entities, sounds, summary);
1534 0 status = tfdemo_open_file(path, &options, &parser);
1535 0 ▶ if (status != TFDEMO_OK) throw_status(aTHX_ status);
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) throw_status(aTHX_ status); 0 0 0
1536 0 RETVAL = new_handle(aTHX_ class_name, parser, NULL);
1537 OUTPUT:
1538 RETVAL
1539  
1540 SV *
1541 open_memory(class_name, data, entities, temp_entities, sounds, summary)
1542 const char *class_name
1543 SV *data
1544 int entities
1545 int temp_entities
1546 int sounds
1547 int summary
1548 PREINIT:
1549 tfdemo_options options;
1550 0 ▶ tfdemo_parser *parser = NULL;
Branch: 1 branch
BranchTrueFalseTotal
tfdemo_parser *parser = NULL; 0 0 0
1551 tfdemo_status status;
1552 STRLEN size;
1553 const char *bytes;
1554 CODE:
1555 0 bytes = SvPVbyte(data, size);
1556 0 options = options_of(entities, temp_entities, sounds, summary);
1557 0 status = tfdemo_open_memory(bytes, size, &options, &parser);
1558 0 ▶ if (status != TFDEMO_OK) throw_status(aTHX_ status);
Branch: 1 branch
BranchTrueFalseTotal
if (status != TFDEMO_OK) throw_status(aTHX_ status); 0 0 0
1559 0 RETVAL = new_handle(aTHX_ class_name, parser, NULL);
1560 OUTPUT:
1561 RETVAL
1562  
1563 SV *
1564 header(self)
1565 SV *self
1566 PREINIT:
1567 demo_parser *demo;
1568 tfdemo_header header;
1569 HV *fields;
1570 CODE:
1571 0 demo = demo_of(aTHX_ self);
1572 0 header = tfdemo_get_header(demo->state);
1573 0 fields = newHV();
1574 0 store(aTHX_ fields, "stamp", new_demo_cstring(aTHX_ header.stamp));
1575 0 store(aTHX_ fields, "demo_protocol", newSViv((IV)header.demo_protocol));
1576 0 store(aTHX_ fields, "network_protocol", newSViv((IV)header.network_protocol));
1577 0 store(aTHX_ fields, "server_name", new_demo_cstring(aTHX_ header.server_name));
1578 0 store(aTHX_ fields, "client_name", new_demo_cstring(aTHX_ header.client_name));
1579 0 store(aTHX_ fields, "map_name", new_demo_cstring(aTHX_ header.map_name));
1580 0 store(aTHX_ fields, "game_directory", new_demo_cstring(aTHX_ header.game_directory));
1581 0 store(aTHX_ fields, "playback_time", newSVnv((NV)header.playback_time));
1582 0 store(aTHX_ fields, "playback_ticks", newSViv((IV)header.playback_ticks));
1583 0 store(aTHX_ fields, "playback_frames", newSViv((IV)header.playback_frames));
1584 0 store(aTHX_ fields, "signon_length", newSViv((IV)header.signon_length));
1585 0 RETVAL = newRV_noinc((SV *)fields);
1586 OUTPUT:
1587 RETVAL
1588  
1589 void
1590 callback_methods(class_name)
1591 const char *class_name
1592 PREINIT:
1593 size_t i;
1594 PPCODE:
1595 PERL_UNUSED_VAR(class_name);
1596 0 ▶ EXTEND(SP, (SSize_t)CALLBACK_COUNT);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)CALLBACK_COUNT); 0 0 0
1597 0 ▶ for (i = 0; i < CALLBACK_COUNT; ++i) mPUSHs(newSVpv(CALLBACK_METHODS[i], 0));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < CALLBACK_COUNT; ++i) mPUSHs(newSVpv(CALLBACK_METHODS[i], 0)); 0 0 0
1598  
1599 int
1600 step(self, handler, mask)
1601 SV *self
1602 SV *handler
1603 UV mask
1604 CODE:
1605 0 RETVAL = handle_frames(aTHX_ demo_of(aTHX_ self), handler, (unsigned)mask, 0);
1606 OUTPUT:
1607 RETVAL
1608  
1609 void
1610 run(self, handler, mask)
1611 SV *self
1612 SV *handler
1613 UV mask
1614 CODE:
1615 0 (void)handle_frames(aTHX_ demo_of(aTHX_ self), handler, (unsigned)mask, 1);
1616  
1617 SV *
1618 last_frame_type(self)
1619 SV *self
1620 PREINIT:
1621 const char *name;
1622 CODE:
1623 0 name = tfdemo_frame_type_name(tfdemo_last_frame_type(demo_of(aTHX_ self)->state));
1624 0 ▶ RETVAL = name ? newSVpv(name, 0) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = name ? newSVpv(name, 0) : newSV(0); 0 0 0
1625 OUTPUT:
1626 RETVAL
1627  
1628 int
1629 last_frame_tick(self)
1630 SV *self
1631 CODE:
1632 0 RETVAL = (int)tfdemo_last_frame_tick(demo_of(aTHX_ self)->state);
1633 OUTPUT:
1634 RETVAL
1635  
1636 SV *
1637 server_info(self)
1638 SV *self
1639 PREINIT:
1640 tfdemo_server_info info;
1641 CODE:
1642 0 info = tfdemo_get_server_info(demo_of(aTHX_ self)->state);
1643 0 ▶ RETVAL = info.present ? new_server_info(aTHX_ &info) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = info.present ? new_server_info(aTHX_ &info) : newSV(0); 0 0 0
1644 OUTPUT:
1645 RETVAL
1646  
1647 SV *
1648 diagnostics(self)
1649 SV *self
1650 PREINIT:
1651 tfdemo_diagnostics diagnostics;
1652 CODE:
1653 0 diagnostics = tfdemo_get_diagnostics(demo_of(aTHX_ self)->state);
1654 0 RETVAL = new_diagnostics(aTHX_ &diagnostics);
1655 OUTPUT:
1656 RETVAL
1657  
1658 SV *
1659 game_state(self)
1660 SV *self
1661 CODE:
1662 0 RETVAL = new_game_state(aTHX_ demo_of(aTHX_ self));
1663 OUTPUT:
1664 RETVAL
1665  
1666 SV *
1667 summary(self)
1668 SV *self
1669 CODE:
1670 0 RETVAL = new_summary(aTHX_ demo_of(aTHX_ self));
1671 OUTPUT:
1672 RETVAL
1673  
1674 SV *
1675 player_by_entity(self, index)
1676 SV *self
1677 int index
1678 PREINIT:
1679 tfdemo_player_info player;
1680 tfdemo_status status;
1681 CODE:
1682 0 status = tfdemo_player_by_entity(demo_of(aTHX_ self)->state, (int32_t)index, &player);
1683 0 RETVAL = player_or_undef(aTHX_ status, &player);
1684 OUTPUT:
1685 RETVAL
1686  
1687 SV *
1688 player_by_user_id(self, user_id)
1689 SV *self
1690 int user_id
1691 PREINIT:
1692 tfdemo_player_info player;
1693 tfdemo_status status;
1694 CODE:
1695 0 status = tfdemo_player_by_user_id(demo_of(aTHX_ self)->state, (int32_t)user_id, &player);
1696 0 RETVAL = player_or_undef(aTHX_ status, &player);
1697 OUTPUT:
1698 RETVAL
1699  
1700 void
1701 players(self)
1702 SV *self
1703 PREINIT:
1704 const tfdemo_parser *parser;
1705 int32_t table_id;
1706 int32_t size;
1707 int32_t index;
1708 PPCODE:
1709 0 parser = demo_of(aTHX_ self)->state;
1710 0 table_id = tfdemo_string_table_find(parser, "userinfo");
1711 0 ▶ size = table_id >= 0 ? tfdemo_string_table_size(parser, table_id) : 0;
Branch: 1 branch
BranchTrueFalseTotal
size = table_id >= 0 ? tfdemo_string_table_size(parser, table_id) : 0; 0 0 0
1712 /* The userinfo entry of a player is at its entity index minus 1. */
1713 0 ▶ for (index = 1; index <= size; ++index) {
Branch: 1 branch
BranchTrueFalseTotal
for (index = 1; index <= size; ++index) { 0 0 0
1714 tfdemo_player_info player;
1715 0 ▶ if (tfdemo_player_by_entity(parser, index, &player) != TFDEMO_OK) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (tfdemo_player_by_entity(parser, index, &player) != TFDEMO_OK) continue; 0 0 0
1716 0 ▶ mXPUSHs(new_player_info(aTHX_ &player));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(new_player_info(aTHX_ &player)); 0 0 0
1717 }
1718  
1719 void
1720 string_table_names(self)
1721 SV *self
1722 PREINIT:
1723 const tfdemo_parser *parser;
1724 int32_t table_id;
1725 int32_t count;
1726 PPCODE:
1727 0 parser = demo_of(aTHX_ self)->state;
1728 0 count = tfdemo_string_table_count(parser);
1729 0 ▶ EXTEND(SP, count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, count); 0 0 0
EXTEND(SP, count); 0 0 0
1730 0 ▶ for (table_id = 0; table_id < count; ++table_id) mPUSHs(new_demo_cstring(aTHX_ tfdemo_string_table_name(parser, table_id)));
Branch: 1 branch
BranchTrueFalseTotal
for (table_id = 0; table_id < count; ++table_id) mPUSHs(new_demo_cstring(aTHX_ tfdemo_string_table_name(parser, table_id))); 0 0 0
1731  
1732 SV *
1733 string_table(self, name)
1734 SV *self
1735 SV *name
1736 PREINIT:
1737 const tfdemo_parser *parser;
1738 int32_t table_id;
1739 CODE:
1740 0 parser = demo_of(aTHX_ self)->state;
1741 0 table_id = tfdemo_string_table_find(parser, SvPVutf8_nolen(name));
1742 0 ▶ RETVAL = table_id >= 0 ? new_string_table(aTHX_ parser, table_id) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = table_id >= 0 ? new_string_table(aTHX_ parser, table_id) : newSV(0); 0 0 0
1743 OUTPUT:
1744 RETVAL
1745  
1746 SV *
1747 entity(self, index)
1748 SV *self
1749 int index
1750 PREINIT:
1751 demo_parser *demo;
1752 const tfdemo_entity *entity;
1753 frame_context frame;
1754 HV *fields;
1755 CODE:
1756 0 demo = demo_of(aTHX_ self);
1757 0 entity = tfdemo_get_entity(demo->state, (int32_t)index);
1758 0 ▶ if (!entity) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!entity) XSRETURN_UNDEF; 0 0 0
1759 0 frame.demo = demo;
1760 0 frame.handler = NULL;
1761 0 frame.error = NULL;
1762 0 fields = (HV *)sv_2mortal((SV *)newHV());
1763 0 store(aTHX_ fields, "index", newSViv((IV)tfdemo_entity_index(entity)));
1764 0 store(aTHX_ fields, "serial", newSViv((IV)tfdemo_entity_serial(entity)));
1765 0 store(aTHX_ fields, "class_name", new_demo_cstring(aTHX_ tfdemo_class_name(demo->state, tfdemo_entity_class(entity))));
1766 0 store(aTHX_ fields, "in_pvs", newSViv(tfdemo_entity_in_pvs(entity) ? 1 : 0));
1767 0 store(aTHX_ fields, "props", new_all_props(aTHX_ &frame, entity));
1768 0 ▶ if (frame.error) croak_sv(sv_2mortal(frame.error));
Branch: 1 branch
BranchTrueFalseTotal
if (frame.error) croak_sv(sv_2mortal(frame.error)); 0 0 0
1769 0 RETVAL = newRV_inc((SV *)fields);
1770 OUTPUT:
1771 RETVAL
1772  
1773 int
1774 CLONE_SKIP(...)
1775 CODE:
1776 PERL_UNUSED_VAR(items);
1777 0 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 0 0
1778 OUTPUT:
1779 RETVAL
1780  
1781 void
1782 DESTROY(self)
1783 SV *self
1784 PREINIT:
1785 demo_parser *demo;
1786 CODE:
1787 0 demo = demo_of(aTHX_ self);
1788 0 ▶ if (demo->connection) tfnet_free(demo->connection);
Branch: 1 branch
BranchTrueFalseTotal
if (demo->connection) tfnet_free(demo->connection); 0 0 0
1789 0 ▶ if (demo->parser) tfdemo_free(demo->parser);
Branch: 1 branch
BranchTrueFalseTotal
if (demo->parser) tfdemo_free(demo->parser); 0 0 0
1790 0 SvREFCNT_dec((SV *)demo->class_keys);
1791 0 Safefree(demo);
1792  
1793 MODULE = Games::TF2::Demo PACKAGE = Games::TF2::Server::_Connection
1794  
1795 PROTOTYPES: DISABLE
1796  
1797 void
1798 names(class_name, what)
1799 const char *class_name
1800 const char *what
1801 PREINIT:
1802 0 const char *const *names = NULL;
1803 0 size_t count = 0;
1804 size_t i;
1805 PPCODE:
1806 PERL_UNUSED_VAR(class_name);
1807 0 ▶ if (strEQ(what, "cvar_status")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(what, "cvar_status")) { 0 0 0
1808 0 names = CVAR_STATUS_NAMES;
1809 0 count = NAME_COUNT(CVAR_STATUS_NAMES);
1810 0 ▶ } else if (strEQ(what, "source")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(what, "source")) { 0 0 0
1811 0 names = SOURCE_NAMES;
1812 0 count = NAME_COUNT(SOURCE_NAMES);
1813 0 ▶ } else if (strEQ(what, "event_type")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(what, "event_type")) { 0 0 0
1814 0 names = EVENT_TYPE_NAMES;
1815 0 count = NAME_COUNT(EVENT_TYPE_NAMES);
1816 } else {
1817 0 croak("Games::TF2::Server: no names of %s", what);
1818 }
1819 0 ▶ EXTEND(SP, (SSize_t)count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)count); 0 0 0
EXTEND(SP, (SSize_t)count); 0 0 0
1820 0 ▶ for (i = 0; i < count; ++i) mPUSHs(newSVpv(names[i], 0));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) mPUSHs(newSVpv(names[i], 0)); 0 0 0
1821  
1822 SV *
1823 connect(class_name, is_player, address, fields)
1824 const char *class_name
1825 int is_player
1826 const char *address
1827 HV *fields
1828 PREINIT:
1829 tfnet_options *options;
1830 0 tfnet_connection *connection = NULL;
1831 tfdemo_status status;
1832 CODE:
1833 0 options = tfnet_options_new(is_player ? TFNET_PLAYER : TFNET_SPECTATOR, address);
1834 0 ▶ if (!options) throw_message(aTHX_ TFDEMO_ERROR_INTERNAL, "Games::TF2::Server: no memory for the options");
Branch: 1 branch
BranchTrueFalseTotal
if (!options) throw_message(aTHX_ TFDEMO_ERROR_INTERNAL, "Games::TF2::Server: no memory for the options"); 0 0 0
1835 0 ENTER;
1836 0 SAVEDESTRUCTOR(tfnet_options_free, options);
1837 0 status = apply_options(aTHX_ options, fields);
1838 0 ▶ if (status == TFDEMO_OK) status = tfnet_connect(options, &connection);
Branch: 1 branch
BranchTrueFalseTotal
if (status == TFDEMO_OK) status = tfnet_connect(options, &connection); 0 0 0
1839 0 check(aTHX_ status);
1840 0 LEAVE;
1841 0 RETVAL = new_handle(aTHX_ class_name, NULL, connection);
1842 OUTPUT:
1843 RETVAL
1844  
1845 void
1846 poll(self, timeout)
1847 SV *self
1848 int timeout
1849 PREINIT:
1850 demo_parser *demo;
1851 frame_context frame;
1852 0 size_t count = 0;
1853 size_t event;
1854 PPCODE:
1855 0 demo = demo_of(aTHX_ self);
1856 0 (void)connection_of(aTHX_ self);
1857 0 check(aTHX_ tfnet_poll(demo->connection, (int32_t)timeout, &count));
1858 0 frame.demo = demo;
1859 0 frame.handler = NULL;
1860 0 frame.error = NULL;
1861 0 ▶ EXTEND(SP, (SSize_t)count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)count); 0 0 0
EXTEND(SP, (SSize_t)count); 0 0 0
1862 0 ▶ for (event = 0; event < count; ++event) mPUSHs(new_event(aTHX_ &frame, event));
Branch: 1 branch
BranchTrueFalseTotal
for (event = 0; event < count; ++event) mPUSHs(new_event(aTHX_ &frame, event)); 0 0 0
1863 0 ▶ if (frame.error) croak_sv(sv_2mortal(frame.error));
Branch: 1 branch
BranchTrueFalseTotal
if (frame.error) croak_sv(sv_2mortal(frame.error)); 0 0 0
1864  
1865 SV *
1866 phase(self)
1867 SV *self
1868 CODE:
1869 0 ▶ RETVAL = newSVpv(NAME_OF(PHASE_NAMES, tfnet_get_phase(connection_of(aTHX_ self))), 0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSVpv(NAME_OF(PHASE_NAMES, tfnet_get_phase(connection_of(aTHX_ self))), 0); 0 0 0
1870 OUTPUT:
1871 RETVAL
1872  
1873 SV *
1874 version(self)
1875 SV *self
1876 CODE:
1877 0 RETVAL = new_demo_cstring(aTHX_ tfnet_get_version(connection_of(aTHX_ self)));
1878 OUTPUT:
1879 RETVAL
1880  
1881 SV *
1882 stats(self)
1883 SV *self
1884 PREINIT:
1885 tfnet_stats stats;
1886 CODE:
1887 0 stats = tfnet_get_stats(connection_of(aTHX_ self));
1888 0 RETVAL = new_stats(aTHX_ &stats);
1889 OUTPUT:
1890 RETVAL
1891  
1892 void
1893 send_command(self, command)
1894 SV *self
1895 SV *command
1896 CODE:
1897 0 check(aTHX_ tfnet_send_command(connection_of(aTHX_ self), sent_text(aTHX_ command)));
1898  
1899 void
1900 set_controls(self, buttons, pitch, yaw, roll, has_movement, forward, side, up, impulse, weapon_select)
1901 SV *self
1902 UV buttons
1903 NV pitch
1904 NV yaw
1905 NV roll
1906 int has_movement
1907 NV forward
1908 NV side
1909 NV up
1910 UV impulse
1911 IV weapon_select
1912 PREINIT:
1913 tfnet_controls controls;
1914 CODE:
1915 0 Zero(&controls, 1, tfnet_controls);
1916 0 controls.buttons = (uint32_t)buttons;
1917 0 controls.view_angles[0] = (float)pitch;
1918 0 controls.view_angles[1] = (float)yaw;
1919 0 controls.view_angles[2] = (float)roll;
1920 0 controls.has_movement = has_movement ? 1 : 0;
1921 0 controls.movement[0] = (float)forward;
1922 0 controls.movement[1] = (float)side;
1923 0 controls.movement[2] = (float)up;
1924 0 controls.impulse = (uint8_t)impulse;
1925 0 controls.weapon_select = (int32_t)weapon_select;
1926 0 check(aTHX_ tfnet_set_controls(connection_of(aTHX_ self), &controls));
1927  
1928 void
1929 set_cvar_answer(self, name, status, value)
1930 SV *self
1931 SV *name
1932 SV *status
1933 SV *value
1934 PREINIT:
1935 int cvar_status;
1936 CODE:
1937 0 cvar_status = value_of_name(aTHX_ CVAR_STATUS_NAMES, NAME_COUNT(CVAR_STATUS_NAMES), "cvar status", status);
1938 0 check(aTHX_ tfnet_set_cvar_answer(connection_of(aTHX_ self), sent_text(aTHX_ name), (tfnet_cvar_status)cvar_status,
1939 0 ▶ SvOK(value) ? sent_text(aTHX_ value) : NULL));
Branch: 1 branch
BranchTrueFalseTotal
SvOK(value) ? sent_text(aTHX_ value) : NULL)); 0 0 0
1940  
1941 void
1942 silence(self, milliseconds)
1943 SV *self
1944 int milliseconds
1945 CODE:
1946 0 check(aTHX_ tfnet_silence(connection_of(aTHX_ self), (int32_t)milliseconds));
1947  
1948 void
1949 disconnect(self, reason)
1950 SV *self
1951 SV *reason
1952 CODE:
1953 0 check(aTHX_ tfnet_disconnect(connection_of(aTHX_ self), sent_text(aTHX_ reason)));
1954  
1955 void
1956 subscribe_entity_index(self, index, on)
1957 SV *self
1958 int index
1959 int on
1960 CODE:
1961 0 check(aTHX_ tfnet_subscribe_entity_index(connection_of(aTHX_ self), (int32_t)index, on ? 1 : 0));
1962  
1963 void
1964 subscribe_entity_class(self, class_name, on)
1965 SV *self
1966 SV *class_name
1967 int on
1968 CODE:
1969 0 check(aTHX_ tfnet_subscribe_entity_class(connection_of(aTHX_ self), sent_text(aTHX_ class_name), on ? 1 : 0));
1970  
1971 void
1972 subscribe_entity_self(self, on)
1973 SV *self
1974 int on
1975 CODE:
1976 0 check(aTHX_ tfnet_subscribe_entity_self(connection_of(aTHX_ self), on ? 1 : 0));
1977  
1978 void
1979 subscribe_prop(self, name, on)
1980 SV *self
1981 SV *name
1982 int on
1983 CODE:
1984 0 check(aTHX_ tfnet_subscribe_prop(connection_of(aTHX_ self), sent_text(aTHX_ name), on ? 1 : 0));
1985  
1986 void
1987 subscribe_events(self, on)
1988 SV *self
1989 int on
1990 CODE:
1991 0 check(aTHX_ tfnet_subscribe_events(connection_of(aTHX_ self), on ? 1 : 0));
1992  
1993 void
1994 subscribe_event(self, name, on)
1995 SV *self
1996 SV *name
1997 int on
1998 CODE:
1999 0 check(aTHX_ tfnet_subscribe_event(connection_of(aTHX_ self), sent_text(aTHX_ name), on ? 1 : 0));
2000  
2001 void
2002 subscribe_source(self, source, on)
2003 SV *self
2004 SV *source
2005 int on
2006 PREINIT:
2007 int value;
2008 CODE:
2009 0 value = value_of_name(aTHX_ SOURCE_NAMES, NAME_COUNT(SOURCE_NAMES), "source", source);
2010 0 check(aTHX_ tfnet_subscribe(connection_of(aTHX_ self), (tfnet_source)value, on ? 1 : 0));
2011  
2012 void
2013 subscribe_stats(self, milliseconds)
2014 SV *self
2015 int milliseconds
2016 CODE:
2017 0 check(aTHX_ tfnet_subscribe_stats(connection_of(aTHX_ self), (int32_t)milliseconds));
2018  
2019 IV
2020 simulation_tick(class_name, value, tick, entity_index)
2021 const char *class_name
2022 IV value
2023 int tick
2024 int entity_index
2025 CODE:
2026 PERL_UNUSED_VAR(class_name);
2027 0 ▶ RETVAL = (IV)tfnet_simulation_tick((int64_t)value, (int32_t)tick, (int32_t)entity_index);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (IV)tfnet_simulation_tick((int64_t)value, (int32_t)tick, (int32_t)entity_index); 0 0 0
2028 OUTPUT:
2029 RETVAL
« blib/lib/Games/TF2/Demo/Item/ConsoleCommand.pm ↑ Summary blib/lib/Games/TF2/Demo/Handler.pm »