コード例 #1
0
        public CustomPawn Load(PrepareCarefully loadout, string name)
        {
            SaveRecordPawnV3 pawnRecord = new SaveRecordPawnV3();
            string           modString  = "";
            string           version    = "";

            try {
                Scribe.loader.InitLoading(ColonistFiles.FilePathForSavedColonist(name));
                Scribe_Values.Look <string>(ref version, "version", "unknown", false);
                Scribe_Values.Look <string>(ref modString, "mods", "", false);

                try {
                    Scribe_Deep.Look <SaveRecordPawnV3>(ref pawnRecord, "colonist", null);
                }
                catch (Exception e) {
                    Messages.Message(modString, MessageTypeDefOf.SilentInput);
                    Messages.Message("EdB.PC.Dialog.PawnPreset.Error.Failed".Translate(), MessageTypeDefOf.RejectInput);
                    Log.Warning(e.ToString());
                    Log.Warning("Colonist was created with the following mods: " + modString);
                    return(null);
                }
            }
            catch (Exception e) {
                Log.Error("Failed to load preset file");
                throw e;
            }
            finally {
                PresetLoader.ClearSaveablesAndCrossRefs();
            }

            if (pawnRecord == null)
            {
                Messages.Message(modString, MessageTypeDefOf.SilentInput);
                Messages.Message("EdB.PC.Dialog.PawnPreset.Error.Failed".Translate(), MessageTypeDefOf.RejectInput);
                Log.Warning("Colonist was created with the following mods: " + modString);
                return(null);
            }

            PresetLoaderVersion3 loader     = new PresetLoaderVersion3();
            CustomPawn           loadedPawn = loader.LoadPawn(pawnRecord);

            if (loadedPawn != null)
            {
                CustomPawn idConflictPawn = PrepareCarefully.Instance.Pawns.FirstOrDefault((CustomPawn p) => {
                    return(p.Id == loadedPawn.Id);
                });
                if (idConflictPawn != null)
                {
                    loadedPawn.GenerateId();
                }
                return(loadedPawn);
            }
            else
            {
                loadout.State.AddError(loader.ModString);
                loadout.State.AddError("EdB.PC.Dialog.Preset.Error.NoCharacter".Translate());
                Log.Warning("Preset was created with the following mods: " + modString);
                return(null);
            }
        }
コード例 #2
0
        //
        // Static Methods
        //
        public static void SaveToFile(PrepareCarefully data, string presetName)
        {
            try {
                Scribe.InitWriting(PresetFiles.FilePathForSavedPreset(presetName), "preset");
                string versionStringFull = "3";
                Scribe_Values.LookValue <string>(ref versionStringFull, "version", null, false);
                bool usePoints      = data.Config.pointsEnabled;
                int  startingPoints = PrepareCarefully.Instance.StartingPoints;
                Scribe_Values.LookValue <bool>(ref usePoints, "usePoints", false, true);
                Scribe_Values.LookValue <int>(ref startingPoints, "startingPoints", 0, true);
                string modString = GenText.ToCommaList(Enumerable.Select <ModContentPack, string>(LoadedModManager.RunningMods, (Func <ModContentPack, string>)(mod => mod.Name)), true);
                Scribe_Values.LookValue <string>(ref modString, "mods", null, false);
                Scribe.EnterNode("colonists");
                foreach (CustomPawn customPawn in data.Pawns)
                {
                    SaveRecordPawnV3 pawn = new SaveRecordPawnV3(customPawn);
                    Scribe_Deep.LookDeep <SaveRecordPawnV3>(ref pawn, "colonist");
                }
                Scribe.ExitNode();

                Scribe.EnterNode("relationships");
                foreach (var r in data.RelationshipManager.ExplicitRelationships)
                {
                    SaveRecordRelationshipV3 s = new SaveRecordRelationshipV3(r);
                    Scribe_Deep.LookDeep <SaveRecordRelationshipV3>(ref s, "relationship");
                }
                Scribe.ExitNode();

                Scribe.EnterNode("equipment");
                foreach (var e in data.Equipment)
                {
                    SelectedEquipment customPawn = e;
                    Scribe_Deep.LookDeep <SelectedEquipment>(ref customPawn, "equipment");
                }
                Scribe.ExitNode();
            }
            catch (Exception e) {
                Log.Error("Failed to save preset file");
                throw e;
            }
            finally {
                Scribe.FinalizeWriting();
                Scribe.mode = LoadSaveMode.Inactive;
            }
        }
