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);
101 die "Invalid demo packet"
102 unless 12 == read $infh, my $angles, 12;
103 die "Invalid demo packet"
104 unless $length == read $infh, my($data), $length;
106 # remove existing cut marks
107 $data =~ s{^\011\n//CUTMARK\n[^\0]*\0}{};
109 if(substr($data, 0, 1) eq "\007") # svc_time
111 $tc = unpack "f", substr $data, 1, 4;
114 if($mode eq 'cut' && defined $tc)
118 $inject_buffer = "\011\n//CUTMARK\nslowmo 100\n\000";
121 if($demo_started < 1 && $tc > $tc0 - 50)
123 $inject_buffer = "\011\n//CUTMARK\nslowmo 10\n\000";
126 if($demo_started < 2 && $tc > $tc0 - 5)
128 $inject_buffer = "\011\n//CUTMARK\nslowmo 1\n\000";
131 if($demo_started < 3 && $tc > $tc0)
135 $inject_buffer = "\011\n//CUTMARK\ncl_capturevideo 1\n\000";
139 $inject_buffer = "\011\n//CUTMARK\nslowmo 0; defer 1 \"slowmo 1\"\n\000";
143 if(!$demo_stopped && $tc > $tc1)
147 $inject_buffer = "\011\n//CUTMARK\ncl_capturevideo 0; defer 0.5 \"disconnect\"\n\000";
151 $inject_buffer = "\011\n//CUTMARK\ndefer 0.5 \"disconnect\"\n\000";
156 elsif($mode eq 'grep')
158 if(my @l = ($data =~ /$pattern/))
170 print " \"", sanitize($_), "\"";
176 next if $mode eq 'grep';
177 if(length($inject_buffer . $data) < 65536)
179 $data = $inject_buffer . $data;
182 print $outfh pack("V", length $data);
183 print $outfh $angles;
187 close $outfh if $mode ne 'grep';