public override bool Run() { if (!IsArg("--decompile-clipboard")) { return(false); } NextArg(); string file = GetCurrentArg(); try { // Parse the workshop code. var workshop = new ConvertTextToElement(Clipboard.GetText()).Get(); // Decompile the parsed workshop code. var workshopToCode = new WorkshopDecompiler(workshop, new FileLobbySettingsResolver(file, workshop.LobbySettings), new CodeFormattingOptions()); string result = workshopToCode.Decompile(); // Create the file. using (var writer = File.CreateText(file)) // Write the code to the file. writer.Write(result); Console.Write("Success"); } catch (Exception ex) { Console.Write(ex.ToString()); } // Done. return(true); }
async Task Listen() { var token = _cancellationTokenSource.Token; string last = null; while (!token.IsCancellationRequested && !token.WaitHandle.WaitOne(500)) { string clipboard = Clipboard.GetText(); // Get clipboard. if (clipboard == last || clipboard == null) { continue; // Clipboard did not change. } last = clipboard; // Clipboard changed. try { // As workshop actions ConvertTextToElement tte = new ConvertTextToElement(clipboard); Workshop workshop = tte.GetActionList(); if (workshop != null) // Determines if the clipboard is an action list. { DebuggerActionSetResult actionStream = new DebuggerActionSetResult(workshop); // Action list successfully parsed. // Get the DeltinScript. VariableCollection = (await _languageServer.DocumentHandler.OnScriptAvailability())?.DebugVariables; // Error obtaining debug variables. if (VariableCollection == null) { return; } // Apply debugger variables. VariableCollection.Apply(actionStream); // Notify the adapter of the new state. _languageServer.Server.SendNotification("debugger.activated"); } } catch (Exception ex) { _languageServer.DebuggerException(ex); } } }
private void RunFile(string script) { string ext = Path.GetExtension(script).ToLower(); // Run .csv file if (ext == ".csv") { Pathmap map = Pathmap.ImportFromCSVFile(script, new ConsolePathmapErrorHandler(new Log("Pathmap"))); if (map != null) { string result = map.ExportAsJSON(); string output = Path.ChangeExtension(script, "pathmap"); using (FileStream fs = File.Create(output)) { Byte[] info = Encoding.Unicode.GetBytes(result); fs.Write(info, 0, info.Length); } Program.Log.Write(LogLevel.Normal, "Created pathmap file at '" + output + "'."); } } // Run .pathmap file else if (ext == ".pathmap") { Editor.FromPathmapFile(script); } // Decompile .ow file else if (ext == ".ow") { string text = File.ReadAllText(script); // Parse the workshop code. var walker = new ConvertTextToElement(text); var workshop = walker.Get(); // Decompile to OSTW. var decompiler = new WorkshopDecompiler(workshop, new OmitLobbySettingsResolver(), new CodeFormattingOptions()); // Result Program.WorkshopCodeResult(decompiler.Decompile()); } // Default: Run OSTW script else { Program.Script(script); } }
public override bool Match(ConvertTextToElement parser, out object value) { // Match the enumerator. foreach (string enumerator in Values) { if (parser.Match(parser.Kw(enumerator), false)) { // Return true if it is found. value = enumerator; return(true); } } // The value was not found. value = null; return(false); }
public override bool Match(ConvertTextToElement parser, out object value) { // Match enabled if (parser.Match(parser.Kw(EnabledKey()), false)) { value = true; return(true); } // Match disabled else if (parser.Match(parser.Kw(DisabledKey()), false)) { value = false; return(true); } // Unknown value = null; return(false); }
public override bool Run() { if (!IsArg("--decompile-clipboard")) { return(false); } NextArg(); string file = GetCurrentArg(); try { // Parse the workshop code. var tte = new ConvertTextToElement(Clipboard.GetText()); var workshop = tte.Get(); // Decompile the parsed workshop code. var workshopToCode = new WorkshopDecompiler(workshop, new FileLobbySettingsResolver(file, workshop.LobbySettings), new CodeFormattingOptions()); string result = workshopToCode.Decompile(); // Create the file. using (var writer = File.CreateText(file)) // Write the code to the file. writer.Write(result); Console.Write("Success"); // Warning if the end of the file was not reached. if (!tte.ReachedEnd) { Console.Write("End of file not reached, stuck at: '" + tte.LocalStream.Substring(0, Math.Min(tte.LocalStream.Length, 50)) + "'"); } } catch (Exception ex) { Console.Write(ex.ToString()); } // Done. return(true); }
public override bool Match(ConvertTextToElement parser, out object value) { // If the setting is an integer, match an integer. if (Integer) { if (parser.Integer(out int i)) { // Integer matched. value = i; return(true); } else { // Integer not matched. value = null; return(false); } } else { // Match a double. if (parser.Double(out double d)) { parser.Match("%"); // Double matched. value = d; return(true); } else { // Double not matched. value = null; return(false); } } }
private LanguageServerOptions AddRequests(LanguageServerOptions options) { // Pathmap creation is seperated into 2 requests, 'pathmapFromClipboard' and 'pathmapApply'. // Pathmap generation request. options.OnRequest <object, string>("pathmapFromClipboard", _ => Task <string> .Run(() => { // Create the error handler for pathmap parser. ServerPathmapHandler error = new ServerPathmapHandler(); // Get the pathmap. 'map' will be null if there is an error. try { Pathmap map = Pathmap.ImportFromCSV(Clipboard.GetText(), error); if (map == null) { return(error.Message); } else { lastMap = map; return("success"); } } catch (Exception ex) { return(ex.Message); } })); // Pathmap save request. options.OnRequest <Newtonsoft.Json.Linq.JToken>("pathmapApply", uriToken => Task.Run(() => { // Save 'lastMap' to a file. string result = lastMap.ExportAsJSON(); string output = uriToken["path"].ToObject <string>().Trim('/'); using (var stream = new StreamWriter(output)) stream.Write(result); })); // Pathmap editor request. options.OnRequest <PathmapDocument, bool>("pathmapEditor", (editFileToken) => Task <bool> .Run(() => { DeltinScript compile; if (editFileToken.Text == null) { string editor = Extras.CombinePathWithDotNotation(null, "!PathfindEditor.del"); compile = new DeltinScript(new TranslateSettings(editor) { OutputLanguage = ConfigurationHandler.OutputLanguage }); } else { compile = Editor.Generate(editFileToken.File, Pathmap.ImportFromText(editFileToken.Text), ConfigurationHandler.OutputLanguage); } Clipboard.SetText(compile.WorkshopCode); return(true); })); // semantic tokens options.OnRequest <Newtonsoft.Json.Linq.JToken, SemanticToken[]>("semanticTokens", (uriToken) => Task <SemanticToken[]> .Run(async() => { await DocumentHandler.WaitForParse(); SemanticToken[] tokens = LastParse?.ScriptFromUri(new Uri(uriToken["fsPath"].ToObject <string>()))?.GetSemanticTokens(); return(tokens ?? new SemanticToken[0]); })); // debugger start options.OnRequest <object>("debugger.start", args => Task.Run(() => { _debugger.Start(); return(new object()); })); // debugger stop options.OnRequest <object>("debugger.stop", args => Task.Run(() => { _debugger.Stop(); return(new object()); })); // debugger scopes options.OnRequest <ScopesArgs, DBPScope[]>("debugger.scopes", args => Task <DBPScope[]> .Run(() => { try { if (_debugger.VariableCollection != null) { return(_debugger.VariableCollection.GetScopes(args)); } } catch (Exception ex) { DebuggerException(ex); } return(new DBPScope[0]); })); // debugger variables options.OnRequest <VariablesArgs, DBPVariable[]>("debugger.variables", args => Task <DBPVariable[]> .Run(() => { try { if (_debugger.VariableCollection != null) { return(_debugger.VariableCollection.GetVariables(args)); } } catch (Exception ex) { DebuggerException(ex); } return(new DBPVariable[0]); })); // debugger evaluate options.OnRequest <EvaluateArgs, EvaluateResponse>("debugger.evaluate", args => Task <EvaluateResponse> .Run(() => { try { return(_debugger.VariableCollection?.Evaluate(args)); } catch (Exception ex) { DebuggerException(ex); return(EvaluateResponse.Empty); } })); // Decompile insert options.OnRequest <DecompileResult>("decompile.insert", () => Task <DecompileResult> .Run(() => { try { var tte = new ConvertTextToElement(Clipboard.GetText()); var workshop = tte.Get(); var code = new WorkshopDecompiler(workshop, new OmitLobbySettingsResolver(), new CodeFormattingOptions()).Decompile(); return(new DecompileResult(tte, code)); } catch (Exception ex) { return(new DecompileResult(ex)); } })); // Decompile file options.OnRequest <DecompileFileArgs, DecompileResult>("decompile.file", args => Task.Run <DecompileResult>(() => { try { // Parse the workshop code. var tte = new ConvertTextToElement(Clipboard.GetText()); var workshop = tte.Get(); // Decompile the parsed workshop code. var workshopToCode = new WorkshopDecompiler(workshop, new FileLobbySettingsResolver(args.File, workshop.LobbySettings), new CodeFormattingOptions()); var code = workshopToCode.Decompile(); var result = new DecompileResult(tte, code); // Only create the decompile was successful. if (result.Success) { // Create the file. using (var writer = File.CreateText(args.File)) // Write the code to the file. writer.Write(code); } return(result); } catch (Exception ex) { return(new DecompileResult(ex)); } })); return(options); }
/// <summary>Decompiles the workshop value.</summary> public abstract bool Match(ConvertTextToElement parser, out object value);
public static Pathmap ImportFromActionSet(string text, IPathmapErrorHandler errorHandler) { ConvertTextToElement tte = new ConvertTextToElement(text); Workshop workshop = tte.GetActionList(); Vertex[] nodeArray = null; Vertex[] segmentArray = null; Vertex[] attributeArray = null; const string nodesOut = "nodesOut", segmentsOut = "segmentsOut", attributesOut = "attributesOut"; // Get the variable values. foreach (var action in workshop.Actions) { if (action is SetVariableAction setVariable) { // Get the variable name. switch (setVariable.Variable.Name) { // Nodes case nodesOut: nodeArray = ExtractVertexArray(nodesOut, errorHandler, setVariable.Value); break; // Segments case segmentsOut: segmentArray = ExtractVertexArray(segmentsOut, errorHandler, setVariable.Value); break; // Attributes case attributesOut: attributeArray = ExtractVertexArray(attributesOut, errorHandler, setVariable.Value); break; } } } if (nodeArray == null) { errorHandler.Error($"Incorrect format, '{nodesOut}' does not exist. Did you compile your pathmap?"); return(null); } if (segmentArray == null) { errorHandler.Error($"Incorrect format, '{segmentsOut}' does not exist. Did you compile your pathmap?"); return(null); } if (attributeArray == null) { errorHandler.Error($"Incorrect format, '{attributesOut}' does not exist. Did you compile your pathmap?"); return(null); } Segment[] segments = new Segment[segmentArray.Length]; for (int i = 0; i < segments.Length; i++) { segments[i] = new Segment((int)segmentArray[i].X, (int)segmentArray[i].Y); } MapAttribute[] attributes = new MapAttribute[attributeArray.Length]; for (int i = 0; i < attributes.Length; i++) { attributes[i] = new MapAttribute( (int)attributeArray[i].X, (int)attributeArray[i].Y, (int)attributeArray[i].Z ); } return(new Pathmap(nodeArray, segments, attributes)); }