3 # Fake demo "cutting" tool
4 # works by looking for time codes in the demo
5 # and injecting playback speed commands
13 $str =~ y/\000-\037//d;
19 my ($in, $out, $tc0, $tc1, $pattern, $capture);
21 my $mode = shift @ARGV;
22 $mode = 'help' if not defined $mode;
24 if($mode eq 'grep' && @ARGV == 2)
29 elsif($mode eq 'uncut' && @ARGV == 2)
34 elsif($mode eq 'cut' && (@ARGV == 4 || @ARGV == 5))
40 $capture = (@ARGV == 5);
44 die "Usage: $0 cut infile outfile tc_start tc_end [--capture], or $0 uncut infile outfile, or $0 grep infile pattern\n"
50 or die "Input and output file may not be the same!";
53 open my $infh, "<", $in
54 or die "open $in: $!";
58 if($mode ne 'grep') # cutting
60 open $outfh, ">", $out
61 or die "open $out: $!";
68 my $cdtrack = <$infh>;
69 print $outfh $cdtrack if $mode ne 'grep';
78 my $inject_buffer = "";
83 unless 4 == read $infh, my $length, 4;
84 $length = unpack("V", $length);
85 die "Invalid demo packet"
86 unless 12 == read $infh, my $angles, 12;
87 die "Invalid demo packet"
88 unless $length == read $infh, my($data), $length;
90 # remove existing cut marks
91 $data =~ s{^\011\n//CUTMARK\n[^\0]*\0}{};
93 if(substr($data, 0, 1) eq "\007") # svc_time
95 $tc = unpack "f", substr $data, 1, 4;
98 if($mode eq 'cut' && defined $tc)
102 $inject_buffer = "\011\n//CUTMARK\nslowmo 100\n\000";
105 if($demo_started < 1 && $tc > $tc0 - 50)
107 $inject_buffer = "\011\n//CUTMARK\nslowmo 10\n\000";
110 if($demo_started < 2 && $tc > $tc0 - 5)
112 $inject_buffer = "\011\n//CUTMARK\nslowmo 1\n\000";
115 if($demo_started < 3 && $tc > $tc0)
119 $inject_buffer = "\011\n//CUTMARK\ncl_capturevideo 1\n\000";
123 $inject_buffer = "\011\n//CUTMARK\nslowmo 0; defer 1 \"slowmo 1\"\n\000";
127 if(!$demo_stopped && $tc > $tc1)
131 $inject_buffer = "\011\n//CUTMARK\ncl_capturevideo 0; defer 0.5 \"disconnect\"\n\000";
135 $inject_buffer = "\011\n//CUTMARK\ndefer 0.5 \"disconnect\"\n\000";
140 elsif($mode eq 'grep')
142 if(my @l = ($data =~ /$pattern/))
154 print " \"", sanitize($_), "\"";
160 next if $mode eq 'grep';
161 if(length($inject_buffer . $data) < 65536)
163 $data = $inject_buffer . $data;
166 print $outfh pack("V", length $data);
167 print $outfh $angles;
171 close $outfh if $mode ne 'grep';