]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/tools/demosplit.pl
e61de1ccd8a4169eb1ce2b1fc9fcb1afd94e364f
[divverent/nexuiz.git] / misc / tools / demosplit.pl
1 #!/usr/bin/perl
2
3 # usage:
4 #   ./demosplit.pl demo.dem
5 # splits the demo into separate demos for each map played and writes them
6 # to demo-0000.dem, demo-0001.dem ...
7
8 use strict;
9 use warnings;
10
11 # constants
12 my $svc_signon = "\001";
13
14 # opening the files
15
16 die "Usage: $0 infile"
17         if @ARGV != 1;
18 my ($in) = @ARGV;
19
20 my $demoname = [$in =~ /^(.*)\.dem$/]->[0];
21 $demoname = "out"
22         if not defined $demoname;
23
24 open my $infh, "<", $in
25         or die "open $in: $!";
26 binmode $infh;
27
28 # 1. CD track
29
30 $/ = "\012";
31 my $cdtrack = <$infh>;
32
33 # 2. packets
34
35 my $outfh;
36 my $outnum = 0;
37
38 for(;;)
39 {
40         last
41                 unless 4 == read $infh, my $length, 4;
42         $length = unpack("V", $length);
43         die "Invalid demo packet"
44                 unless 12 == read $infh, my $angles, 12;
45         die "Invalid demo packet"
46                 unless $length == read $infh, my($data), $length;
47
48         use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper $data;
49
50         if($data =~ m{
51                 ^
52                 $svc_signon
53                 $
54         }sx)
55         {
56                 close $outfh
57                         if $outfh;
58                 my $outname = sprintf("%s-%04d.dem", $demoname, $outnum++);
59                 open $outfh, ">", $outname
60                         or die "open $outname: $!";
61                 binmode $outfh;
62                 print $outfh $cdtrack;
63                 print "Writing to $outname...\n";
64         }
65
66         #die "No signon received"
67         #       unless $outfh;
68         #print $outfh pack("V", length $data);
69         #print $outfh $angles;
70         #print $outfh $data;
71 }
72
73 close $outfh;
74 close $infh;