PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/MJS/Interpreter.cs

https://bitbucket.org/realquaisaq/mcrevive
C# | 2141 lines | 2044 code | 32 blank | 65 comment | 971 complexity | f7e3658119489266f7486ea9b28e6730 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /* This source file is originally written by Jesbus
  2. * and may be used in other software and edited
  3. * freely as long as you put Jesbus's name in the
  4. * credits of the compilation and as long as this
  5. * comment remains fully intact.
  6. *
  7. * The MJS interpreter is currently used for
  8. * command and event scripting in MCRevive
  9. * (Minecraft Classic server software).
  10. */
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  17. namespace MJS
  18. {
  19. public static class XMath
  20. {
  21. public static double Parse(string input)
  22. {
  23. ExpressionParser MathParser = new ExpressionParser();
  24. Hashtable MathParseHashes = new Hashtable();
  25. return MathParser.Parse(input, MathParseHashes);
  26. }
  27. public static double Pythagoras(double a, double b)
  28. {
  29. a = Math.Abs(a);
  30. b = Math.Abs(b);
  31. return Math.Sqrt(a * a + b * b);
  32. }
  33. public static double Pythagoras(double a, double b, double c)
  34. {
  35. return Pythagoras(a, Pythagoras(b, c));
  36. }
  37. public static double Max(double a, double b, double c)
  38. {
  39. return Math.Max(a, Math.Max(b, c));
  40. }
  41. public static double Max(double a, double b, double c, double d)
  42. {
  43. return Math.Max(Math.Max(a, b), Math.Max(c, d));
  44. }
  45. }
  46. public static class Script
  47. {
  48. public static List<Thread> ScriptThreads = new List<Thread>();
  49. public static void Execute(string file, string arguments, bool deleteWhenFinished = false)
  50. {
  51. if (!File.Exists(file)) { Events.Log("Script file " + file + " does not exist."); return; }
  52. Thread scriptThread = new Thread(new ThreadStart(delegate
  53. {
  54. string[] lines = File.ReadAllLines(file);
  55. string[] blocks = new string[255];
  56. string[] numberVarNames = new string[255];
  57. double[] numberVars = new double[255];
  58. int numberVarCount = 0;
  59. string[] stringVarNames = new string[255];
  60. string[] stringVars = new string[255];
  61. int stringVarCount = 0;
  62. string[] mapVarNames = new string[255];
  63. Map[] mapVars = new Map[255];
  64. int mapVarCount = 0;
  65. string[] playerVarNames = new string[255];
  66. Player[] playerVars = new Player[255];
  67. int playerVarCount = 0;
  68. int i = 0;
  69. int j = 0;
  70. double parseTryer;
  71. #region CREATE ARGUMENT VARIABLES
  72. string varType = "";
  73. string varName = "";
  74. string varValue = "";
  75. foreach (string argPart in arguments.Split(' '))
  76. {
  77. if (i % 3 == 0) varType = argPart;
  78. else if (i % 3 == 1) varName = argPart.Remove(0, 1);
  79. else
  80. {
  81. varValue = argPart.Replace("[space]", " ");
  82. if (varType == "number")
  83. {
  84. if (!System.Double.TryParse(varValue, out parseTryer))
  85. {
  86. Events.Log("Could not execute script " + file + ": argument \"" + varType + " $" + varName + " " + varValue + "\" contains an error.");
  87. goto stopScript;
  88. }
  89. numberVars[numberVarCount] = parseTryer;
  90. numberVarNames[numberVarCount] = varName;
  91. numberVarCount++;
  92. }
  93. else if (varType == "string")
  94. {
  95. stringVarNames[stringVarCount] = varName;
  96. stringVars[stringVarCount] = varValue;
  97. stringVarCount++;
  98. }
  99. else if (varType == "map")
  100. {
  101. mapVars[mapVarCount] = Map.Find(varValue);
  102. if (mapVars[mapVarCount] == null)
  103. {
  104. Events.Log("Could not execute script " + file + ": no map called " + varValue + " is loaded.");
  105. goto stopScript;
  106. }
  107. mapVarNames[mapVarCount] = varName;
  108. mapVarCount++;
  109. }
  110. else if (varType == "player")
  111. {
  112. //Server.s.Log(varName + "=" + varValue);
  113. playerVars[playerVarCount] = Player.Find(varValue);
  114. if (playerVars[playerVarCount] == null)
  115. {
  116. Events.Log("Could not execute script " + file + ": no player called " + varValue + " is connected.");
  117. goto stopScript;
  118. }
  119. //Events.Log("player:" + playerVars[playerVarCount].name);
  120. mapVars[mapVarCount] = playerVars[playerVarCount].map;
  121. mapVarNames[mapVarCount] = varName + ".$map";
  122. mapVarCount++;
  123. playerVarNames[playerVarCount] = varName;
  124. playerVarCount++;
  125. }
  126. else
  127. {
  128. Events.Log("Could not execute script " + file + ": Variable type " + varType + " does not exist in MCfresh.");
  129. goto stopScript;
  130. }
  131. }
  132. i++;
  133. }
  134. #endregion
  135. int curBlock = 0;
  136. string[] block_type = new string[255];
  137. bool[] block_skipping = new bool[255];
  138. bool[] block_executeElse = new bool[255];
  139. int[] block_inBlockCount = new int[255];
  140. int[] block_repeatCurBlock = new int[255];
  141. int[] block_repeatStartLine = new int[255];
  142. block_type[0] = "main";
  143. block_skipping[0] = false;
  144. block_executeElse[0] = false;
  145. bool writingTempThreadFile = false;
  146. int blocksInThreadCount = 0;
  147. StreamWriter tempThreadFileWriter = null;
  148. string tempThreadFileName = "";
  149. bool ifNot = false;
  150. bool inTryBlock = false;
  151. bool skipToCatch = false;
  152. bool executeCatch = false;
  153. int catch_inBlockCount = 0;
  154. int tryCurBlock = 0;
  155. int lineNum = 0;
  156. string lineError;
  157. bool fatalError;
  158. while (lineNum < lines.Length)
  159. {
  160. try
  161. {
  162. lineError = "";
  163. fatalError = false;
  164. ifNot = false;
  165. string line = lines[lineNum].Trim();
  166. if (line.StartsWith("//") || line.StartsWith("#") || line.StartsWith("'")) goto nextLine; // So peoplezz can adzz commentzz
  167. #region BLOCK HANDLING
  168. if (writingTempThreadFile)
  169. {
  170. if (line == "{") { blocksInThreadCount++; tempThreadFileWriter.WriteLine(line); }
  171. else if (line == "}" && blocksInThreadCount != 0) { blocksInThreadCount--; tempThreadFileWriter.WriteLine(line); }
  172. else if (line == "}")
  173. {
  174. tempThreadFileWriter.Flush();
  175. tempThreadFileWriter.Close();
  176. writingTempThreadFile = false;
  177. curBlock--;
  178. Execute(tempThreadFileName, "", true);
  179. }
  180. else { tempThreadFileWriter.WriteLine(line); }
  181. goto nextLine;
  182. }
  183. if (skipToCatch)
  184. {
  185. if (line.ToLower() == "catch") { skipToCatch = false; executeCatch = true; }
  186. goto nextLine;
  187. }
  188. if (block_type[curBlock] == "catch" && !executeCatch)
  189. {
  190. if (line == "{") catch_inBlockCount++;
  191. else if (line == "}" && catch_inBlockCount != 0) catch_inBlockCount--;
  192. else if (line == "}")
  193. {
  194. executeCatch = false;
  195. block_type[curBlock] = null;
  196. curBlock--;
  197. }
  198. goto nextLine;
  199. }
  200. if (curBlock != 0)
  201. {
  202. if (block_skipping[curBlock - 1])
  203. {
  204. if (line == "{") block_inBlockCount[curBlock]++;
  205. else if (line == "}" && block_inBlockCount[curBlock] != 0)
  206. {
  207. block_inBlockCount[curBlock]--;
  208. }
  209. else if (line == "}")
  210. {
  211. block_skipping[curBlock - 1] = false;
  212. curBlock--;
  213. }
  214. goto nextLine;
  215. }
  216. }
  217. if (line.ToLower() == "else")
  218. {
  219. block_type[curBlock + 1] = "else";
  220. goto nextLine;
  221. }
  222. if (line == "{")
  223. {
  224. curBlock++;
  225. if (block_type[curBlock] == "if")
  226. {
  227. if (block_executeElse[curBlock - 1])
  228. {
  229. block_skipping[curBlock - 1] = true;
  230. block_inBlockCount[curBlock] = 0;
  231. }
  232. else
  233. {
  234. block_skipping[curBlock - 1] = false;
  235. }
  236. }
  237. else if (block_type[curBlock] == "else")
  238. {
  239. if (!block_executeElse[curBlock - 1])
  240. {
  241. block_skipping[curBlock - 1] = true;
  242. block_inBlockCount[curBlock] = 0;
  243. }
  244. else
  245. {
  246. block_skipping[curBlock - 1] = false;
  247. }
  248. }
  249. else if (block_type[curBlock] == "repeat")
  250. {
  251. block_repeatCurBlock[curBlock - 1] = curBlock - 1;
  252. block_repeatStartLine[curBlock - 1] = lineNum - 1;
  253. block_skipping[curBlock - 1] = false;
  254. block_inBlockCount[curBlock - 1] = 0;
  255. }
  256. else if (block_type[curBlock] == "thread")
  257. {
  258. i = 0;
  259. while (File.Exists("mjs/threads/temp" + i + ".mjs"))
  260. {
  261. i++;
  262. }
  263. tempThreadFileWriter = new StreamWriter(File.Create("mjs/threads/temp" + i + ".mjs"));
  264. tempThreadFileName = "mjs/threads/temp" + i + ".mjs";
  265. writingTempThreadFile = true;
  266. }
  267. goto nextLine;
  268. }
  269. if (line == "}")
  270. {
  271. if (block_type[curBlock] == "repeat")
  272. {
  273. lineNum = block_repeatStartLine[curBlock];
  274. curBlock--;
  275. goto nextLine;
  276. }
  277. else
  278. {
  279. if (block_type[curBlock] == "try") inTryBlock = false;
  280. block_inBlockCount[curBlock] = 0;
  281. block_type[curBlock] = null;
  282. block_executeElse[curBlock] = false;
  283. block_skipping[curBlock] = false;
  284. }
  285. curBlock--;
  286. goto nextLine;
  287. }
  288. if (line.ToLower() == "exit")
  289. {
  290. lineNum = lines.Length;
  291. goto stopScript;
  292. }
  293. string[] lineParts = line.Split(' ');
  294. if (lineParts.Length != 1)
  295. {
  296. if (lineParts[1] == "not")
  297. {
  298. ifNot = true;
  299. i = 1;
  300. while (i < lineParts.Length)
  301. {
  302. i++;
  303. if (i < lineParts.Length) lineParts[i - 1] = lineParts[i];
  304. }
  305. }
  306. }
  307. #endregion
  308. #region GROUP THE REMAINS OF STRINGS THAT WERE SPLIT APART INTO ONE ELEMENT OF lineParts
  309. i = 0;
  310. bool inString = false;
  311. int linePartStringStart = 0;
  312. int nulls = 0;
  313. while (i < lineParts.Length)
  314. {
  315. if (!inString && lineParts[i].StartsWith("\"") && !lineParts[i].EndsWith("\""))
  316. {
  317. lineParts[i] += " ";
  318. inString = true;
  319. linePartStringStart = i;
  320. }
  321. else if (inString)
  322. {
  323. if (lineParts[i].EndsWith("\""))
  324. {
  325. lineParts[linePartStringStart] += lineParts[i];
  326. inString = false;
  327. }
  328. else lineParts[linePartStringStart] += lineParts[i] + " ";
  329. lineParts[i] = null;
  330. nulls++;
  331. }
  332. i++;
  333. }
  334. /// REMOVE ALL NULL VALUES FROM lineParts:
  335. string[] newLineParts = new string[lineParts.Length - nulls];
  336. i = 0;
  337. j = 0;
  338. while (i < lineParts.Length)
  339. {
  340. if (lineParts[i] != null)
  341. {
  342. newLineParts[j] = lineParts[i];
  343. j++;
  344. }
  345. i++;
  346. }
  347. lineParts = newLineParts;
  348. #endregion
  349. #region GROUP THE REMAINS OF MATH EXPRESSIONS THAT WERE SPLIT APART INTO ONE ELEMENT OF lineParts
  350. i = 0;
  351. bool inExpression = false;
  352. int linePartExpressionStart = 0;
  353. nulls = 0;
  354. while (i < lineParts.Length)
  355. {
  356. if (!inExpression && lineParts[i].StartsWith("(") && !lineParts[i].EndsWith(")"))
  357. {
  358. inExpression = true;
  359. linePartExpressionStart = i;
  360. }
  361. else if (inExpression)
  362. {
  363. if (lineParts[i].EndsWith(")"))
  364. {
  365. lineParts[linePartExpressionStart] += lineParts[i];
  366. inString = false;
  367. }
  368. else lineParts[linePartExpressionStart] += lineParts[i];
  369. lineParts[i] = null;
  370. nulls++;
  371. }
  372. i++;
  373. }
  374. string[] newLineParts2 = new string[lineParts.Length - nulls];
  375. i = 0;
  376. j = 0;
  377. while (i < lineParts.Length)
  378. {
  379. if (lineParts[i] != null)
  380. {
  381. newLineParts2[j] = lineParts[i];
  382. j++;
  383. }
  384. i++;
  385. }
  386. lineParts = newLineParts2;
  387. #endregion
  388. // Update this for every new command:
  389. #region CONVERSIONS AND VARIABLE TO VALUE REPLACEMENTS
  390. if (lineParts[0] == "if") lineParts[0] = "If";
  391. i = 0;
  392. while (i < lineParts.Length && lineParts[i] != null)
  393. {
  394. string shouldBe = "";
  395. string whatItIs = "";
  396. if (lineParts[0] == "GlobalMessage" && i == 1) { shouldBe = "string"; goto done1; }
  397. if (lineParts[0] == "PlayerMessage" && i == 2) { shouldBe = "string"; goto done1; }
  398. if (lineParts[0] == "GenerateMap" && i == 2) { shouldBe = "string"; goto done1; }
  399. if (lineParts[0] == "GenerateMap" && (i == 3 || i == 4 || i == 5)) { shouldBe = "number"; goto done1; }
  400. if (lineParts[0] == "LoadMap" && lineParts[1].StartsWith("\"") && i == 1) { shouldBe = "string"; goto done1; }
  401. if (lineParts[0] == "LoadMap" && i == 1) { shouldBe = "map"; goto done1; }
  402. if (lineParts[0] == "UnloadMap" && i == 1) { shouldBe = "map"; goto done1; }
  403. //if (lineParts[0] == "SaveMap" && i == 1) { shouldBe = "map"; goto done1; }
  404. if (lineParts[0] == "ConsoleMessage" && i == 1) { shouldBe = "string"; goto done1; }
  405. if (lineParts[0] == "KickPlayer" && i == 1) { shouldBe = "player"; goto done1; }
  406. if (lineParts[0] == "KickPlayer" && i == 2) { shouldBe = "string"; goto done1; }
  407. if (lineParts[0] == "MovePlayer" && i == 1) { shouldBe = "player"; goto done1; }
  408. if (lineParts[0] == "MovePlayer" && i == 2) { shouldBe = "map"; goto done1; }
  409. if (lineParts[0] == "CreateFile" && i == 1) { shouldBe = "string"; goto done1; }
  410. if (lineParts[0] == "CreateDirectory" && i == 1) { shouldBe = "string"; goto done1; }
  411. if (lineParts[0] == "DeleteFile" && i == 1) { shouldBe = "string"; goto done1; }
  412. if (lineParts[0] == "DeleteDirectory" && i == 1) { shouldBe = "string"; goto done1; }
  413. if (lineParts[0] == "TruncateFile" && i == 1) { shouldBe = "string"; goto done1; }
  414. if (lineParts[0] == "TruncateDirectory" && i == 1) { shouldBe = "string"; goto done1; }
  415. if (lineParts[0] == "FileWriteLine" && i == 1) { shouldBe = "string"; goto done1; }
  416. if (lineParts[0] == "FileWriteLine" && i == 2) { shouldBe = "string"; goto done1; }
  417. if (lineParts[0] == "ExecuteScript" && i == 1) { shouldBe = "string"; goto done1; }
  418. if (lineParts[0] == "TriggerEvent" && i == 1) { shouldBe = "string"; goto done1; }
  419. if (lineParts[0] == "SetBlock" && i == 2) { shouldBe = "number"; goto done1; }
  420. if (lineParts[0] == "SetBlock" && i == 3) { shouldBe = "number"; goto done1; }
  421. if (lineParts[0] == "SetBlock" && i == 4) { shouldBe = "number"; goto done1; }
  422. if (lineParts[0] == "SetBlock" && i == 5) { shouldBe = "number"; goto done1; }
  423. if (lineParts[0] == "GetBlock" && i == 2) { shouldBe = "number"; goto done1; }
  424. if (lineParts[0] == "GetBlock" && i == 3) { shouldBe = "number"; goto done1; }
  425. if (lineParts[0] == "GetBlock" && i == 4) { shouldBe = "number"; goto done1; }
  426. if (lineParts[0] == "GetBlock" && i == 5) { shouldBe = "number"; goto done1; }
  427. if (lineParts[0] == "SendBlock" && i == 2) { shouldBe = "number"; goto done1; }
  428. if (lineParts[0] == "SendBlock" && i == 3) { shouldBe = "number"; goto done1; }
  429. if (lineParts[0] == "SendBlock" && i == 4) { shouldBe = "number"; goto done1; }
  430. if (lineParts[0] == "SendBlock" && i == 5) { shouldBe = "number"; goto done1; }
  431. if (lineParts[0] == "Replace" && i == 2) { shouldBe = "string"; goto done1; }
  432. if (lineParts[0] == "Replace" && i == 3) { shouldBe = "string"; goto done1; }
  433. if (lineParts[0] == "Sleep" && i == 1) { shouldBe = "number"; goto done1; }
  434. if (lineParts[0] == "Wait" && i == 1) { shouldBe = "number"; goto done1; }
  435. if (lineParts[0] == "If" && lineParts[1] == "FileExists" && i == 2) { shouldBe = "string"; goto done1; }
  436. if (lineParts[0] == "If" && lineParts[1] == "DirectoryExists" && i == 2) { shouldBe = "string"; goto done1; }
  437. if (lineParts[0] == "If" && lineParts[1] == "FileContainsLine" && i == 2) { shouldBe = "string"; goto done1; }
  438. if (lineParts[0] == "If" && lineParts[1] == "FileContainsLine" && i == 3) { shouldBe = "string"; goto done1; }
  439. if (lineParts[0] == "If" && lineParts[1] == "FileContainsString" && i == 2) { shouldBe = "string"; goto done1; }
  440. if (lineParts[0] == "If" && lineParts[1] == "FileContainsString" && i == 3) { shouldBe = "string"; goto done1; }
  441. if (lineParts[0] == "If" && lineParts[1] == "FileContainsText" && i == 2) { shouldBe = "string"; goto done1; }
  442. if (lineParts[0] == "If" && lineParts[1] == "FileContainsText" && i == 3) { shouldBe = "string"; goto done1; }
  443. if (lineParts[0] == "If" && lineParts[1] == "FileContains" && i == 2) { shouldBe = "string"; goto done1; }
  444. if (lineParts[0] == "If" && lineParts[1] == "FileContains" && i == 3) { shouldBe = "string"; goto done1; }
  445. if (lineParts[0] == "If" && lineParts[1] == "FileRemoveLine" && i == 2) { shouldBe = "string"; goto done1; }
  446. if (lineParts[0] == "If" && lineParts[1] == "FileRemoveLine" && i == 3) { shouldBe = "string"; goto done1; }
  447. if (lineParts[0] == "If" && lineParts[1] == "StringContains" && i == 2) { shouldBe = "string"; goto done1; }
  448. if (lineParts[0] == "If" && lineParts[1] == "StringContains" && i == 3) { shouldBe = "string"; goto done1; }
  449. if (lineParts[0] == "If" && lineParts[1] == "StringContainsString" && i == 2) { shouldBe = "string"; goto done1; }
  450. if (lineParts[0] == "If" && lineParts[1] == "StringContainsString" && i == 3) { shouldBe = "string"; goto done1; }
  451. if (lineParts[0] == "If" && lineParts[1] == "StringStartsWith" && i == 2) { shouldBe = "string"; goto done1; }
  452. if (lineParts[0] == "If" && lineParts[1] == "StringStartsWith" && i == 3) { shouldBe = "string"; goto done1; }
  453. if (lineParts[0] == "If" && lineParts[1] == "StringStartsWithString" && i == 2) { shouldBe = "string"; goto done1; }
  454. if (lineParts[0] == "If" && lineParts[1] == "StringStartsWithString" && i == 3) { shouldBe = "string"; goto done1; }
  455. if (lineParts[0] == "If" && lineParts[1] == "StringEndsWith" && i == 2) { shouldBe = "string"; goto done1; }
  456. if (lineParts[0] == "If" && lineParts[1] == "StringEndsWith" && i == 3) { shouldBe = "string"; goto done1; }
  457. if (lineParts[0] == "If" && lineParts[1] == "StringEndsWithString" && i == 2) { shouldBe = "string"; goto done1; }
  458. if (lineParts[0] == "If" && lineParts[1] == "StringEndsWithString" && i == 3) { shouldBe = "string"; goto done1; }
  459. if (lineParts[0] == "If" && (lineParts[2] == "<" || lineParts[2] == ">" || lineParts[2] == "<=" || lineParts[2] == ">=") && i == 1) { shouldBe = "number"; goto done1; }
  460. if (lineParts[0] == "If" && (lineParts[2] == "<" || lineParts[2] == ">" || lineParts[2] == "<=" || lineParts[2] == ">=") && i == 3) { shouldBe = "number"; goto done1; }
  461. if (lineParts[0] == "If" && (lineParts[2] == "==" || lineParts[2] == "!=") && (i == 1 || i == 3))
  462. {
  463. if (System.Double.TryParse(lineParts[1], out parseTryer) || System.Double.TryParse(lineParts[3], out parseTryer)) { shouldBe = "number"; goto done1; }
  464. if (lineParts[1].StartsWith("\"") || lineParts[3].StartsWith("\"")) { shouldBe = "string"; goto done1; }
  465. if (lineParts[1].StartsWith("$") && lineParts[3].StartsWith("$"))
  466. {
  467. lineParts[1] = lineParts[1].Remove(0, 1);
  468. i = 0;
  469. while (i < 255)
  470. {
  471. if (mapVarNames[i] != null) { shouldBe = "map"; goto done1; }
  472. if (playerVarNames[i] != null) { shouldBe = "player"; goto done1; }
  473. if (stringVarNames[i] != null) { shouldBe = "string"; goto done1; }
  474. if (numberVarNames[i] != null) { shouldBe = "number"; goto done1; }
  475. i++;
  476. }
  477. lineError = "Variable $" + lineParts[1] + " does not exist.";
  478. fatalError = true;
  479. goto nextLine;
  480. }
  481. lineError = "Could not compare " + lineParts[1] + " with " + lineParts[3];
  482. fatalError = true;
  483. goto nextLine;
  484. }
  485. if (lineParts.Length > 2)
  486. {
  487. if (lineParts[0] == "If" && (lineParts[2] == "<" || lineParts[2] == ">" || lineParts[2] == "<=" || lineParts[2] == ">=") && (i == 1 || i == 3)) { shouldBe = "number"; goto done1; }
  488. if ((lineParts[1] == "+=" || lineParts[1] == "-=" || lineParts[1] == "*=" || lineParts[1] == "/=" || lineParts[1] == "^=") && i == 2) { shouldBe = "number"; goto done1; }
  489. }
  490. if ((lineParts[0].ToLower() == "sleep" || lineParts[0].ToLower() == "wait") && i == 1) { shouldBe = "number"; goto done1; }
  491. if (lineParts.Length >= 3)
  492. {
  493. if (lineParts[1] == "=" && i == 2) // Assigning value to variable
  494. {
  495. lineParts[0] = lineParts[0].Remove(0, 1);
  496. j = 0;
  497. while (j < 255)
  498. {
  499. if (numberVarNames[j] == lineParts[0]) { shouldBe = "number"; }
  500. if (stringVarNames[j] == lineParts[0]) { shouldBe = "string"; }
  501. if (playerVarNames[j] == lineParts[0]) { if (lineParts[2].StartsWith("\"")) { shouldBe = "string"; } else if (lineParts[2].StartsWith("$")) { goto done3; } else { lineError = "Syntax error! After = a player variable or a string is expected."; } }
  502. if (mapVarNames[j] == lineParts[0]) { if (lineParts[2].StartsWith("\"")) { shouldBe = "string"; } else if (lineParts[2].StartsWith("$")) { goto done3; } else { lineError = "Syntax error! After = a map variable or a string is expected."; } }
  503. if (shouldBe != "")
  504. {
  505. lineParts[0] = "$" + lineParts[0];
  506. goto done1;
  507. }
  508. j++;
  509. }
  510. lineError = "Could not assign value to variable. Variable $" + lineParts[1] + " does not exist.";
  511. goto nextLine;
  512. }
  513. }
  514. if (lineParts.Length >= 4)
  515. {
  516. if (lineParts[2] == "=" & i == 3) // Assigning value to variable after creating the variable
  517. {
  518. //shouldBe = lineParts[0]; // as the type is already given in the first line part, easy-peasy <-- does not apply anymore :p
  519. switch (lineParts[0])
  520. {
  521. case "string":
  522. shouldBe = "string";
  523. break;
  524. case "number":
  525. shouldBe = "number";
  526. break;
  527. default:
  528. goto done3; // if a map or player variable is being declared, skip the conversions and replacements as they need to be done else in the kajlkfafadlkfkl
  529. }
  530. goto done1;
  531. }
  532. }
  533. goto done3;
  534. done1:
  535. if (lineParts[i].StartsWith("\"")) { whatItIs = "string"; goto done2; }
  536. else if (System.Double.TryParse(lineParts[i], out parseTryer)) { whatItIs = "number"; goto done2; }
  537. else if (lineParts[i].StartsWith("(") && lineParts[i].EndsWith(")")) { whatItIs = "math"; goto done2; }
  538. else if (!lineParts[i].StartsWith("$")) { whatItIs = "map/player"; goto done2; } // old >.>
  539. else
  540. {
  541. //Server.s.Log(lineParts[i]);
  542. lineParts[i] = lineParts[i].Remove(0, 1);
  543. if (lineParts[i] == "$server.$defaultcolor") { lineParts[1] = "\"" + Vars.Color + "\""; goto done2; }
  544. //Server.s.Log(lineParts[i]);
  545. j = 0;
  546. while (j < 255)
  547. {
  548. if (lineParts[i] == playerVarNames[j] + ".$rank.$name.$length") { lineParts[i] = playerVars[j].rank.name.Length.ToString(); whatItIs = "number"; goto done2; }
  549. if (lineParts[i] == playerVarNames[j] + ".$map.$name.$length") { lineParts[i] = playerVars[j].map.name.Length.ToString(); whatItIs = "number"; goto done2; }
  550. if (lineParts[i] == playerVarNames[j] + ".$map.$owner.$length") { lineParts[i] = playerVars[j].map.owner.Length.ToString(); whatItIs = "number"; goto done2; }
  551. if (lineParts[i] == mapVarNames[j] + ".$name.$length") { lineParts[i] = mapVars[j].name.Length.ToString(); whatItIs = "number"; goto done2; }
  552. if (lineParts[i] == mapVarNames[j] + ".$players.$count") { lineParts[i] = mapVars[j].players.Count.ToString(); whatItIs = "number"; goto done2; }
  553. if (lineParts[i] == mapVarNames[j] + ".$owner.$length") { lineParts[i] = mapVars[j].owner.Length.ToString(); whatItIs = "number"; goto done2; }
  554. if (lineParts[i] == playerVarNames[j] + ".$rank.$color") { lineParts[i] = "\"" + playerVars[j].rank.color + "\""; whatItIs = "string"; goto done2; }
  555. if (lineParts[i] == playerVarNames[j] + ".$rank.$name") { lineParts[i] = "\"" + playerVars[j].rank.name + "\""; whatItIs = "string"; goto done2; }
  556. if (lineParts[i] == playerVarNames[j] + ".$name.$length") { lineParts[i] = playerVars[j].name.Length.ToString(); whatItIs = "number"; goto done2; }
  557. if (lineParts[i] == playerVarNames[j] + ".$map.$name") { lineParts[i] = "\"" + playerVars[j].map.name + "\""; whatItIs = "string"; goto done2; }
  558. if (lineParts[i] == playerVarNames[j] + ".$map.$creator") { lineParts[i] = "\"" + playerVars[j].map.owner + "\""; whatItIs = "string"; goto done2; }
  559. if (lineParts[i] == mapVarNames[j] + ".$name") { lineParts[i] = "\"" + mapVars[j].name + "\""; whatItIs = "string"; goto done2; }
  560. if (lineParts[i] == mapVarNames[j] + ".$creator") { lineParts[i] = "\"" + mapVars[j].name + "\""; whatItIs = "string"; goto done2; }
  561. if (lineParts[i] == playerVarNames[j] + ".$name") { lineParts[i] = "\"" + playerVars[j].name + "\""; whatItIs = "string"; goto done2; }
  562. if (lineParts[i] == playerVarNames[j] + ".$titlecolor") { lineParts[i] = "\"" + playerVars[j].titleColor + "\""; whatItIs = "string"; goto done2; }
  563. if (lineParts[i] == playerVarNames[j] + ".$title") { lineParts[i] = "\"" + playerVars[j].title + "\""; whatItIs = "string"; goto done2; }
  564. if (lineParts[i] == playerVarNames[j] + ".$map") { lineParts[i] = playerVars[j].map.name; whatItIs = "map"; goto done2; }
  565. if (lineParts[i] == playerVarNames[j] + ".$rank") { lineParts[i] = playerVars[j].rank.permission.ToString(); whatItIs = "number"; goto done2; }
  566. if (lineParts[i] == stringVarNames[j] + ".$length") { lineParts[i] = stringVars[j].Length.ToString(); whatItIs = "number"; goto done2; }
  567. if (lineParts[i] == numberVarNames[j]) { lineParts[i] = numberVars[j].ToString(); whatItIs = "number"; goto done2; }
  568. if (lineParts[i] == stringVarNames[j]) { lineParts[i] = "\"" + stringVars[j].ToString() + "\""; whatItIs = "string"; goto done2; }
  569. if (lineParts[i] == playerVarNames[j]) { whatItIs = "player"; goto done2; } // lineParts[i] = playerVars[j].name; }
  570. if (lineParts[i] == mapVarNames[j]) { whatItIs = "map"; goto done2; } // lineParts[i] = mapVars[j].name; }
  571. j++;
  572. }
  573. if (whatItIs == "")
  574. {
  575. lineError = "Could not find variable $" + lineParts[i];
  576. goto nextLine;
  577. }
  578. }
  579. lineError = "Unknown syntax error.";
  580. goto nextLine;
  581. done2:
  582. //if (whatItIs == "map/player" && (shouldBe == "map" || shouldBe == "player")) goto done3; <-- nope, removed that outta here
  583. if (whatItIs == "string")
  584. {
  585. lineParts[i] = lineParts[i].Replace("$server.$defaultcolor", Vars.Color);
  586. j = 0;
  587. while (j < 255)
  588. {
  589. //try { if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + playerVarNames[j] + ".$rank.$name.$length", playerVars[j].group.name.Length); } catch { }
  590. try { if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + playerVarNames[j] + ".$map.$name.$length", playerVars[j].map.name.ToString()); }
  591. catch { }
  592. try { if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + playerVarNames[j] + ".$map.$owner.$length", playerVars[j].map.name.ToString()); }
  593. catch { }
  594. try { if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + playerVarNames[j] + ".$rank.$name.$length", playerVars[j].rank.name.ToString()); }
  595. catch { }
  596. try { if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + playerVarNames[j] + ".$rank.$color.$length", playerVars[j].rank.color.Length.ToString()); }
  597. catch { }
  598. try { if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + mapVarNames[j] + ".$map.$name", playerVars[j].map.name); }
  599. catch { }
  600. try { if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + playerVarNames[j] + ".$map.$owner", playerVars[j].map.owner); }
  601. catch { }
  602. try { if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + playerVarNames[j] + ".$rank.$name", playerVars[j].rank.name); }
  603. catch { }
  604. try { if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + playerVarNames[j] + ".$rank.$color", playerVars[j].rank.color); }
  605. catch { }
  606. try { if (mapVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + mapVarNames[j] + ".$players.$count", mapVars[j].players.Count.ToString()); }
  607. catch { }
  608. try { if (mapVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + mapVarNames[j] + ".$name.$length", mapVars[j].name.Length.ToString()); }
  609. catch { }
  610. try { if (mapVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + mapVarNames[j] + ".$creator.$length", mapVars[j].owner.Length.ToString()); }
  611. catch { }
  612. try { if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + playerVarNames[j] + ".$name", playerVars[j].name); }
  613. catch { }
  614. try { if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + playerVarNames[j] + ".$rank", playerVars[j].rank.color + playerVars[j].rank.name); }
  615. catch { }
  616. try { if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + playerVarNames[j] + ".$titlecolor", playerVars[j].titleColor); }
  617. catch { }
  618. try { if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + playerVarNames[j] + ".$title", playerVars[j].title); }
  619. catch { }
  620. try { if (mapVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + mapVarNames[j] + ".$name", mapVars[j].name); }
  621. catch { }
  622. try { if (mapVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + mapVarNames[j] + ".$creator", mapVars[j].owner); }
  623. catch { }
  624. try { if (stringVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + stringVarNames[j] + ".$length", stringVars[j].Length.ToString()); }
  625. catch { }
  626. try { if (stringVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + stringVarNames[j], stringVars[j]); }
  627. catch { }
  628. try { if (numberVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + numberVarNames[j], numberVars[j].ToString()); } catch { }
  629. j++;
  630. }
  631. }
  632. if (whatItIs == "math") ///// FIRST CONVERT ALL MATH TO NUMBER. THEN LATER CONVERT THE NUMBERS TO OTHER STUFF, IF NECESSARY :)
  633. {
  634. j = 0;
  635. while (j < 255)
  636. {
  637. if (numberVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + numberVarNames[j], numberVars[j].ToString());
  638. if (playerVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + playerVarNames[j] + ".$map.$players.$count", playerVars[j].map.players.Count.ToString());
  639. if (mapVarNames[j] != null) lineParts[i] = lineParts[i].Replace("$" + mapVarNames[j] + ".$players.$count", mapVars[j].players.Count.ToString());
  640. j++;
  641. }
  642. try
  643. {
  644. lineParts[i] = XMath.Parse(lineParts[i]).ToString();
  645. whatItIs = "number";
  646. }
  647. catch (Exception e) // ehhh, does this work?
  648. {
  649. lineError = "Could not parse math " + lineParts[i] + ": " + e.Message;
  650. goto nextLine;
  651. }
  652. }
  653. if (whatItIs == shouldBe) goto done3;
  654. if (whatItIs == "string" && shouldBe == "number")////// STRING --> NUMBER
  655. {
  656. if (!System.Double.TryParse(ParseString(lineParts[i]), out parseTryer))
  657. {
  658. if (Block.Number(ParseString(lineParts[i])) == 50)
  659. {
  660. lineError = "Could not convert string " + lineParts[i] + " to number.";
  661. goto nextLine;
  662. }
  663. else
  664. {
  665. lineParts[i] = Convert.ToInt16(Block.Number(ParseString(lineParts[i]))).ToString();
  666. }
  667. }
  668. else
  669. {
  670. lineParts[i] = ParseString(lineParts[i]);
  671. }
  672. goto done3;
  673. }
  674. else if (whatItIs == "number" && shouldBe == "string")
  675. {
  676. lineParts[i] = "\"" + lineParts[i] + "\"";
  677. }
  678. else
  679. {
  680. lineError = "Failed to convert " + whatItIs + " to " + shouldBe + ".";
  681. goto nextLine;
  682. }
  683. #region removed conversions
  684. /* //// MADE IMPOSSIBLE SINCE MAPS AND PLAYERS ARE ONLY VARIABLES
  685. else if (whatItIs == "string" && shouldBe == "map") STRING --> MAP
  686. {
  687. if (Map.Find(lineParts[i]) == null)
  688. {
  689. lineError = "Could not convert string \"" + lineParts[i] + "\" to map, no such map is loaded.";
  690. goto nextLine;
  691. }
  692. else
  693. {
  694. lineParts[i] = ParseString(lineParts[i]);
  695. }
  696. }
  697. */
  698. /*
  699. else if (whatItIs == "string" && shouldBe == "player") ////// STRING --> PLAYER
  700. {
  701. if (Player.Find(lineParts[i]) == null)
  702. {
  703. lineError = "Could not convert string \"" + lineParts[i] + "\" to player, no such player is connected.";
  704. goto nextLine;
  705. }
  706. else
  707. {
  708. lineParts[i] = ParseString(lineParts[i]);
  709. }
  710. }
  711. */
  712. #endregion
  713. done3:
  714. i++;
  715. }
  716. #endregion
  717. #region Try Catch
  718. if (lineParts[0].ToLower() == "try")
  719. {
  720. if (inTryBlock)
  721. {
  722. lineError = "You can't have a try block inside another try block, sorry.";
  723. block_type[curBlock + 1] = "nothing";
  724. goto nextLine;
  725. }
  726. block_type[curBlock + 1] = "try";
  727. inTryBlock = true;
  728. executeCatch = false;
  729. tryCurBlock = curBlock;
  730. goto nextLine;
  731. }
  732. if (lineParts[0].ToLower() == "catch")
  733. {
  734. block_type[curBlock + 1] = "catch";
  735. goto nextLine;
  736. }
  737. #endregion
  738. #region Repeat StopRepeat
  739. if (line.ToLower() == "repeat")
  740. {
  741. block_type[curBlock + 1] = "repeat";
  742. goto nextLine;
  743. }
  744. if (line.ToLower() == "stop repeat" || line.ToLower() == "stop repeating" || line.ToLower() == "stoprepeat" || line.ToLower() == "stoprepeating")
  745. {
  746. i = lineNum;
  747. double blocksLess = 0;
  748. while (i >= 0)
  749. {
  750. if (lines[i].Trim() == "{") blocksLess++;
  751. else if (lines[i].Trim() == "}") blocksLess--;
  752. else

Large files files are truncated, but you can click here to view the full file