Ejemplo 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);
        }
Ejemplo n.º 2
0
        public void initTemplates()
        {
            Log("initTemplates called");
            //Create templates object if needed.
            //This can happen when the object is cloned in the editor (On Load won't be called).
            if (templatesModel == null)
            {
                templatesModel = new TemplatesModel(this.part, this.vessel, new LogDelegate(Log));
            }
            templatesModel.templateNodeName = templateNodes;
            templatesModel.templateTypes    = _templateTypes;

            if (templatesModel.templateNodes == null)
            {
                Log("OnStart templateNodes == null!");
                return;
            }

            //Set default template if needed
            //This will happen when we're in the editor.
            if (string.IsNullOrEmpty(shortName))
            {
                shortName = _defaultTemplate;
            }

            //Set current template index
            CurrentTemplateIndex = templatesModel.FindIndexOfTemplate(shortName);
            if (CurrentTemplateIndex == -1)
            {
                CurrentTemplateIndex = 0;
                shortName            = templatesModel[CurrentTemplateIndex].GetValue("shortName");
            }

            //If we have only one template then hide the next/prev buttons
            if (templatesModel.templateNodes.Count <ConfigNode>() == 1)
            {
                Events["NextType"].guiActive          = false;
                Events["NextType"].guiActiveEditor    = false;
                Events["NextType"].guiActiveUnfocused = false;
                Events["PrevType"].guiActive          = false;
                Events["PrevType"].guiActiveEditor    = false;
                Events["PrevType"].guiActiveUnfocused = false;
            }
            else if (templatesModel.templateNodes.Count <ConfigNode>() >= 2)
            {
                Events["NextType"].guiActive          = true;
                Events["NextType"].guiActiveEditor    = true;
                Events["NextType"].guiActiveUnfocused = true;
                Events["PrevType"].guiActive          = true;
                Events["PrevType"].guiActiveEditor    = true;
                Events["PrevType"].guiActiveUnfocused = true;
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        public override void OnLoad(ConfigNode node)
        {
            ConfigNode[] resourceNodes = node.GetNodes("RESOURCE");
            PartResource resource = null;
            string resourceName;
            string protoNodeKey;
            string myPartName = getMyPartName();
            ConfigNode protoNode = null;

            base.OnLoad(node);
            protoNodeKey = myPartName + this.moduleName;
            Log("OnLoad: " + myPartName + " " + node + " Scene: " + HighLogic.LoadedScene.ToString());

            //Watch for the editor attach event
            this.part.OnEditorAttach += OnEditorAttach;

            if (protoPartNodes.ContainsKey(protoNodeKey))
            {
                //Get the proto config node
                protoNode = protoPartNodes[protoNodeKey];

                //Name of the nodes to use as templates
                templateNodes = protoNode.GetValue("templateNodes");

                //Also get template types
                _templateTypes = protoNode.GetValue("templateTypes");
            }

            //Create the templatesModel
            templatesModel = new TemplatesModel(this.part, this.vessel, new LogDelegate(Log), templateNodes, _templateTypes);

            //If we have resources in our node then load them.
            if (resourceNodes != null)
            {
                //Clear any existing resources. We shouldn't have any...
                _templateResources.Clear();

                foreach (ConfigNode resourceNode in resourceNodes)
                {
                    resourceName = resourceNode.GetValue("name");
                    if (this.part.Resources.Contains(resourceName))
                    {
                        resource = this.part.Resources[resourceName];
                        if (isInflatable)
                        {
                            if (isDeployed)
                                resource.maxAmount = double.Parse(resourceNode.GetValue("maxAmount"));
                            else
                                resource.maxAmount = 1.0f;
                        }

                        else
                        {
                            resource.maxAmount = double.Parse(resourceNode.GetValue("maxAmount"));
                        }
                    }
                    else
                    {
                        resource = this.part.AddResource(resourceNode);
                    }

                    _templateResources.Add(resource);
                }
            }
        }
Ejemplo n.º 6
0
        public void initTemplates()
        {
            Log("initTemplates called");
            //Create templates object if needed.
            //This can happen when the object is cloned in the editor (On Load won't be called).
            if (templatesModel == null)
                templatesModel = new TemplatesModel(this.part, this.vessel, new LogDelegate(Log));
            templatesModel.templateNodeName = templateNodes;
            templatesModel.templateTypes = _templateTypes;

            if (templatesModel.templateNodes == null)
            {
                Log("OnStart templateNodes == null!");
                return;
            }

            //Set default template if needed
            //This will happen when we're in the editor.
            if (string.IsNullOrEmpty(shortName))
                shortName = _defaultTemplate;

            //Set current template index
            CurrentTemplateIndex = templatesModel.FindIndexOfTemplate(shortName);
            if (CurrentTemplateIndex == -1)
            {
                CurrentTemplateIndex = 0;
                shortName = templatesModel[CurrentTemplateIndex].GetValue("shortName");
            }

            //If we have only one template then hide the next/prev buttons
            if (templatesModel.templateNodes.Count<ConfigNode>() == 1)
            {
                Events["NextType"].guiActive = false;
                Events["NextType"].guiActiveEditor = false;
                Events["NextType"].guiActiveUnfocused = false;
                Events["PrevType"].guiActive = false;
                Events["PrevType"].guiActiveEditor = false;
                Events["PrevType"].guiActiveUnfocused = false;
            }
            else if (templatesModel.templateNodes.Count<ConfigNode>() >= 2)
            {
                Events["NextType"].guiActive = true;
                Events["NextType"].guiActiveEditor = true;
                Events["NextType"].guiActiveUnfocused = true;
                Events["PrevType"].guiActive = true;
                Events["PrevType"].guiActiveEditor = true;
                Events["PrevType"].guiActiveUnfocused = true;
            }
        }
Ejemplo n.º 7
0
        public override void OnLoad(ConfigNode node)
        {
            ConfigNode[] resourceNodes = node.GetNodes("RESOURCE");
            PartResource resource      = null;
            string       resourceName;
            string       protoNodeKey;
            string       myPartName = getMyPartName();
            ConfigNode   protoNode  = null;

            base.OnLoad(node);
            protoNodeKey = myPartName + this.moduleName;
            Log("OnLoad: " + myPartName + " " + node + " Scene: " + HighLogic.LoadedScene.ToString());

            //Watch for the editor attach event
            this.part.OnEditorAttach += OnEditorAttach;

            if (protoPartNodes.ContainsKey(protoNodeKey))
            {
                //Get the proto config node
                protoNode = protoPartNodes[protoNodeKey];

                //Name of the nodes to use as templates
                templateNodes = protoNode.GetValue("templateNodes");

                //Also get template types
                _templateTypes = protoNode.GetValue("templateTypes");
            }

            //Create the templatesModel
            templatesModel = new TemplatesModel(this.part, this.vessel, new LogDelegate(Log), templateNodes, _templateTypes);

            //If we have resources in our node then load them.
            if (resourceNodes != null)
            {
                //Clear any existing resources. We shouldn't have any...
                _templateResources.Clear();

                foreach (ConfigNode resourceNode in resourceNodes)
                {
                    resourceName = resourceNode.GetValue("name");
                    if (this.part.Resources.Contains(resourceName))
                    {
                        resource = this.part.Resources[resourceName];
                        if (isInflatable)
                        {
                            if (isDeployed)
                            {
                                resource.maxAmount = double.Parse(resourceNode.GetValue("maxAmount"));
                            }
                            else
                            {
                                resource.maxAmount = 1.0f;
                            }
                        }

                        else
                        {
                            resource.maxAmount = double.Parse(resourceNode.GetValue("maxAmount"));
                        }
                    }
                    else
                    {
                        resource = this.part.AddResource(resourceNode);
                    }

                    _templateResources.Add(resource);
                }
            }
        }