Fixed bug when staging a KER part.
Fixed bug when staging a KER part.

--- a/KerbalEngineer/Flight/ActionMenu.cs
+++ b/KerbalEngineer/Flight/ActionMenu.cs
@@ -34,6 +34,7 @@
     /// <summary>

     ///     Graphical controller for section interaction in the form of a menu system.

     /// </summary>

+    [KSPAddon(KSPAddon.Startup.Flight, false)]

     public class ActionMenu : MonoBehaviour

     {

         #region Constants

@@ -184,7 +185,7 @@
         {

             try

             {

-                if (!this.isOpen)

+                if (!this.isOpen || (this.button != null && this.button.State == RUIToggleButton.ButtonState.DISABLED))

                 {

                     return;

                 }


--- a/KerbalEngineer/Flight/DisplayStack.cs
+++ b/KerbalEngineer/Flight/DisplayStack.cs
@@ -19,6 +19,7 @@
 

 #region Using Directives

 

+using System;

 using System.Collections.Generic;

 

 using KerbalEngineer.Extensions;

@@ -34,6 +35,7 @@
     /// <summary>

     ///     Graphical controller for displaying stacked sections.

     /// </summary>

+    [KSPAddon(KSPAddon.Startup.Flight, false)]

     public class DisplayStack : MonoBehaviour

     {

         #region Instance

@@ -61,13 +63,21 @@
         /// </summary>

         private void Awake()

         {

-            if (Instance == null)

-            {

-                Instance = this;

-            }

-            else

-            {

-                Destroy(this);

+            try

+            {

+                if (Instance == null)

+                {

+                    Instance = this;

+                    Logger.Log("ActionMenu->Awake");

+                }

+                else

+                {

+                    Destroy(this);

+                }

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "DisplayStack->Awake");

             }

         }

 

@@ -76,11 +86,18 @@
         /// </summary>

         private void Start()

         {

-            this.windowId = this.GetHashCode();

-            this.InitialiseStyles();

-            this.Load();

-

-            RenderingManager.AddToPostDrawQueue(0, this.Draw);

+            try

+            {

+                this.windowId = this.GetHashCode();

+                this.InitialiseStyles();

+                this.Load();

+                RenderingManager.AddToPostDrawQueue(0, this.Draw);

+                Logger.Log("ActionMenu->Start");

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "DisplayStack->Start");

+            }

         }

 

         #endregion

@@ -113,36 +130,43 @@
         /// </summary>

         private void InitialiseStyles()

         {

-            this.windowStyle = new GUIStyle(HighLogic.Skin.window)

-            {

-                margin = new RectOffset(),

-                padding = new RectOffset(5, 5, 0, 5)

-            };

-

-            this.titleStyle = new GUIStyle(HighLogic.Skin.label)

-            {

-                margin = new RectOffset(0, 0, 5, 3),

-                padding = new RectOffset(),

-                alignment = TextAnchor.MiddleCenter,

-                fontSize = 13,

-                fontStyle = FontStyle.Bold,

-                stretchWidth = true

-            };

-

-            this.buttonStyle = new GUIStyle(HighLogic.Skin.button)

-            {

-                normal =

-                {

-                    textColor = Color.white

-                },

-                margin = new RectOffset(),

-                padding = new RectOffset(),

-                alignment = TextAnchor.MiddleCenter,

-                fontSize = 11,

-                fontStyle = FontStyle.Bold,

-                fixedWidth = 60.0f,

-                fixedHeight = 25.0f,

-            };

+            try

+            {

+                this.windowStyle = new GUIStyle(HighLogic.Skin.window)

+                {

+                    margin = new RectOffset(),

+                    padding = new RectOffset(5, 5, 0, 5)

+                };

+

+                this.titleStyle = new GUIStyle(HighLogic.Skin.label)

+                {

+                    margin = new RectOffset(0, 0, 5, 3),

+                    padding = new RectOffset(),

+                    alignment = TextAnchor.MiddleCenter,

+                    fontSize = 13,

+                    fontStyle = FontStyle.Bold,

+                    stretchWidth = true

+                };

+

+                this.buttonStyle = new GUIStyle(HighLogic.Skin.button)

+                {

+                    normal =

+                    {

+                        textColor = Color.white

+                    },

+                    margin = new RectOffset(),

+                    padding = new RectOffset(),

+                    alignment = TextAnchor.MiddleCenter,

+                    fontSize = 11,

+                    fontStyle = FontStyle.Bold,

+                    fixedWidth = 60.0f,

+                    fixedHeight = 25.0f,

+                };

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "DisplayStack->InitialiseStyles");

+            }

         }

 

         #endregion

@@ -151,9 +175,21 @@
 

         private void Update()

         {

-            if (Input.GetKeyDown(KeyCode.Backslash))

-            {

-                this.Hidden = !this.Hidden;

+            try

+            {

+                if (FlightEngineerCore.Instance == null)

+                {

+                    return;

+                }

+

+                if (Input.GetKeyDown(KeyCode.Backslash))

+                {

+                    this.Hidden = !this.Hidden;

+                }

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "DisplayStack->Update");

             }

         }

 

@@ -166,16 +202,28 @@
         /// </summary>

         private void Draw()

         {

-            if (this.resizeRequested || this.numberOfStackSections != SectionLibrary.Instance.NumberOfStackSections)

-            {

-                this.numberOfStackSections = SectionLibrary.Instance.NumberOfStackSections;

-                this.windowPosition.height = 0;

-                this.resizeRequested = false;

-            }

-

-            if (!this.Hidden && (SectionLibrary.Instance.NumberOfStackSections > 0 || this.ShowControlBar))

-            {

-                this.windowPosition = GUILayout.Window(this.windowId, this.windowPosition, this.Window, string.Empty, this.windowStyle).ClampToScreen();

+            try

+            {

+                if (FlightEngineerCore.Instance == null)

+                {

+                    return;

+                }

+

+                if (this.resizeRequested || this.numberOfStackSections != SectionLibrary.Instance.NumberOfStackSections)

+                {

+                    this.numberOfStackSections = SectionLibrary.Instance.NumberOfStackSections;

+                    this.windowPosition.height = 0;

+                    this.resizeRequested = false;

+                }

+

+                if (!this.Hidden && (SectionLibrary.Instance.NumberOfStackSections > 0 || this.ShowControlBar))

+                {

+                    this.windowPosition = GUILayout.Window(this.windowId, this.windowPosition, this.Window, string.Empty, this.windowStyle).ClampToScreen();

+                }

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "DisplayStack->Draw");

             }

         }

 

@@ -184,18 +232,25 @@
         /// </summary>

         private void Window(int windowId)

         {

-            if (this.ShowControlBar)

-            {

-                this.DrawControlBar();

-            }

-

-            if (SectionLibrary.Instance.NumberOfStackSections > 0)

-            {

-                this.DrawSections(SectionLibrary.Instance.StockSections);

-                this.DrawSections(SectionLibrary.Instance.CustomSections);

-            }

-

-            GUI.DragWindow();

+            try

+            {

+                if (this.ShowControlBar)

+                {

+                    this.DrawControlBar();

+                }

+

+                if (SectionLibrary.Instance.NumberOfStackSections > 0)

+                {

+                    this.DrawSections(SectionLibrary.Instance.StockSections);

+                    this.DrawSections(SectionLibrary.Instance.CustomSections);

+                }

+

+                GUI.DragWindow();

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "DisplayStack->Widnow");

+            }

         }

 

         /// <summary>

@@ -203,10 +258,17 @@
         /// </summary>

         private void DrawControlBar()

         {

-            GUILayout.Label("FLIGHT ENGINEER " + EngineerGlobals.AssemblyVersion, this.titleStyle);

-

-            this.DrawControlBarButtons(SectionLibrary.Instance.StockSections);

-            this.DrawControlBarButtons(SectionLibrary.Instance.CustomSections);

+            try

+            {

+                GUILayout.Label("FLIGHT ENGINEER " + EngineerGlobals.AssemblyVersion, this.titleStyle);

+

+                this.DrawControlBarButtons(SectionLibrary.Instance.StockSections);

+                this.DrawControlBarButtons(SectionLibrary.Instance.CustomSections);

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "DisplayStack->DrawControlBar");

+            }

         }

 

         /// <summary>

