diff options
Diffstat (limited to 'src/engine/e_demorec.c')
| -rw-r--r-- | src/engine/e_demorec.c | 378 |
1 files changed, 316 insertions, 62 deletions
diff --git a/src/engine/e_demorec.c b/src/engine/e_demorec.c index 1fae2b77..07d7d628 100644 --- a/src/engine/e_demorec.c +++ b/src/engine/e_demorec.c @@ -2,12 +2,18 @@ #include <base/system.h> #include "e_demorec.h" #include "e_memheap.h" +#include "e_snapshot.h" +#include "e_compression.h" +#include "e_network.h" #include "e_if_other.h" static IOHANDLE record_file = 0; static const unsigned char header_marker[8] = {'T', 'W', 'D', 'E', 'M', 'O', 0, 1}; /* Record */ +static int record_lasttickmarker = -1; +static int record_lastkeyframe; +static unsigned char record_lastsnapshotdata[MAX_SNAPSHOT_SIZE]; int demorec_isrecording() { return record_file != 0; } @@ -37,24 +43,143 @@ int demorec_record_start(const char *filename, const char *netversion, const cha header.crc[3] = (crc)&0xff; io_write(record_file, &header, sizeof(header)); + record_lastkeyframe = -1; + record_lasttickmarker = -1; + dbg_msg("demorec/record", "Recording to '%s'", filename); return 0; } -void demorec_record_write(const char *type, int size, const void *data) +/* + Tickmarker + 7 = Always set + 6 = Keyframe flag + 0-5 = Delta tick + + Normal + 7 = Not set + 5-6 = Type + 0-4 = Size +*/ + +enum +{ + CHUNKTYPEFLAG_TICKMARKER = 0x80, + CHUNKTICKFLAG_KEYFRAME = 0x40, /* only when tickmarker is set*/ + + CHUNKMASK_TICK = 0x3f, + CHUNKMASK_TYPE = 0x60, + CHUNKMASK_SIZE = 0x1f, + + CHUNKTYPE_SNAPSHOT = 1, + CHUNKTYPE_MESSAGE = 2, + CHUNKTYPE_DELTA = 3, + + CHUNKFLAG_BIGSIZE = 0x10 +}; + +static void demorec_record_write_tickmarker(int tick, int keyframe) { - DEMOREC_CHUNK chunk; + if(record_lasttickmarker == -1 || tick-record_lasttickmarker > 63 || keyframe) + { + unsigned char chunk[5]; + chunk[0] = CHUNKTYPEFLAG_TICKMARKER; + chunk[1] = (tick>>24)&0xff; + chunk[2] = (tick>>16)&0xff; + chunk[3] = (tick>>8)&0xff; + chunk[4] = (tick)&0xff; + + if(keyframe) + chunk[0] |= CHUNKTICKFLAG_KEYFRAME; + + io_write(record_file, chunk, sizeof(chunk)); + } + else + { + unsigned char chunk[1]; + chunk[0] = CHUNKTYPEFLAG_TICKMARKER | (tick-record_lasttickmarker); + io_write(record_file, chunk, sizeof(chunk)); + } + + record_lasttickmarker = tick; +} + +static void demorec_record_write(int type, const void *data, int size) +{ + char buffer[64*1024]; + char buffer2[64*1024]; + unsigned char chunk[3]; + if(!record_file) return; + + size = intpack_compress(data, size, buffer); + size = netcommon_compress(buffer, size, buffer2, sizeof(buffer2)); + + + chunk[0] = ((type&0x3)<<5); + if(size < 30) + { + chunk[0] |= size; + io_write(record_file, chunk, 1); + } + else + { + if(size < 256) + { + chunk[0] |= 30; + chunk[1] = size&0xff; + io_write(record_file, chunk, 2); + } + else + { + chunk[0] |= 31; + chunk[1] = size&0xff; + chunk[2] = size>>8; + io_write(record_file, chunk, 3); + } + } - chunk.type[0] = type[0]; - chunk.type[1] = type[1]; - chunk.type[2] = type[2]; - chunk.type[3] = type[3]; - chunk.size = size; - swap_endian(&chunk.size, sizeof(int), 1); - io_write(record_file, &chunk, sizeof(chunk)); - io_write(record_file, data, size); + io_write(record_file, buffer2, size); +} + +void demorec_record_snapshot(int tick, const void *data, int size) +{ + record_emptydeltatick = -1; + + if(record_lastkeyframe == -1 || (tick-record_lastkeyframe) > SERVER_TICK_SPEED*5) + { + /* write full tickmarker */ + demorec_record_write_tickmarker(tick, 1); + + /* write snapshot */ + demorec_record_write(CHUNKTYPE_SNAPSHOT, data, size); + + record_lastkeyframe = tick; + mem_copy(record_lastsnapshotdata, data, size); + } + else + { + /* create delta, prepend tick */ + char delta_data[MAX_SNAPSHOT_SIZE+sizeof(int)]; + int delta_size; + + /* write tickmarker */ + demorec_record_write_tickmarker(tick, 0); + + delta_size = snapshot_create_delta((SNAPSHOT*)record_lastsnapshotdata, (SNAPSHOT*)data, &delta_data); + if(delta_size) + { + /* record delta */ + demorec_record_write(CHUNKTYPE_DELTA, delta_data, delta_size); + mem_copy(record_lastsnapshotdata, data, size); + } + } +} + +void demorec_record_message(const void *data, int size) +{ + demorec_record_write(CHUNKTYPE_MESSAGE, data, size); } int demorec_record_stop() @@ -69,7 +194,6 @@ int demorec_record_stop() } /* Playback */ - typedef struct KEYFRAME { long filepos; @@ -83,25 +207,77 @@ typedef struct KEYFRAME_SEARCH } KEYFRAME_SEARCH; static IOHANDLE play_file = 0; -static DEMOREC_PLAYCALLBACK play_callback = 0; +static DEMOREC_PLAYCALLBACK play_callback_snapshot = 0; +static DEMOREC_PLAYCALLBACK play_callback_message = 0; static KEYFRAME *keyframes = 0; static DEMOREC_PLAYBACKINFO playbackinfo; -const DEMOREC_PLAYBACKINFO *demorec_playback_info() { return &playbackinfo; } +static unsigned char playback_lastsnapshotdata[MAX_SNAPSHOT_SIZE]; +static int playback_lastsnapshotdata_size = -1; + +const DEMOREC_PLAYBACKINFO *demorec_playback_info() { return &playbackinfo; } int demorec_isplaying() { return play_file != 0; } -int demorec_playback_registercallback(DEMOREC_PLAYCALLBACK cb) +int demorec_playback_registercallbacks(DEMOREC_PLAYCALLBACK snapshot_cb, DEMOREC_PLAYCALLBACK message_cb) { - play_callback = cb; + play_callback_snapshot = snapshot_cb; + play_callback_message = message_cb; return 0; } -static int read_chunk_header(DEMOREC_CHUNK *chunk) +static int read_chunk_header(int *type, int *size, int *tick) { - if(io_read(play_file, chunk, sizeof(*chunk)) != sizeof(*chunk)) + unsigned char chunk = 0; + + *size = 0; + *type = 0; + + if(io_read(play_file, &chunk, sizeof(chunk)) != sizeof(chunk)) return -1; - swap_endian(&chunk->size, sizeof(int), 1); + + if(chunk&CHUNKTYPEFLAG_TICKMARKER) + { + /* decode tick marker */ + int tickdelta = chunk&(CHUNKMASK_TICK); + *type = chunk&(CHUNKTYPEFLAG_TICKMARKER|CHUNKTICKFLAG_KEYFRAME); + + if(tickdelta == 0) + { + unsigned char tickdata[4]; + if(io_read(play_file, tickdata, sizeof(tickdata)) != sizeof(tickdata)) + return -1; + *tick = (tickdata[0]<<24) | (tickdata[1]<<16) | (tickdata[2]<<8) | tickdata[3]; + } + else + { + *tick += tickdelta; + } + + } + else + { + /* decode normal chunk */ + *type = (chunk&CHUNKMASK_TYPE)>>5; + *size = chunk&CHUNKMASK_SIZE; + + if(*size == 30) + { + unsigned char sizedata[1]; + if(io_read(play_file, sizedata, sizeof(sizedata)) != sizeof(sizedata)) + return -1; + *size = sizedata[0]; + + } + else if(*size == 31) + { + unsigned char sizedata[2]; + if(io_read(play_file, sizedata, sizeof(sizedata)) != sizeof(sizedata)) + return -1; + *size = (sizedata[1]<<8) | sizedata[0]; + } + } + return 0; } @@ -111,7 +287,8 @@ static void scan_file() HEAP *heap = 0; KEYFRAME_SEARCH *first_key = 0; KEYFRAME_SEARCH *current_key = 0; - DEMOREC_CHUNK chunk; + /*DEMOREC_CHUNK chunk;*/ + int chunk_size, chunk_type, chunk_tick = 0; int i; heap = memheap_create(); @@ -122,37 +299,38 @@ static void scan_file() while(1) { long current_pos = io_tell(play_file); - - if(read_chunk_header(&chunk)) + + if(read_chunk_header(&chunk_type, &chunk_size, &chunk_tick)) break; /* read the chunk */ - - if(mem_comp(chunk.type, "TICK", 4) == 0) + if(chunk_type&CHUNKTYPEFLAG_TICKMARKER) { - KEYFRAME_SEARCH *key; - DEMOREC_TICKMARKER marker; - io_read(play_file, &marker, chunk.size); - swap_endian(&marker.tick, sizeof(int), 1); - - /* save the position */ - key = memheap_allocate(heap, sizeof(KEYFRAME_SEARCH)); - key->frame.filepos = current_pos; - key->frame.tick = marker.tick; - key->next = 0; - if(current_key) - current_key->next = key; - if(!first_key) - first_key = key; - current_key = key; + if(chunk_type&CHUNKTICKFLAG_KEYFRAME) + { + KEYFRAME_SEARCH *key; + + /* save the position */ + key = memheap_allocate(heap, sizeof(KEYFRAME_SEARCH)); + key->frame.filepos = current_pos; + key->frame.tick = chunk_tick; + key->next = 0; + if(current_key) + current_key->next = key; + if(!first_key) + first_key = key; + current_key = key; + playbackinfo.seekable_points++; + } if(playbackinfo.first_tick == -1) - playbackinfo.first_tick = marker.tick; - playbackinfo.last_tick = marker.tick; - playbackinfo.seekable_points++; + playbackinfo.first_tick = chunk_tick; + playbackinfo.last_tick = chunk_tick; + dbg_msg("", "%x %d", chunk_type, chunk_tick); } - else - io_skip(play_file, chunk.size); + else if(chunk_size) + io_skip(play_file, chunk_size); + } /* copy all the frames to an array instead for fast access */ @@ -167,41 +345,109 @@ static void scan_file() static void do_tick() { - static char buffer[64*1024]; - DEMOREC_CHUNK chunk; + static char compresseddata[MAX_SNAPSHOT_SIZE]; + static char decompressed[MAX_SNAPSHOT_SIZE]; + static char data[MAX_SNAPSHOT_SIZE]; + int chunk_size, chunk_type, chunk_tick; + int data_size; + int got_snapshot = 0; /* update ticks */ playbackinfo.previous_tick = playbackinfo.current_tick; playbackinfo.current_tick = playbackinfo.next_tick; + chunk_tick = playbackinfo.current_tick; while(1) { - int r = read_chunk_header(&chunk); - if(chunk.size > sizeof(buffer)) - { - dbg_msg("demorec/playback", "chunk is too big %d", chunk.size); - r = 1; - } - - if(r) + if(read_chunk_header(&chunk_type, &chunk_size, &chunk_tick)) { /* stop on error or eof */ + dbg_msg("demorec", "end of file"); demorec_playback_stop(); break; } /* read the chunk */ - io_read(play_file, buffer, chunk.size); + if(chunk_size) + { + if(io_read(play_file, compresseddata, chunk_size) != chunk_size) + { + /* stop on error or eof */ + dbg_msg("demorec", "error reading chunk"); + demorec_playback_stop(); + break; + } + + data_size = netcommon_decompress(compresseddata, chunk_size, decompressed, sizeof(decompressed)); + if(data_size < 0) + { + /* stop on error or eof */ + dbg_msg("demorec", "error during network decompression"); + demorec_playback_stop(); + break; + } - if(mem_comp(chunk.type, "TICK", 4) == 0) + data_size = intpack_decompress(decompressed, data_size, data); + + if(data_size < 0) + { + dbg_msg("demorec", "error during intpack decompression"); + demorec_playback_stop(); + break; + } + } + + if(chunk_type == CHUNKTYPE_DELTA) { - DEMOREC_TICKMARKER marker = *(DEMOREC_TICKMARKER *)buffer; - swap_endian(&marker.tick, sizeof(int), 1); - playbackinfo.next_tick = marker.tick; - break; + /* process delta snapshot */ + static char newsnap[MAX_SNAPSHOT_SIZE]; + + got_snapshot = 1; + + data_size = snapshot_unpack_delta((SNAPSHOT*)playback_lastsnapshotdata, (SNAPSHOT*)newsnap, data, data_size); + + if(data_size >= 0) + { + if(play_callback_snapshot) + play_callback_snapshot(newsnap, data_size); + + playback_lastsnapshotdata_size = data_size; + mem_copy(playback_lastsnapshotdata, newsnap, data_size); + } + else + dbg_msg("demorec", "error duing unpacking of delta, err=%d", data_size); + } + else if(chunk_type == CHUNKTYPE_SNAPSHOT) + { + /* process full snapshot */ + got_snapshot = 1; + + playback_lastsnapshotdata_size = data_size; + mem_copy(playback_lastsnapshotdata, data, data_size); + if(play_callback_snapshot) + play_callback_snapshot(data, data_size); + } + else + { + /* if there were no snapshots in this tick, replay the last one */ + if(!got_snapshot && play_callback_snapshot && playback_lastsnapshotdata_size != -1) + { + got_snapshot = 1; + play_callback_snapshot(playback_lastsnapshotdata, playback_lastsnapshotdata_size); + } + + /* check the remaining types */ + if(chunk_type&CHUNKTYPEFLAG_TICKMARKER) + { + playbackinfo.next_tick = chunk_tick; + break; + } + else if(chunk_type == CHUNKTYPE_MESSAGE) + { + if(play_callback_message) + play_callback_message(data, data_size); + } } - else if(play_callback) - play_callback(chunk, buffer); } } @@ -238,6 +484,8 @@ int demorec_playback_load(const char *filename) playbackinfo.current_tick = -1; playbackinfo.previous_tick = -1; playbackinfo.speed = 1; + + playback_lastsnapshotdata_size = -1; /* read the header */ io_read(play_file, &playbackinfo.header, sizeof(playbackinfo.header)); @@ -256,6 +504,12 @@ int demorec_playback_load(const char *filename) return 0; } +int demorec_playback_nextframe() +{ + do_tick(); + return demorec_isplaying(); +} + int demorec_playback_play() { /* fill in previous and next tick */ @@ -326,7 +580,7 @@ int demorec_playback_update() int64 curtick_start = (playbackinfo.current_tick)*freq/SERVER_TICK_SPEED; int64 prevtick_start = (playbackinfo.previous_tick)*freq/SERVER_TICK_SPEED; playbackinfo.intratick = (playbackinfo.current_time - prevtick_start) / (float)(curtick_start-prevtick_start); - playbackinfo.ticktime = (playbackinfo.current_time - prevtick_start) / (freq/(float)SERVER_TICK_SPEED); + playbackinfo.ticktime = (playbackinfo.current_time - prevtick_start) / (float)freq; } if(playbackinfo.current_tick == playbackinfo.previous_tick || |