PageRenderTime 69ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/MJS/Interpreter.cs

https://bitbucket.org/realquaisaq/mcrevive
C# | 2141 lines | 2044 code | 32 blank | 65 comment | 971 complexity | f7e3658119489266f7486ea9b28e6730 MD5 | raw 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 if (lines[i].Trim().ToLower() == "repeat") goto repeatFound;
  753. i--;
  754. }
  755. lineError = "How the hell can it stop repeating when its not repeating :')";
  756. goto nextLine;
  757. repeatFound:
  758. curBlock -= (int)blocksLess - 1;
  759. i = lineNum;
  760. while (i <= lines.Length)
  761. {
  762. if (lines[i].Trim() == "{") blocksLess++;
  763. else if (lines[i].Trim() == "}") blocksLess--;
  764. if (blocksLess == 0) goto repeatEndFound;
  765. i++;
  766. }
  767. repeatEndFound:
  768. lineNum = i;
  769. goto nextLine;
  770. }
  771. #endregion
  772. #region Thread
  773. if (line.ToLower() == "thread")
  774. {
  775. block_type[curBlock + 1] = "thread";
  776. goto nextLine;
  777. }
  778. #endregion
  779. if (lineParts.Length >= 2)
  780. {
  781. #region CREATE NUMBER VARIABLE
  782. if (lineParts[0] == "number" && lineParts[1].StartsWith("$"))
  783. {
  784. lineParts[1] = lineParts[1].Remove(0, 1);
  785. i = 0;
  786. while (i < 255)
  787. {
  788. if (numberVarNames[i] == lineParts[1] || stringVarNames[i] == lineParts[1] || mapVarNames[i] == lineParts[1] || playerVarNames[i] == lineParts[1])
  789. {
  790. lineError = "Could not create number variable $" + lineParts[1] + ", variable already exists.";
  791. goto nextLine;
  792. }
  793. i++;
  794. }
  795. numberVarNames[numberVarCount] = lineParts[1];
  796. if (lineParts.Length > 3)
  797. {
  798. if (lineParts[2] == "=")
  799. {
  800. if (!System.Double.TryParse(lineParts[3], out numberVars[numberVarCount]))
  801. {
  802. lineError = "Number expected but syntax error found.";
  803. goto nextLine;
  804. }
  805. //numberVars[numberVarCount] = Convert.ToDouble(lineParts[3]);
  806. }
  807. }
  808. numberVarCount++;
  809. goto nextLine;
  810. }
  811. #endregion
  812. #region CREATE STRING VARIABLE
  813. if (lineParts[0] == "string" && lineParts[1].StartsWith("$"))
  814. {
  815. lineParts[1] = lineParts[1].Remove(0, 1);
  816. i = 0;
  817. while (i < 255)
  818. {
  819. if (numberVarNames[i] == lineParts[1] || stringVarNames[i] == lineParts[1] || mapVarNames[i] == lineParts[1] || playerVarNames[i] == lineParts[1])
  820. {
  821. lineError = "Could not create string variable $" + lineParts[1] + ", variable already exists.";
  822. goto nextLine;
  823. }
  824. i++;
  825. }
  826. stringVarNames[stringVarCount] = lineParts[1];
  827. if (lineParts.Length > 2)
  828. {
  829. if (lineParts[2] == "=")
  830. {
  831. if (!lineParts[3].StartsWith("\"") || !lineParts[3].EndsWith("\""))
  832. {
  833. lineError = "String expected but syntax error found.";
  834. goto nextLine;
  835. }
  836. stringVars[stringVarCount] = ParseString(lineParts[3]);
  837. }
  838. }
  839. stringVarCount++;
  840. goto nextLine;
  841. }
  842. #endregion
  843. #region CREATE MAP VARIABLE
  844. if (lineParts[0] == "map" && lineParts[1].StartsWith("$"))
  845. {
  846. lineParts[1] = lineParts[1].Remove(0, 1);
  847. i = 0;
  848. while (i < 255)
  849. {
  850. if (numberVarNames[i] == lineParts[1] || stringVarNames[i] == lineParts[1] || mapVarNames[i] == lineParts[1] || playerVarNames[i] == lineParts[1])
  851. {
  852. lineError = "Could not create map variable $" + lineParts[1] + ", variable already exists.";
  853. goto nextLine;
  854. }
  855. i++;
  856. }
  857. mapVarNames[mapVarCount] = lineParts[1];
  858. stringVarNames[stringVarCount] = lineParts[1] + ".$name";
  859. stringVarCount++;
  860. if (lineParts.Length > 2)
  861. {
  862. if (lineParts[2] == "=")
  863. {
  864. mapVars[mapVarCount] = Map.Find(ParseString(lineParts[3]));
  865. if (mapVars[mapVarCount] == null)
  866. {
  867. lineError = "Could not find map \"" + lineParts[3] + "\"";
  868. goto nextLine;
  869. }
  870. }
  871. }
  872. mapVarCount++;
  873. goto nextLine;
  874. }
  875. #endregion
  876. #region CREATE PLAYER VARIABLE
  877. if (lineParts[0] == "player" && lineParts[1].StartsWith("$"))
  878. {
  879. lineParts[1] = lineParts[1].Remove(0, 1);
  880. i = 0;
  881. while (i < 255)
  882. {
  883. if (numberVarNames[i] == lineParts[1] || stringVarNames[i] == lineParts[1] || mapVarNames[i] == lineParts[1] || playerVarNames[i] == lineParts[1])
  884. {
  885. lineError = "Could not create player variable $" + lineParts[1] + ", variable already exists.";
  886. goto nextLine;
  887. }
  888. i++;
  889. }
  890. playerVarNames[playerVarCount] = lineParts[1];
  891. mapVarNames[mapVarCount] = lineParts[1] + ".$map";
  892. stringVarNames[stringVarCount] = lineParts[1] + ".$map.$name";
  893. if (lineParts.Length > 2)
  894. {
  895. if (lineParts[2] == "=")
  896. {
  897. if (lineParts[3].StartsWith("\"") && lineParts[3].EndsWith("\""))
  898. {
  899. playerVars[playerVarCount] = Player.Find(ParseString(lineParts[3]));
  900. if (playerVars[playerVarCount] == null)
  901. {
  902. lineError = "Could not find player \"" + lineParts[3] + "\"";
  903. goto nextLine;
  904. }
  905. }
  906. else if (lineParts[3].StartsWith("$"))
  907. {
  908. lineParts[1] = lineParts[1].Remove(0, 1);
  909. i = 0;
  910. while (i < 255)
  911. {
  912. if (playerVarNames[i] == lineParts[1]) goto varFound002;
  913. i++;
  914. }
  915. lineError = "Could not find player variable $" + lineParts[1] + ".";
  916. goto nextLine;
  917. varFound002:
  918. playerVars[playerVarCount] = playerVars[i];
  919. }
  920. mapVars[mapVarCount] = playerVars[playerVarCount].map;
  921. stringVars[stringVarCount] = playerVars[playerVarCount].map.name;
  922. }
  923. }
  924. mapVarCount++;
  925. playerVarCount++;
  926. stringVarCount++;
  927. goto nextLine;
  928. }
  929. #endregion
  930. #region NON-STATIC VARIABLE FUNCTIONS
  931. if (lineParts[0].ToLower() == "addvar" || lineParts[0].ToLower() == "addvariable" || lineParts[0].ToLower() == "addnonstaticvar" || lineParts[0].ToLower() == "addnonstaticvariable")
  932. {
  933. string newVar = lineParts[1];
  934. string extends = "";
  935. if (newVar.StartsWith("player.$"))
  936. {
  937. extends = "player";
  938. }
  939. else if (newVar.StartsWith("map.$"))
  940. {
  941. extends = "map";
  942. }
  943. else
  944. {
  945. lineError = "Syntax error!";
  946. goto nextLine;
  947. }
  948. newVar = newVar.Replace(extends+".$", "");
  949. if (Directory.Exists("memory\\" + extends + "\\mjs\\" + newVar))
  950. {
  951. lineError = "Variable already exists;";
  952. goto nextLine;
  953. }
  954. else
  955. {
  956. Directory.CreateDirectory("memory\\" + extends + "\\mjs\\" + newVar);
  957. }
  958. goto nextLine;
  959. }
  960. #endregion
  961. // Foundvar 7 t/m 9:
  962. #region ASSIGN VALUE TO VARIABLES
  963. if (lineParts[1] == "=")
  964. {
  965. i = 0;
  966. varType = "";
  967. lineParts[0] = lineParts[0].Remove(0, 1);
  968. while (i < 255)
  969. {
  970. if (numberVarNames[i] == lineParts[0]) { varType = "number"; goto foundVar7; }
  971. if (stringVarNames[i] == lineParts[0]) { varType = "string"; goto foundVar7; }
  972. if (playerVarNames[i] == lineParts[0]) { varType = "player"; goto foundVar7; }
  973. if (mapVarNames[i] == lineParts[0]) { varType = "map"; goto foundVar7; }
  974. i++;
  975. }
  976. lineError = "Could not assign variable, variable $" + lineParts[0] + " does not exist.";
  977. goto nextLine;
  978. foundVar7:
  979. lineParts[0] = "$" + lineParts[0];
  980. if (varType == "string") { stringVars[i] = ParseString(lineParts[2]); goto nextLine; }
  981. if (varType == "number") { numberVars[i] = System.Double.Parse(lineParts[2]); goto nextLine; }
  982. if (varType == "player")
  983. {
  984. if (lineParts[2].StartsWith("$"))
  985. {
  986. lineParts[2] = lineParts[2].Remove(0, 1);
  987. j = 0;
  988. while (j < 255)
  989. {
  990. if (playerVarNames[j] == lineParts[2]) goto foundVar8;
  991. j++;
  992. }
  993. lineError = "Could not find player variable $" + lineParts[2] + ".";
  994. goto nextLine;
  995. foundVar8:
  996. playerVars[i] = playerVars[j];
  997. goto nextLine;
  998. }
  999. else if (lineParts[2].StartsWith("\""))
  1000. {
  1001. lineParts[2] = ParseString(lineParts[2]);
  1002. playerVars[i] = Player.Find(lineParts[2]);
  1003. if (playerVars[i] != null) lineError = "Could not assign player variable. No player " + lineParts[2] + " is connected.";
  1004. goto nextLine;
  1005. }
  1006. // no extra else is needed. that error is already handled somewhere :)
  1007. goto nextLine;
  1008. }
  1009. if (varType == "map")
  1010. {
  1011. if (lineParts[2].StartsWith("$"))
  1012. {
  1013. lineParts[2] = lineParts[2].Remove(0, 1);
  1014. j = 0;
  1015. while (j < 255)
  1016. {
  1017. if (mapVarNames[j] == lineParts[2]) goto foundVar9;
  1018. j++;
  1019. }
  1020. lineError = "Could not find map variable $" + lineParts[2] + ".";
  1021. goto nextLine;
  1022. foundVar9:
  1023. mapVars[i] = mapVars[j];
  1024. goto nextLine;
  1025. }
  1026. else if (lineParts[2].StartsWith("\""))
  1027. {
  1028. lineParts[2] = ParseString(lineParts[2]);
  1029. mapVars[i] = Map.Find(lineParts[2]);
  1030. if (mapVars[i] != null) lineError = "Could not assign player variable. No map " + lineParts[2] + " is loaded.";
  1031. goto nextLine;
  1032. }
  1033. // no extra else is needed. that error is already handled somewhere :)
  1034. goto nextLine;
  1035. }
  1036. }
  1037. #endregion
  1038. // Foundvar 10 t/m 16:
  1039. #region NUMBERS ++ -- += -= *= /= ^=
  1040. if (lineParts[1] == "++")
  1041. {
  1042. lineParts[0] = lineParts[0].Remove(0, 1);
  1043. i = 0;
  1044. while (i < 255)
  1045. {
  1046. if (lineParts[0] == numberVarNames[i]) goto foundVar10;
  1047. i++;
  1048. }
  1049. lineError = "Could not find variable $" + lineParts[0] + " :(";
  1050. goto nextLine;
  1051. foundVar10:
  1052. numberVars[i]++;
  1053. goto nextLine;
  1054. }
  1055. if (lineParts[1] == "--")
  1056. {
  1057. lineParts[0] = lineParts[0].Remove(0, 1);
  1058. i = 0;
  1059. while (i < 255)
  1060. {
  1061. if (lineParts[0] == numberVarNames[i]) goto foundVar11;
  1062. i++;
  1063. }
  1064. lineError = "Could not find variable $" + lineParts[0] + " :(";
  1065. goto nextLine;
  1066. foundVar11:
  1067. numberVars[i]--;
  1068. goto nextLine;
  1069. }
  1070. if (lineParts[1] == "+=")
  1071. {
  1072. lineParts[0] = lineParts[0].Remove(0, 1);
  1073. i = 0;
  1074. while (i < 255)
  1075. {
  1076. if (lineParts[0] == numberVarNames[i]) goto foundVar12;
  1077. i++;
  1078. }
  1079. lineError = "Could not find variable $" + lineParts[0] + " :(";
  1080. goto nextLine;
  1081. foundVar12:
  1082. try
  1083. {
  1084. numberVars[i] += System.Double.Parse(lineParts[2]);
  1085. }
  1086. catch (Exception e)
  1087. {
  1088. lineError = "Failed to change number variable $" + lineParts[0] + ": " + e.Message;
  1089. }
  1090. goto nextLine;
  1091. }
  1092. if (lineParts[1] == "-=")
  1093. {
  1094. lineParts[0] = lineParts[0].Remove(0, 1);
  1095. i = 0;
  1096. while (i < 255)
  1097. {
  1098. if (lineParts[0] == numberVarNames[i]) goto foundVar13;
  1099. i++;
  1100. }
  1101. lineError = "Could not find variable $" + lineParts[0] + " :(";
  1102. goto nextLine;
  1103. foundVar13:
  1104. try
  1105. {
  1106. numberVars[i] -= System.Double.Parse(lineParts[2]);
  1107. }
  1108. catch (Exception e)
  1109. {
  1110. lineError = "Failed to change number variable $" + lineParts[0] + ": " + e.Message;
  1111. }
  1112. goto nextLine;
  1113. }
  1114. if (lineParts[1] == "*=")
  1115. {
  1116. lineParts[0] = lineParts[0].Remove(0, 1);
  1117. i = 0;
  1118. while (i < 255)
  1119. {
  1120. if (lineParts[0] == numberVarNames[i]) goto foundVar14;
  1121. i++;
  1122. }
  1123. lineError = "Could not find variable $" + lineParts[0] + " :(";
  1124. goto nextLine;
  1125. foundVar14:
  1126. try
  1127. {
  1128. numberVars[i] *= System.Double.Parse(lineParts[2]);
  1129. }
  1130. catch (Exception e)
  1131. {
  1132. lineError = "Failed to change number variable $" + lineParts[0] + ": " + e.Message;
  1133. }
  1134. goto nextLine;
  1135. }
  1136. if (lineParts[1] == "/=")
  1137. {
  1138. lineParts[0] = lineParts[0].Remove(0, 1);
  1139. i = 0;
  1140. while (i < 255)
  1141. {
  1142. if (lineParts[0] == numberVarNames[i]) goto foundVar15;
  1143. i++;
  1144. }
  1145. lineError = "Could not find variable $" + lineParts[0] + " :(";
  1146. goto nextLine;
  1147. foundVar15:
  1148. try
  1149. {
  1150. numberVars[i] /= System.Double.Parse(lineParts[2]);
  1151. }
  1152. catch (Exception e)
  1153. {
  1154. lineError = "Failed to change number variable $" + lineParts[0] + ": " + e.Message;
  1155. }
  1156. goto nextLine;
  1157. }
  1158. if (lineParts[1] == "^=")
  1159. {
  1160. lineParts[0] = lineParts[0].Remove(0, 1);
  1161. i = 0;
  1162. while (i < 255)
  1163. {
  1164. if (lineParts[0] == numberVarNames[i]) goto foundVar16;
  1165. i++;
  1166. }
  1167. lineError = "Could not find variable $" + lineParts[0] + " :(";
  1168. goto nextLine;
  1169. foundVar16:
  1170. try
  1171. {
  1172. numberVars[i] = Math.Pow(numberVars[i], System.Double.Parse(lineParts[2]));
  1173. }
  1174. catch (Exception e)
  1175. {
  1176. lineError = "Failed to change number variable $" + lineParts[0] + ": " + e.Message;
  1177. }
  1178. goto nextLine;
  1179. }
  1180. #endregion
  1181. #region GlobalMessage PlayerMessage ConsoleMessage
  1182. if (lineParts[0] == "GlobalMessage")
  1183. {
  1184. Events.GlobalMessage(ParseString(lineParts[1]));
  1185. goto nextLine;
  1186. }
  1187. if (lineParts[0] == "PlayerMessage")
  1188. {
  1189. //Server.s.Log(lineParts[1]);
  1190. lineParts[1] = lineParts[1].Remove(0, 1);
  1191. //Server.s.Log(lineParts[1]);
  1192. i = 0;
  1193. while (i < 255)
  1194. {
  1195. if (playerVarNames[i] == lineParts[1]) goto varFound001;
  1196. i++;
  1197. }
  1198. lineError = "Could not find player variable $" + lineParts[1] + ".";
  1199. goto nextLine;
  1200. varFound001:
  1201. Events.Log(playerVars[i].name);
  1202. playerVars[i].SendMessage(ParseString(lineParts[2]));
  1203. goto nextLine;
  1204. }
  1205. if (lineParts[0] == "ConsoleMessage")
  1206. {
  1207. Events.Log(ParseString(lineParts[1]));
  1208. goto nextLine;
  1209. }
  1210. #endregion
  1211. // Foundvar 0 t/m 6:
  1212. #region GenerateMap LoadMap FindMap UnloadMap SaveMap DeleteMap
  1213. if (lineParts[0] == "GenerateMap")
  1214. {
  1215. lineParts[1] = lineParts[1].Remove(0, 1);
  1216. ushort width, height, depth;
  1217. i = 0;
  1218. while (i < 255)
  1219. {
  1220. if (mapVarNames[i] == lineParts[1]) goto foundVar0;
  1221. i++;
  1222. }
  1223. Events.Log("No variable of type map named " + lineParts[1] + " exists.");
  1224. goto nextLine;
  1225. foundVar0:
  1226. if (!System.UInt16.TryParse(lineParts[3], out width)) { lineError = "The map width must be an integer."; goto nextLine; };
  1227. if (!System.UInt16.TryParse(lineParts[4], out height)) { lineError = "The map height must be an integer."; goto nextLine; };
  1228. if (!System.UInt16.TryParse(lineParts[5], out depth)) { lineError = "The map depth must be an integer."; goto nextLine; };
  1229. mapVars[i] = new Map(ParseString(lineParts[2]), width, height, depth, new ushort[width*height*depth], null);
  1230. //Event.Trigger("MapGenerate", "string $name " + mapVars[i].name);
  1231. goto nextLine;
  1232. }
  1233. else if (lineParts[0] == "LoadMap")
  1234. {
  1235. Map loadedLevel = null;
  1236. if (lineParts[1].StartsWith("\""))
  1237. {
  1238. lineParts[1] = ParseString(lineParts[1]);
  1239. if (!File.Exists("levels/" + lineParts[1] + ".mcqlvl") && !File.Exists("levels/" + lineParts[1] + ".lvl"))
  1240. {
  1241. lineError = "The map " + lineParts[1] + " does not exist.";
  1242. goto nextLine;
  1243. }
  1244. Events.LoadMap(lineParts[1]);
  1245. Events.GetMaps();
  1246. loadedLevel = Map.Find(lineParts[1]);
  1247. }
  1248. else// if (lineParts[1].StartsWith("$"))
  1249. { // damn, somehow the $ is already chopped off somewhere else. quick fix!
  1250. //lineParts[1] = lineParts[1].Remove(0, 1);
  1251. i = 0;
  1252. while (i < 255)
  1253. {
  1254. if (mapVarNames[i] == lineParts[1]) goto foundVar1;
  1255. i++;
  1256. }
  1257. lineError = "Failed to load map. No variable of type map named " + lineParts[1] + " exists.";
  1258. goto nextLine;
  1259. foundVar1:
  1260. Events.LoadMap(mapVars[i].name);
  1261. }
  1262. //else
  1263. //{
  1264. // lineError = "Unexpected characters \"" + lineParts[1] + "\"";
  1265. // goto nextLine;
  1266. //}
  1267. if (lineParts.Length > 2)
  1268. {
  1269. lineParts[2] = lineParts[2].Remove(0, 1);
  1270. i = 0;
  1271. while (i < 255)
  1272. {
  1273. if (mapVarNames[i] == lineParts[2]) goto foundVar2;
  1274. i++;
  1275. }
  1276. lineError = "No variable of type map named " + lineParts[2] + " exists.";
  1277. goto nextLine;
  1278. foundVar2:
  1279. mapVars[i] = loadedLevel;
  1280. }
  1281. goto nextLine;
  1282. }
  1283. else if (lineParts[0] == "FindMap")
  1284. {
  1285. lineParts[2] = lineParts[2].Remove(0, 1);
  1286. i = 0;
  1287. while (i < 255)
  1288. {
  1289. if (mapVarNames[i] == lineParts[2]) goto foundVar3;
  1290. i++;
  1291. }
  1292. lineError = "No variable of type map named " + lineParts[2] + " exists.";
  1293. goto nextLine;
  1294. foundVar3:
  1295. mapVars[i] = Map.Find(ParseString(lineParts[1]));
  1296. if (mapVars[i] == null)
  1297. {
  1298. lineError = "Could not find map " + lineParts[1] + ". No such map is loaded.";
  1299. }
  1300. goto nextLine;
  1301. }
  1302. else if (lineParts[0] == "UnloadMap")
  1303. {
  1304. Map unloadedLevel;
  1305. if (lineParts[1].StartsWith("\""))
  1306. {
  1307. lineParts[1] = ParseString(lineParts[1]);
  1308. if (!File.Exists("maps/" + lineParts[1] + ".map"))
  1309. {
  1310. lineError = "Failed to unload map. File maps/" + lineParts[1] + ".map does not exist.";
  1311. goto nextLine;
  1312. }
  1313. unloadedLevel = Map.Find(lineParts[1]);
  1314. if (unloadedLevel == null)
  1315. {
  1316. lineError = "Failed to unload map. No map called " + lineParts[1] + " is loaded.";
  1317. }
  1318. }
  1319. else if (lineParts[1].StartsWith("$"))
  1320. {
  1321. lineParts[2] = lineParts[2].Remove(0, 1);
  1322. i = 0;
  1323. while (i < 255)
  1324. {
  1325. if (mapVarNames[i] == lineParts[2]) goto foundVar4;
  1326. i++;
  1327. }
  1328. lineError = "Failed to unload map. No variable of type map named " + lineParts[2] + " exists.";
  1329. goto nextLine;
  1330. foundVar4:
  1331. mapVars[i].Unload();
  1332. }
  1333. else
  1334. {
  1335. lineError = "Unexpected characters \"" + lineParts[1] + "\"";
  1336. goto nextLine;
  1337. }
  1338. goto nextLine;
  1339. }
  1340. else if (lineParts[0] == "SaveMap")
  1341. {
  1342. Map savedLevel;
  1343. if (lineParts[1].StartsWith("\""))
  1344. {
  1345. lineParts[1] = ParseString(lineParts[1]);
  1346. if (!File.Exists("maps/" + lineParts[1] + ".map"))
  1347. {
  1348. lineError = "Failed to save map. File maps/" + lineParts[1] + ".map does not exist.";
  1349. goto nextLine;
  1350. }
  1351. savedLevel = Map.Find(lineParts[1]);
  1352. if (savedLevel == null)
  1353. {
  1354. lineError = "Failed to save map. No map called " + lineParts[1] + " is loaded.";
  1355. goto nextLine;
  1356. }
  1357. savedLevel.Save();
  1358. }
  1359. else if (lineParts[1].StartsWith("$"))
  1360. {
  1361. lineParts[1] = lineParts[1].Remove(0, 1);
  1362. i = 0;
  1363. while (i < 255)
  1364. {
  1365. if (mapVarNames[i] == lineParts[1]) goto foundVar5;
  1366. i++;
  1367. }
  1368. lineError = "Failed to save map. No variable of type map named $" + lineParts[1] + " exists.";
  1369. goto nextLine;
  1370. foundVar5:
  1371. mapVars[i].Save();
  1372. }
  1373. else
  1374. {
  1375. lineError = "Unexpected characters \"" + lineParts[1] + "\"";
  1376. goto nextLine;
  1377. }
  1378. goto nextLine;
  1379. }
  1380. else if (lineParts[0] == "DeleteMap")
  1381. {
  1382. if (lineParts[1].StartsWith("\""))
  1383. {
  1384. lineParts[1] = ParseString(lineParts[1]);
  1385. if (!File.Exists("levels/" + lineParts[1] + ".lvl") && !File.Exists("levels/" + lineParts[1] + ".mcqlvl")) { lineError = "Error while attempting to delete map " + lineParts[1] + ": It doesnt exist!"; goto nextLine; }
  1386. try { Map.Find(lineParts[1]).Unload(); }
  1387. catch { }
  1388. try { File.Delete("levels/" + lineParts[1] + ".mcqlvl"); }
  1389. catch { File.Delete("levels/" + lineParts[1] + ".lvl"); }
  1390. }
  1391. else if (lineParts[1].StartsWith("$"))
  1392. {
  1393. lineParts[1] = lineParts[1].Remove(0, 1);
  1394. i = 0;
  1395. while (i < 255)
  1396. {
  1397. if (mapVarNames[i] == lineParts[1]) goto foundVar6;
  1398. i++;
  1399. }
  1400. lineError = "Failed to delete map. No variable of type map named " + lineParts[1] + " exists.";
  1401. goto nextLine;
  1402. foundVar6:
  1403. try { mapVars[i].Unload(); }
  1404. catch { }
  1405. try { File.Delete("levels/" + mapVars[i].name + ".mcqlvl"); }
  1406. catch { File.Delete("levels/" + mapVars[i].name + ".lvl"); }
  1407. }
  1408. else
  1409. {
  1410. lineError = "Unexpected characters \"" + lineParts[1] + "\"";
  1411. goto nextLine;
  1412. }
  1413. goto nextLine;
  1414. }
  1415. #endregion
  1416. // Foundvar 17 t/m 19:
  1417. // Foundvar 23 t/m 25:
  1418. #region KickPlayer MovePlayer TeleportPlayer CommandPlayer
  1419. if (lineParts[0] == "KickPlayer")
  1420. {
  1421. lineParts[1] = lineParts[1].Remove(0, 1);
  1422. i = 0;
  1423. while (i < 255)
  1424. {
  1425. if (playerVarNames[i] == lineParts[1]) goto foundVar17;
  1426. i++;
  1427. }
  1428. lineError = "Could not find player variable $" + lineParts[1] + ".";
  1429. goto nextLine;
  1430. foundVar17:
  1431. if (lineParts.Length == 3) playerVars[i].Kick(ParseString(lineParts[2]));
  1432. else if (lineParts.Length == 2) playerVars[i].Kick("");
  1433. }
  1434. if (lineParts[0] == "MovePlayer")
  1435. {
  1436. lineParts[1] = lineParts[1].Remove(0, 1);
  1437. i = 0;
  1438. while (i < 255)
  1439. {
  1440. if (playerVarNames[i] == lineParts[1]) goto foundVar18;
  1441. i++;
  1442. }
  1443. lineError = "Could not find player variable $" + lineParts[1] + ".";
  1444. goto nextLine;
  1445. foundVar18:
  1446. Player p = playerVars[i];
  1447. lineParts[1] = lineParts[1].Remove(0, 1);
  1448. i = 0;
  1449. while (i < 255)
  1450. {
  1451. if (mapVarNames[i] == lineParts[2]) goto foundVar19;
  1452. i++;
  1453. }
  1454. lineError = "Could not find map variable $" + lineParts[2] + ".";
  1455. goto nextLine;
  1456. foundVar19:
  1457. string mapName = ParseString(lineParts[2]);
  1458. Map foundLevel = mapVars[i];
  1459. if (foundLevel != null)
  1460. {
  1461. Map startLevel = p.map;
  1462. if (p.map == foundLevel)
  1463. {
  1464. lineError = "Could not move player " + p.name + " to map " + mapName + ", they are already there.";
  1465. goto nextLine;
  1466. }
  1467. p.SendMap(foundLevel);
  1468. }
  1469. else
  1470. {
  1471. lineError = "Could not move player " + p.name + " to map " + mapName + ", no such map is loaded.";
  1472. }
  1473. GC.Collect();
  1474. GC.WaitForPendingFinalizers();
  1475. goto nextLine;
  1476. }
  1477. if (lineParts[0] == "TeleportPlayer")
  1478. {
  1479. Player p1, p2;
  1480. lineParts[1] = lineParts[1].Remove(0, 1);
  1481. i = 0;
  1482. while (i < 255)
  1483. {
  1484. if (playerVarNames[i] == lineParts[1]) goto foundVar23;
  1485. i++;
  1486. }
  1487. lineError = "Could not find player variable $" + lineParts[1] + ".";
  1488. goto nextLine;
  1489. foundVar23:
  1490. p1 = playerVars[i];
  1491. lineParts[2] = lineParts[2].Remove(0, 1);
  1492. i = 0;
  1493. while (i < 255)
  1494. {
  1495. if (playerVarNames[2] == lineParts[2]) goto foundVar24;
  1496. i++;
  1497. }
  1498. lineError = "Could not find player variable $" + lineParts[2] + ".";
  1499. goto nextLine;
  1500. foundVar24:
  1501. p2 = playerVars[i];
  1502. p1.SendPos(p2.pos[0], p2.pos[1], p2.pos[2], p2.rot[0], 0);
  1503. }
  1504. if (lineParts[0] == "CommandPlayer")
  1505. {
  1506. Player p1;
  1507. lineParts[1] = lineParts[1].Remove(0, 1);
  1508. i = 0;
  1509. while (i < 255)
  1510. {
  1511. if (playerVarNames[i] == lineParts[1]) goto foundVar25;
  1512. i++;
  1513. }
  1514. lineError = "Could not find player variable $" + lineParts[1] + ".";
  1515. goto nextLine;
  1516. foundVar25:
  1517. p1 = playerVars[i];
  1518. Command cmd = Command.Find(lineParts[2]);
  1519. if (cmd != null)
  1520. {
  1521. if (lineParts.Length == 3) cmd.Use(p1, "");
  1522. else
  1523. {
  1524. try
  1525. {
  1526. cmd.Use(p1, ParseString(lineParts[3]));
  1527. }
  1528. catch { lineError = "Something went wrong while executing /" + lineParts[2] + " " + lineParts[3]; goto nextLine; }
  1529. }
  1530. }
  1531. else { lineError = "Command " + lineParts[2] + " does not exist."; goto nextLine; }
  1532. }
  1533. #endregion
  1534. #region ExecuteScript TriggerEvent
  1535. if (lineParts[0] == "ExecuteScript")
  1536. {
  1537. string path = ParseString(lineParts[1]);
  1538. if (!path.StartsWith("mjs/") && !path.StartsWith("mjs\\")) path = "mjs/" + path;
  1539. if (!File.Exists(path)) { lineError = "Could not execute script " + path + ". File does not exist."; goto nextLine; }
  1540. if (lineParts.Length > 2)
  1541. {
  1542. i = 2;
  1543. string argumentString = "";
  1544. while (i < lineParts.Length)
  1545. {
  1546. if (!lineParts[i].StartsWith("$")) { lineError = "Failed to execute script because when calling another script, all input must be variables."; goto nextLine; }
  1547. lineParts[i] = lineParts[i].Remove(0, 1);
  1548. varType = "";
  1549. j = 0;
  1550. while (j < 255)
  1551. {
  1552. if (numberVarNames[j] == lineParts[i]) { varType = "number"; goto varFound8; }
  1553. if (stringVarNames[j] == lineParts[i]) { varType = "string"; goto varFound8; }
  1554. if (playerVarNames[j] == lineParts[i]) { varType = "player"; goto varFound8; }
  1555. if (mapVarNames[j] == lineParts[i]) { varType = "map"; goto varFound8; }
  1556. j++;
  1557. }
  1558. lineError = "Could not find variable $" + lineParts[i] + ".";
  1559. goto nextLine;
  1560. varFound8:
  1561. if (varType == "number") argumentString += varType + " $" + lineParts[i] + " " + numberVars[j].ToString();
  1562. if (varType == "player") argumentString += varType + " $" + lineParts[i] + " " + playerVars[j].name;
  1563. if (varType == "string") argumentString += varType + " $" + lineParts[i] + " " + stringVars[j].Replace(" ", "[space]");
  1564. if (varType == "map") argumentString += varType + " $" + lineParts[i] + " " + mapVars[j].name;
  1565. i++;
  1566. if (i < lineParts[i].Length) argumentString += " ";
  1567. }
  1568. Execute(path, argumentString);
  1569. }
  1570. else Execute(path, "");
  1571. goto nextLine;
  1572. }
  1573. if (lineParts[0] == "TriggerEvent")
  1574. {
  1575. string path = ParseString(lineParts[1]);
  1576. if (!path.StartsWith("mjs/") && !path.StartsWith("mjs/events")) path = "mjs/events/" + path;
  1577. if (!path.StartsWith("mjs/") && !path.StartsWith("mjs/")) path = "mjs/" + path;
  1578. if (!path.EndsWith(".mjs")) path += ".mjs";
  1579. if (!File.Exists(path)) { lineError = "Could not execute script " + path + ". File does not exist."; goto nextLine; }
  1580. if (lineParts.Length > 2)
  1581. {
  1582. i = 2;
  1583. string argumentString = "";
  1584. while (i < lineParts.Length)
  1585. {
  1586. if (!lineParts[i].StartsWith("$")) { lineError = "Failed to execute script because when calling another script, all input must be variables."; goto nextLine; }
  1587. lineParts[i] = lineParts[i].Remove(0, 1);
  1588. varType = "";
  1589. j = 0;
  1590. while (j < 255)
  1591. {
  1592. if (numberVarNames[j] == lineParts[i]) { varType = "number"; goto varFound8; }
  1593. if (stringVarNames[j] == lineParts[i]) { varType = "string"; goto varFound8; }
  1594. if (playerVarNames[j] == lineParts[i]) { varType = "player"; goto varFound8; }
  1595. if (mapVarNames[j] == lineParts[i]) { varType = "map"; goto varFound8; }
  1596. j++;
  1597. }
  1598. lineError = "Could not find variable $" + lineParts[i] + ".";
  1599. goto nextLine;
  1600. varFound8:
  1601. if (varType == "number") argumentString += varType + " $" + lineParts[i] + " " + numberVars[j].ToString();
  1602. if (varType == "player") argumentString += varType + " $" + lineParts[i] + " " + playerVars[j].name;
  1603. if (varType == "string") argumentString += varType + " $" + lineParts[i] + " " + stringVars[j].Replace(" ", "[space]");
  1604. if (varType == "map") argumentString += varType + " $" + lineParts[i] + " " + mapVars[j].name;
  1605. i++;
  1606. if (i < lineParts[i].Length) argumentString += " ";
  1607. }
  1608. Execute(path, argumentString);
  1609. }
  1610. else Execute(path, "");
  1611. }
  1612. #endregion
  1613. #region If
  1614. if (lineParts[0].ToLower() == "if")
  1615. {
  1616. bool isTrue = false;
  1617. if (lineParts[1] == "FileExists")
  1618. {
  1619. if (File.Exists(ParseString(lineParts[2]))) isTrue = true;
  1620. else isTrue = false;
  1621. }
  1622. //else if (lineParts[1] == "VarExists")
  1623. //{
  1624. // if (lineParts[2].StartsWith("player."))
  1625. // {
  1626. // lineParts[2] = lineParts[2].Remove(0, 7);
  1627. // isTrue = Player.variableNames.Contains(lineParts[2]);
  1628. // goto done101010;
  1629. // }
  1630. // if (lineParts[2].StartsWith("\"")) lineParts[2] = ParseString(lineParts[2]);
  1631. // if (lineParts[2].StartsWith("$")) lineParts[2] = lineParts[2].Remove(0, 1);
  1632. // i = 0;
  1633. // while (i < XMath.Max(mapVarCount, playerVarCount, stringVarCount, numberVarCount))
  1634. // {
  1635. // if (mapVarNames[i] == lineParts[2]) { isTrue = true; goto done101010; }
  1636. // if (stringVarNames[i] == lineParts[2]) { isTrue = true; goto done101010; }
  1637. // if (playerVarNames[i] == lineParts[2]) { isTrue = true; goto done101010; }
  1638. // if (numberVarNames[i] == lineParts[2]) { isTrue = true; goto done101010; }
  1639. // i++;
  1640. // }
  1641. // isTrue = false;
  1642. //done101010:
  1643. // i = 0;
  1644. //} I think its up to you to make this ;)
  1645. else if (lineParts[1] == "DirectoryExists")
  1646. {
  1647. if (Directory.Exists(ParseString(lineParts[2]))) isTrue = true;
  1648. else isTrue = false;
  1649. }
  1650. else if (lineParts[1] == "MapExists")
  1651. {
  1652. if (File.Exists("levels/" + ParseString(lineParts[2]) + ".mcqlvl") || File.Exists("levels/" + ParseString(lineParts[2]) + ".lvl")) isTrue = true;
  1653. else isTrue = false;
  1654. }
  1655. else if (lineParts[1] == "MapLoaded")
  1656. {
  1657. if (Map.Find(ParseString(lineParts[2])) == null) isTrue = false;
  1658. else isTrue = true;
  1659. }
  1660. else if (lineParts.Length >= 3 && (lineParts[2] == "<" || lineParts[2] == ">" || lineParts[2] == ">=" || lineParts[2] == "<="))
  1661. {
  1662. if (lineParts[2]==">")
  1663. {
  1664. if (int.Parse(lineParts[1])>int.Parse(lineParts[3])) isTrue = true;
  1665. else isTrue = false;
  1666. }
  1667. else if (lineParts[2]=="<")
  1668. {
  1669. if (int.Parse(lineParts[1])<int.Parse(lineParts[3])) isTrue = true;
  1670. else isTrue = false;
  1671. }
  1672. else if (lineParts[2]=="<=")
  1673. {
  1674. if (int.Parse(lineParts[1])<=int.Parse(lineParts[3])) isTrue = true;
  1675. else isTrue = false;
  1676. }
  1677. else if (lineParts[2]==">=")
  1678. {
  1679. if (int.Parse(lineParts[1])>=int.Parse(lineParts[3])) isTrue = true;
  1680. else isTrue = false;
  1681. }
  1682. }
  1683. else if (lineParts.Length >= 3 && (lineParts[2] == "==" || lineParts[2] == "!="))
  1684. {
  1685. if (lineParts[1].StartsWith("\""))
  1686. {
  1687. //Events.Log("lololol");
  1688. //Arr.Print(lineParts);
  1689. if (lineParts[1] == lineParts[3]) isTrue = true;
  1690. else isTrue = false;
  1691. }
  1692. else if (System.Double.TryParse(lineParts[1], out parseTryer))
  1693. {
  1694. if (lineParts[1] == lineParts[3]) isTrue = true;
  1695. else isTrue = false;
  1696. }
  1697. else if (lineParts[1].StartsWith("$"))
  1698. {
  1699. lineParts[1] = lineParts[1].Remove(0, 1);
  1700. lineParts[3] = lineParts[3].Remove(0, 1);
  1701. i = 0;
  1702. varType = "";
  1703. while (i < 255)
  1704. {
  1705. if (lineParts[1] == mapVarNames[i]) { varType = "map"; goto varFound2001; }
  1706. if (lineParts[1] == playerVarNames[i]) { varType = "player"; goto varFound2001; }
  1707. i++;
  1708. }
  1709. lineError = "Could not find variable $" + lineParts[1] + "..... You really depressed the interpreter here. :(";
  1710. goto nextLine;
  1711. varFound2001:
  1712. j = 0;
  1713. string varType2 = "";
  1714. while (j < 255)
  1715. {
  1716. if (lineParts[3] == mapVarNames[j]) { varType = "map"; goto varFound2002; }
  1717. if (lineParts[3] == playerVarNames[j]) { varType = "player"; goto varFound2002; }
  1718. j++;
  1719. }
  1720. lineError = "Could not find variable $" + lineParts[3] + "..... You really depressed the interpreter here.";
  1721. goto nextLine;
  1722. varFound2002:
  1723. if (varType != varType2)
  1724. {
  1725. lineError = "You cant compare a map with a player. Add .$map after a player variable to get the map he/she is in.";
  1726. goto nextLine;
  1727. }
  1728. if (varType == "map" && mapVars[i] == mapVars[j]) isTrue = true;
  1729. else if (varType == "map") isTrue = false;
  1730. else if (playerVars[i] == playerVars[j]) isTrue = true;
  1731. else isTrue = false;
  1732. }
  1733. else
  1734. {
  1735. lineError = "Syntax error. Expected a serious value to compare, but got some random characters... :(";
  1736. }
  1737. if (lineParts[2] == "!=") isTrue ^= true;
  1738. //goto nextLine;
  1739. }
  1740. else if (lineParts[1] == "FileContainsLine")
  1741. {
  1742. lineParts[2] = ParseString(lineParts[2]);
  1743. lineParts[3] = ParseString(lineParts[3]);
  1744. if (!File.Exists(lineParts[2])) { lineError = "Could not open file " + lineParts[2] + ", it does not exist!"; goto nextLine; }
  1745. if (File.ReadAllLines(lineParts[2]).Contains(lineParts[3])) isTrue = true;
  1746. else isTrue = false;
  1747. }
  1748. else if (lineParts[1] == "FileContainsText" || lineParts[1] == "FileContainsString")
  1749. {
  1750. lineParts[2] = ParseString(lineParts[2]);
  1751. lineParts[3] = ParseString(lineParts[3]);
  1752. if (!File.Exists(lineParts[2])) { lineError = "Could not open file " + lineParts[2] + ", it does not exist!"; goto nextLine; }
  1753. if (File.ReadAllText(lineParts[2]).Contains(lineParts[3])) isTrue = true;
  1754. else isTrue = false;
  1755. }
  1756. else if (lineParts[1] == "StringContains" || lineParts[1] == "StringContainsString")
  1757. {
  1758. if (ParseString(lineParts[2]).Contains(ParseString(lineParts[3]))) isTrue = true;
  1759. else isTrue = false;
  1760. }
  1761. else if (lineParts[1] == "StringStartsWith" || lineParts[1] == "StringStartsWithString")
  1762. {
  1763. if (ParseString(lineParts[2]).StartsWith(ParseString(lineParts[3]))) isTrue = true;
  1764. else isTrue = false;
  1765. }
  1766. else if (lineParts[1] == "StringEndsWith" || lineParts[1] == "StringEndsWithString")
  1767. {
  1768. if (ParseString(lineParts[2]).StartsWith(ParseString(lineParts[3]))) isTrue = true;
  1769. else isTrue = false;
  1770. }
  1771. else if (lineParts[1] == "PlayerConnected")
  1772. {
  1773. if (Player.Find(ParseString(lineParts[2])) == null) isTrue = false;
  1774. else isTrue = true;
  1775. }
  1776. block_type[curBlock + 1] = "if";
  1777. isTrue ^= ifNot; // I know, I'm a binary logic geek, and I really love Xor gates. :')
  1778. block_executeElse[curBlock] = !isTrue; // Because when the statement is true, it should execute the if block, not the else block.
  1779. goto nextLine;
  1780. }
  1781. #endregion
  1782. #region CreateDirectory CreateFile DeleteDirectory DeleteFile FileWriteLine FileRemoveLine
  1783. if (lineParts[0] == "CreateDirectory")
  1784. {
  1785. lineParts[1] = ParseString(lineParts[1]);
  1786. if (Directory.Exists(lineParts[1]))
  1787. {
  1788. lineError = "Cant create directory " + lineParts[1] + ", it already exists!";
  1789. }
  1790. else
  1791. {
  1792. Directory.CreateDirectory(lineParts[1]);
  1793. }
  1794. goto nextLine;
  1795. }
  1796. if (lineParts[0] == "CreateFile")
  1797. {
  1798. lineParts[1] = ParseString(lineParts[1]);
  1799. if (File.Exists(lineParts[1]))
  1800. {
  1801. lineError = "Cant create file " + lineParts[1] + ", it already exists!";
  1802. }
  1803. else
  1804. {
  1805. File.Create(lineParts[1]);
  1806. }
  1807. goto nextLine;
  1808. }
  1809. if (lineParts[0] == "DeleteDirectory")
  1810. {
  1811. lineParts[1] = ParseString(lineParts[1]);
  1812. if (!Directory.Exists(lineParts[1]))
  1813. {
  1814. lineError = "Cant delete directory " + lineParts[1] + ", it doesn't exists!";
  1815. }
  1816. else
  1817. {
  1818. Directory.Delete(lineParts[1], true);
  1819. }
  1820. goto nextLine;
  1821. }
  1822. if (lineParts[0] == "DeleteFile")
  1823. {
  1824. lineParts[1] = ParseString(lineParts[1]);
  1825. if (!File.Exists(lineParts[1]))
  1826. {
  1827. lineError = "Cant delete file " + lineParts[1] + ", it doesn't exists!";
  1828. }
  1829. else
  1830. {
  1831. File.Delete(lineParts[1]);
  1832. }
  1833. goto nextLine;
  1834. }
  1835. if (lineParts[0] == "TruncateFile")
  1836. {
  1837. lineParts[1] = ParseString(lineParts[1]);
  1838. if (!File.Exists(lineParts[1]))
  1839. {
  1840. lineError = "Cant truncate file " + lineParts[1] + ", it doesn't exists!";
  1841. }
  1842. else
  1843. {
  1844. File.Open(lineParts[1], FileMode.Truncate).Close();
  1845. }
  1846. goto nextLine;
  1847. }
  1848. if (lineParts[0] == "TruncateDirectory")
  1849. {
  1850. lineParts[1] = ParseString(lineParts[1]);
  1851. if (!Directory.Exists(lineParts[1]))
  1852. {
  1853. lineError = "Cant truncate directory " + lineParts[1] + ", it doesn't exists!";
  1854. }
  1855. else
  1856. {
  1857. Directory.Delete(lineParts[1], true);
  1858. Directory.CreateDirectory(lineParts[1]);
  1859. }
  1860. goto nextLine;
  1861. }
  1862. if (lineParts[0] == "FileWriteLine")
  1863. {
  1864. lineParts[1] = ParseString(lineParts[1]);
  1865. lineParts[2] = ParseString(lineParts[2]);
  1866. if (!File.Exists(lineParts[1]))
  1867. {
  1868. lineError = "Cant write to file " + lineParts[1] + ", it doesn't exists!";
  1869. }
  1870. else
  1871. {
  1872. StreamWriter writer = new StreamWriter(File.Open(lineParts[1], FileMode.Append));
  1873. writer.WriteLine(lineParts[2]);
  1874. writer.Flush();
  1875. writer.Close();
  1876. }
  1877. goto nextLine;
  1878. }
  1879. if (lineParts[0] == "FileRemoveLine")
  1880. {
  1881. lineParts[1] = ParseString(lineParts[1]);
  1882. lineParts[2] = ParseString(lineParts[2]);
  1883. if (!File.Exists(lineParts[1]))
  1884. {
  1885. lineError = "Cant change file " + lineParts[1] + ", it doesn't exists!";
  1886. }
  1887. else
  1888. {
  1889. string[] the_lines = File.ReadAllLines(lineParts[1]);
  1890. File.Delete(lineParts[1]);
  1891. File.Create(lineParts[1]);
  1892. StreamWriter writer = new StreamWriter(File.Open(lineParts[1], FileMode.Append));
  1893. foreach (string a_line in the_lines)
  1894. {
  1895. if (a_line != lineParts[2]) writer.WriteLine(lineParts[2]);
  1896. }
  1897. writer.Flush();
  1898. writer.Close();
  1899. }
  1900. goto nextLine;
  1901. }
  1902. #endregion
  1903. #region SetBlock GetBlock SendBlock
  1904. if (lineParts[0] == "SetBlock")
  1905. {
  1906. Map theLevel;
  1907. lineParts[1] = lineParts[1].Remove(0, 1);
  1908. i = 0;
  1909. while (i < 255)
  1910. {
  1911. if (mapVarNames[i] == lineParts[1]) { theLevel = mapVars[i]; goto varFound004; }
  1912. i++;
  1913. }
  1914. lineError = "Failed to set block, could not find map variable $" + lineParts[1] + ".";
  1915. goto nextLine;
  1916. varFound004:
  1917. try
  1918. {
  1919. theLevel.SetBlock(ushort.Parse(lineParts[2]), ushort.Parse(lineParts[3]), ushort.Parse(lineParts[4]), Convert.ToByte(ushort.Parse(lineParts[5])));
  1920. }
  1921. catch
  1922. {
  1923. lineError = "Failed to set block. Probably failed to parse one of the integers.";
  1924. goto nextLine;
  1925. }
  1926. goto nextLine;
  1927. }
  1928. if (lineParts[0] == "GetBlock")
  1929. {
  1930. Map theLevel;
  1931. lineParts[1] = lineParts[1].Remove(0, 1);
  1932. i = 0;
  1933. while (i < 255)
  1934. {
  1935. if (mapVarNames[i] == lineParts[1]) { theLevel = mapVars[i]; goto varFound9; }
  1936. i++;
  1937. }
  1938. lineError = "Failed to get block, could not find map variable $" + lineParts[1] + ".";
  1939. goto nextLine;
  1940. varFound9:
  1941. try
  1942. {
  1943. theLevel.GetBlock(ushort.Parse(lineParts[2]), ushort.Parse(lineParts[3]), ushort.Parse(lineParts[4]));
  1944. }
  1945. catch
  1946. {
  1947. lineError = "Failed to set block. Probably failed to parse one of the integers.";
  1948. goto nextLine;
  1949. }
  1950. }
  1951. if (lineParts[0] == "SendBlock")
  1952. {
  1953. lineParts[1] = lineParts[1].Remove(0, 1);
  1954. i = 0;
  1955. while (i < 255)
  1956. {
  1957. if (playerVarNames[i] == lineParts[1]) goto varFound006;
  1958. i++;
  1959. }
  1960. lineError = "Could not find player variable $" + lineParts[1] + ".";
  1961. goto nextLine;
  1962. varFound006:
  1963. try
  1964. {
  1965. playerVars[i].SendBlockchange(ushort.Parse(lineParts[2]), ushort.Parse(lineParts[3]), ushort.Parse(lineParts[4]), ushort.Parse(lineParts[5]));
  1966. }
  1967. catch
  1968. {
  1969. lineError = "Failed to send block. Probably failed to parse one of the integers.";
  1970. goto nextLine;
  1971. }
  1972. }
  1973. #endregion
  1974. // Foundvar 20 t/m 22:
  1975. #region MakeLowerCase MakeUpperCase Replace
  1976. if (lineParts[0] == "MakeLowerCase")
  1977. {
  1978. lineParts[1] = lineParts[1].Remove(0, 1);
  1979. i = 0;
  1980. while (i < 255)
  1981. {
  1982. if (stringVarNames[i] == lineParts[1]) { goto foundVar20; }
  1983. i++;
  1984. }
  1985. lineError = "Could not find string variable $" + lineParts[1];
  1986. goto nextLine;
  1987. foundVar20:
  1988. stringVars[i] = stringVars[i].ToLower();
  1989. goto nextLine;
  1990. }
  1991. if (lineParts[0] == "MakeUpperCase")
  1992. {
  1993. lineParts[1] = lineParts[1].Remove(0, 1);
  1994. i = 0;
  1995. while (i < 255)
  1996. {
  1997. if (stringVarNames[i] == lineParts[1]) { goto foundVar21; }
  1998. i++;
  1999. }
  2000. lineError = "Could not find string variable $" + lineParts[1];
  2001. goto nextLine;
  2002. foundVar21:
  2003. stringVars[i] = stringVars[i].ToUpper();
  2004. goto nextLine;
  2005. }
  2006. if (lineParts[0] == "Replace")
  2007. {
  2008. lineParts[1] = lineParts[1].Remove(0, 1);
  2009. i = 0;
  2010. while (i < 255)
  2011. {
  2012. if (stringVarNames[i] == lineParts[1]) { goto foundVar22; }
  2013. i++;
  2014. }
  2015. lineError = "Could not find string variable $" + lineParts[1];
  2016. goto nextLine;
  2017. foundVar22:
  2018. stringVars[i] = stringVars[i].Replace(ParseString(lineParts[2]),ParseString(lineParts[3]));
  2019. goto nextLine;
  2020. }
  2021. #endregion
  2022. #region Sleep Wait Exit
  2023. if (lineParts[0].ToLower() == "sleep" || lineParts[0].ToLower() == "wait")
  2024. {
  2025. try
  2026. {
  2027. Thread.Sleep(System.Int16.Parse(lineParts[1]));
  2028. }
  2029. catch
  2030. {
  2031. lineError = "Sleeping failed: Could not parse integer " + lineParts[1];
  2032. }
  2033. goto nextLine;
  2034. }
  2035. if (lineParts[0].ToLower() == "exit")
  2036. {
  2037. goto stopScript;
  2038. }
  2039. #endregion
  2040. }
  2041. lineError = "Unknown command " + lineParts[0];
  2042. nextLine:
  2043. lineNum++;
  2044. if (lineError != "")
  2045. {
  2046. if (inTryBlock)
  2047. {
  2048. skipToCatch = true;
  2049. curBlock = tryCurBlock;
  2050. }
  2051. else if (!fatalError) Events.Log("Non-fatal error in " + file + " on line " + lineNum + ": " + lineError);
  2052. else { Events.Log("Fatal error in " + file + " on line " + lineNum + ": " + lineError); goto stopScript; }
  2053. }
  2054. //Events.Log("Line " + (lineNum-1) + " ends with curBlock " + curBlock + ".");
  2055. }
  2056. catch (Exception e)
  2057. {
  2058. lineNum++;
  2059. if (inTryBlock)
  2060. {
  2061. skipToCatch = true;
  2062. curBlock = tryCurBlock;
  2063. }
  2064. else
  2065. {
  2066. System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(e, true);
  2067. Events.Log("Non-fatal but unknown error in " + file + " on line " + lineNum + ": " + e.Message);
  2068. Events.Log("Line: " + trace.GetFrame(0).GetFileLineNumber() + " Column: " + trace.GetFrame(0).GetFileColumnNumber());
  2069. }
  2070. }
  2071. }
  2072. stopScript:
  2073. i = 0;
  2074. if (deleteWhenFinished) File.Delete(file);
  2075. ScriptThreads.Remove(Thread.CurrentThread);
  2076. }));
  2077. ScriptThreads.Add(scriptThread);
  2078. scriptThread.Start();
  2079. return;
  2080. }
  2081. public static string ParseString(string str)
  2082. {
  2083. return str.Remove(str.Length - 1, 1).Remove(0, 1);
  2084. }
  2085. public static void WriteExampleScripts()
  2086. {
  2087. if (!File.Exists("mjs/commands/aboutMJS.mjs"))
  2088. {
  2089. StreamWriter sw = new StreamWriter(File.Create("mjs/commands/aboutMJS.mjs"));
  2090. sw.WriteLine("PlayerMessage $player \"Dear $player.$name\", you just used a command written in MJS.");
  2091. sw.WriteLine("PlayerMessage $player \"MJS is an easy-to-learn minecraft classic command and event scripting language.\"");
  2092. sw.WriteLine("PlayerMessage $player \"A guide on MJS is under construction on mcrevive.tk\"");
  2093. sw.Flush();
  2094. sw.Close();
  2095. }
  2096. }
  2097. public static void ReloadMJS()
  2098. {
  2099. Events.Log("Force-killing all " + ScriptThreads.Count + " running MJS scripts...");
  2100. ScriptThreads.ForEach((t) => { t.Abort(); });
  2101. ScriptThreads.Clear();
  2102. Events.Log("Re-triggering ServerStart event...");
  2103. Event.Trigger("ServerStart","");
  2104. Events.Log("MJS reloaded.");
  2105. }
  2106. }
  2107. }