From d823912fbf5c9cac6283402d63a2ebb1ff66f97c Mon Sep 17 00:00:00 2001 From: div0 Date: Fri, 13 Feb 2009 18:46:12 +0000 Subject: [PATCH] try to make this shader parsing match DP's exactly git-svn-id: svn://svn.icculus.org/nexuiz/trunk@5871 f962a42d-fe04-0410-a3ab-8c8b0445ebaa --- misc/tools/shader-checksums.pl | 230 +++++++++++++++++++-------------- 1 file changed, 134 insertions(+), 96 deletions(-) diff --git a/misc/tools/shader-checksums.pl b/misc/tools/shader-checksums.pl index bd2b16a20..233648bfb 100755 --- a/misc/tools/shader-checksums.pl +++ b/misc/tools/shader-checksums.pl @@ -4,6 +4,64 @@ use strict; use warnings; use Digest::MD5; +my $data = do { undef local $/; ; }; +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 $find_texture_names = grep { /^-t$/ } @ARGV; my $dump_shaders = grep { /^-d$/ } @ARGV; my @match = grep { !/^-/ } @ARGV; -my $shadertext = ""; -my $level = 0; +my $shadertext; my $curshader; -while() -{ - s/\r//gs; - chomp; - - 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..@line-1]->[0]; - if(defined $brace_index) - { - unshift @nextline, splice @line, $brace_index || 1; - } +while(gettoken 0) +{ + $curshader = normalize_path $com_token; + $shadertext = ""; - # allow initial { token - if(@line >= 2 && $line[0] eq '{') + if(!gettoken(0) || $com_token ne "{") { - unshift @nextline, splice @line, 1; + die "parsing error - expected \"{\", found \"$com_token\""; } + + $shadertext .= "{\n"; - # in level 0, make the map name a separate token - if($level == 0 && @line >= 2) + while(gettoken 0) { - unshift @nextline, splice @line, 1; - } - - $shadertext .= "@line\n"; + last if $com_token eq "}"; - 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) + if($com_token eq "{") { - $level = 0; + # shader layer + # we're not actually parsing this + + $shadertext .= " {\n"; - 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) - { - # 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; - } + my @parameter = (); - if($dump_shaders) + while($com_token ne "\n" && $com_token ne "}") { - print "| $_\n" for split /\n/, $shadertext; + push @parameter, $com_token; + last unless gettoken 1; } + + $shadertext .= " @parameter\n"; + + last if $com_token eq "}"; } - $curshader = undef; + $shadertext .= " }\n"; } - } - elsif($level == 0) - { - warn "shader name already set" - if defined $curshader; - $curshader = normalize_path $line[0]; - $shadertext = ""; + + 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"; } - if(@nextline) + $shadertext .= "}\n"; + + if(!@match || grep { $_ eq $curshader } @match) { - @line = @nextline; - @nextline = (); - goto nextline; - } -} + printf "%s %s\n", Digest::MD5::md5_hex($shadertext), $curshader; -if($level != 0) -{ - die "missing } line"; + 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; + } + } } -- 2.39.2