Example #1
0
        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);
            }
        }
        private void CompileAndCreateInstance(string program, string storage)
        {
            if (MySession.Static.EnableIngameScripts == false)
            {
                return;
            }
            m_terminationReason = ScriptTerminationReason.None;
            try
            {
                if (MyFakes.ENABLE_ROSLYN_SCRIPTS)
                {
#if !XB1
                    m_assembly = MyScriptCompiler.Static.Compile(
                        MyApiTarget.Ingame,
                        Path.Combine(MyFileSystem.UserDataPath, GetAssemblyName()),
                        MyScriptCompiler.Static.GetIngameScript(program, "Program", typeof(MyGridProgram).Name),
                        m_compilerMessages).Result;

                    m_compilerErrors.Clear();
                    m_compilerErrors.AddRange(m_compilerMessages.Select(m => m.Text));

                    CreateInstance(m_assembly, m_compilerErrors, storage);
#else // XB1
#if !XB1_SKIPASSERTFORNOW
                    System.Diagnostics.Debug.Assert(false, "No scripts on XB1");
#endif // !XB1_SKIPASSERTFORNOW
#endif // XB1
                }
                else
                {
                    Assembly temp = null;
                    MyGuiScreenEditor.CompileProgram(program, m_compilerErrors, ref temp);
                    if (temp != null)
                    {
#if !XB1 // XB1_NOILINJECTOR
                        m_assembly = IlInjector.InjectCodeToAssembly("IngameScript_safe", temp, typeof(IlInjector).GetMethod("CountInstructions", BindingFlags.Public | BindingFlags.Static), typeof(IlInjector).GetMethod("CountMethodCalls", BindingFlags.Public | BindingFlags.Static));
#else // XB1
                        System.Diagnostics.Debug.Assert(false, "No scripts on XB1");
                        return;
#endif // XB1

                        CreateInstance(m_assembly, m_compilerErrors, storage);
                    }
                }
            }
            catch (Exception ex)
            {
                string response = MyTexts.GetString(MySpaceTexts.ProgrammableBlock_Exception_ExceptionCaught) + ex.Message;
                SetDetailedInfo(response);
            }
        }
        public void OpenEditor()
        {
            if (m_editorData == null)
            {
                m_editorData = "void Main(string argument)\n{\n}";
            }

            m_editorScreen = new MyGuiScreenEditor(missionTitle: MyTexts.GetString(MySpaceTexts.ProgrammableBlock_CodeEditor_Title),
                                                   currentObjectivePrefix: "",
                                                   currentObjective: "",
                                                   description: m_editorData,
                                                   resultCallback: SaveCode,
                                                   saveCodeCallback: SaveCode,
                                                   okButtonCaption: MyTexts.GetString(MySpaceTexts.ProgrammableBlock_CodeEditor_SaveExit));
            MyGuiScreenGamePlay.TmpGameplayScreenHolder = MyGuiScreenGamePlay.ActiveGameplayScreen;
            MyScreenManager.AddScreen(MyGuiScreenGamePlay.ActiveGameplayScreen = m_editorScreen);
        }
        void OpenEditor()
        {
            if (m_editorData == null)
            {
                var constructorInfo = ToIndentedComment(MyTexts.GetString(MySpaceTexts.ProgrammableBlock_DefaultScript_Constructor).Trim());
                var saveInfo        = ToIndentedComment(MyTexts.GetString(MySpaceTexts.ProgrammableBlock_DefaultScript_Save).Trim());
                var mainInfo        = ToIndentedComment(MyTexts.GetString(MySpaceTexts.ProgrammableBlock_DefaultScript_Main).Trim());
                m_editorData = string.Format(DEFAULT_SCRIPT_TEMPLATE, constructorInfo, saveInfo, mainInfo);
            }

            m_editorScreen = new MyGuiScreenEditor(missionTitle: MyTexts.GetString(MySpaceTexts.ProgrammableBlock_CodeEditor_Title),
                                                   currentObjectivePrefix: "",
                                                   currentObjective: "",
                                                   description: m_editorData,
                                                   resultCallback: SaveCode,
                                                   saveCodeCallback: SaveCode,
                                                   okButtonCaption: MyTexts.GetString(MySpaceTexts.ProgrammableBlock_CodeEditor_SaveExit));
            MyGuiScreenGamePlay.TmpGameplayScreenHolder = MyGuiScreenGamePlay.ActiveGameplayScreen;
            MyScreenManager.AddScreen(MyGuiScreenGamePlay.ActiveGameplayScreen = m_editorScreen);
        }
        private void CompileAndCreateInstance(string program, string storage)
        {
            if (MySession.Static.EnableIngameScripts == false)
            {
                return;
            }
            m_wasTerminated = false;
            m_mainMethod    = null;
            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));

                    var type = m_assembly.GetType("Program");
                    if (type != null)
                    {
                        IlInjector.RestartCountingInstructions(MAX_NUM_EXECUTED_INSTRUCTIONS);
                        try
                        {
                            m_instance         = Activator.CreateInstance(type);
                            m_programGridGroup = type.GetField("GridTerminalSystem", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
                            m_storageField     = type.GetField("Storage", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
                            m_meField          = type.GetField("Me", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
                            m_echoField        = type.GetField("Echo", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
                            m_elapsedTimeField = type.GetField("ElapsedTime", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
                            if (m_programGridGroup != null)
                            {
                                // First try to get the main method with a string argument. If this fails, try to get one without.
                                m_mainMethod = type.GetMethod("Main", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(string) }, null);
                                m_mainMethodSupportsArgument = m_mainMethod != null;
                                if (m_mainMethod == null)
                                {
                                    m_mainMethod = type.GetMethod("Main", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                                }
                            }
                            if (m_storageField != null)
                            {
                                m_storageField.SetValue(m_instance, storage);
                            }
                            if (m_meField != null)
                            {
                                m_meField.SetValue(m_instance, this);
                            }
                            if (m_echoField != null)
                            {
                                m_echoField.SetValue(m_instance, new Action <string>(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);
                    }
                }
            }
        }