public ExperimentInfo(ScienceExperiment stockDef, ConfigNode expInfoNode)
        {
            // if we have a custom "KERBALISM_EXPERIMENT" definition for the experiment, load it, else just use an empty node to avoid nullrefs
            if (expInfoNode == null)
            {
                expInfoNode = new ConfigNode();
            }

            this.stockDef = stockDef;
            ExperimentId  = stockDef.id;

            // We have some custom handling for breaking ground ROC experiments
            IsROC = ExperimentId.StartsWith("ROCScience");

            if (IsROC)
            {
                Title = "ROC: " + stockDef.experimentTitle;                     // group ROC together in the science archive (sorted by Title)
            }
            else
            {
                Title = stockDef.experimentTitle;
            }

            // A new bool field was added in 1.7 for serenity : applyScienceScale
            // if not specified, the default is `true`, which is the case for all non-serenity science defs
            // serenity ground experiments and ROCs have applyScienceScale = false.
            // for ground experiment, baseValue = science generated per hour
            // for ROC experiments, it doesn't change anything because they are all configured with baseValue = scienceCap
            if (this.stockDef.applyScienceScale)
            {
                DataSize = this.stockDef.baseValue * this.stockDef.dataScale;
            }
            else
            {
                DataSize = this.stockDef.scienceCap * this.stockDef.dataScale;
            }

            // load the included experiments ids in a string array, we will populate the list after
            // all ExperimentInfos are created. (can't do it here as they may not exist yet)
            includedExperimentsId = expInfoNode.GetValues("IncludeExperiment");

            UnlockResourceSurvey = Lib.ConfigValue(expInfoNode, "UnlockResourceSurvey", false);
            SampleMass           = Lib.ConfigValue(expInfoNode, "SampleMass", 0.0);
            IsSample             = SampleMass > 0.0;
            if (IsSample)
            {
                // make sure we don't produce NaN values down the line because of odd/wrong configs
                if (DataSize <= 0.0)
                {
                    Lib.Log(ExperimentId + " has DataSize=" + DataSize + ", your configuration is broken!", Lib.LogLevel.Warning);
                    DataSize = 1.0;
                }
                MassPerMB = SampleMass / DataSize;
            }
            else
            {
                MassPerMB = 0.0;
            }

            // Patch stock science def restrictions as BodyAllowed/BodyNotAllowed restrictions
            if (!(expInfoNode.HasValue("BodyAllowed") || expInfoNode.HasValue("BodyNotAllowed")))
            {
                if (IsROC)
                {
                    // Parse the ROC definition name to find which body it's available on
                    // This rely on the ROC definitions having the body name in the ExperimentId
                    foreach (CelestialBody body in FlightGlobals.Bodies)
                    {
                        if (ExperimentId.IndexOf(body.name, StringComparison.OrdinalIgnoreCase) != -1)
                        {
                            expInfoNode.AddValue("BodyAllowed", body.name);
                            break;
                        }
                    }
                }

                // parse the stock atmosphere restrictions into our own
                if (stockDef.requireAtmosphere)
                {
                    expInfoNode.AddValue("BodyAllowed", "Atmospheric");
                }
                else if (stockDef.requireNoAtmosphere)
                {
                    expInfoNode.AddValue("BodyNotAllowed", "Atmospheric");
                }
            }

            ExpBodyConditions = new BodyConditions(expInfoNode);

            foreach (string virtualBiomeStr in expInfoNode.GetValues("VirtualBiome"))
            {
                if (Enum.IsDefined(typeof(VirtualBiome), virtualBiomeStr))
                {
                    VirtualBiomes.Add((VirtualBiome)Enum.Parse(typeof(VirtualBiome), virtualBiomeStr));
                }
                else
                {
                    Lib.Log("Experiment definition `{0}` has unknown VirtualBiome={1}", Lib.LogLevel.Warning, ExperimentId, virtualBiomeStr);
                }
            }

            IgnoreBodyRestrictions = Lib.ConfigValue(expInfoNode, "IgnoreBodyRestrictions", false);

            uint situationMask    = 0;
            uint biomeMask        = 0;
            uint virtualBiomeMask = 0;

            // if defined, override stock situation / biome mask
            if (expInfoNode.HasValue("Situation"))
            {
                foreach (string situation in expInfoNode.GetValues("Situation"))
                {
                    string[] sitAtBiome = situation.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
                    if (sitAtBiome.Length == 0 || sitAtBiome.Length > 2)
                    {
                        continue;
                    }

                    ScienceSituation scienceSituation = ScienceSituationUtils.ScienceSituationDeserialize(sitAtBiome[0]);

                    if (scienceSituation != ScienceSituation.None)
                    {
                        situationMask += scienceSituation.BitValue();

                        if (sitAtBiome.Length == 2)
                        {
                            if (sitAtBiome[1].Equals("Biomes", StringComparison.OrdinalIgnoreCase))
                            {
                                biomeMask += scienceSituation.BitValue();
                            }
                            else if (sitAtBiome[1].Equals("VirtualBiomes", StringComparison.OrdinalIgnoreCase) && VirtualBiomes.Count > 0)
                            {
                                virtualBiomeMask += scienceSituation.BitValue();
                            }
                        }
                    }
                    else
                    {
                        Lib.Log("Experiment definition `{0}` has unknown situation : `{1}`", Lib.LogLevel.Warning, ExperimentId, sitAtBiome[0]);
                    }
                }
            }
            else
            {
                situationMask = stockDef.situationMask;
                biomeMask     = stockDef.biomeMask;
            }

            if (situationMask == 0)
            {
                Lib.Log("Experiment definition `{0}` : `0` situationMask is unsupported, patching to `BodyGlobal`", Lib.LogLevel.Message, ExperimentId);
                situationMask = ScienceSituation.BodyGlobal.BitValue();
                HasDBSubjects = false;
            }
            else
            {
                HasDBSubjects = !Lib.ConfigValue(expInfoNode, "IsGeneratingSubjects", false);
            }

            string error;
            uint   stockSituationMask;
            uint   stockBiomeMask;

            if (!ScienceSituationUtils.ValidateSituationBitMask(ref situationMask, biomeMask, out stockSituationMask, out stockBiomeMask, out error))
            {
                Lib.Log("Experiment definition `{0}` is incorrect :\n{1}", Lib.LogLevel.Error, ExperimentId, error);
            }

            SituationMask          = situationMask;
            BiomeMask              = biomeMask;
            VirtualBiomeMask       = virtualBiomeMask;
            stockDef.situationMask = stockSituationMask;
            stockDef.biomeMask     = stockBiomeMask;
        }
