PageRenderTime 139ms CodeModel.GetById 21ms RepoModel.GetById 2ms app.codeStats 1ms

/MCQuai/MJS/Interpreter.cs

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

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