private void onParameterAdded(Contract c, ContractParameter cP)
        {
            contractContainer cc = contractParser.getActiveContract(c.ContractGuid);

            if (cc == null)
            {
                return;
            }

            var missions = getMissionsContaining(cc.ID);

            for (int i = missions.Count - 1; i >= 0; i--)
            {
                contractMission m = missions[i];

                if (m == null)
                {
                    continue;
                }

                contractUIObject cUI = m.getContract(cc.ID);

                if (cUI == null)
                {
                    continue;
                }

                cUI.AddParameter();
            }
        }
        internal bool addMissionList(contractMission mission)
        {
            if (!missionList.Contains(mission.MissionTitle))
            {
                missionList.Add(mission.MissionTitle, mission);
                return(true);
            }
            else
            {
                DMC_MBE.LogFormatted("Missions List Already Contains Mission Of Name: [{0}]; Please Rename", name);
            }

            return(false);
        }
        internal bool addMissionList(string name)
        {
            if (!missionList.Contains(name))
            {
                contractMission mission = new contractMission(name);
                missionList.Add(name, mission);
                return(true);
            }
            else
            {
                DMC_MBE.LogFormatted("Missions List Already Contains Mission Of Name: [{0}]; Please Rename", name);
            }

            return(false);
        }
        internal void setCurrentMission(string s)
        {
            contractMission m = getMissionList(s, true);

            if (m != null)
            {
                currentMission = m;
            }
            else
            {
                currentMission = masterMission;
            }


            contractWindow.Instance.setMission(currentMission);
        }
 internal void removeMissionList(string name, bool delete = true)
 {
     if (missionList.Contains(name))
     {
         if (delete)
         {
             contractMission c = missionList[name];
             c = null;
         }
         missionList.Remove(name);
     }
     else
     {
         DMC_MBE.LogFormatted("No Mission List Of Name: [{0}] Found", name);
     }
 }
        //Used to add the master contract mission list; usually when something has gone wrong
        internal void addFullMissionList()
        {
            string s = "MasterMission";

            if (missionList.Contains(s))
            {
                removeMissionList(s);
            }

            if (addMissionList(s))
            {
                missionList[s].MasterMission = true;
                addAllContractsToMaster();
                masterMission = missionList[s];
            }
        }
        public override void OnSave(ConfigNode node)
        {
            try
            {
                if (contractLoader.Settings != null)
                {
                    contractLoader.Settings.Save();
                }

                saveWindow(windowRects[currentScene(HighLogic.LoadedScene)]);

                ConfigNode scenes = new ConfigNode("Contracts_Window_Parameters");

                //Scene settings
                scenes.AddValue("WindowPosition", stringConcat(windowPos, windowPos.Length));
                scenes.AddValue("WindowVisible", stringConcat(windowVisible, windowVisible.Length));


                for (int i = missionList.Count - 1; i >= 0; i--)
                {
                    contractMission m = missionList.At(i);

                    if (m == null)
                    {
                        continue;
                    }

                    ConfigNode missionNode = new ConfigNode("Contracts_Window_Mission");

                    missionNode.AddValue("MissionName", m.InternalName);
                    missionNode.AddValue("ActiveListID", m.stringConcat(m.ActiveMissionList));
                    missionNode.AddValue("HiddenListID", m.stringConcat(m.HiddenMissionList));
                    missionNode.AddValue("VesselIDs", m.vesselConcat(currentMission));
                    missionNode.AddValue("AscendingSort", m.AscendingOrder);
                    missionNode.AddValue("ShowActiveList", m.ShowActiveMissions);
                    missionNode.AddValue("SortMode", (int)m.OrderMode);

                    scenes.AddNode(missionNode);
                }

                node.AddNode(scenes);
            }
            catch (Exception e)
            {
                DMC_MBE.LogFormatted("Contracts Window Settings Cannot Be Saved: {0}", e);
            }
        }
        //Initializes all missions that were added during the loading process
        internal void loadAllMissionLists()
        {
            if (missionList.Count <= 0)
            {
                addFullMissionList();
            }
            else
            {
                for (int i = 0; i < missionList.Count; i++)
                {
                    contractMission m = missionList.At(i);

                    if (m == null)
                    {
                        continue;
                    }

                    if (m.MasterMission)
                    {
                        m.buildMissionList();

                        List <contractContainer> active = contractParser.getActiveContracts;

                        int l = active.Count;

                        for (int j = 0; j < l; j++)
                        {
                            contractContainer c = active[j];

                            if (c == null)
                            {
                                continue;
                            }

                            m.addContract(c, true, false);
                        }

                        masterMission = m;
                    }
                    else
                    {
                        m.buildMissionList();
                    }
                }
            }
        }
        //Returns an ordered list of missions for the main window; the master mission is always first
        internal List <contractMission> getAllMissions()
        {
            List <contractMission> mList    = new List <contractMission>();
            List <contractMission> tempList = new List <contractMission>();

            for (int i = 0; i < missionList.Count; i++)
            {
                contractMission m = missionList.At(i);

                if (m == null)
                {
                    continue;
                }

                if (m.MasterMission)
                {
                    mList.Add(m);
                }
                else
                {
                    tempList.Add(m);
                }
            }

            if (mList.Count == 0)
            {
                if (addMissionList("MasterMission"))
                {
                    mList.Add(getMissionList("MasterMission"));
                }
            }

            tempList.Sort((a, b) => RUIutils.SortAscDescPrimarySecondary(false, a.ActiveContracts.CompareTo(b.ActiveContracts), a.MissionTitle.CompareTo(b.MissionTitle)));

            if (tempList.Count > 0)
            {
                mList.AddRange(tempList);
            }

            return(mList);
        }
        //Adds all contracts to the master mission
        private void addAllContractsToMaster()
        {
            contractMission Master = null;

            for (int i = missionList.Count - 1; i >= 0; i--)
            {
                contractMission m = missionList.At(i);

                if (m == null)
                {
                    continue;
                }

                if (!m.MasterMission)
                {
                    continue;
                }

                Master = m;
                break;
            }

            if (Master != null)
            {
                List <contractContainer> active = contractParser.getActiveContracts;

                int l = active.Count;

                for (int j = 0; j < l; j++)
                {
                    contractContainer c = active[j];

                    if (c == null)
                    {
                        continue;
                    }

                    Master.addContract(c, true, true);
                }
            }
        }
        internal contractMission setLoadedMission(Vessel v)
        {
            if (v == null)
            {
                return(masterMission);
            }

            for (int i = 0; i < missionList.Count; i++)
            {
                contractMission m = missionList.At(i);

                if (m == null)
                {
                    continue;
                }

                if (m.ContainsVessel(v))
                {
                    return(m);
                }
            }

            return(masterMission);
        }
        //Convert all of our saved strings into the appropriate arrays for each game scene
        public override void OnLoad(ConfigNode node)
        {
            instance = this;

            try
            {
                //The first step is manually checking for active contracts from the Game ConfigNode (ie persistent.sfs file); the count of active contracts will be used later when the window is loading
                ConfigNode gameNode = HighLogic.CurrentGame.config;
                if (gameNode != null)
                {
                    ConfigNode contractSystemNode = gameNode.GetNodes("SCENARIO").FirstOrDefault(c => c.GetValue("name") == "ContractSystem");
                    if (contractSystemNode != null)
                    {
                        ConfigNode cNode = contractSystemNode.GetNode("CONTRACTS");
                        if (cNode != null)
                        {
                            foreach (ConfigNode C in cNode.GetNodes("CONTRACT"))
                            {
                                if (C == null)
                                {
                                    continue;
                                }

                                if (C.HasValue("autoAccept"))
                                {
                                    if (C.GetValue("autoAccept") == "True")
                                    {
                                        continue;
                                    }
                                }

                                if (C.HasValue("state"))
                                {
                                    if (C.GetValue("state") == "Active")
                                    {
                                        contractCount++;
                                    }
                                }
                            }
                        }
                        else
                        {
                            DMC_MBE.LogFormatted("Contract System Can't Be Checked... Node Invalid");
                        }
                    }
                }

                ConfigNode scenes = node.GetNode("Contracts_Window_Parameters");
                if (scenes != null)
                {
                    windowPos     = stringSplit(scenes.GetValue("WindowPosition"));
                    windowVisible = stringSplitBool(scenes.GetValue("WindowVisible"));
                    int[] winPos = new int[4] {
                        windowPos[4 * currentScene(HighLogic.LoadedScene)], windowPos[(4 * currentScene(HighLogic.LoadedScene)) + 1], windowPos[(4 * currentScene(HighLogic.LoadedScene)) + 2], windowPos[(4 * currentScene(HighLogic.LoadedScene)) + 3]
                    };

                    //All saved contract missions are loaded here
                    //Each mission has a separate contract list
                    foreach (ConfigNode m in scenes.GetNodes("Contracts_Window_Mission"))
                    {
                        if (m == null)
                        {
                            continue;
                        }

                        string name;
                        string activeString = "";
                        string hiddenString = "";
                        string vesselString = "";
                        bool   ascending, showActive;
                        int    sortMode;
                        bool   master = false;

                        if (m.HasValue("MissionName"))
                        {
                            name = m.GetValue("MissionName");
                        }
                        else
                        {
                            continue;
                        }

                        if (name == "MasterMission")
                        {
                            master = true;
                        }

                        if (m.HasValue("ActiveListID"))
                        {
                            activeString = m.GetValue("ActiveListID");
                        }
                        if (m.HasValue("HiddenListID"))
                        {
                            hiddenString = m.GetValue("HiddenListID");
                        }
                        if (m.HasValue("VesselIDs"))
                        {
                            vesselString = m.GetValue("VesselIDs");
                        }

                        if (!bool.TryParse(m.GetValue("AscendingSort"), out ascending))
                        {
                            ascending = true;
                        }
                        if (!bool.TryParse(m.GetValue("ShowActiveList"), out showActive))
                        {
                            showActive = true;
                        }
                        if (!int.TryParse(m.GetValue("SortMode"), out sortMode))
                        {
                            sortMode = 0;
                        }

                        contractMission mission = new contractMission(name, activeString, hiddenString, vesselString, ascending, showActive, sortMode, master);

                        if (master)
                        {
                            masterMission = mission;
                            DMC_MBE.LogFormatted_DebugOnly("Setting Master Mission During Load");
                        }

                        if (!missionList.Contains(name))
                        {
                            missionList.Add(name, mission);
                        }
                    }
                    loadWindow(winPos);
                }
            }
            catch (Exception e)
            {
                DMC_MBE.LogFormatted("Contracts Window Settings Cannot Be Loaded: {0}", e);
            }
        }