STRCHECK.PL 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #
  2. # Command & Conquer Generals Zero Hour™
  3. # Copyright 2025 Electronic Arts Inc.
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. sub ShowWarning;
  19. sub ShowError;
  20. $noxScript = shift || 'C:\Nox\Game\nox.str';
  21. $outFile = shift;
  22. open(STR, $noxScript) || die "Can't open Nox string file: $!\n";
  23. if ($outFile)
  24. {
  25. open(STDOUT, ">$outFile") || die "Can't open output file: $!\n";
  26. }
  27. select((select(STDOUT), $| = 1)[0]);
  28. $state = 0;
  29. $line = 0;
  30. $strcount = 0;
  31. $strMaxCount = 32;
  32. $lastFileName = "";
  33. $lastFileNameLine = 0;
  34. $ignoreErrors = 0;
  35. $errCount = 0;
  36. %labels = ();
  37. while (<STR>)
  38. {
  39. $strline = $_;
  40. $line++;
  41. $strline =~ s/\/\/.*//g;
  42. $strline =~ s/^\s*//g;
  43. $strline =~ s/\s*$//g;
  44. if (! ($strline eq ""))
  45. {
  46. if ($state == 0)
  47. {
  48. if (! ($strline =~ /^[^\"][^ :]*:[^ :]*[^\"]$/))
  49. {
  50. ShowError($line, $strline, "Expecting 'file:name'\n");
  51. }
  52. else
  53. {
  54. if (defined ($labels{$strline}))
  55. {
  56. ShowWarning($line, $strline, "Duplicate label at line $labels{$strline}\n");
  57. }
  58. $labels{$strline} = $line;
  59. $lastFileName = $strline;
  60. $lastFileNameLine = $line;
  61. $strcount = 0;
  62. $ignoreErrors = 0;
  63. $state = 1;
  64. }
  65. }
  66. elsif ($state == 1)
  67. {
  68. if (++$strcount >= $strMaxCount)
  69. {
  70. ShowError($line, $strline, "Too many strings ($strcount) - suspected missing END from section $lastFileName on line $lastFileNameLine\n");
  71. }
  72. elsif ($strline =~ /^END$/i)
  73. {
  74. $state = 0;
  75. }
  76. elsif ($strline =~ /\/n/i)
  77. {
  78. ShowWarning($line, $strline, "'/n'? This should probably be '\\n' instead\n");
  79. }
  80. elsif ($strline =~ /^\"[^\"]*[^\"]$/)
  81. {
  82. $state = 2;
  83. }
  84. else
  85. {
  86. $strline =~ s/\"\s*={0,1}\s*[a-zA-Z0-9_\.]*[^\"]$/\"/i;
  87. if (! ($strline =~ /^\".*\"$/))
  88. {
  89. if ($strline =~ /^[^\"][^ :]*:[^ :]*[^\"]$/)
  90. {
  91. ShowError($line, $strline, "Missing quote - suspected missing END from section $lastFileName on line $lastFileNameLine\n");
  92. }
  93. else
  94. {
  95. ShowError($line, $strline, "Missing quote\n");
  96. }
  97. }
  98. }
  99. }
  100. elsif ($state == 2)
  101. {
  102. $strline =~ s/\"\s*={0,1}\s*[a-zA-Z0-9_\.]*[^\"]$/\"/i;
  103. if (++$strcount >= $strMaxCount)
  104. {
  105. ShowError($line, $strline, "Too many strings ($strcount) - suspected missing END from section $lastFileName on line $lastFileNameLine\n");
  106. }
  107. elsif ($strline =~ /\/n/i)
  108. {
  109. ShowWarning($line, $strline, "'/n'? This should probably be '\\n' instead\n");
  110. }
  111. elsif ($strline =~ /^[^\"].*\"$/)
  112. {
  113. $state = 1;
  114. }
  115. elsif (! ($strline =~ /^[^\"].*[^\"]$/))
  116. {
  117. ShowError($line, $strline, "Extra quote character found at start of line\n");
  118. }
  119. elsif ($strline =~ /^END$/i)
  120. {
  121. ShowWarning($line, $strline, "Suspiciously placed 'END'. Maybe missing an end-quote from previous section\n");
  122. }
  123. }
  124. }
  125. }
  126. if ($state != 0)
  127. {
  128. ShowError($line, $strline, "Missing END\n");
  129. }
  130. print STDOUT "$line lines processed\n";
  131. close(STR);
  132. if ($errCount == 0)
  133. {
  134. open(RESULT, ">strcheck.err") || die "Can't open output file: $!\n";
  135. print RESULT "OK";
  136. close (RESULT);
  137. }
  138. exit($errCount);
  139. sub ShowWarning
  140. {
  141. my ($errLine, $errString, $errDesc) = @_;
  142. if (length($errString) > 32)
  143. {
  144. $errString = substr($errString, 0, 32) . "...";
  145. }
  146. if ($ignoreErrors == 0)
  147. {
  148. print STDOUT "$noxScript($errLine) : warning: '$errString' : $errDesc";
  149. }
  150. $state = 0;
  151. $ignoreErrors = 1;
  152. }
  153. sub ShowError
  154. {
  155. my ($errLine, $errString, $errDesc) = @_;
  156. if (length($errString) > 32)
  157. {
  158. $errString = substr($errString, 0, 32) . "...";
  159. }
  160. if ($ignoreErrors == 0)
  161. {
  162. print STDOUT "$noxScript($errLine) : error: '$errString' : $errDesc";
  163. $errCount++;
  164. }
  165. $state = 0;
  166. $ignoreErrors = 1;
  167. }