File: checkrefs.pl

package info (click to toggle)
0ad 0.0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,292 kB
  • sloc: cpp: 245,166; ansic: 200,249; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (581 lines) | stat: -rwxr-xr-x 15,745 bytes parent folder | download | duplicates (4)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use strict;
use warnings;
use Data::Dumper;
use File::Find;
use XML::Simple;
use JSON;

use Entity;

use constant CHECK_MAPS_XML => 0;
use constant ROOT_ACTORS => 1;

my @files;
my @roots;
my @deps;

my $vfsroot = '../../../binaries/data/mods';

sub vfs_to_physical
{
    my ($vfspath) = @_;
    my $fn = "$vfsroot/public/$vfspath";
    if (not -e $fn)
    {
        $fn = "$vfsroot/mod/$vfspath";
    }
    return $fn;
}

sub vfs_to_relative_to_mods
{
    my ($vfspath) = @_;
    my $fn = "public/$vfspath";
    return $fn;
}

sub find_files
{
    my ($vfspath, $extn) = @_;
    my @files;
    my $find_process = sub {
        return $File::Find::prune = 1 if $_ eq '.svn';
        my $n = $File::Find::name;
        return if /~$/;
        return unless -f $_;
        return unless /\.($extn)$/;
        $n =~ s~\Q$vfsroot\E/(public|mod)/~~;
        push @files, $n;
    };
    find({ wanted => $find_process }, "$vfsroot/public/$vfspath");
    find({ wanted => $find_process }, "$vfsroot/mod/$vfspath") if -d "$vfsroot/mod/$vfspath";

    return @files;
}

