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 = "";
80 use constant DEMOMSG_CLIENT_TO_SERVER => 0x80000000;
84 unless 4 == read $infh, my $length, 4;
85 $length = unpack("V", $length);
86 if($length & DEMOMSG_CLIENT_TO_SERVER)
88 # client-to-server packet
89 $length = $length & ~DEMOMSG_CLIENT_TO_SERVER;
90 die "Invalid demo packet"
91 unless 12 == read $infh, my $angles, 12;
92 die "Invalid demo packet"
93 unless $length == read $infh, my($data), $length;
95 next if $mode eq 'grep';
96 print $outfh pack("V", length($data) | DEMOMSG_CLIENT_TO_SERVER);
100 die "Invalid demo packet"
101 unless 12 == read $infh, my $angles, 12;
102 die "Invalid demo packet"
103 unless $length == read $infh, my($data), $length;
105 # remove existing cut marks
106 $data =~ s{^\011\n//CUTMARK\n[^\0]*\0}{};
108 if(substr($data, 0, 1) eq "\007") # svc_time
110 $tc = unpack "f", substr $data, 1, 4;
113 if($mode eq 'cut' && defined $tc)
117 $inject_buffer = "\011\n//CUTMARK\nslowmo 100\n\000";
120 if($demo_started < 1 && $tc > $tc0 - 50)
122 $inject_buffer = "\011\n//CUTMARK\nslowmo 10\n\000";
125 if($demo_started < 2 && $tc > $tc0 - 5)
127 $inject_buffer = "\011\n//CUTMARK\nslowmo 1\n\000";
130 if($demo_started < 3 && $tc > $tc0)
134 $inject_buffer = "\011\n//CUTMARK\ncl_capturevideo 1\n\000";
138 $inject_buffer = "\011\n//CUTMARK\nslowmo 0; defer 1 \"slowmo 1\"\n\000";
142 if(!$demo_stopped && $tc > $tc1)
146 $inject_buffer = "\011\n//CUTMARK\ncl_capturevideo 0; defer 0.5 \"disconnect\"\n\000";
150 $inject_buffer = "\011\n//CUTMARK\ndefer 0.5 \"disconnect\"\n\000";
155 elsif($mode eq 'grep')
157 if(my @l = ($data =~ /$pattern/))
169 print " \"", sanitize($_), "\"";
175 next if $mode eq 'grep';
176 if(length($inject_buffer . $data) < 65536)
178 $data = $inject_buffer . $data;
181 print $outfh pack("V", length $data);
182 print $outfh $angles;
186 close $outfh if $mode ne 'grep';