]> git.xonotic.org Git - xonotic/xonotic.git/blobdiff - misc/tools/bsptool.pl
bsptool: add a feature to replace shaders in finished bsp
[xonotic/xonotic.git] / misc / tools / bsptool.pl
index 84f23233aa388f3f91c86422623f394f6db5dc57..1a4352f2bf7860dba3a626c1ba194d043cfb77b5 100755 (executable)
@@ -22,6 +22,7 @@ Operations are:
   Information requests:
     -i                print info about the BSP file
     -xlumpname        extract a lump (see -i)
+    -S                list used shaders
 
   Changes:
     -dlumpname        delete a lump (see -i)
@@ -32,6 +33,7 @@ Operations are:
     -lpng             externalize the lightmaps as PNG
     -ltga             externalize the lightmaps as TGA
     -mMESSAGE         set the BSP file comment message
+    -Sfrom=to         replace a texture (shader) by name (already replaced shaders are not touched)
 
   Save commands:
     -o                actually apply the changes to the BSP
@@ -93,7 +95,11 @@ sub DecodeLump($@)
                        $spec .= "$2$3 ";
                        my $f = $1;
                        my $n = $3;
-                       if($n eq '')
+                       if($2 eq 'a')
+                       {
+                               push @decoders, sub { ($item->{$f} = $data[$idx++]) =~ s/\0//g; };
+                       }
+                       elsif($n eq '')
                        {
                                push @decoders, sub { $item->{$f} = $data[$idx++]; };
                        }
@@ -141,7 +147,11 @@ sub EncodeLump($@)
                        my $spec = "$2$3";
                        my $f = $1;
                        my $n = $3;
-                       if($n eq '')
+                       if($2 eq 'a')
+                       {
+                               push @encoders, sub { $data .= pack $spec, $item->{$f}; };
+                       }
+                       elsif($n eq '')
                        {
                                push @encoders, sub { $data .= pack $spec, $item->{$f}; };
                        }
@@ -218,12 +228,14 @@ for(@ARGV)
 {
        if(/^-i$/) # info
        {
-               my $total = 17 * 8 + 8 + length($msg);
+               my $msgalign = [0, 3, 2, 1]->[length($msg) % 4];
+               my $total = 17 * 8 + 8 + length($msg) + $msgalign;
                my $max = 0;
                for(0..@bsp-1)
                {
                        my $nl = length $bsp[$_]->[2];
-                       $total += $nl;
+                       my $align = [0, 3, 2, 1]->[$nl % 4];
+                       $total += $nl + $align;
                        print "BSP lump $_ ($lumpname[$_]): offset $bsp[$_]->[0] length $bsp[$_]->[1] newlength $nl\n";
                        my $endpos = $bsp[$_]->[0] + $bsp[$_]->[1];
                        $max = $endpos if $max < $endpos;
@@ -479,6 +491,30 @@ for(@ARGV)
                        unless defined $id;
                print $bsp[$id]->[2];
        }
+       elsif(/^-S(.*)=(.*)$/)
+       {
+               my $from = $1;
+               my $to = $2;
+               our @replaced = ();
+               my @l = DecodeLump $bsp[$lumpid{textures}]->[2], qw/name=a64 flags=V contents=V/;
+               for(0..@l-1)
+               {
+                       next if $replaced[$_];
+                       if($l[$_]->{name} eq $from)
+                       {
+                               $replaced[$_] = 1;
+                               $l[$_]->{name} = $to;
+                       }
+               }
+               $bsp[$lumpid{textures}]->[2] = EncodeLump \@l, qw/name=a64 flags=V contents=V/;
+       }
+       elsif(/^-S$/)
+       {
+               for(DecodeLump $bsp[$lumpid{textures}]->[2], qw/name=a64 flags=V contents=V/)
+               {
+                       print "$_->{name}\n";
+               }
+       }
        elsif(/^-o(.+)?$/) # write the final BSP file
        {
                my $outfile = $1;
@@ -487,18 +523,23 @@ for(@ARGV)
                open my $fh, ">", $outfile
                        or die "$outfile: $!";
                print $fh $header;
-               my $pos = 17 * 8 + tell($fh) + length $msg;
+               my $msgalign = [0, 3, 2, 1]->[length($msg) % 4];
+               my $pos = 17 * 8 + tell($fh) + length($msg) + $msgalign;
                for(@bsp)
                {
+                       my $align = [0, 3, 2, 1]->[length($_->[2]) % 4];
                        $_->[0] = $pos;
                        $_->[1] = length $_->[2];
-                       $pos += $_->[1];
+                       $pos += $_->[1] + $align;
                        print $fh pack "VV", $_->[0], $_->[1];
                }
                print $fh $msg;
+               print $fh "\x00" x $msgalign;
                for(@bsp)
                {
+                       my $align = [0, 3, 2, 1]->[length($_->[2]) % 4];
                        print $fh $_->[2];
+                       print $fh "\x00" x $align;
                }
                close $fh;
                print STDERR "Wrote $outfile\n";