Example #2
0
        /// <summary>
        /// Create our SubjectData by parsing the stock "experiment@situation" subject id string.
        /// Used for asteroid samples, for compatibility with RnD archive data of removed mods and for converting stock ScienceData into SubjectData
        /// </summary>
        public static SubjectData GetSubjectDataFromStockId(string stockSubjectId, ScienceSubject RnDSubject = null)
        {
            SubjectData subjectData = null;

            if (unknownSubjectDatas.TryGetValue(stockSubjectId, out subjectData))
            {
                return(subjectData);
            }

            string[] expAndSit = stockSubjectId.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);

            if (expAndSit.Length != 2)
            {
                Lib.Log("Could not parse the SubjectData from subjectId '" + stockSubjectId + "' : bad format");
                return(null);
            }

            // the recovery experiment subject are created in ResearchAndDevelopment.reverseEngineerRecoveredVessel, called on the vessel recovery event
            // it use a non-standard situation system ("body" + a situation enum "RecoverySituations"). We ignore those.
            if (expAndSit[0] == "recovery")
            {
                return(null);
            }

            ExperimentInfo expInfo = GetExperimentInfo(expAndSit[0]);

            if (expInfo == null)
            {
                Lib.Log("Could not parse the SubjectData from subjectId '" + stockSubjectId + "' : the experiment id '" + expAndSit[0] + "' doesn't exists");
                return(null);
            }

            // for subject ids created with the ResearchAndDevelopment.GetExperimentSubject overload that take a "sourceUId" string,
            // the sourceUId is added to the situation after a "_"
            // in stock this seems to be used only for asteroids, and I don't think any mod use it.
            string extraSituationInfo = string.Empty;

            if (expAndSit[1].Contains("_"))
            {
                string[] sitAndAsteroid = expAndSit[1].Split('_');
                // remove
                expAndSit[1] = sitAndAsteroid[0];
                // asteroid are saved as "part.partInfo.name + part.flightID", and the part name always end with a randomly generated "AAA-000" string
                extraSituationInfo = Regex.Match(sitAndAsteroid[1], ".*?-[0-9][0-9][0-9]").Value;
                // if no match, just use the unmodified string
                if (extraSituationInfo == string.Empty)
                {
                    extraSituationInfo = sitAndAsteroid[1];
                }
            }

            string[] bodyAndBiome = expAndSit[1].Split(ScienceSituationUtils.validSituationsStrings, StringSplitOptions.RemoveEmptyEntries);
            string   situation;

            if (bodyAndBiome.Length == 1)
            {
                situation = expAndSit[1].Substring(bodyAndBiome[0].Length);
            }
            else if (bodyAndBiome.Length == 2)
            {
                situation = expAndSit[1].Substring(bodyAndBiome[0].Length, expAndSit[1].Length - bodyAndBiome[0].Length - bodyAndBiome[1].Length);
            }
            else
            {
                Lib.Log("Could not parse the SubjectData from subjectId '" + stockSubjectId + "' : the situation doesn't exists");
                return(null);
            }

            CelestialBody subjectBody = null;

            foreach (CelestialBody body in FlightGlobals.Bodies)
            {
                if (body.name == bodyAndBiome[0])
                {
                    subjectBody = body;
                    break;
                }
            }

            if (subjectBody == null)
            {
                // TODO : DMOS asteroid experiments are doing : "magScan@AsteroidInSpaceLowCarbonaceous7051371", those subjects will be discarded entirely here
                // because the body "Asteroid" doesn't exists, consequently it's impossible to create the Situation object.
                // To handle that, maybe we could implement a derived class "UnknownSituation" from Situation that can handle a completely random subject format
                Lib.Log("Could not parse the SubjectData from subjectId '" + stockSubjectId + "' : the body '" + bodyAndBiome[0] + "' doesn't exist");
                return(null);
            }

            ScienceSituation scienceSituation = ScienceSituationUtils.ScienceSituationDeserialize(situation);

            int biomeIndex = -1;

            if (bodyAndBiome.Length == 2 && ScienceSituationUtils.IsBodyBiomesRelevantForExperiment(scienceSituation, expInfo) && subjectBody.BiomeMap != null)
            {
                for (int i = 0; i < subjectBody.BiomeMap.Attributes.Length; i++)
                {
                    // Note : a stock subject has its spaces in the biome name removed but prior versions of kerbalism didn't do that,
                    // so we try to fix it, in order not to create duplicates in the RnD archives.

                    // TODO : also, we need to remove the "reentry" subjects, as stock is failing to parse them, altough this is in a try/catch block and handled gracefully.
                    string sanitizedBiome = bodyAndBiome[1].Replace(" ", string.Empty);
                    if (RnDSubject != null && extraSituationInfo == string.Empty && sanitizedBiome != bodyAndBiome[1])
                    {
                        string correctedSubjectId = expAndSit[0] + "@" + bodyAndBiome[0] + situation + sanitizedBiome;
                        RnDSubject.id = correctedSubjectId;

                        Dictionary <string, ScienceSubject> stockSubjects = Lib.ReflectionValue <Dictionary <string, ScienceSubject> >(ResearchAndDevelopment.Instance, "scienceSubjects");
                        if (stockSubjects.Remove(stockSubjectId) && !stockSubjects.ContainsKey(correctedSubjectId))
                        {
                            stockSubjects.Add(correctedSubjectId, RnDSubject);
                        }

                        Lib.Log("RnD subject load : misformatted subject '" + stockSubjectId + "' was corrected to '" + correctedSubjectId + "'");
                    }

                    if (subjectBody.BiomeMap.Attributes[i].name.Replace(" ", string.Empty).Equals(sanitizedBiome, StringComparison.OrdinalIgnoreCase))
                    {
                        biomeIndex = i;
                        break;
                    }
                }
            }

            int       bodyIndex       = subjectBody.flightGlobalsIndex;
            Situation vesselSituation = new Situation(bodyIndex, scienceSituation, biomeIndex);

            // if the subject is a "doable" subject, we should have it in the DB.
            if (extraSituationInfo == string.Empty)
            {
                subjectData = GetSubjectData(expInfo, vesselSituation);
            }

            // else create the subjectdata. this can happen either because :
            // - it's a subject using the stock "extra id" system (asteroid samples)
            // - the subject was created in RnD prior to an experiment definition config change
            // - it was created by a mod that does things in a non-stock way (ex : DMOS anomaly scans uses the anomaly name as biomes)
            if (subjectData == null)
            {
                if (bodyAndBiome.Length == 2 && bodyAndBiome[1] != string.Empty && string.IsNullOrEmpty(extraSituationInfo))
                {
                    extraSituationInfo = bodyAndBiome[1];
                }

                UnknownSubjectData unknownSubjectData = new UnknownSubjectData(expInfo, vesselSituation, stockSubjectId, RnDSubject, extraSituationInfo);
                subjectData = unknownSubjectData;
                unknownSubjectDatas.Add(stockSubjectId, unknownSubjectData);
                expBodiesSituationsBiomesSubject.AddSubject(subjectData.ExpInfo, bodyIndex, scienceSituation, biomeIndex, subjectData);
            }

            return(subjectData);
        }