@@ -214,23 +276,30 @@
         /// </summary>

         private void DrawControlBarButtons(IEnumerable<SectionModule> sections)

         {

-            var index = 0;

-            foreach (var section in sections)

-            {

-                if (index % 4 == 0)

-                {

-                    if (index > 0)

+            try

+            {

+                var index = 0;

+                foreach (var section in sections)

+                {

+                    if (index % 4 == 0)

                     {

-                        GUILayout.EndHorizontal();

+                        if (index > 0)

+                        {

+                            GUILayout.EndHorizontal();

+                        }

+                        GUILayout.BeginHorizontal();

                     }

-                    GUILayout.BeginHorizontal();

-                }

-                section.IsVisible = GUILayout.Toggle(section.IsVisible, section.Abbreviation.ToUpper(), this.buttonStyle);

-                index++;

-            }

-            if (index > 0)

-            {

-                GUILayout.EndHorizontal();

+                    section.IsVisible = GUILayout.Toggle(section.IsVisible, section.Abbreviation.ToUpper(), this.buttonStyle);

+                    index++;

+                }

+                if (index > 0)

+                {

+                    GUILayout.EndHorizontal();

+                }

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "DisplayStack->DrawControlBarButtons");

             }

         }

 

@@ -239,12 +308,19 @@
         /// </summary>

         private void DrawSections(IEnumerable<SectionModule> sections)

         {

-            foreach (var section in sections)

-            {

-                if (!section.IsFloating)

-                {

-                    section.Draw();

-                }

+            try

+            {

+                foreach (var section in sections)

+                {

+                    if (!section.IsFloating)

+                    {

+                        section.Draw();

+                    }

+                }

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "DisplayStack->DrawSections");

             }

         }

 