コード例 #3
0
        public bool Load(PrepareCarefully loadout, Page_ConfigureStartingPawnsCarefully charMakerPage, string colonistName)
        {
            SaveRecordPawnV3 pawnRecord = new SaveRecordPawnV3();
            string           modString  = "";
            string           version    = "";

            try {
                Scribe.InitLoading(ColonistFiles.FilePathForSavedColonist(colonistName));
                Scribe_Values.LookValue <string>(ref version, "version", "unknown", false);
                Scribe_Values.LookValue <string>(ref modString, "mods", "", false);

                try {
                    Scribe_Deep.LookDeep <SaveRecordPawnV3>(ref pawnRecord, "colonist", null);
                }
                catch (Exception e) {
                    Messages.Message(modString, MessageSound.Silent);
                    Messages.Message("EdB.ColonistLoadFailed".Translate(), MessageSound.RejectInput);
                    Log.Warning(e.ToString());
                    Log.Warning("Colonist was created with the following mods: " + modString);
                    return(false);
                }
            }
            catch (Exception e) {
                Log.Error("Failed to load preset file");
                throw e;
            }
            finally {
                Scribe.mode = LoadSaveMode.Inactive;
            }

            PresetLoaderVersion3 loader = new PresetLoaderVersion3();

            charMakerPage.AddColonist(loader.LoadPawn(pawnRecord));
            if (loader.Failed)
            {
                Messages.Message(loader.ModString, MessageSound.Silent);
                Messages.Message("EdB.ColonistThingDefFailed".Translate(), MessageSound.SeriousAlert);
                Log.Warning("Preset was created with the following mods: " + modString);
                return(false);
            }

            return(true);
        }
コード例 #4
0
        //
        // Static Methods
        //
        public static void SaveToFile(PrepareCarefully loadout, Page_ConfigureStartingPawnsCarefully page, string colonistName)
        {
            try {
                Scribe.InitWriting(ColonistFiles.FilePathForSavedColonist(colonistName), "colonist");
                string versionStringFull = "3";
                Scribe_Values.LookValue <string>(ref versionStringFull, "version", null, false);
                string modString = GenText.ToCommaList(Enumerable.Select <ModContentPack, string>(LoadedModManager.RunningMods, (Func <ModContentPack, string>)(mod => mod.Name)), true);
                Scribe_Values.LookValue <string>(ref modString, "mods", null, false);

                SaveRecordPawnV3 pawn = new SaveRecordPawnV3(page.SelectedPawn);
                Scribe_Deep.LookDeep <SaveRecordPawnV3>(ref pawn, "colonist");
            }
            catch (Exception e) {
                Log.Error("Failed to save preset file");
                throw e;
            }
            finally {
                Scribe.FinalizeWriting();
                Scribe.mode = LoadSaveMode.Inactive;
            }
        }
