r5871 - trunk/misc/tools
DONOTREPLY at icculus.org
DONOTREPLY at icculus.org
Fri Feb 13 13:46:13 EST 2009
Author: div0
Date: 2009-02-13 13:46:12 -0500 (Fri, 13 Feb 2009)
New Revision: 5871
Modified:
trunk/misc/tools/shader-checksums.pl
Log:
try to make this shader parsing match DP's exactly
Modified: trunk/misc/tools/shader-checksums.pl
===================================================================
--- trunk/misc/tools/shader-checksums.pl 2009-02-13 18:18:08 UTC (rev 5870)
+++ trunk/misc/tools/shader-checksums.pl 2009-02-13 18:46:12 UTC (rev 5871)
@@ -4,6 +4,64 @@
use warnings;
use Digest::MD5;
+my $data = do { undef local $/; <STDIN>; };
+my $com_token;
+sub gettoken($)
+{
+ my ($returnnewline) = @_;
+ $com_token = undef;
+
+skipwhite:
+ if($returnnewline)
+ {
+ $data =~ s/^[ \t]*//;
+ }
+ else
+ {
+ $data =~ s/^[ \t\r\n]*//;
+ }
+
+ return 0
+ if $data eq "";
+
+ $data =~ s/^\r\n/\n/;
+
+ $data =~ s/^\/\/[^\r\n]*// and goto skipwhite;
+
+ $data =~ s/^\/\*.*?\*\/// and goto skipwhite;
+
+ if($data =~ s/^(["'])(.*?)\1//)
+ {
+ my $str = $1;
+ my %q = ( "\\" => "\\", "n" => "\n", "t" => "\t" );
+ $str =~ s/\\([\\nt])/$q{$1}/ge;
+ $com_token = $str;
+ return 1;
+ }
+
+ if($data =~ s/^\r//)
+ {
+ $com_token = "\n";
+ return 1;
+ }
+
+ if($data =~ s/^([][\n{})(:,;])//)
+ {
+ $com_token = $1;
+ return 1;
+ }
+
+ if($data =~ s/^([^][ \t\r\n{})(:,;]*)//)
+ {
+ $com_token = $1;
+ return 1;
+ }
+
+ die "fallthrough?";
+ $com_token = "";
+ return 1;
+}
+
sub normalize_path($)
{
my ($p) = @_;
@@ -17,122 +75,102 @@
my $dump_shaders = grep { /^-d$/ } @ARGV;
my @match = grep { !/^-/ } @ARGV;
-my $shadertext = "";
-my $level = 0;
+my $shadertext;
my $curshader;
-while(<STDIN>)
+
+while(gettoken 0)
{
- s/\r//gs;
- chomp;
+ $curshader = normalize_path $com_token;
+ $shadertext = "";
- s/\/\/.*//s;
- s/^\s+//;
- s/\s+$//;
- next if /^$/;
-
- my @line = map { s/"//g; $_; } split /\s+/, $_;
- my @nextline = ();
-
-nextline:
- # allow trailing } token
- my $brace_index = [grep { $line[$_] eq "}" } 0.. at line-1]->[0];
- if(defined $brace_index)
+ if(!gettoken(0) || $com_token ne "{")
{
- unshift @nextline, splice @line, $brace_index || 1;
+ die "parsing error - expected \"{\", found \"$com_token\"";
}
+
+ $shadertext .= "{\n";
- # allow initial { token
- if(@line >= 2 && $line[0] eq '{')
+ while(gettoken 0)
{
- unshift @nextline, splice @line, 1;
- }
+ last if $com_token eq "}";
- # in level 0, make the map name a separate token
- if($level == 0 && @line >= 2)
- {
- unshift @nextline, splice @line, 1;
- }
+ if($com_token eq "{")
+ {
+ # shader layer
+ # we're not actually parsing this
- $shadertext .= "@line\n";
+ $shadertext .= " {\n";
- if($line[0] eq '{')
- {
- die "{ line without shader name"
- unless defined $curshader;
- die "{ line in level $level"
- if $level >= 2;
- ++$level;
- }
- elsif($line[0] eq '}')
- {
- die "} line contains other stuff"
- unless @line == 1;
- die "} line without shader name"
- unless defined $curshader;
- die "{ line in level $level"
- if $level <= 0;
- --$level;
- if($level <= 0)
- {
- $level = 0;
-
- if(!@match || grep { $_ eq $curshader } @match)
+ while(gettoken 0)
{
- printf "%s %s\n", Digest::MD5::md5_hex($shadertext), $curshader;
+ last if $com_token eq "}";
+ next if $com_token eq "\n";
- if($find_texture_names)
+ my @parameter = ();
+
+ while($com_token ne "\n" && $com_token ne "}")
{
- # find out possibly loaded textures
- my @maps = ($shadertext =~ /^(?:clampmap|map|q3r_lightimage|q3r_editorimage) ([^\$].*)$/gim);
- for($shadertext =~ /^animmap \S+ (.*)$/gim)
- {
- push @maps, split / /, $_;
- }
- for($shadertext =~ /^skyparms (.*)$/gim)
- {
- for(split / /, $_)
- {
- next if $_ eq "-";
- push @maps, "$_"."_lf";
- push @maps, "$_"."_ft";
- push @maps, "$_"."_rt";
- push @maps, "$_"."_bk";
- push @maps, "$_"."_up";
- push @maps, "$_"."_dn";
- }
- }
- @maps = ($curshader)
- if @maps == 0;
- printf "* %s %s\n", $_, $curshader
- for map { normalize_path $_ } @maps;
+ push @parameter, $com_token;
+ last unless gettoken 1;
}
- if($dump_shaders)
- {
- print "| $_\n" for split /\n/, $shadertext;
- }
+ $shadertext .= " @parameter\n";
+
+ last if $com_token eq "}";
}
- $curshader = undef;
+ $shadertext .= " }\n";
}
+
+ my @parameter = ();
+
+ while($com_token ne "\n" && $com_token ne "}")
+ {
+ push @parameter, $com_token;
+ last unless gettoken 1;
+ }
+
+ next if @parameter < 1;
+
+ $shadertext .= " @parameter\n";
}
- elsif($level == 0)
+
+ $shadertext .= "}\n";
+
+ if(!@match || grep { $_ eq $curshader } @match)
{
- warn "shader name already set"
- if defined $curshader;
- $curshader = normalize_path $line[0];
- $shadertext = "";
- }
+ printf "%s %s\n", Digest::MD5::md5_hex($shadertext), $curshader;
- if(@nextline)
- {
- @line = @nextline;
- @nextline = ();
- goto nextline;
+ if($find_texture_names)
+ {
+ # find out possibly loaded textures
+ my @maps = ($shadertext =~ /^(?:clampmap|map|q3r_lightimage|q3r_editorimage) ([^\$].*)$/gim);
+ for($shadertext =~ /^animmap \S+ (.*)$/gim)
+ {
+ push @maps, split / /, $_;
+ }
+ for($shadertext =~ /^skyparms (.*)$/gim)
+ {
+ for(split / /, $_)
+ {
+ next if $_ eq "-";
+ push @maps, "$_"."_lf";
+ push @maps, "$_"."_ft";
+ push @maps, "$_"."_rt";
+ push @maps, "$_"."_bk";
+ push @maps, "$_"."_up";
+ push @maps, "$_"."_dn";
+ }
+ }
+ @maps = ($curshader)
+ if @maps == 0;
+ printf "* %s %s\n", $_, $curshader
+ for map { normalize_path $_ } @maps;
+ }
+
+ if($dump_shaders)
+ {
+ print "| $_\n" for split /\n/, $shadertext;
+ }
}
}
-
-if($level != 0)
-{
- die "missing } line";
-}
More information about the nexuiz-commits
mailing list