示例#1
0
        /// <summary>
        /// Store fitted engine parameters in the database so they can be accessed later
        /// </summary>
        /// <param name="engine">Engine to associated this config node with</param>
        /// <param name="node">Config node describing engine parameters (both input parameters and fitted parameters)</param>
        public static void SetNodeForEngine(ModuleEnginesSolver engine, ConfigNode node)
        {
            string partName   = engine.part.name;
            string engineType = engine.GetType().Name;
            string engineID   = engine.engineID;

            Assembly assembly = engine.GetType().Assembly;

            node.SetValue("engineID", engineID, true);
            node.SetValue("DeclaringAssemblyVersion", EngineDatabase.AssemblyVersion(assembly), true);
            node.SetValue("DeclaringAssemblyChecksum", EngineDatabase.AssemblyChecksum(assembly), true);
            node.SetValue("SolverEnginesVersion", SolverEnginesVersion, true);
            node.SetValue("SolverEnginesAssemblyChecksum", SolverEnginesAssemblyChecksum, true);

            ConfigNode partNode  = database.GetNode(partName);
            int        nodeIndex = 0;

            if (partNode != null)
            {
                ConfigNode[] moduleNodes = partNode.GetNodes(engineType);
                for (int i = 0; i < moduleNodes.Length; i++)
                {
                    ConfigNode mNode = moduleNodes[i];
                    if (mNode.GetValue("engineID") == engineID)
                    {
                        nodeIndex = i;
                    }
                }
            }
            else
            {
                partNode = new ConfigNode(partName);
                database.AddNode(partNode);
                nodeIndex = 0;
            }

            partNode.SetNode(engineType, node, nodeIndex, true);

            SaveDatabase();
        }
        virtual public void FitEngineIfNecessary()
        {
            bool doFit = false;

            foreach (EngineParameterInfo entry in engineFitParameters)
            {
                if (ShouldFitParameter(entry))
                {
                    doFit = true;
                    break;
                }
            }

            // No parameters can be fit
            if (!doFit)
            {
                return;
            }

            doFit = false;

            ConfigNode node = EngineDatabase.GetNodeForEngine(this);

            if (node != null)
            {
                doFit |= EngineDatabase.PluginUpdateCheck(this, node);

                // Check for changes
                foreach (EngineParameterInfo entry in engineFitParameters)
                {
                    // Don't check things we're going to fit
                    if (ShouldFitParameter(entry))
                    {
                        continue;
                    }

                    if (!entry.EqualsValueInNode(node))
                    {
                        doFit = true;
                        break;
                    }
                }
                if (!doFit && node != null)
                {
                    Debug.Log("[" + this.GetType().Name + "] Reading engine params from cache for engine " + part.name);

                    CreateEngineIfNecessary();

                    foreach (EngineParameterInfo entry in engineFitParameters)
                    {
                        // Only copy things that would be fitted
                        if (ShouldFitParameter(entry))
                        {
                            entry.SetValueFromNode(node);
                        }
                    }
                    PushFitParamsToSolver();
                }
            }
            else
            {
                doFit = true;
            }

            if (doFit)
            {
                Debug.Log("[" + this.GetType().Name + "] Fitting params for engine " + part.name);

                CreateEngineIfNecessary();

                // Copy valid fit results from database - they might still be correct
                if (node != null)
                {
                    foreach (EngineParameterInfo entry in engineFitParameters)
                    {
                        // Only copy things that would be fitted
                        if (ShouldFitParameter(entry))
                        {
                            entry.SetValueFromNode(node);
                        }
                    }
                }

                // Make sure everything has the correct value
                PushFitParamsToSolver();
                DoEngineFit();

                ConfigNode newNode = new ConfigNode();

                foreach (EngineParameterInfo entry in engineFitParameters)
                {
                    newNode.SetValue(entry.Name, entry.GetValueStr(), true);
                }

                EngineDatabase.SetNodeForEngine(this, newNode);
            }
        }