private void AddWatch() { var code = _addWatchEdit.Text; if (string.IsNullOrWhiteSpace(code)) { return; } var options = ScriptInstanceShared.GetScriptOptions(_reflectionManager); var script = CSharpScript.Create(code, options, typeof(ScriptGlobalsShared)); ScriptRunner <object> @delegate; try { @delegate = script.CreateDelegate(); } catch (CompilationErrorException compilationError) { _watchesVBox.AddChild(new CompilationErrorControl(string.Join('\n', compilationError.Diagnostics))); return; } var control = new WatchControl(@delegate); _watchesVBox.AddChild(control); _addWatchEdit.Clear(); }
public ScriptConsoleClient() { Title = Loc.GetString("Robust C# Interactive (CLIENT)"); ScriptInstanceShared.InitDummy(); _globals = new ScriptGlobalsImpl(this); IoCManager.InjectDependencies(this); OutputPanel.AddText(Loc.GetString(@"Robust C# interactive console (CLIENT).")); OutputPanel.AddText(">"); }
public WatchWindow() { _reflectionManager = IoCManager.Resolve <IReflectionManager>(); ScriptInstanceShared.InitDummy(); Title = "Watch Window"; var mainVBox = new BoxContainer { Orientation = LayoutOrientation.Vertical, MinSize = (500, 300), Children = { (_watchesVBox = new BoxContainer { Orientation = LayoutOrientation.Vertical, VerticalExpand = true }), new BoxContainer { Orientation = LayoutOrientation.Horizontal, Children = { (_addWatchEdit = new HistoryLineEdit { HorizontalExpand = true, PlaceHolder = "Add watch (C# interactive)" }), (_addWatchButton = new Button { Text = "Add" }) } } }, }; _addWatchButton.OnPressed += _ => AddWatch(); _addWatchEdit.OnTextEntered += _ => AddWatch(); Contents.AddChild(mainVBox); SetSize = (300, 300); }
protected override void WriteSyntax(object toString) { var code = toString.ToString(); if (code == null) { return; } var options = ScriptInstanceShared.GetScriptOptions(_owner._reflectionManager).AddReferences(typeof(Image).Assembly); var script = CSharpScript.Create(code, options, typeof(ScriptGlobals)); script.Compile(); var syntax = new FormattedMessage(); ScriptInstanceShared.AddWithSyntaxHighlighting(script, syntax, code, _owner._highlightWorkspace); _owner.OutputPanel.AddMessage(syntax); }
public WatchWindow() { _reflectionManager = IoCManager.Resolve <IReflectionManager>(); ScriptInstanceShared.InitDummy(); Title = Loc.GetString("Watch Window"); var mainVBox = new VBoxContainer { CustomMinimumSize = (500, 300), Children = { (_watchesVBox = new VBoxContainer { SizeFlagsVertical = SizeFlags.FillExpand }), new HBoxContainer { Children = { (_addWatchEdit = new HistoryLineEdit { SizeFlagsHorizontal = SizeFlags.FillExpand, PlaceHolder = Loc.GetString("Add watch (C# interactive)") }), (_addWatchButton = new Button { Text = Loc.GetString("Add") }) } } }, }; _addWatchButton.OnPressed += _ => AddWatch(); _addWatchEdit.OnTextEntered += _ => AddWatch(); Contents.AddChild(mainVBox); }
private void ReceiveScriptStart(MsgScriptStart message) { var reply = _netManager.CreateNetMessage <MsgScriptStartAck>(); reply.ScriptSession = message.ScriptSession; reply.WasAccepted = false; if (!_playerManager.TryGetSessionByChannel(message.MsgChannel, out var session)) { _netManager.ServerSendMessage(reply, message.MsgChannel); return; } if (!_conGroupController.CanViewVar(session)) { Logger.WarningS("script", "Client {0} tried to access Scripting without permissions.", session); _netManager.ServerSendMessage(reply, message.MsgChannel); return; } var instances = _instances.GetOrNew(session); if (instances.ContainsKey(message.ScriptSession)) { // Already got one with this ID, client's problem. _netManager.ServerSendMessage(reply, message.MsgChannel); return; } ScriptInstanceShared.InitDummy(); var instance = new ScriptInstance(); instances.Add(message.ScriptSession, instance); reply.WasAccepted = true; _netManager.ServerSendMessage(reply, message.MsgChannel); }
public override void show(object obj) { write(ScriptInstanceShared.SafeFormat(obj)); }
private async void ReceiveScriptEval(MsgScriptEval message) { if (!_playerManager.TryGetSessionByChannel(message.MsgChannel, out var session)) { return; } if (!_conGroupController.CanViewVar(session)) { Logger.WarningS("script", "Client {0} tried to access Scripting without permissions.", session); return; } if (!_instances.TryGetValue(session, out var instances) || !instances.TryGetValue(message.ScriptSession, out var instance)) { return; } var replyMessage = _netManager.CreateNetMessage <MsgScriptResponse>(); replyMessage.ScriptSession = message.ScriptSession; var code = message.Code; instance.InputBuffer.AppendLine(code); var tree = SyntaxFactory.ParseSyntaxTree(SourceText.From(instance.InputBuffer.ToString()), ScriptInstanceShared.ParseOptions); if (!SyntaxFactory.IsCompleteSubmission(tree)) { replyMessage.WasComplete = false; _netManager.ServerSendMessage(replyMessage, message.MsgChannel); return; } replyMessage.WasComplete = true; code = instance.InputBuffer.ToString().Trim(); instance.InputBuffer.Clear(); Script newScript; if (instance.State != null) { newScript = instance.State.Script.ContinueWith(code); } else { var options = ScriptInstanceShared.GetScriptOptions(_reflectionManager); newScript = CSharpScript.Create(code, options, typeof(ScriptGlobals)); } // Compile ahead of time so that we can do syntax highlighting correctly for the echo. newScript.Compile(); // Echo entered script. var echoMessage = new FormattedMessage(); ScriptInstanceShared.AddWithSyntaxHighlighting(newScript, echoMessage, code, instance.HighlightWorkspace); replyMessage.Echo = echoMessage; var msg = new FormattedMessage(); try { instance.RunningScript = true; if (instance.State != null) { instance.State = await newScript.RunFromAsync(instance.State, _ => true); } else { instance.State = await newScript.RunAsync(instance.Globals, _ => true); } } catch (CompilationErrorException e) { msg.PushColor(Color.Crimson); foreach (var diagnostic in e.Diagnostics) { msg.AddText(diagnostic.ToString()); msg.AddText("\n"); } replyMessage.Response = msg; _netManager.ServerSendMessage(replyMessage, message.MsgChannel); return; } finally { instance.RunningScript = false; } if (instance.OutputBuffer.Length != 0) { msg.AddText(instance.OutputBuffer.ToString()); instance.OutputBuffer.Clear(); } if (instance.State.Exception != null) { msg.PushColor(Color.Crimson); msg.AddText(CSharpObjectFormatter.Instance.FormatException(instance.State.Exception)); } else if (ScriptInstanceShared.HasReturnValue(newScript)) { msg.AddText(CSharpObjectFormatter.Instance.FormatObject(instance.State.ReturnValue)); } replyMessage.Response = msg; _netManager.ServerSendMessage(replyMessage, message.MsgChannel); }
protected override async void Run() { var code = InputBar.Text; InputBar.Clear(); // Remove > or . at the end of the output panel. OutputPanel.RemoveEntry(^ 1); _inputBuffer.AppendLine(code); _linesEntered += 1; var tree = SyntaxFactory.ParseSyntaxTree(SourceText.From(_inputBuffer.ToString()), ScriptInstanceShared.ParseOptions); if (!SyntaxFactory.IsCompleteSubmission(tree)) { if (_linesEntered == 1) { OutputPanel.AddText($"> {code}"); } else { OutputPanel.AddText($". {code}"); } OutputPanel.AddText("."); return; } code = _inputBuffer.ToString().Trim(); // Remove echo of partial submission from the output panel. for (var i = 1; i < _linesEntered; i++) { OutputPanel.RemoveEntry(^ 1); } _inputBuffer.Clear(); _linesEntered = 0; Script newScript; if (_state != null) { newScript = _state.Script.ContinueWith(code); } else { var options = ScriptInstanceShared.GetScriptOptions(_reflectionManager); newScript = CSharpScript.Create(code, options, typeof(ScriptGlobals)); } // Compile ahead of time so that we can do syntax highlighting correctly for the echo. newScript.Compile(); // Echo entered script. var echoMessage = new FormattedMessage(); echoMessage.PushColor(Color.FromHex("#D4D4D4")); echoMessage.AddText("> "); ScriptInstanceShared.AddWithSyntaxHighlighting(newScript, echoMessage, code, _highlightWorkspace); OutputPanel.AddMessage(echoMessage); try { if (_state != null) { _state = await newScript.RunFromAsync(_state, _ => true); } else { _state = await newScript.RunAsync(_globals, _ => true); } } catch (CompilationErrorException e) { var msg = new FormattedMessage(); msg.PushColor(Color.Crimson); foreach (var diagnostic in e.Diagnostics) { msg.AddText(diagnostic.ToString()); msg.AddText("\n"); } OutputPanel.AddMessage(msg); OutputPanel.AddText(">"); return; } if (_state.Exception != null) { var msg = new FormattedMessage(); msg.PushColor(Color.Crimson); msg.AddText(CSharpObjectFormatter.Instance.FormatException(_state.Exception)); OutputPanel.AddMessage(msg); } else if (ScriptInstanceShared.HasReturnValue(newScript)) { var msg = new FormattedMessage(); msg.AddText(CSharpObjectFormatter.Instance.FormatObject(_state.ReturnValue)); OutputPanel.AddMessage(msg); } OutputPanel.AddText(">"); }