private void OnProgramTermination(ScriptTerminationReason reason) { m_terminationReason = reason; m_instance = null; m_assembly = null; m_echoOutput.Clear(); m_previousRunTimestamp = 0; }
private void OnProgramTermination() { m_wasTerminated = true; m_instance = null; m_assembly = null; m_echoOutput.Clear(); m_previousRunTimestamp = 0; }
private void OnProgramTermination(ScriptTerminationReason reason) { m_terminationReason = reason; m_instance = null; m_assembly = null; m_echoOutput.Clear(); m_runtime.Reset(); }
private void CompileAndCreateInstance(string program, string storage) { if (MySession.Static.EnableIngameScripts == false) { return; } m_wasTerminated = false; Assembly temp = null; MyGuiScreenEditor.CompileProgram(program, m_compilerErrors, ref temp); if (temp != null) { try { m_assembly = IlInjector.InjectCodeToAssembly("IngameScript_safe", temp, typeof(IlInjector).GetMethod("CountInstructions", BindingFlags.Public | BindingFlags.Static), typeof(IlInjector).GetMethod("CountMethodCalls", BindingFlags.Public | BindingFlags.Static)); var type = m_assembly.GetType("Program"); if (type != null) { IlInjector.RestartCountingInstructions(MAX_NUM_EXECUTED_INSTRUCTIONS); IlInjector.RestartCountingMethods(MAX_NUM_METHOD_CALLS); try { m_instance = Activator.CreateInstance(type) as IMyGridProgram; if (m_instance != null) { m_previousRunTimestamp = 0; m_instance.Storage = storage; m_instance.Me = this; m_instance.Echo = EchoTextToDetailInfo; } } catch (TargetInvocationException ex) { if (ex.InnerException != null) { string response = MyTexts.GetString(MySpaceTexts.ProgrammableBlock_Exception_ExceptionCaught) + ex.InnerException.Message; if (DetailedInfo.ToString() != response) { SyncObject.SendProgramResponseMessage(response); WriteProgramResponse(response); } } } } } catch (Exception ex) { string response = MyTexts.GetString(MySpaceTexts.ProgrammableBlock_Exception_ExceptionCaught) + ex.Message; if (DetailedInfo.ToString() != response) { SyncObject.SendProgramResponseMessage(response); WriteProgramResponse(response); } } } }
private void CompileAndCreateInstance(string program, string storage) { if (MySession.Static.EnableIngameScripts == false) { return; } m_terminationReason = ScriptTerminationReason.None; try { Assembly temp = null; MyGuiScreenEditor.CompileProgram(program, m_compilerErrors, ref temp); if (temp != null) { m_assembly = IlInjector.InjectCodeToAssembly("IngameScript_safe", temp, typeof(IlInjector).GetMethod("CountInstructions", BindingFlags.Public | BindingFlags.Static), typeof(IlInjector).GetMethod("CountMethodCalls", BindingFlags.Public | BindingFlags.Static)); var type = m_assembly.GetType("Program"); if (type != null) { m_instance = FormatterServices.GetUninitializedObject(type) as IMyGridProgram; var constructor = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); string response; if (m_instance == null || constructor == null) { response = MyTexts.GetString(MySpaceTexts.ProgrammableBlock_Exception_NoValidConstructor); SetDetailedInfo(response); return; } m_runtime.Reset(); m_instance.Runtime = m_runtime; m_instance.Storage = storage; m_instance.Me = this; m_instance.Echo = EchoTextToDetailInfo; RunSandboxedProgramAction(p => { constructor.Invoke(p, null); if (!m_instance.HasMainMethod) { if (m_echoOutput.Length > 0) { response = m_echoOutput.ToString(); } response = MyTexts.GetString(MySpaceTexts.ProgrammableBlock_Exception_NoMain); OnProgramTermination(ScriptTerminationReason.NoEntryPoint); } }, out response); SetDetailedInfo(response); } } } catch (Exception ex) { string response = MyTexts.GetString(MySpaceTexts.ProgrammableBlock_Exception_ExceptionCaught) + ex.Message; SetDetailedInfo(response); } }
/// <summary> /// Runs the Save method of the given program, if there is any. /// </summary> /// <param name="gridProgram"></param> /// <returns></returns> public static string Save(IMyGridProgram gridProgram) { if (gridProgram == null) { throw new ArgumentNullException(nameof(gridProgram)); } if (gridProgram.HasSaveMethod) { gridProgram.Save(); } return(gridProgram.Storage); }
/// <summary> /// Runs the given program. If the program's runtime derives from <see cref="SimpleRuntimeInfo" />, it's metrics will /// be updated. /// </summary> /// <param name="gridProgram">The program to run</param> /// <param name="argument">The argument to pass to the program</param> /// <param name="timeSinceLastRun">The simulated time since the last time the program was run</param> /// <param name="updateType">The source of this simulated run. Defaults to Trigger.</param> public static void Run(IMyGridProgram gridProgram, string argument = "", TimeSpan timeSinceLastRun = default(TimeSpan), UpdateType updateType = UpdateType.Trigger) { if (gridProgram == null) { throw new ArgumentNullException(nameof(gridProgram)); } var runtime = gridProgram.Runtime as SimpleRuntimeInfo; if (runtime != null) { runtime.TimeSinceLastRun = timeSinceLastRun; } var stopwatch = Stopwatch.StartNew(); gridProgram.Main(argument ?? "", updateType); if (runtime != null) { runtime.LastRunTimeMs = stopwatch.Elapsed.TotalMilliseconds; } }
private void CompileAndCreateInstance(string program,string storage) { if (MySession.Static.EnableIngameScripts == false) { return; } m_wasTerminated = false; Assembly temp = null; MyGuiScreenEditor.CompileProgram(program, m_compilerErrors, ref temp); if (temp != null) { try { m_assembly = IlInjector.InjectCodeToAssembly("IngameScript_safe", temp, typeof(IlInjector).GetMethod("CountInstructions", BindingFlags.Public | BindingFlags.Static), typeof(IlInjector).GetMethod("CountMethodCalls", BindingFlags.Public | BindingFlags.Static)); var type = m_assembly.GetType("Program"); if (type != null) { IlInjector.RestartCountingInstructions(MAX_NUM_EXECUTED_INSTRUCTIONS); IlInjector.RestartCountingMethods(MAX_NUM_METHOD_CALLS); try { m_instance = Activator.CreateInstance(type) as IMyGridProgram; if (m_instance != null) { m_previousRunTimestamp = 0; m_instance.Storage = storage; m_instance.Me = this; m_instance.Echo = EchoTextToDetailInfo; } } catch (TargetInvocationException ex) { if (ex.InnerException != null) { string response = MyTexts.GetString(MySpaceTexts.ProgrammableBlock_Exception_ExceptionCaught) + ex.InnerException.Message; if (DetailedInfo.ToString() != response) { SyncObject.SendProgramResponseMessage(response); WriteProgramResponse(response); } } } } } catch (Exception ex) { string response = MyTexts.GetString(MySpaceTexts.ProgrammableBlock_Exception_ExceptionCaught) + ex.Message; if (DetailedInfo.ToString() != response) { SyncObject.SendProgramResponseMessage(response); WriteProgramResponse(response); } } } }