From 95b77f7a2654a4edc247f2516dbaa6c237869fbf Mon Sep 17 00:00:00 2001 From: Michael Oborne Date: Fri, 21 Jul 2023 16:04:42 +1000 Subject: [PATCH] Script: Ironpython update. from 2.7 to 3.4 --- ExtLibs/Utilities/StringRedirectWriter.cs | 16 ++++++++++++++++ MissionPlanner.csproj | 4 ++-- Script.cs | 8 ++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/ExtLibs/Utilities/StringRedirectWriter.cs b/ExtLibs/Utilities/StringRedirectWriter.cs index 134f332507..886008dadc 100644 --- a/ExtLibs/Utilities/StringRedirectWriter.cs +++ b/ExtLibs/Utilities/StringRedirectWriter.cs @@ -1,11 +1,27 @@ using System; using System.Text; using System.IO; +using System.Linq; namespace MissionPlanner.Utilities { public delegate void StringWrittenEvent(object sender, string writtenString); + public class StringRedirectStream : MemoryStream + { + public StringRedirectStream(StringRedirectWriter outputWriter) + { + OutputWriter = outputWriter; + } + + public StringRedirectWriter OutputWriter { get; set; } + + public override void Write(byte[] buffer, int offset, int count) + { + OutputWriter.Write(buffer.Select(a=>(char)a).ToArray(), offset, count); + } + } + /// /// This class only pretends to be a string writer /// when in fact it raises an event whenever a string is written diff --git a/MissionPlanner.csproj b/MissionPlanner.csproj index 8745146dac..bc919abcaf 100644 --- a/MissionPlanner.csproj +++ b/MissionPlanner.csproj @@ -605,8 +605,8 @@ 1.0.1 - - + + 2.0.2 diff --git a/Script.cs b/Script.cs index aa6cef3cf3..7d128e218f 100644 --- a/Script.cs +++ b/Script.cs @@ -53,16 +53,16 @@ public Script(bool redirectOutput = false) scope.SetVariable("mavutil", this); scope.SetVariable("Joystick", MainV2.joystick); - engine.CreateScriptSourceFromString("print 'hello world from python'").Execute(scope); - engine.CreateScriptSourceFromString("print cs.roll").Execute(scope); + engine.CreateScriptSourceFromString("print('hello world from python')").Execute(scope); + engine.CreateScriptSourceFromString("print(cs.roll)").Execute(scope); if (redirectOutput) { //Redirect output through this writer //this writer will not actually write to the memorystreams OutputWriter = new Utilities.StringRedirectWriter(); - engine.Runtime.IO.SetErrorOutput(new MemoryStream(), OutputWriter); - engine.Runtime.IO.SetOutput(new MemoryStream(), OutputWriter); + engine.Runtime.IO.SetErrorOutput(new StringRedirectStream(OutputWriter), OutputWriter); + engine.Runtime.IO.SetOutput(new StringRedirectStream(OutputWriter), OutputWriter); } else OutputWriter = null;