コード例 #5
0
        public CustomPawn LoadPawn(SaveRecordPawnV3 record)
        {
            PawnKindDef pawnKindDef = null;

            if (record.pawnKindDef != null)
            {
                pawnKindDef = DefDatabase <PawnKindDef> .GetNamedSilentFail(record.pawnKindDef);

                if (pawnKindDef == null)
                {
                    Log.Warning("Prepare Carefully could not find the pawn kind definition for the saved character: \"" + record.pawnKindDef + "\"");
                    return(null);
                }
            }

            ThingDef pawnThingDef = ThingDefOf.Human;

            if (record.thingDef != null)
            {
                ThingDef thingDef = DefDatabase <ThingDef> .GetNamedSilentFail(record.thingDef);

                if (thingDef != null)
                {
                    pawnThingDef = thingDef;
                }
            }

            Pawn source;

            if (pawnKindDef != null)
            {
                source = new Randomizer().GenerateKindOfColonist(pawnKindDef);
            }
            else
            {
                source = new Randomizer().GenerateColonist();
            }
            source.health.Reset();

            CustomPawn pawn = new CustomPawn(source);

            if (pawn.Id == null)
            {
                pawn.GenerateId();
            }
            else
            {
                pawn.Id = record.id;
            }

            pawn.Type = CustomPawnType.Colonist;

            pawn.Gender = record.gender;
            if (record.age > 0)
            {
                pawn.ChronologicalAge = record.age;
                pawn.BiologicalAge    = record.age;
            }
            if (record.chronologicalAge > 0)
            {
                pawn.ChronologicalAge = record.chronologicalAge;
            }
            if (record.biologicalAge > 0)
            {
                pawn.BiologicalAge = record.biologicalAge;
            }

            pawn.FirstName = record.firstName;
            pawn.NickName  = record.nickName;
            pawn.LastName  = record.lastName;

            HairDef h = FindHairDef(record.hairDef);

            if (h != null)
            {
                pawn.HairDef = h;
            }
            else
            {
                Log.Warning("Could not load hair definition \"" + record.hairDef + "\"");
                Failed = true;
            }

            pawn.HeadGraphicPath      = record.headGraphicPath;
            pawn.Pawn.story.hairColor = record.hairColor;

            if (record.melanin >= 0.0f)
            {
                pawn.MelaninLevel = record.melanin;
            }
            else
            {
                pawn.MelaninLevel = PawnColorUtils.FindMelaninValueFromColor(record.skinColor);
            }
            // Set the skin color (only for Alien Races).
            if (pawn.AlienRace != null)
            {
                pawn.SkinColor = record.skinColor;
            }

            Backstory backstory = FindBackstory(record.childhood);

            if (backstory != null)
            {
                pawn.Childhood = backstory;
            }
            else
            {
                Log.Warning("Could not load childhood backstory definition \"" + record.childhood + "\"");
                Failed = true;
            }
            if (record.adulthood != null)
            {
                backstory = FindBackstory(record.adulthood);
                if (backstory != null)
                {
                    pawn.Adulthood = backstory;
                }
                else
                {
                    Log.Warning("Could not load adulthood backstory definition \"" + record.adulthood + "\"");
                    Failed = true;
                }
            }

            // Get the body type from the save record.  If there's no value in the save, then assign the
            // default body type from the pawn's backstories.
            // TODO: 1.0

            /*
             * BodyType? bodyType = null;
             * try {
             *  bodyType = (BodyType)Enum.Parse(typeof(BodyType), record.bodyType);
             * }
             * catch (Exception) {
             * }
             * if (!bodyType.HasValue) {
             *  if (pawn.Adulthood != null) {
             *      bodyType = pawn.Adulthood.BodyTypeFor(pawn.Gender);
             *  }
             *  else {
             *      bodyType = pawn.Childhood.BodyTypeFor(pawn.Gender);
             *  }
             * }
             * if (bodyType.HasValue) {
             *  pawn.BodyType = bodyType.Value;
             * }
             */
            BodyTypeDef bodyType = null;

            try {
                bodyType = DefDatabase <BodyTypeDef> .GetNamedSilentFail(record.bodyType);
            }
            catch (Exception) {
            }
            if (bodyType == null)
            {
                if (pawn.Adulthood != null)
                {
                    bodyType = pawn.Adulthood.BodyTypeFor(pawn.Gender);
                }
                else
                {
                    bodyType = pawn.Childhood.BodyTypeFor(pawn.Gender);
                }
            }
            if (bodyType != null)
            {
                pawn.BodyType = bodyType;
            }

            pawn.ClearTraits();
            for (int i = 0; i < record.traitNames.Count; i++)
            {
                string traitName = record.traitNames[i];
                Trait  trait     = FindTrait(traitName, record.traitDegrees[i]);
                if (trait != null)
                {
                    pawn.AddTrait(trait);
                }
                else
                {
                    Log.Warning("Could not load trait definition \"" + traitName + "\"");
                    Failed = true;
                }
            }

            for (int i = 0; i < record.skillNames.Count; i++)
            {
                string   name = record.skillNames[i];
                SkillDef def  = FindSkillDef(pawn.Pawn, name);
                if (def == null)
                {
                    Log.Warning("Could not load skill definition \"" + name + "\"");
                    Failed = true;
                    continue;
                }
                pawn.currentPassions[def]  = record.passions[i];
                pawn.originalPassions[def] = record.passions[i];
                pawn.SetOriginalSkillLevel(def, record.skillValues[i]);
                pawn.SetUnmodifiedSkillLevel(def, record.skillValues[i]);
            }
            if (record.originalPassions != null && record.originalPassions.Count == record.skillNames.Count)
            {
                for (int i = 0; i < record.skillNames.Count; i++)
                {
                    string   name = record.skillNames[i];
                    SkillDef def  = FindSkillDef(pawn.Pawn, name);
                    if (def == null)
                    {
                        Log.Warning("Could not load skill definition \"" + name + "\"");
                        Failed = true;
                        continue;
                    }
                    //pawn.originalPassions[def] = record.originalPassions[i];
                }
            }

            foreach (var layer in PrepareCarefully.Instance.Providers.PawnLayers.GetLayersForPawn(pawn))
            {
                if (layer.Apparel)
                {
                    pawn.SetSelectedApparel(layer, null);
                    pawn.SetSelectedStuff(layer, null);
                }
            }
            for (int i = 0; i < record.apparelLayers.Count; i++)
            {
                int       layerIndex = record.apparelLayers[i];
                PawnLayer layer      = PrepareCarefully.Instance.Providers.PawnLayers.FindLayerFromDeprecatedIndex(layerIndex);
                if (layer == null)
                {
                    Log.Warning("Could not find pawn layer from saved pawn layer index: \"" + layerIndex + "\"");
                    Failed = true;
                    continue;
                }
                ThingDef def = DefDatabase <ThingDef> .GetNamedSilentFail(record.apparel[i]);

                if (def == null)
                {
                    Log.Warning("Could not load thing definition for apparel \"" + record.apparel[i] + "\"");
                    Failed = true;
                    continue;
                }
                ThingDef stuffDef = null;
                if (!string.IsNullOrEmpty(record.apparelStuff[i]))
                {
                    stuffDef = DefDatabase <ThingDef> .GetNamedSilentFail(record.apparelStuff[i]);

                    if (stuffDef == null)
                    {
                        Log.Warning("Could not load stuff definition \"" + record.apparelStuff[i] + "\" for apparel \"" + record.apparel[i] + "\"");
                        Failed = true;
                        continue;
                    }
                }
                pawn.SetSelectedApparel(layer, def);
                pawn.SetSelectedStuff(layer, stuffDef);
                pawn.SetColor(layer, record.apparelColors[i]);
            }

            OptionsHealth healthOptions = PrepareCarefully.Instance.Providers.Health.GetOptions(pawn);

            for (int i = 0; i < record.implants.Count; i++)
            {
                SaveRecordImplantV3 implantRecord  = record.implants[i];
                UniqueBodyPart      uniqueBodyPart = healthOptions.FindBodyPartByName(implantRecord.bodyPart, implantRecord.bodyPartIndex != null ? implantRecord.bodyPartIndex.Value : 0);
                if (uniqueBodyPart == null)
                {
                    uniqueBodyPart = FindReplacementBodyPart(healthOptions, implantRecord.bodyPart);
                }
                if (uniqueBodyPart == null)
                {
                    Log.Warning("Prepare Carefully could not add the implant because it could not find the needed body part \"" + implantRecord.bodyPart + "\""
                                + (implantRecord.bodyPartIndex != null ? " with index " + implantRecord.bodyPartIndex : ""));
                    Failed = true;
                    continue;
                }
                BodyPartRecord bodyPart = uniqueBodyPart.Record;
                if (implantRecord.recipe != null)
                {
                    RecipeDef recipeDef = FindRecipeDef(implantRecord.recipe);
                    if (recipeDef == null)
                    {
                        Log.Warning("Prepare Carefully could not add the implant because it could not find the recipe definition \"" + implantRecord.recipe + "\"");
                        Failed = true;
                        continue;
                    }
                    bool found = false;
                    foreach (var p in recipeDef.appliedOnFixedBodyParts)
                    {
                        if (p.defName.Equals(bodyPart.def.defName))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        Log.Warning("Prepare carefully could not apply the saved implant recipe \"" + implantRecord.recipe + "\" to the body part \"" + bodyPart.def.defName + "\".  Recipe does not support that part.");
                        Failed = true;
                        continue;
                    }
                    Implant implant = new Implant();
                    implant.BodyPartRecord = bodyPart;
                    implant.recipe         = recipeDef;
                    implant.label          = implant.Label;
                    pawn.AddImplant(implant);
                }
            }

            foreach (var injuryRecord in record.injuries)
            {
                HediffDef def = DefDatabase <HediffDef> .GetNamedSilentFail(injuryRecord.hediffDef);

                if (def == null)
                {
                    Log.Warning("Prepare Carefully could not add the injury because it could not find the hediff definition \"" + injuryRecord.hediffDef + "\"");
                    Failed = true;
                    continue;
                }
                InjuryOption option = healthOptions.FindInjuryOptionByHediffDef(def);
                if (option == null)
                {
                    Log.Warning("Prepare Carefully could not add the injury because it could not find a matching injury option for the saved hediff \"" + injuryRecord.hediffDef + "\"");
                    Failed = true;
                    continue;
                }
                BodyPartRecord bodyPart = null;
                if (injuryRecord.bodyPart != null)
                {
                    UniqueBodyPart uniquePart = healthOptions.FindBodyPartByName(injuryRecord.bodyPart,
                                                                                 injuryRecord.bodyPartIndex != null ? injuryRecord.bodyPartIndex.Value : 0);
                    if (uniquePart == null)
                    {
                        uniquePart = FindReplacementBodyPart(healthOptions, injuryRecord.bodyPart);
                    }
                    if (uniquePart == null)
                    {
                        Log.Warning("Prepare Carefully could not add the injury because it could not find the needed body part \"" + injuryRecord.bodyPart + "\""
                                    + (injuryRecord.bodyPartIndex != null ? " with index " + injuryRecord.bodyPartIndex : ""));
                        Failed = true;
                        continue;
                    }
                    bodyPart = uniquePart.Record;
                }
                Injury injury = new Injury();
                injury.Option         = option;
                injury.BodyPartRecord = bodyPart;
                if (injuryRecord.severity != null)
                {
                    injury.Severity = injuryRecord.Severity;
                }
                if (injuryRecord.painFactor != null)
                {
                    injury.PainFactor = injuryRecord.PainFactor;
                }
                pawn.AddInjury(injury);
            }

            pawn.CopySkillsAndPassionsToPawn();
            pawn.ClearPawnCaches();

            return(pawn);
        }
