/// <summary> /// Execute a string of code. /// </summary> /// <param name="code">String to execute.</param> /// <param name="silent">Defines whether console should hide code that's being executed.</param> public static void Execute(string code, bool silent = true) { Core.ConsoleInputQueue.Clear(); if (!silent) { Core.WriteLine(new ColorContainer(0, 131, 63), $"> {code}"); } if (LuaThread != null && LuaThread.IsAlive) { LuaThread.Abort(); } LuaThread = new Thread(() => { try { var stopWatch = Stopwatch.StartNew(); Environment.DoString($"{code}"); stopWatch.Stop(); Core.WriteLine($"Script executed in: {stopWatch.ElapsedMilliseconds}ms"); } catch (SyntaxErrorException exception) { Core.WriteLine(new ColorContainer(177, 31, 41), $"Syntax Error: {exception}"); } catch (IndexOutOfRangeException exception) { Core.WriteLine(new ColorContainer(177, 31, 41), $"Engine Error: {exception}"); } catch (ScriptRuntimeException exception) { Core.WriteLine(new ColorContainer(177, 31, 41), $"Runtime Error: {exception}"); } catch (Exception exception) { Core.WriteLine(new ColorContainer(177, 31, 41), $"Exception: {exception}"); } }) { Name = "Lua Thread", IsBackground = true }; LuaThread.Start(); }
/// <summary> /// Download a script from workshop. /// </summary> public static void DownloadPackage(string url, string name = "package") { Core.WriteLine(new ColorContainer(89, 73, 163), $@"Downloading workshop package: {name}"); if (File.Exists($@"Data\Packages\{name}.zip")) { Core.WriteLine(new ColorContainer(177, 31, 41), "A package of this name already exists!"); return; } NetClient.Headers["User-Agent"] = "ScribeBot - Workshop - Download Package"; NetClient.DownloadFile(url, $@"Data\Packages\{name}.zip"); Core.WriteLine(new ColorContainer(89, 73, 163), $@"Downloaded workshop package: {name}"); }
/// <summary> /// Fetch workshop scripts. /// </summary> /// <returns>List of downloadable packages.</returns> public static Dictionary <string, string> GetPackageList() { Core.WriteLine(new ColorContainer(89, 73, 163), "Fetching workshop list.", new ColorContainer(177, 31, 41), "\nWARNING: Using this function too often might get you temporarily IP banned from Github API!"); var list = new Dictionary <string, string>(); NetClient.Headers["User-Agent"] = "ScribeBot - Workshop Content Fetching"; IEnumerable <JToken> tokens = JArray.Parse(NetClient.DownloadString(WorkshopAddress)).Children(); tokens = tokens.Where(x => !x["name"].ToString().Equals("README.md")).ToList(); tokens.ToList().ForEach(x => list[Path.GetFileNameWithoutExtension((string)x["name"])] = (string)x["download_url"]); Core.WriteLine(new ColorContainer(0, 131, 63), "Workshop list fetched. Happy downloading!"); return(list); }
/// <summary> /// Creates a package (zip archive) using supplied files. /// </summary> /// <param name="packageName">Name of file to create.</param> /// <param name="filePaths">Array of paths to files to insert into archive.</param> /// <returns>A generated package.</returns> public static Package Create(string packageName, string[] filePaths) { packageName = packageName.ToLower(); try { var archive = ZipFile.Open($@"Data\Packages\{packageName}.zip", ZipArchiveMode.Create); filePaths.ToList().ForEach(x => archive.CreateEntryFromFile(x, Path.GetFileName(x))); archive.Dispose(); Core.WriteLine(new ColorContainer(0, 131, 63), $@"Package {packageName}.zip created!"); } catch (Exception e) { Core.WriteLine(new ColorContainer(177, 31, 41), e.Message); } return(new Package($@"Data\Packages\{packageName}.zip"));; }
/// <summary> /// Manual initializer. /// </summary> public static void Initialize() => Core.WriteLine(new ColorContainer(205, 205, 205), "-- SCRIPTER INITIALIZED");