File: shader-checksums.pl

package info (click to toggle)
nexuiz-data 2.5.2-9
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,294,288 kB
  • sloc: ansic: 10,523; perl: 6,845; sh: 2,188; java: 1,417; xml: 969; lisp: 519; ruby: 136; makefile: 115
file content (176 lines) | stat: -rwxr-xr-x 2,987 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/perl

use strict;
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) = @_;
	$p =~ s/\\/\//g;
	$p =~ s/(?:\.jpg|\.png|\.tga)$//gi;
	$p = lc $p;
	return $p;
}

my $find_texture_names = grep { /^-t$/ } @ARGV;
my $dump_shaders = grep { /^-d$/ } @ARGV;
my @match = grep { !/^-/ } @ARGV;

my $shadertext;
my $curshader;

while(gettoken 0)
{
	$curshader = normalize_path $com_token;
	$shadertext = "";

	if(!gettoken(0) || $com_token ne "{")
	{
		die "parsing error - expected \"{\", found \"$com_token\"";
	}
	
	$shadertext .= "{\n";

	while(gettoken 0)
	{
		last if $com_token eq "}";

		if($com_token eq "{")
		{
			# shader layer
			# we're not actually parsing this

			$shadertext .= "	{\n";

			while(gettoken 0)
			{
				last if $com_token eq "}";
				next if $com_token eq "\n";

				my @parameter = ();

				while($com_token ne "\n" && $com_token ne "}")
				{
					push @parameter, $com_token;
					last unless gettoken 1;
				}

				$shadertext .= "		@parameter\n";

				last if $com_token eq "}";
			}

			$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";
	}

	$shadertext .= "}\n";

	if(!@match || grep { $_ eq $curshader } @match)
	{
		printf "%s  %s\n", Digest::MD5::md5_hex($shadertext), $curshader;

		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;
		}
	}
}