コード例 #6
0
        public CustomPawn Load(PrepareCarefully loadout, string name)
        {
            SaveRecordPawnV3 pawnRecord = new SaveRecordPawnV3();
            string           modString  = "";
            string           version    = "";

            try {
                Scribe.loader.InitLoading(ColonistFiles.FilePathForSavedColonist(name));
                Scribe_Values.Look <string>(ref version, "version", "unknown", false);
                Scribe_Values.Look <string>(ref modString, "mods", "", false);

                try {
                    Scribe_Deep.Look <SaveRecordPawnV3>(ref pawnRecord, "colonist", null);
                }
                catch (Exception e) {
                    Messages.Message(modString, MessageTypeDefOf.SilentInput);
                    Messages.Message("EdB.PC.Dialog.PawnPreset.Error.Failed".Translate(), MessageTypeDefOf.RejectInput);
                    Log.Warning(e.ToString());
                    Log.Warning("Colonist was created with the following mods: " + modString);
                    return(null);
                }
            }
            catch (Exception e) {
                Log.Error("Failed to load preset file");
                throw e;
            }
            finally {
                // I don't fully understand how these cross-references and saveables are resolved, but
                // if we don't clear them out, we get null pointer exceptions.
                HashSet <IExposable> saveables = (HashSet <IExposable>)(typeof(PostLoadIniter).GetField("saveablesToPostLoad", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Scribe.loader.initer));
                saveables.Clear();
                List <IExposable> crossReferencingExposables = (List <IExposable>)(typeof(CrossRefHandler).GetField("crossReferencingExposables", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Scribe.loader.crossRefs));
                crossReferencingExposables.Clear();
                Scribe.loader.FinalizeLoading();
            }

            if (pawnRecord == null)
            {
                Messages.Message(modString, MessageTypeDefOf.SilentInput);
                Messages.Message("EdB.PC.Dialog.PawnPreset.Error.Failed".Translate(), MessageTypeDefOf.RejectInput);
                Log.Warning("Colonist was created with the following mods: " + modString);
                return(null);
            }

            PresetLoaderVersion3 loader     = new PresetLoaderVersion3();
            CustomPawn           loadedPawn = loader.LoadPawn(pawnRecord);

            if (loadedPawn != null)
            {
                CustomPawn idConflictPawn = PrepareCarefully.Instance.Pawns.FirstOrDefault((CustomPawn p) => {
                    return(p.Id == loadedPawn.Id);
                });
                if (idConflictPawn != null)
                {
                    loadedPawn.GenerateId();
                }
                return(loadedPawn);
            }
            else
            {
                loadout.State.AddError(loader.ModString);
                loadout.State.AddError("EdB.PC.Dialog.Preset.Error.NoCharacter".Translate());
                Log.Warning("Preset was created with the following mods: " + modString);
                return(null);
            }
        }
