public void ScriptRun(object obj) { engine = Python.CreateEngine(); CodeWraper wraper = (CodeWraper)obj; string code = wraper.GetCode(); scope = engine.CreateScope(); var streamOut = new MemoryStream(); scope.SetVariable("WIDTH", DisplaySystem.WIDTH); scope.SetVariable("HEIGHT", DisplaySystem.HEIGHT); scope.SetVariable("__tick__", new Action(Tick)); scope.SetVariable("set_color", new Action <int, int, int>(SetColor)); scope.SetVariable("set_back", new Action <int, int, int>(SetBackColor)); scope.SetVariable("set_bord", new Action <int, int, int>(SetBorderColor)); scope.SetVariable("set_error", new Action <int, int, int>(SetErrorColor)); scope.SetVariable("randint", new Func <int, int, int>(RandomInteger)); scope.SetVariable("clear_screen", new Action(ClearScreen)); scope.SetVariable("draw_circle", new Action <int, int, int>(DrawCircle)); scope.SetVariable("draw_line", new Action <int, int, int, int>(DrawLine)); scope.SetVariable("draw_rect", new Action <int, int, int, int>(DrawRect)); scope.SetVariable("draw_pixel", new Action <int, int>(DrawPixel)); scope.SetVariable("draw_pixels", new Action <int, int, int, int>(DrawPixels)); scope.SetVariable("auto_apply", new Action <bool>(SetAutoapply)); scope.SetVariable("apply", new Action(Apply)); scope.SetVariable("delta_time", new Func <float>(GetDeltaTime)); scope.SetVariable("get_keys", new Func <string[]>(GetDownedKeys)); scope.SetVariable("char", new Func <string, int>(GetCharByNum)); scope.SetVariable("type", new Action <int, int, int>(TyperChar)); scope.SetVariable("input", new Func <string>(GetInput)); scope.SetVariable("beep", new Action <float, float>(Beep)); aborted = false; EventStreamWriter outputWr = new EventStreamWriter(streamOut); outputWr.StringWritten += new EventHandler <EventWithArgs <string> >(StringWritten); engine.Runtime.IO.SetOutput(streamOut, outputWr); try { engine.Execute(code, scope); } catch (Exception e) { _console.AddToLog(e.Message); } if (aborted) { _console.Writter.Reset(); _console.AddToActions(_console.ResetOutput); } else { _console.AddToActions(_console.ResetOutputStopped); } stream.Abort(); }
public void CreateThread(string code) { CodeWraper wraper = new CodeWraper(code, _console); stream = new Thread(ScriptRun); stream.IsBackground = true; stream.Start(wraper); }