1. #!/usr/bin/perl
  2.  
  3. # check_ents.pl
  4. # For finding entities in ENT files who have more than 64 key/value pairs
  5. # By Faxmachinen
  6.  
  7. use strict;
  8.  
  9. use constant MAX_PROPS => 64;
  10.  
  11.  
  12. local $/; # Slurp entire files
  13.  
  14. unless (@ARGV)
  15. {
  16. $0 =~ m/([^\\\/]+)$/;
  17. print "Usage: $1 <level1.ent> [level2.ent] [...]\n";
  18. exit 0;
  19. }
  20.  
  21. foreach my $file (@ARGV)
  22. {
  23. my $num_ents = 0;
  24. my @bad_ents = ();
  25.  
  26. unless (open FILE, $file)
  27. {
  28. print "Could not open \"$file\".\n";
  29. next;
  30. }
  31.  
  32. print "Scanning file \"$file\"... ";
  33. my $file_content = <FILE>;
  34. foreach ($file_content =~ m/\{([^\}]*)\}/sg)
  35. {
  36. $num_ents++;
  37. # We use an array to count number of key/value pairs, since outputs often have duplicate key names.
  38. my @props = m/"([^"]*)"/sg;
  39. my $num_props = scalar @props / 2;
  40. if ($num_props > &MAX_PROPS)
  41. {
  42. # Then we use a map to get information about bad entities.
  43. my %props = @props;
  44. my $name = 'UNKNOWN';
  45. my $hid = -1;
  46. if (exists $props{'targetname'}) { $name = $props{'targetname'}; }
  47. if (exists $props{'classname'}) { $name = $props{'classname'}; }
  48. if (exists $props{'origin'}) { $name .= " @ <$props{'origin'}>"; }
  49. if (exists $props{'hammerid'}) { $hid = $props{'hammerid'}; }
  50. push @bad_ents, [$name, $num_props, $hid];
  51. }
  52. }
  53. print "$num_ents entities scanned: ";
  54. if (@bad_ents)
  55. {
  56. print "\n";
  57. print "\t$_->[0] (hammerid $_->[2]) has $_->[1] key/value pairs, max is " . &MAX_PROPS . ".\n" foreach (@bad_ents);
  58. }
  59. else
  60. {
  61. print "No bad entities.\n"
  62. }
  63. }
  64.