コード例 #7
0
        public CustomPawn LoadPawn(SaveRecordPawnV3 record)
        {
            // TODO: Ahlpa 14 Evaluate
            Pawn source = new Randomizer().GenerateColonist();
            //Pawn source = PawnGenerator.GeneratePawn(PawnKindDefOf.Colonist, Faction.OfPlayer);

            CustomPawn pawn = new CustomPawn(source);

            pawn.Gender = record.gender;
            if (record.age > 0)
            {
                pawn.ChronologicalAge = record.age;
                pawn.BiologicalAge    = record.age;
            }
            if (record.chronologicalAge > 0)
            {
                pawn.ChronologicalAge = record.chronologicalAge;
            }
            if (record.biologicalAge > 0)
            {
                pawn.BiologicalAge = record.biologicalAge;
            }

            pawn.FirstName = record.firstName;
            pawn.NickName  = record.nickName;
            pawn.LastName  = record.lastName;

            HairDef h = FindHairDef(record.hairDef);

            if (h != null)
            {
                pawn.HairDef = h;
            }
            else
            {
                Log.Warning("Could not load hair definition \"" + record.hairDef + "\"");
                Failed = true;
            }

            pawn.HeadGraphicPath = record.headGraphicPath;
            pawn.SetColor(PawnLayers.Hair, record.hairColor);
            pawn.SetColor(PawnLayers.HeadType, record.skinColor);
            Backstory backstory = FindBackstory(record.childhood);

            if (backstory != null)
            {
                pawn.Childhood = backstory;
            }
            else
            {
                Log.Warning("Could not load childhood backstory definition \"" + record.childhood + "\"");
                Failed = true;
            }
            backstory = FindBackstory(record.adulthood);
            if (backstory != null)
            {
                pawn.Adulthood = backstory;
            }
            else
            {
                Log.Warning("Could not load adulthood backstory definition \"" + record.adulthood + "\"");
                Failed = true;
            }

            int traitCount = pawn.Traits.Count();

            for (int i = 0; i < traitCount; i++)
            {
                pawn.ClearTrait(i);
            }
            for (int i = 0; i < record.traitNames.Count; i++)
            {
                string traitName = record.traitNames[i];
                if (i >= traitCount)
                {
                    break;
                }
                Trait trait = FindTrait(traitName, record.traitDegrees[i]);
                if (trait != null)
                {
                    pawn.SetTrait(i, trait);
                }
                else
                {
                    Log.Warning("Could not load trait definition \"" + traitName + "\"");
                    Failed = true;
                }
            }

            for (int i = 0; i < record.skillNames.Count; i++)
            {
                string   name = record.skillNames[i];
                SkillDef def  = FindSkillDef(pawn.Pawn, name);
                if (def == null)
                {
                    Log.Warning("Could not load skill definition \"" + name + "\"");
                    Failed = true;
                    continue;
                }
                pawn.currentPassions[def]  = record.passions[i];
                pawn.originalPassions[def] = record.passions[i];
                pawn.SetOriginalSkillLevel(def, record.skillValues[i]);
                pawn.SetUnmodifiedSkillLevel(def, record.skillValues[i]);
            }
            if (record.originalPassions != null && record.originalPassions.Count == record.skillNames.Count)
            {
                for (int i = 0; i < record.skillNames.Count; i++)
                {
                    string   name = record.skillNames[i];
                    SkillDef def  = FindSkillDef(pawn.Pawn, name);
                    if (def == null)
                    {
                        Log.Warning("Could not load skill definition \"" + name + "\"");
                        Failed = true;
                        continue;
                    }
                    //pawn.originalPassions[def] = record.originalPassions[i];
                }
            }

            for (int i = 0; i < PawnLayers.Count; i++)
            {
                if (PawnLayers.IsApparelLayer(i))
                {
                    pawn.SetSelectedApparel(i, null);
                    pawn.SetSelectedStuff(i, null);
                }
            }
            for (int i = 0; i < record.apparelLayers.Count; i++)
            {
                int      layer = record.apparelLayers[i];
                ThingDef def   = DefDatabase <ThingDef> .GetNamedSilentFail(record.apparel[i]);

                if (def == null)
                {
                    Log.Warning("Could not load thing definition for apparel \"" + record.apparel[i] + "\"");
                    Failed = true;
                    continue;
                }
                ThingDef stuffDef = null;
                if (!string.IsNullOrEmpty(record.apparelStuff[i]))
                {
                    stuffDef = DefDatabase <ThingDef> .GetNamedSilentFail(record.apparelStuff[i]);

                    if (stuffDef == null)
                    {
                        Log.Warning("Could not load stuff definition \"" + record.apparelStuff[i] + "\" for apparel \"" + record.apparel[i] + "\"");
                        Failed = true;
                        continue;
                    }
                }
                pawn.SetSelectedApparel(layer, def);
                pawn.SetSelectedStuff(layer, stuffDef);
                pawn.SetColor(layer, record.apparelColors[i]);
            }

            for (int i = 0; i < record.implants.Count; i++)
            {
                SaveRecordImplantV3 implantRecord = record.implants[i];
                BodyPartRecord      bodyPart      = PrepareCarefully.Instance.HealthManager.ImplantManager.FindReplaceableBodyPartByName(implantRecord.bodyPart);
                if (bodyPart == null)
                {
                    Log.Warning("Could not find replaceable body part definition \"" + implantRecord.bodyPart + "\"");
                    Failed = true;
                    continue;
                }
                if (implantRecord.recipe != null)
                {
                    RecipeDef recipeDef = FindRecipeDef(implantRecord.recipe);
                    if (recipeDef == null)
                    {
                        Log.Warning("Could not find recipe definition \"" + implantRecord.recipe + "\"");
                        Failed = true;
                        continue;
                    }
                    bool found = false;
                    foreach (var p in recipeDef.appliedOnFixedBodyParts)
                    {
                        if (p.defName.Equals(bodyPart.def.defName))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        Log.Warning("Body part \"" + bodyPart.def.defName + "\" does not match recipe used to replace it");
                        Failed = true;
                        continue;
                    }
                    Implant implant = new Implant();
                    implant.BodyPartRecord = bodyPart;
                    implant.recipe         = recipeDef;
                    implant.label          = implant.Label;
                    pawn.AddImplant(implant);
                }
            }

            foreach (var injuryRecord in record.injuries)
            {
                HediffDef def = DefDatabase <HediffDef> .GetNamedSilentFail(injuryRecord.hediffDef);

                if (def == null)
                {
                    Log.Warning("Could not find hediff definition \"" + injuryRecord.hediffDef + "\"");
                    Failed = true;
                    continue;
                }
                InjuryOption option = PrepareCarefully.Instance.HealthManager.InjuryManager.FindOptionByHediffDef(def);
                if (option == null)
                {
                    Log.Warning("Could not find injury option for \"" + injuryRecord.hediffDef + "\"");
                    Failed = true;
                    continue;
                }
                BodyPartRecord bodyPart = null;
                if (injuryRecord.bodyPart != null)
                {
                    bodyPart = PrepareCarefully.Instance.HealthManager.FirstBodyPartRecord(injuryRecord.bodyPart);
                    if (bodyPart == null)
                    {
                        Log.Warning("Could not find body part \"" + injuryRecord.bodyPart + "\"");
                        Failed = true;
                        continue;
                    }
                }
                Injury injury = new Injury();
                injury.Option         = option;
                injury.BodyPartRecord = bodyPart;
                if (injuryRecord.severity != null)
                {
                    injury.Severity = injuryRecord.Severity;
                }
                pawn.AddInjury(injury);
            }

            pawn.RandomInjuries  = record.randomInjuries;
            pawn.RandomRelations = record.randomRelations;
            pawn.ClearCachedAbilities();
            pawn.ClearCachedLifeStage();

            return(pawn);
        }
