From e7104a805effb656a0350e603ed6947f81375048 Mon Sep 17 00:00:00 2001 From: div0 Date: Sun, 7 Sep 2008 15:43:59 +0000 Subject: [PATCH] add a weird demo "cutter", and a race demo record extractor git-svn-id: svn://svn.icculus.org/nexuiz/trunk@4402 f962a42d-fe04-0410-a3ab-8c8b0445ebaa --- misc/demotc-race-record-extractor.sh | 21 ++++ misc/demotc.pl | 140 +++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 misc/demotc-race-record-extractor.sh create mode 100755 misc/demotc.pl diff --git a/misc/demotc-race-record-extractor.sh b/misc/demotc-race-record-extractor.sh new file mode 100644 index 000000000..fa1712d7a --- /dev/null +++ b/misc/demotc-race-record-extractor.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +d=$1 +i=0 +perl demotc.pl "$d" 'all-time fastest lap record with (.*)\n' | while IFS=" " read -r timecode result; do + timecode=${timecode%:} + result=${result#\"} + result=${result%\"} + + minutes=${result%%:*} + result=${result#*:} + seconds=${result%%.*} + result=${result#*.} + tenths=$result + + timecode_start=`echo "$timecode - $minutes*60 - $seconds - $tenths*0.1 - 2" | bc -l` + timecode_end=`echo "$timecode + 2" | bc -l` + i=$(($i + 1)) + perl demotc.pl "$d" "playback-$i.dem" "$timecode_start" "$timecode_end" + perl demotc.pl "$d" "capture-$i.dem" "$timecode_start" "$timecode_end" --capture +done diff --git a/misc/demotc.pl b/misc/demotc.pl new file mode 100755 index 000000000..ac441345b --- /dev/null +++ b/misc/demotc.pl @@ -0,0 +1,140 @@ +#!/usr/bin/perl + +# Fake demo "cutting" tool +# works by looking for time codes in the demo +# and injecting playback speed commands + +# usage: +# ./demotc.pl infile outfile tc0 tc1 - cuts the demo file for playback +# ./demotc.pl infile outfile tc0 tc1 --capture - cuts the demo file for video capture (it will automatically write a dpvideo001.avi file of the selected time range when playing) +# ./demotc.pl infile pattern - looks for a pattern, prints parentheses matches + +use strict; +use warnings; + +sub sanitize($) +{ + my ($str) = @_; + $str =~ y/\000-\037//d; + return $str; +} + +# opening the files + +die "Usage: $0 infile outfile tc_start tc_end [--capture], or $0 infile pattern" + if @ARGV != 2 && @ARGV != 4 && @ARGV != 5; +my ($in, $out, $tc0, $tc1, $capture) = (@ARGV, undef, undef, undef); + +$in ne $out + or die "Input and output file may not be the same!"; + +open my $infh, "<", $in + or die "open $in: $!"; +binmode $infh; + +my $outfh; +if(defined $tc0) # cutting +{ + open $outfh, ">", $out + or die "open $out: $!"; + binmode $outfh; +} + +# 1. CD track + +$/ = "\012"; +my $cdtrack = <$infh>; +print $outfh $cdtrack if $outfh; + +# 2. packets + +my $tc = 0; + +my $first = 1; +my $demo_started = 0; +my $demo_stopped = 0; +my $inject_buffer = ""; + +for(;;) +{ + last + unless 4 == read $infh, my $length, 4; + $length = unpack("V", $length); + die "Invalid demo packet" + unless 12 == read $infh, my $angles, 12; + die "Invalid demo packet" + unless $length == read $infh, my($data), $length; + + if(substr($data, 0, 1) eq "\007") # svc_time + { + $tc = unpack "f", substr $data, 1, 4; + } + + if(defined $tc0) + { + if($first) + { + $inject_buffer = "\011\nslowmo 100\n\000"; + $first = 0; + } + if($demo_started < 1 && $tc > $tc0 - 50) + { + $inject_buffer = "\011\nslowmo 10\n\000"; + $demo_started = 1; + } + if($demo_started < 2 && $tc > $tc0 - 5) + { + $inject_buffer = "\011\nslowmo 1\n\000"; + $demo_started = 2; + } + if($demo_started < 3 && $tc > $tc0) + { + if($capture) + { + $inject_buffer = "\011\ncl_capturevideo 1\n\000"; + } + else + { + $inject_buffer = "\011\nslowmo 0; defer 1 \"slowmo 1\"\n\000"; + } + $demo_started = 3; + } + if(!$demo_stopped && $tc > $tc1) + { + if($capture) + { + $inject_buffer = "\011\ncl_capturevideo 0; defer 0.5 \"disconnect\"\n\000"; + } + else + { + $inject_buffer = "\011\ndefer 0.5 \"disconnect\"\n\000"; + } + $demo_stopped = 1; + } + } + else + { + if(my @l = ($data =~ /$out/)) + { + print "$tc:"; + for(@l) + { + print " \"", sanitize($_), "\""; + } + print "\n"; + } + } + + next unless $outfh; + if(length($data . $inject_buffer) < 65536) + { + $data .= $inject_buffer; + $inject_buffer = ""; + } + print $outfh pack("V", length $data); + print $outfh $angles; + print $outfh $data; +} + +close $outfh if $outfh; +close $infh; -- 2.39.2