sub parse_json_file
{
    my ($vfspath) = @_;
    open my $fh, vfs_to_physical($vfspath) or die "Failed to open '$vfspath': $!";
    # decode_json expects a UTF-8 string and doesn't handle BOMs, so we strip those
    # (see http://trac.wildfiregames.com/ticket/1556)
    return decode_json(do { local $/; my $file = <$fh>; $file =~ s/^\xEF\xBB\xBF//; $file });
}

sub add_entities
{
    print "Loading entities...\n";

    my @entfiles = find_files('simulation/templates', 'xml');
    s~^simulation/templates/(.*)\.xml$~$1~ for @entfiles;

    for my $f (sort @entfiles)
    {
        my $path = "simulation/templates/$f.xml";
        push @files, $path;
        my $ent = Entity::load_inherited($f);

        push @deps, [ $path, "simulation/templates/" . $ent->{Entity}{'@parent'}{' content'} . ".xml" ] if $ent->{Entity}{'@parent'};

        if ($f !~ /^template_/)
        {
            push @roots, $path;
            if ($ent->{Entity}{VisualActor})
            {
                push @deps, [ $path, "art/actors/" . $ent->{Entity}{VisualActor}{Actor}{' content'} ] if $ent->{Entity}{VisualActor}{Actor};
                push @deps, [ $path, "art/actors/" . $ent->{Entity}{VisualActor}{FoundationActor}{' content'} ] if $ent->{Entity}{VisualActor}{FoundationActor};
            }

            if ($ent->{Entity}{Sound})
            {
                my $gender = $ent->{Entity}{Identity}{Gender}{' content'} || "male";
                my $lang = $ent->{Entity}{Identity}{Lang}{' content'} || "greek";

                for (grep ref($_), values %{$ent->{Entity}{Sound}{SoundGroups}})
                {
                    # see simulation/components/Sound.js and Identity.js for explanation
                    my $soundPath = $_->{' content'};
                    $soundPath =~ s/{gender}/$gender/g;
                    $soundPath =~ s/{lang}/$lang/g;
                    push @deps, [ $path, "audio/" . $soundPath ];
                }
            }

            if ($ent->{Entity}{Identity})
            {
                push @deps, [ $path, "art/textures/ui/session/portraits/" . $ent->{Entity}{Identity}{Icon}{' content'} ] if $ent->{Entity}{Identity}{Icon};
            }
        }
    }
}

sub add_actors
{
    print "Loading actors...\n";

    my @actorfiles = find_files('art/actors', 'xml');
    for my $f (sort @actorfiles)
    {
        push @files, $f;

        push @roots, $f if ROOT_ACTORS;

        my $actor = XMLin(vfs_to_physical($f), ForceArray => [qw(group variant texture prop animation)], KeyAttr => []) or die "Failed to parse '$f': $!";

        for my $group (@{$actor->{group}})
        {
            for my $variant (@{$group->{variant}})
            {
                push @deps, [ $f, "art/meshes/$variant->{mesh}" ] if $variant->{mesh};
                push @deps, [ $f, "art/particles/$variant->{particles}{file}" ] if $variant->{particles}{file};
                for my $tex (@{$variant->{textures}{texture}})
                {
                    push @deps, [ $f, "art/textures/skins/$tex->{file}" ] if $tex->{file};
                }
                for my $prop (@{$variant->{props}{prop}})
                {
                    push @deps, [ $f, "art/actors/$prop->{actor}" ] if $prop->{actor};
                }
                for my $anim (@{$variant->{animations}{animation}})
                {
                    push @deps, [ $f, "art/animation/$anim->{file}" ] if $anim->{file};
                }
            }
        }

        push @deps, [ $f, "art/materials/$actor->{material}" ] if $actor->{material};
    }
}

sub add_art
{
    print "Loading art files...\n";
    push @files, find_files('art/textures/particles', 'dds|png|jpg|tga');
    push @files, find_files('art/textures/terrain', 'dds|png|jpg|tga');
    push @files, find_files('art/textures/skins', 'dds|png|jpg|tga');
    push @files, find_files('art/meshes', 'pmd|dae');
    push @files, find_files('art/animation', 'psa|dae');
}

sub add_materials
{
    print "Loading materials...\n";
    my @materialfiles = find_files('art/materials', 'xml');
    for my $f (sort @materialfiles)
    {
        push @files, $f;

        my $material = XMLin(vfs_to_physical($f), ForceArray => [qw(alternative)], KeyAttr => []);
        for my $alternative (@{$material->{alternative}})
        {
            push @deps, [ $f, "art/materials/$alternative->{material}" ] if $alternative->{material};
        }
    }
}

sub add_particles
{
    print "Loading particles...\n";
    my @particlefiles = find_files('art/particles', 'xml');
    for my $f (sort @particlefiles)
    {
        push @files, $f;

        my $particle = XMLin(vfs_to_physical($f));
        push @deps, [ $f, "$particle->{texture}" ] if $particle->{texture};
    }
}

sub add_maps_xml
{
    print "Loading maps XML...\n";
    my @mapfiles = find_files('maps/scenarios', 'xml');
    push @mapfiles, find_files('maps/skirmishes', 'xml');
    for my $f (sort @mapfiles)
    {
        print "  $f\n";

        push @files, $f;

        push @roots, $f;

        my $map = XMLin(vfs_to_physical($f), ForceArray => [qw(Entity)], KeyAttr => []) or die "Failed to parse '$f': $!";

        my %used;
        for my $entity (@{$map->{Entities}{Entity}})
        {
            $used{$entity->{Template}} = 1;
        }

        for my $template (keys %used)
        {
            if ($template =~ /^actor\|(.*)$/)
            {
                # Handle special 'actor|' case
                push @deps, [ $f, "art/actors/$1" ];
            }
            else
            {
                if ($template =~ /^resource\|(.*)$/)
                {
                    # Handle special 'resource|' case
                    $template = $1;
                }
                push @deps, [ $f, "simulation/templates/$template.xml" ];
            }
        }

        # Map previews
        my $settings = decode_json($map->{ScriptSettings});
        push @deps, [ $f, "art/textures/ui/session/icons/mappreview/" . $settings->{Preview} ] if $settings->{Preview};
    }
}

sub add_maps_pmp
{
    print "Loading maps PMP...\n";

    # Need to generate terrain texture filename=>path lookup first
    my %terrains;
    for my $f (find_files('art/terrains', 'xml'))
    {
        $f =~ /([^\/]+)\.xml/ or die;

        # ignore terrains.xml
        if ($f !~ /terrains.xml$/)
        {
            warn "Duplicate terrain name '$1' (from '$terrains{$1}' and '$f')\n" if $terrains{$1};
            $terrains{$1} = $f;
        }
    }

    my @mapfiles = find_files('maps/scenarios', 'pmp');
    push @mapfiles, find_files('maps/skirmishes', 'pmp');
    for my $f (sort @mapfiles)
    {
        push @files, $f;

        push @roots, $f;

        open my $fh, vfs_to_physical($f) or die "Failed to open '$f': $!";
        binmode $fh;

        my $buf;

        read $fh, $buf, 4;
        die "Invalid PMP header ($buf) in '$f'" unless $buf eq "PSMP";

        read $fh, $buf, 4;
        my $version = unpack 'V', $buf;
        die "Invalid PMP version ($version) in '$f'" unless $version == 6;

        read $fh, $buf, 4;
        my $datasize = unpack 'V', $buf;

        read $fh, $buf, 4;
        my $mapsize = unpack 'V', $buf;

        seek $fh, 2 * ($mapsize*16+1)*($mapsize*16+1), 1; # heightmap

        read $fh, $buf, 4;
        my $numtexs = unpack 'V', $buf;

        for (0..$numtexs-1)
        {
            read $fh, $buf, 4;
            my $len = unpack 'V', $buf;
            my $str;
            read $fh, $str, $len;

            push @deps, [ $f, $terrains{$str} || "art/terrains/(unknown)/$str" ];
        }

        # ignore patches data
    }
}

sub add_soundgroups
{
    print "Loading sound groups...\n";
    my @soundfiles = find_files('audio', 'xml');
    for my $f (sort @soundfiles)
    {
        push @files, $f;

        my $sound = XMLin(vfs_to_physical($f), ForceArray => [qw(Sound)], KeyAttr => []) or die "Failed to parse '$f': $!";

        my $path = $sound->{Path};
        $path =~ s/\/$//; # strip optional trailing slash

        for (@{$sound->{Sound}})
        {
            push @deps, [$f, "$path/$_" ];
        }
    }
}

sub add_audio
{
    print "Loading audio files...\n";
    push @files, find_files('audio', 'ogg');
}

sub add_gui_xml
{
    print "Loading GUI XML...\n";
    my @guifiles = find_files('gui', 'xml');
    for my $f (sort @guifiles)
    {
        push @files, $f;

        if ($f =~ /^gui\/page_/)
        {
            push @roots, $f;
            my $xml = XMLin(vfs_to_physical($f), ForceArray => [qw(include)], KeyAttr => []) or die "Failed to parse '$f': $!";

            for my $include (@{$xml->{include}})
            {
                # If including an entire directory, find all the *.xml files
                if ($include =~ /\/$/)
                {
                    push @deps, [ $f, $_ ] for find_files("gui/$include", 'xml');
                }
                else
                {
                    push @deps, [ $f, "gui/$include" ];
                }
            }
        }
        else
        {
            my $xml = XMLin(vfs_to_physical($f), ForceArray => [qw(object script action sprite image)], KeyAttr => [], KeepRoot => 1) or die "Failed to parse '$f': $!";
            my $name = (keys %$xml)[0];
            if ($name eq 'objects' or $name eq 'object')
            {
                for (grep ref $_ , @{$xml->{objects}{script}})
                {
                    push @deps, [ $f, $_->{file} ] if $_->{file};
                    if ($_->{directory})
                    {
                        # If including an entire directory, find all the *.js files
                        push @deps, [ $f, $_ ] for find_files($_->{directory}, 'js')
                    }
                }
                my $add_objects;
                $add_objects = sub
                {
                    my ($parent) = @_;
                    for my $obj (@{$parent->{object}})
                    {
                        # TODO: look at sprites, styles, etc
                        $add_objects->($obj);
                    }
                };
                $add_objects->($xml->{objects});
            }
            elsif ($name eq 'setup')
            {
                # TODO: look at sprites, styles, etc
            }
            elsif ($name eq 'styles')
            {
                # TODO: look at sprites, styles, etc
            }
            elsif ($name eq 'sprites')
            {
                for my $sprite (@{$xml->{sprites}{sprite}})
                {
                    for my $image (@{$sprite->{image}})
                    {
                        push @deps, [ $f, "art/textures/ui/$image->{texture}" ] if $image->{texture};
                    }
                }
            }
            else
            {
                print "Unexpected GUI XML root element '$name':\n" . Dumper $xml;
                exit;
            }
        }
    }
}

sub add_gui_data
{
    print "Loading GUI data...\n";
    push @files, find_files('gui', 'js');
    push @files, find_files('art/textures/ui', 'dds|png|jpg|tga');
}

sub add_civs
{
    print "Loading civs...\n";

    my @civfiles = find_files('simulation/data/civs', 'json');
    for my $f (sort @civfiles)
    {
        push @files, $f;

        push @roots, $f;

        my $civ = parse_json_file($f);

        push @deps, [ $f, "art/textures/ui/" . $civ->{Emblem} ];

        push @deps, [ $f, "audio/music/" . $_->{File} ] for @{$civ->{Music}};
    }
}

sub add_rms
{
    print "Loading random maps...\n";

    push @files, find_files('maps/random', 'js');
    my @rmsdefs = find_files('maps/random', 'json');

    for my $f (sort @rmsdefs)
    {
        push @files, $f;

        push @roots, $f;

        my $rms = parse_json_file($f);

        push @deps, [ $f, "maps/random/" . $rms->{settings}{Script} ];

        # Map previews
        push @deps, [ $f, "art/textures/ui/session/icons/mappreview/" . $rms->{settings}{Preview} ] if $rms->{settings}{Preview};
    }
}

sub add_techs
{
    print "Loading techs...\n";

    my @techfiles = find_files('simulation/data/technologies', 'json');
    for my $f (sort @techfiles)
    {
        push @files, $f;
        push @roots, $f;

        my $tech = parse_json_file($f);

        push @deps, [ $f, "art/textures/ui/session/portraits/technologies/" . $tech->{icon} ] if $tech->{icon};
        push @deps, [ $f, "simulation/data/technologies/" . $tech->{supersedes} . ".json" ] if $tech->{supersedes};
    }
}

sub add_terrains
{
    print "Loading terrains...\n";

    my @terrains = find_files('art/terrains', 'xml');
    for my $f (sort @terrains)
    {
        # ignore terrains.xml
        if ($f !~ /terrains.xml$/)
        {
            push @files, $f;

            my $terrain = XMLin(vfs_to_physical($f), ForceArray => [qw(texture)], KeyAttr => []) or die "Failed to parse '$f': $!";

            for my $texture (@{$terrain->{textures}{texture}})
            {
                push @deps, [ $f, "art/textures/terrain/$texture->{file}" ] if $texture->{file};
            }
            push @deps, [ $f, "art/materials/$terrain->{material}" ] if $terrain->{material};
        }
    }
}


sub check_deps
{
    my %files;
    @files{@files} = ();

    my %lcfiles;
    @lcfiles{map lc($_), @files} = @files;

    my %revdeps;
    for my $d (@deps)
    {
        push @{$revdeps{$d->[1]}}, $d->[0];
    }

    for my $f (sort keys %revdeps)
    {
        next if exists $files{$f};
        warn "Missing file '$f' referenced by: " . (join ', ', map "'$_'", map vfs_to_relative_to_mods($_), sort @{$revdeps{$f}}) . "\n";

        if (exists $lcfiles{lc $f})
        {
            warn "### Case-insensitive match (found '$lcfiles{lc $f}')\n";
        }
    }
}

sub check_unused
{
    my %reachable;
    @reachable{@roots} = ();

    my %deps;
    for my $d (@deps)
    {
        push @{$deps{$d->[0]}}, $d->[1];
    }

    while (1)
    {
        my @newreachable;
        for my $r (keys %reachable)
        {
            push @newreachable, grep { not exists $reachable{$_} } @{$deps{$r}};
        }
        last if @newreachable == 0;
        @reachable{@newreachable} = ();
    }

    for my $f (sort @files)
    {
        next if exists $reachable{$f};
        warn "Unused file '" . vfs_to_relative_to_mods($f) . "'\n";
    }
}


add_maps_xml() if CHECK_MAPS_XML;

add_maps_pmp();

add_entities();

add_actors();

add_art();

add_materials();

add_particles();

add_soundgroups();
add_audio();

add_gui_xml();
add_gui_data();

add_civs();

add_rms();

add_techs();

add_terrains();

# TODO: add non-skin textures, and all the references to them

print "\n";
check_deps();
print "\n";
check_unused();