utils.tscript 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. function testRpl()
  2. {
  3. ToolUtilityScripts::appendLineToFunction("data/prototyping/prototyping.tscript", "Prototyping", "onDestroyGameServer", "//AAAAAAAAAAAAAAAAAAAAA");
  4. }
  5. function Tools::appendLineToFunction(%file, %nameSpace, %functionName, %appendLine)
  6. {
  7. %fileOutput = new ArrayObject(){};
  8. %fileObj = new FileObject(){};
  9. %insideFunction = false;
  10. %scopeDepth = 0;
  11. if ( %fileObj.openForRead( %file ) )
  12. {
  13. while ( !%fileObj.isEOF() )
  14. {
  15. %line = %fileObj.readLine();
  16. if(strIsMatchExpr("*function *(*)*", %line))
  17. {
  18. %fileOutput.add(%line);
  19. %start = strpos(%line, "function ");
  20. %end = strpos(%line, "(", %start);
  21. %scannedFunctionName = "";
  22. if(%start != -1 && %end != -1)
  23. {
  24. %scannedFunctionName = getSubStr(%line, %start + 9, %end-%start-9);
  25. }
  26. %matchesFunctionSig = false;
  27. if(%nameSpace !$= "")
  28. {
  29. if(%scannedFunctionName $= (%nameSpace @ "::" @ %functionName))
  30. %matchesFunctionSig = true;
  31. }
  32. else
  33. {
  34. if(%scannedFunctionName $= %functionName)
  35. %matchesFunctionSig = true;
  36. }
  37. if(!%matchesFunctionSig)
  38. continue;
  39. %insideFunction = true;
  40. if(strIsMatchExpr("*{*", %line))
  41. {
  42. %scopeDepth++;
  43. }
  44. if(strIsMatchExpr("*}*", %line))
  45. {
  46. %scopeDepth--;
  47. }
  48. }
  49. else
  50. {
  51. if(%insideFunction && strIsMatchExpr("*{*", %line))
  52. {
  53. %scopeDepth++;
  54. }
  55. if(%insideFunction && strIsMatchExpr("*}*", %line))
  56. {
  57. %scopeDepth--;
  58. if(%scopeDepth == 0) //we've fully backed out of the function scope, so resolve back to the parent
  59. {
  60. %insideFunction = false;
  61. %fileOutput.add(%appendLine);
  62. }
  63. }
  64. %fileOutput.add(%line);
  65. }
  66. }
  67. %fileObj.close();
  68. }
  69. if ( %fileObj.openForWrite( %file ) )
  70. {
  71. for(%i=0; %i < %fileOutput.count(); %i++)
  72. {
  73. %line = %fileOutput.getKey(%i);
  74. %fileObj.writeLine(%line);
  75. }
  76. %fileObj.close();
  77. }
  78. }
  79. function Tools::findInFile(%file, %findText, %startingLineId)
  80. {
  81. if(%startingLineId $= "")
  82. %startingLineId = 0;
  83. %fileObj = new FileObject(){};
  84. if ( %fileObj.openForRead( %file ) )
  85. {
  86. %lineId = 0;
  87. while ( !%fileObj.isEOF() )
  88. {
  89. if(%lineId >= %startingLineId)
  90. {
  91. %line = %fileObj.readLine();
  92. if(strIsMatchExpr(%findText, %line))
  93. {
  94. return %lineId;
  95. }
  96. }
  97. %lineId++;
  98. }
  99. %fileObj.close();
  100. }
  101. return -1;
  102. }
  103. function Tools::findInFunction(%file, %nameSpace, %functionName, %findText)
  104. {
  105. %fileObj = new FileObject(){};
  106. %insideFunction = false;
  107. %scopeDepth = 0;
  108. if ( %fileObj.openForRead( %file ) )
  109. {
  110. %lineId = -1;
  111. while ( !%fileObj.isEOF() )
  112. {
  113. %line = %fileObj.readLine();
  114. %lineId++;
  115. if(strIsMatchExpr("*function *(*)*", %line))
  116. {
  117. %start = strpos(%line, "function ");
  118. %end = strpos(%line, "(", %start);
  119. %scannedFunctionName = "";
  120. if(%start != -1 && %end != -1)
  121. {
  122. %scannedFunctionName = getSubStr(%line, %start + 9, %end-%start-9);
  123. }
  124. %matchesFunctionSig = false;
  125. if(%nameSpace !$= "")
  126. {
  127. if(%scannedFunctionName $= (%nameSpace @ "::" @ %functionName))
  128. %matchesFunctionSig = true;
  129. }
  130. else
  131. {
  132. if(%scannedFunctionName $= %functionName)
  133. %matchesFunctionSig = true;
  134. }
  135. if(!%matchesFunctionSig)
  136. continue;
  137. %insideFunction = true;
  138. if(strIsMatchExpr("*{*", %line))
  139. {
  140. %scopeDepth++;
  141. }
  142. if(strIsMatchExpr("*}*", %line))
  143. {
  144. %scopeDepth--;
  145. }
  146. }
  147. else
  148. {
  149. if(%insideFunction && strIsMatchExpr("*{*", %line))
  150. {
  151. %scopeDepth++;
  152. }
  153. if(%insideFunction && strIsMatchExpr("*}*", %line))
  154. {
  155. %scopeDepth--;
  156. if(%scopeDepth == 0) //we've fully backed out of the function scope, so resolve back to the parent
  157. {
  158. %insideFunction = false;
  159. break;
  160. }
  161. }
  162. if(%insideFunction && strIsMatchExpr(%findText, %line))
  163. {
  164. break;
  165. }
  166. }
  167. }
  168. %fileObj.close();
  169. }
  170. return %lineId;
  171. }
  172. function Tools::insertInFile(%file, %insertLineId, %insertText, %insertBefore)
  173. {
  174. %fileOutput = new ArrayObject(){};
  175. %fileObj = new FileObject(){};
  176. if ( %fileObj.openForRead( %file ) )
  177. {
  178. %lineId = 0;
  179. while ( !%fileObj.isEOF() )
  180. {
  181. %line = %fileObj.readLine();
  182. if(%insertLineId == %lineId)
  183. {
  184. if(!%insertBefore || %insertBefore $= "")
  185. {
  186. %fileOutput.add(%line);
  187. %fileOutput.add(%insertText);
  188. }
  189. else
  190. {
  191. %fileOutput.add(%insertText);
  192. %fileOutput.add(%line);
  193. }
  194. }
  195. else
  196. {
  197. %fileOutput.add(%line);
  198. }
  199. %lineId++;
  200. }
  201. %fileObj.close();
  202. }
  203. if ( %fileObj.openForWrite( %file ) )
  204. {
  205. for(%i=0; %i < %fileOutput.count(); %i++)
  206. {
  207. %line = %fileOutput.getKey(%i);
  208. %fileObj.writeLine(%line);
  209. }
  210. %fileObj.close();
  211. }
  212. }