Exemplo n.º 1
0
        protected bool canLoadModule(ConfigNode node)
        {
            string value;

            //If we are in career mode, make sure we have unlocked the tech node.
            if (ResearchAndDevelopment.Instance != null)
            {
                value = node.GetValue("TechRequired");
                if (!string.IsNullOrEmpty(value) && (HighLogic.CurrentGame.Mode == Game.Modes.CAREER || HighLogic.CurrentGame.Mode == Game.Modes.SCIENCE_SANDBOX))
                {
                    if (ResearchAndDevelopment.GetTechnologyState(value) != RDTech.State.Available)
                    {
                        return(false);
                    }
                }
            }

            //Now check for required mod
            value = node.GetValue("needs");
            if (!string.IsNullOrEmpty(value))
            {
                if (TemplatesModel.CheckNeeds(value) != EInvalidTemplateReasons.TemplateIsValid)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        public ModuleResourceConverter AddFromTemplate(ConfigNode node)
        {
            string converterName = node.GetValue("ConverterName");

            Log("AddFromTemplate called for converter: " + converterName);

            ConfigNode settingsNode = null;

            if (converterStates.ContainsKey(converterName))
            {
                settingsNode = converterStates[converterName];
            }

            string value = node.GetValue("needs");

            if (string.IsNullOrEmpty(value) == false)
            {
                if (TemplatesModel.CheckNeeds(value) == EInvalidTemplateReasons.RequiredModuleNotFound)
                {
                    return(null);
                }
            }

            //Courtesy of http://forum.kerbalspaceprogram.com/threads/27851-part-AddModule%28ConfigNode-node%29-NullReferenceException-in-PartModule-Load%28node%29-help
            ModuleResourceConverter converter = (ModuleResourceConverter)this.part.AddModule(node.GetValue("name"));

            object[]   parameters   = new object[] { };
            MethodInfo awakenMethod = typeof(PartModule).GetMethod("Awake", BindingFlags.Instance | BindingFlags.NonPublic);

            if (awakenMethod == null)
            {
                Log("No awaken method!");
                return(null);
            }
            awakenMethod.Invoke(converter, parameters);
            converter.OnAwake();
            converter.OnActive();

            if (settingsNode != null)
            {
                foreach (ConfigNode.Value nodeValue in settingsNode.values)
                {
                    if (nodeValue.name != "name")
                    {
                        node.SetValue(nodeValue.name, nodeValue.value, true);
                    }
                }
                //Actions
                if (settingsNode.HasNode("ACTIONS"))
                {
                    ConfigNode actionsNode = settingsNode.GetNode("ACTIONS");
                    BaseAction action;

                    foreach (ConfigNode nodeAction in actionsNode.nodes)
                    {
                        action = converter.Actions[nodeAction.name];
                        if (action != null)
                        {
                            action.actionGroup = (KSPActionGroup)Enum.Parse(typeof(KSPActionGroup), nodeAction.GetValue("actionGroup"));
                        }
                    }
                }
            }
            converter.Load(node);

            if (HighLogic.LoadedSceneIsFlight)
            {
                switch (this.part.vessel.situation)
                {
                case Vessel.Situations.ORBITING:
                    converter.OnStart(PartModule.StartState.Orbital);
                    break;

                case Vessel.Situations.LANDED:
                    converter.OnStart(PartModule.StartState.Landed);
                    break;

                case Vessel.Situations.SPLASHED:
                    converter.OnStart(PartModule.StartState.Splashed);
                    break;

                case Vessel.Situations.SUB_ORBITAL:
                    converter.OnStart(PartModule.StartState.SubOrbital);
                    break;

                case Vessel.Situations.FLYING:
                    converter.OnStart(PartModule.StartState.Flying);
                    break;

                default:
                    converter.OnStart(PartModule.StartState.None);
                    break;
                }
            }

            else
            {
                converter.OnStart(PartModule.StartState.None);
            }
            converter.EnableModule();
            setConverterState(converter);

            //Remove the converter's GUI
            RunHeadless(converter);

            //Add it to the list
            this.converters.Add(converter);
            Debug.Log("Added converter " + converter.ConverterName);

            return(converter);
        }
Exemplo n.º 3
0
        public EInvalidTemplateReasons CanUseTemplate(ConfigNode nodeTemplate)
        {
            string     value;
            PartModule requiredModule;
            EInvalidTemplateReasons invalidTemplateReason;

            //Make sure the vessel object is set
            if (this.vessel == null)
            {
                this.vessel = this.part.vessel;
            }

            //If we are in career mode, make sure we have unlocked the tech node.
            if (ResearchAndDevelopment.Instance != null)
            {
                value = nodeTemplate.GetValue("TechRequired");
                if (string.IsNullOrEmpty(value))
                {
                    return(EInvalidTemplateReasons.TemplateIsValid);
                }

                if (ResearchAndDevelopment.GetTechnologyState(value) != RDTech.State.Available)
                {
                    return(EInvalidTemplateReasons.TechNotUnlocked);
                }
            }

            //If we need a specific mod then check for it.
            value = nodeTemplate.GetValue("needs");
            if (string.IsNullOrEmpty(value) == false)
            {
                invalidTemplateReason = TemplatesModel.CheckNeeds(value);

                if (invalidTemplateReason != EInvalidTemplateReasons.TemplateIsValid)
                {
                    return(invalidTemplateReason);
                }
            }

            //If we need a specific module then check for it.
            value = nodeTemplate.GetValue("requiresModule");
            if (string.IsNullOrEmpty(value) == false)
            {
                requiredModule = this.part.Modules[value];
                if (requiredModule == null)
                {
                    return(EInvalidTemplateReasons.RequiredModuleNotFound);
                }
            }

            //If we need a specific template type then check for it.
            value = nodeTemplate.GetValue("templateType");
            if (string.IsNullOrEmpty(value) == false)
            {
                //if we have template types then see if the templateType is in the list.
                //Otherwise, we're good.
                if (string.IsNullOrEmpty(_templateTypes) == false)
                {
                    if (_templateTypes.Contains(value) == false)
                    {
                        return(EInvalidTemplateReasons.RequiredModuleNotFound);
                    }
                }
            }

            //If we're in the editor, then that's all we need to check.
            if (HighLogic.LoadedSceneIsEditor)
            {
                return(EInvalidTemplateReasons.TemplateIsValid);
            }

            return(EInvalidTemplateReasons.TemplateIsValid);
        }