project_files/frontlib/demo.c
changeset 7177 bf6cf4dd847a
parent 7175 038e3415100a
child 7179 f84805e6df03
equal deleted inserted replaced
7175:038e3415100a 7177:bf6cf4dd847a
     1 #include "demo.h"
       
     2 #include "logging.h"
       
     3 
       
     4 #include <stdbool.h>
       
     5 #include <stdio.h>
       
     6 #include <string.h>
       
     7 
       
     8 static int demo_record(flib_vector demoBuffer, const void *data, size_t len) {
       
     9 	if(flib_vector_append(demoBuffer, data, len) < len) {
       
    10 		flib_log_e("Error recording demo: Out of memory.");
       
    11 		return -1;
       
    12 	} else {
       
    13 		return 0;
       
    14 	}
       
    15 }
       
    16 
       
    17 int flib_demo_record_from_engine(flib_vector demoBuffer, const uint8_t *message, const char *playerName) {
       
    18 	if(!demoBuffer || !message || !playerName) {
       
    19 		flib_log_e("Call to flib_demo_record_from_engine with demoBuffer==null or message==null or playerName==null");
       
    20 		return -1;
       
    21 	}
       
    22 
       
    23 	if(strchr("?CEiQqHb", message[1])) {
       
    24 		return 0; // Those message types are not recorded in a demo.
       
    25 	}
       
    26 
       
    27 	if(message[1] == 's') {
       
    28 		if(message[0] >= 3) {
       
    29 			// Chat messages are reformatted to make them look as if they were received, not sent.
       
    30 			// Get the actual chat message as C string
       
    31 			char chatMsg[256];
       
    32 			memcpy(chatMsg, message+2, message[0]-3);
       
    33 			chatMsg[message[0]-3] = 0;
       
    34 
       
    35 			// If the message starts with /me, it will be displayed differently.
       
    36 			char converted[257];
       
    37 			bool memessage = message[0] >= 7 && !memcmp(message+2, "/me ", 4);
       
    38 			const char *template = memessage ? "s\x02* %s %s  " : "s\x01%s: %s  ";
       
    39 			int size = snprintf(converted+1, 256, template, playerName, chatMsg);
       
    40 			converted[0] = size>255 ? 255 : size;
       
    41 			return demo_record(demoBuffer, converted, converted[0]+1);
       
    42 		} else {
       
    43 			return 0; // Malformed chat message is no reason to abort...
       
    44 		}
       
    45 	} else {
       
    46 		return demo_record(demoBuffer, message, message[0]+1);
       
    47 	}
       
    48 }
       
    49 
       
    50 int flib_demo_record_to_engine(flib_vector demoBuffer, const uint8_t *message, size_t len) {
       
    51 	if(!demoBuffer || (len>0 && !message)) {
       
    52 		flib_log_e("Call to flib_demo_record_to_engine with demoBuffer==null or message==null");
       
    53 		return -1;
       
    54 	}
       
    55 	return demo_record(demoBuffer, message, len);
       
    56 }
       
    57 
       
    58 void flib_demo_replace_gamemode(flib_buffer buf, char gamemode) {
       
    59 	size_t msgStart = 0;
       
    60 	char *data = (char*)buf.data;
       
    61 	while(msgStart+2 < buf.size) {
       
    62 		if(!memcmp(data+msgStart, "\x02T", 2)) {
       
    63 			data[msgStart+2] = gamemode;
       
    64 		}
       
    65 		msgStart += (uint8_t)data[msgStart]+1;
       
    66 	}
       
    67 }