コード例 #8
0
        //
        // Static Methods
        //
        public static void SaveToFile(PrepareCarefully data, string presetName)
        {
            bool problem = false;

            try {
                // Verify that all pawns have non-null identifiers.
                foreach (CustomPawn customPawn in data.Pawns)
                {
                    if (customPawn.Id == null)
                    {
                        customPawn.GenerateId();
                    }
                }
                foreach (var g in data.RelationshipManager.ParentChildGroups)
                {
                    foreach (var parent in g.Parents)
                    {
                        if (parent.Pawn != null && parent.Pawn.Id == null)
                        {
                            parent.Pawn.GenerateId();
                        }
                        foreach (var child in g.Children)
                        {
                            if (child.Pawn != null && child.Pawn.Id == null)
                            {
                                child.Pawn.GenerateId();
                            }
                        }
                    }
                }

                // Start saving.
                Scribe.saver.InitSaving(PresetFiles.FilePathForSavedPreset(presetName), "preset");
                string versionStringFull = "3";
                Scribe_Values.Look <string>(ref versionStringFull, "version", null, false);
                bool usePoints      = data.Config.pointsEnabled;
                int  startingPoints = PrepareCarefully.Instance.StartingPoints;
                Scribe_Values.Look <bool>(ref usePoints, "usePoints", false, true);
                Scribe_Values.Look <int>(ref startingPoints, "startingPoints", 0, true);
                string modString = GenText.ToCommaList(Enumerable.Select <ModContentPack, string>(LoadedModManager.RunningMods, (Func <ModContentPack, string>)(mod => mod.Name)), true);
                Scribe_Values.Look <string>(ref modString, "mods", null, false);
                // Save pawns.
                Scribe.EnterNode("colonists");
                foreach (CustomPawn customPawn in data.Pawns)
                {
                    try {
                        SaveRecordPawnV3 pawn = new SaveRecordPawnV3(customPawn);
                        Scribe_Deep.Look <SaveRecordPawnV3>(ref pawn, "colonist");
                    }
                    catch (Exception e) {
                        problem = true;
                        Log.Warning("Prepare Carefully failed to save a pawn into the preset: " + presetName);
                        Log.Warning(e.Message);
                        Log.Warning(e.StackTrace);
                    }
                }
                Scribe.ExitNode();

                // Save hidden pawns.
                List <CustomPawn> hiddenPawns = new List <CustomPawn>();
                foreach (var g in data.RelationshipManager.ParentChildGroups)
                {
                    foreach (var parent in g.Parents)
                    {
                        if (parent.Hidden)
                        {
                            if (parent.Pawn != null)
                            {
                                hiddenPawns.Add(parent.Pawn);
                            }
                            else
                            {
                                Log.Warning("Prepare Carefully found an empty pawn in a parent child relationship while saving the preset.  Skipping that pawn.");
                            }
                        }
                        foreach (var child in g.Children)
                        {
                            if (child.Hidden)
                            {
                                if (child.Pawn != null)
                                {
                                    hiddenPawns.Add(child.Pawn);
                                }
                                else
                                {
                                    Log.Warning("Prepare Carefully found an empty pawn in a parent child relationship while saving the preset.  Skipping that pawn.");
                                }
                            }
                        }
                    }
                }
                Scribe.EnterNode("hiddenPawns");
                foreach (CustomPawn customPawn in hiddenPawns)
                {
                    try {
                        SaveRecordPawnV3 pawn = new SaveRecordPawnV3(customPawn);
                        Scribe_Deep.Look <SaveRecordPawnV3>(ref pawn, "hiddenPawn");
                    }
                    catch (Exception e) {
                        problem = true;
                        Log.Warning("Prepare Carefully failed to save a hidden pawn into the preset: " + presetName);
                        Log.Warning(e.Message);
                        Log.Warning(e.StackTrace);
                    }
                }
                Scribe.ExitNode();

                Scribe.EnterNode("relationships");
                foreach (var r in data.RelationshipManager.Relationships)
                {
                    if (r.source != null && r.target != null && r.def != null && r.source.Id != null && r.target.Id != null)
                    {
                        SaveRecordRelationshipV3 s = new SaveRecordRelationshipV3(r);
                        Scribe_Deep.Look <SaveRecordRelationshipV3>(ref s, "relationship");
                    }
                    else
                    {
                        problem = true;
                        Log.Warning("Prepare Carefully found an invalid custom relationship when saving a preset: " + presetName);
                        if (r.target != null && r.source != null)
                        {
                            Log.Warning("  Relationship = { source = " + r.source.Id + ", target = " + r.target.Id + ", relationship = " + r.def + "}");
                        }
                        else
                        {
                            Log.Warning("  Relationship = { source = " + r.source + ", target = " + r.target + ", relationship = " + r.def + "}");
                        }
                    }
                }
                Scribe.ExitNode();

                Scribe.EnterNode("parentChildGroups");
                foreach (var g in data.RelationshipManager.ParentChildGroups)
                {
                    if (g.Children.Count == 0 || (g.Parents.Count == 0 && g.Children.Count == 1))
                    {
                        continue;
                    }
                    SaveRecordParentChildGroupV3 group = new SaveRecordParentChildGroupV3();
                    group.parents  = new List <string>();
                    group.children = new List <string>();
                    foreach (var p in g.Parents)
                    {
                        if (p.Pawn == null)
                        {
                            problem = true;
                            Log.Warning("Prepare Carefully found an invalid parent/child relationship when saving the preset: " + presetName);
                            continue;
                        }
                        else
                        {
                            group.parents.Add(p.Pawn.Id);
                        }
                    }
                    foreach (var p in g.Children)
                    {
                        if (p.Pawn == null)
                        {
                            problem = true;
                            Log.Warning("Prepare Carefully found an invalid parent/child relationship when saving the preset: " + presetName);
                            continue;
                        }
                        else
                        {
                            group.children.Add(p.Pawn.Id);
                        }
                    }
                    try {
                        Scribe_Deep.Look <SaveRecordParentChildGroupV3>(ref group, "group");
                    }
                    catch (Exception) {
                        problem = true;
                        Log.Warning("Prepare Carefully failed to save a parent child group when saving the preset: " + presetName);
                    }
                }
                Scribe.ExitNode();

                Scribe.EnterNode("equipment");
                foreach (var e in data.Equipment)
                {
                    try {
                        EquipmentSaveRecord record = new EquipmentSaveRecord(e);
                        Scribe_Deep.Look <EquipmentSaveRecord>(ref record, "equipment");
                    }
                    catch {
                        problem = true;
                        Log.Warning("Failed to save equipment to preset: " + e.ThingDef.defName);
                    }
                }
                Scribe.ExitNode();
            }
            catch (Exception e) {
                PrepareCarefully.Instance.State.AddError("EdB.PC.Dialog.Preset.Error.SaveFailed".Translate());
                Log.Error("Failed to save preset file");
                throw e;
            }
            finally {
                Scribe.saver.FinalizeSaving();
                Scribe.mode = LoadSaveMode.Inactive;
                if (problem)
                {
                    PrepareCarefully.Instance.State.AddError("EdB.PC.Dialog.Preset.Error.PartialSaveFailure".Translate());
                }
            }
        }