@@ -257,9 +333,16 @@
         /// </summary>

         private void OnDestroy()

         {

-            this.Save();

-

-            RenderingManager.RemoveFromPostDrawQueue(0, this.Draw);

+            try

+            {

+                this.Save();

+                RenderingManager.RemoveFromPostDrawQueue(0, this.Draw);

+                Logger.Log("ActionMenu->OnDestroy");

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "DisplayStack->OnDestroy");

+            }

         }

 

         #endregion

@@ -271,11 +354,18 @@
         /// </summary>

         private void Save()

         {

-            var handler = new SettingHandler();

-            handler.Set("showControlBar", this.ShowControlBar);

-            handler.Set("windowPositionX", this.windowPosition.x);

-            handler.Set("windowPositionY", this.windowPosition.y);

-            handler.Save("DisplayStack.xml");

+            try

+            {

+                var handler = new SettingHandler();

+                handler.Set("showControlBar", this.ShowControlBar);

+                handler.Set("windowPositionX", this.windowPosition.x);

+                handler.Set("windowPositionY", this.windowPosition.y);

+                handler.Save("DisplayStack.xml");

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "DisplayStack->Save");

+            }

         }

 

         /// <summary>

@@ -283,10 +373,17 @@
         /// </summary>

         private void Load()

         {

-            var handler = SettingHandler.Load("DisplayStack.xml");

-            this.ShowControlBar = handler.Get("showControlBar", this.ShowControlBar);

-            this.windowPosition.x = handler.Get("windowPositionX", this.windowPosition.x);

-            this.windowPosition.y = handler.Get("windowPositionY", this.windowPosition.y);

+            try

+            {

+                var handler = SettingHandler.Load("DisplayStack.xml");

+                this.ShowControlBar = handler.Get("showControlBar", this.ShowControlBar);

+                this.windowPosition.x = handler.Get("windowPositionX", this.windowPosition.x);

+                this.windowPosition.y = handler.Get("windowPositionY", this.windowPosition.y);

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "DisplayStack->Load");

+            }

         }

 

         #endregion


--- a/KerbalEngineer/Flight/FlightEngineerCore.cs
+++ b/KerbalEngineer/Flight/FlightEngineerCore.cs
@@ -19,6 +19,7 @@
 

 #region Using Directives

 

+using System;

 using System.Collections.Generic;

 

 using KerbalEngineer.Flight.Readouts;

@@ -51,13 +52,19 @@
         /// </summary>

         private void Awake()

         {

-            Instance = this;

-

-            this.ActionMenu = this.gameObject.AddComponent<ActionMenu>();

-            this.DisplayStack = this.gameObject.AddComponent<DisplayStack>();

-            this.SectionWindows = new List<SectionWindow>();

-            this.SectionEditors = new List<SectionEditor>();

-            this.UpdatableModules = new List<IUpdatable>();

+            try

+            {

+                Instance = this;

+

+                this.SectionWindows = new List<SectionWindow>();

+                this.SectionEditors = new List<SectionEditor>();

+                this.UpdatableModules = new List<IUpdatable>();

+                Logger.Log("FlightEngineerCore->Awake");

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "FlightEngineerCore->Awake");

+            }

         }

 

         /// <summary>

