PageRenderTime 63ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/MCQuai/MJS/Interpreter.cs

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