#!/usr/bin/perl
# check_ents.pl
# For finding entities in ENT files who have more than 64 key/value pairs
# By Faxmachinen
use strict;
use constant MAX_PROPS => 64;
local $/;
# Slurp entire files
unless (@ARGV)
{
print "Usage: $1 <level1.ent> [level2.ent] [...]\n";
}
foreach my $file (@ARGV)
{
my $num_ents = 0;
my @bad_ents = ();
unless (open FILE,
$file) {
print "Could not open \"$file\".\n";
next;
}
print "Scanning file \"$file\"... ";
my $file_content = <FILE>;
foreach ($file_content =~
m/\
{([^\
}]*
)\
}/sg
) {
$num_ents++;
# We use an array to count number of key/value pairs, since outputs often have duplicate key names.
my @props =
m/
"([^"]*
)"/sg; my $num_props = scalar @props / 2;
if ($num_props > &MAX_PROPS)
{
# Then we use a map to get information about bad entities.
my %props = @props;
my $name = 'UNKNOWN';
my $hid = -1;
if (exists $props{'targetname'}) { $name = $props{'targetname'}; }
if (exists $props{'classname'}) { $name = $props{'classname'}; }
if (exists $props{'origin'}) { $name .= " @ <$props{'origin'}>"; }
if (exists $props{'hammerid'}) { $hid = $props{'hammerid'}; }
push @bad_ents, [$name, $num_props, $hid];
}
}
print "$num_ents entities scanned: ";
if (@bad_ents)
{
print "\n";
print "\t$_->[0] (hammerid $_->[2]) has $_->[1] key/value pairs, max is " . &MAX_PROPS . ".\n" foreach (@bad_ents);
}
else
{
print "No bad entities.\n"
}
}