@@ -65,8 +72,16 @@
         /// </summary>

         private void Start()

         {

-            SectionLibrary.Instance.Load();

-            ReadoutLibrary.Instance.Reset();

+            try

+            {

+                SectionLibrary.Instance.Load();

+                ReadoutLibrary.Instance.Reset();

+                Logger.Log("FlightEngineerCore->Start");

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "FlightEngineerCore->Start");

+            }

         }

 

         #endregion

@@ -74,16 +89,6 @@
         #region Properties

 

         /// <summary>

-        ///     Gets and sets the ActionMenu object.

-        /// </summary>

-        public ActionMenu ActionMenu { get; set; }

-

-        /// <summary>

-        ///     Gets and sets the DisplayStack object.

-        /// </summary>

-        public DisplayStack DisplayStack { get; set; }

-

-        /// <summary>

         ///     Gets the section windows for floating sections.

         /// </summary>

         public List<SectionWindow> SectionWindows { get; private set; }

@@ -107,8 +112,15 @@
         /// </summary>

         private void Update()

         {

-            SectionLibrary.Instance.Update();

-            this.UpdateModules();

+            try

+            {

+                SectionLibrary.Instance.Update();

+                this.UpdateModules();

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "FlightEngineerCore->Update");

+            }

         }

 

         /// <summary>

@@ -116,21 +128,28 @@
         /// </summary>

         private void UpdateModules()

         {

-            foreach (var updatable in this.UpdatableModules)

-            {

-                if (updatable is IUpdateRequest)

-                {

-                    var request = updatable as IUpdateRequest;

-                    if (request.UpdateRequested)

+            try

+            {

+                foreach (var updatable in this.UpdatableModules)

+                {

+                    if (updatable is IUpdateRequest)

+                    {

+                        var request = updatable as IUpdateRequest;

+                        if (request.UpdateRequested)

+                        {

+                            updatable.Update();

+                            request.UpdateRequested = false;

+                        }

+                    }

+                    else

                     {

                         updatable.Update();

-                        request.UpdateRequested = false;

                     }

                 }

-                else

-                {

-                    updatable.Update();

-                }

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "FlightEngineerCore->UpdateModules");

             }

         }

 

@@ -143,30 +162,27 @@
         /// </summary>

         private void OnDestroy()

         {

-            SectionLibrary.Instance.Save();

-

-            foreach (var window in this.SectionWindows)

-            {

-                print("[FlightEngineer]: Destroying Floating Window for " + window.ParentSection.Name);

-                Destroy(window);

-            }

-

-            foreach (var editor in this.SectionEditors)

-            {

-                print("[FlightEngineer]: Destroying Editor Window for " + editor.ParentSection.Name);

-                Destroy(editor);

-            }

-

-            if (this.ActionMenu != null)

-            {

-                print("[FlightEngineer]: Destroying ActionMenu");

-                DestroyImmediate(this.ActionMenu);

-            }

-

-            if (this.DisplayStack != null)

-            {

-                print("[FlightEngineer]: Destroying DisplayStack");

-                DestroyImmediate(this.DisplayStack);

+            try

+            {

+                SectionLibrary.Instance.Save();

+

+                foreach (var window in this.SectionWindows)

+                {

+                    print("[FlightEngineer]: Destroying Floating Window for " + window.ParentSection.Name);

+                    Destroy(window);

+                }

+

+                foreach (var editor in this.SectionEditors)

+                {

+                    print("[FlightEngineer]: Destroying Editor Window for " + editor.ParentSection.Name);

+                    Destroy(editor);

+                }

+

+                Logger.Log("FlightEngineerCore->OnDestroy");

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "FlightEngineerCore->OnDestroy");

             }

         }

 

@@ -179,11 +195,19 @@
         /// </summary>

         public SectionWindow AddSectionWindow(SectionModule section)

         {

-            var window = this.gameObject.AddComponent<SectionWindow>();

-            window.ParentSection = section;

-            window.WindowPosition = new Rect(section.FloatingPositionX, section.FloatingPositionY, 0, 0);

-            this.SectionWindows.Add(window);

-            return window;

+            try

+            {

+                var window = this.gameObject.AddComponent<SectionWindow>();

+                window.ParentSection = section;

+                window.WindowPosition = new Rect(section.FloatingPositionX, section.FloatingPositionY, 0, 0);

+                this.SectionWindows.Add(window);

+                return window;

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "FlightEngineerCore->AddSectionWindow");

+                return null;

+            }

         }

 

         /// <summary>

@@ -191,11 +215,19 @@
         /// </summary>

         public SectionEditor AddSectionEditor(SectionModule section)

         {

-            var editor = this.gameObject.AddComponent<SectionEditor>();

-            editor.ParentSection = section;

-            editor.WindowPosition = new Rect(section.EditorPositionX, section.EditorPositionY, SectionEditor.Width, SectionEditor.Height);

-            this.SectionEditors.Add(editor);

-            return editor;

+            try

+            {

+                var editor = this.gameObject.AddComponent<SectionEditor>();

+                editor.ParentSection = section;

+                editor.WindowPosition = new Rect(section.EditorPositionX, section.EditorPositionY, SectionEditor.Width, SectionEditor.Height);

+                this.SectionEditors.Add(editor);

+                return editor;

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "FlightEngineerCore->AddSectionEditor");

+                return null;

+            }

         }

 

         /// <summary>

@@ -203,9 +235,16 @@
         /// </summary>

         public void AddUpdatable(IUpdatable updatable)

         {

-            if (!this.UpdatableModules.Contains(updatable))

-            {

-                this.UpdatableModules.Add(updatable);

+            try

+            {

+                if (!this.UpdatableModules.Contains(updatable))

+                {

+                    this.UpdatableModules.Add(updatable);

+                }

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "FlightEngineerCore->AddUpdatable");

             }

         }

 


--- a/KerbalEngineer/Flight/FlightEngineerModule.cs
+++ b/KerbalEngineer/Flight/FlightEngineerModule.cs
@@ -19,6 +19,7 @@
 

 #region Using Directives

 

+using System;

 using System.Linq;

 

 #endregion

@@ -46,23 +47,30 @@
         /// </summary>

         private void Update()

         {

-            if (!HighLogic.LoadedSceneIsFlight)

+            try

             {

-                return;

-            }

+                if (!HighLogic.LoadedSceneIsFlight)

+                {

+                    return;

+                }

 

-            if (this.vessel == FlightGlobals.ActiveVessel)

-            {

-                // Checks for an existing instance of FlightEngineerCore, and if this part is the first part containing FlightEngineerModule within the vessel.

-                if (this.flightEngineerCore == null && this.part == this.vessel.parts.FirstOrDefault(p => p.Modules.Contains("FlightEngineerModule")))

+                if (this.vessel == FlightGlobals.ActiveVessel)

                 {

-                    this.flightEngineerCore = this.gameObject.AddComponent<FlightEngineerCore>();

+                    // Checks for an existing instance of FlightEngineerCore, and if this part is the first part containing FlightEngineerModule within the vessel.

+                    if (flightEngineerCore == null && this.part == this.vessel.parts.FirstOrDefault(p => p.Modules.Contains("FlightEngineerModule")))

+                    {

+                        flightEngineerCore = this.gameObject.AddComponent<FlightEngineerCore>();

+                    }

+                }

+                else if (flightEngineerCore != null)

+                {

+                    // Using DestroyImmediate to force early destruction and keep saving/loading in synch when switching vessels.

+                    DestroyImmediate(flightEngineerCore);

                 }

             }

-            else if (this.flightEngineerCore != null)

+            catch (Exception ex)

             {

-                // Using DestroyImmediate to force early destruction and keep saving/loading in synch when switching vessels.

-                DestroyImmediate(this.flightEngineerCore);

+                Logger.Exception(ex, "FlightEngineerModule->Update");

             }

         }

 

@@ -75,9 +83,16 @@
         /// </summary>

         private void OnDestroy()

         {

-            if (this.flightEngineerCore != null)

+            try

             {

-                DestroyImmediate(this.flightEngineerCore);

+                if (flightEngineerCore != null)

+                {

+                    DestroyImmediate(flightEngineerCore);

+                }

+            }

+            catch (Exception ex)

+            {

+                Logger.Exception(ex, "FlightEngineerModule->OnDestroy");

             }

         }

 


 Binary files a/Output/KerbalEngineer/KerbalEngineer.dll and b/Output/KerbalEngineer/KerbalEngineer.dll differ