public SaveRecordPawnV4(CustomPawn pawn) { this.id = pawn.Id; this.thingDef = pawn.Pawn.def.defName; this.type = pawn.Type.ToString(); if (pawn.Type == CustomPawnType.World && pawn.Faction != null) { this.faction = new SaveRecordFactionV4(); this.faction.def = pawn.Faction.Def != null ? pawn.Faction.Def.defName : null; this.faction.index = pawn.Faction.Index; this.faction.leader = pawn.Faction.Leader; } this.pawnKindDef = pawn.OriginalKindDef != null ? pawn.OriginalKindDef.defName : pawn.Pawn.kindDef.defName; this.originalFactionDef = pawn.OriginalFactionDef != null ? pawn.OriginalFactionDef.defName : null; this.gender = pawn.Gender; if (pawn.Adulthood != null) { this.adulthood = pawn.Adulthood.identifier; } else { this.adulthood = pawn.LastSelectedAdulthoodBackstory?.identifier; } this.childhood = pawn.Childhood.identifier; this.skinColor = pawn.Pawn.story.SkinColor; this.melanin = pawn.Pawn.story.melanin; this.hairDef = pawn.HairDef.defName; this.hairColor = pawn.Pawn.story.hairColor; this.headGraphicPath = pawn.HeadGraphicPath; this.bodyType = pawn.BodyType.defName; this.firstName = pawn.FirstName; this.nickName = pawn.NickName; this.lastName = pawn.LastName; this.age = 0; this.biologicalAge = pawn.BiologicalAge; this.chronologicalAge = pawn.ChronologicalAge; foreach (var trait in pawn.Traits) { if (trait != null) { this.traitNames.Add(trait.def.defName); this.traitDegrees.Add(trait.Degree); } } foreach (var skill in pawn.Pawn.skills.skills) { SaveRecordSkillV4 skillRecord = new SaveRecordSkillV4(); skillRecord.name = skill.def.defName; skillRecord.value = pawn.GetUnmodifiedSkillLevel(skill.def); skillRecord.passion = pawn.currentPassions[skill.def]; this.skills.Add(skillRecord); } foreach (var layer in PrepareCarefully.Instance.Providers.PawnLayers.GetLayersForPawn(pawn)) { if (layer.Apparel) { ThingDef apparelThingDef = pawn.GetAcceptedApparel(layer); Color color = pawn.GetColor(layer); if (apparelThingDef != null) { ThingDef apparelStuffDef = pawn.GetSelectedStuff(layer); SaveRecordApparelV4 apparelRecord = new SaveRecordApparelV4(); apparelRecord.layer = layer.Name; apparelRecord.apparel = apparelThingDef.defName; apparelRecord.stuff = apparelStuffDef != null ? apparelStuffDef.defName : ""; apparelRecord.color = color; this.apparel.Add(apparelRecord); } } } OptionsHealth healthOptions = PrepareCarefully.Instance.Providers.Health.GetOptions(pawn); foreach (Implant implant in pawn.Implants) { var saveRecord = new SaveRecordImplantV3(implant); if (implant.BodyPartRecord != null) { UniqueBodyPart part = healthOptions.FindBodyPartsForRecord(implant.BodyPartRecord); if (part != null && part.Index > 0) { saveRecord.bodyPartIndex = part.Index; } } this.implants.Add(saveRecord); } foreach (Injury injury in pawn.Injuries) { var saveRecord = new SaveRecordInjuryV3(injury); if (injury.BodyPartRecord != null) { UniqueBodyPart part = healthOptions.FindBodyPartsForRecord(injury.BodyPartRecord); if (part != null && part.Index > 0) { saveRecord.bodyPartIndex = part.Index; } } this.injuries.Add(saveRecord); } ThingComp alienComp = ProviderAlienRaces.FindAlienCompForPawn(pawn.Pawn); if (alienComp != null) { alien = new SaveRecordAlienV4(); alien.crownType = ProviderAlienRaces.GetCrownTypeFromComp(alienComp); alien.skinColor = ProviderAlienRaces.GetSkinColorFromComp(alienComp); alien.skinColorSecond = ProviderAlienRaces.GetSkinColorSecondFromComp(alienComp); alien.hairColorSecond = ProviderAlienRaces.GetHairColorSecondFromComp(alienComp); } }
public static Pawn Copy(this Pawn source) { PawnHealthState savedHealthState = source.health.State; Pawn result = (Pawn)ThingMaker.MakeThing(source.kindDef.race, null); result.kindDef = source.kindDef; result.SetFactionDirect(source.Faction); PawnComponentsUtility.CreateInitialComponents(result); // Copy gender. result.gender = source.gender; // Copy name; NameTriple nameTriple = source.Name as NameTriple; NameSingle nameSingle = source.Name as NameSingle; if (nameTriple != null) { result.Name = new NameTriple(nameTriple.First, nameTriple.Nick, nameTriple.Last); } else if (nameSingle != null) { result.Name = new NameSingle(nameSingle.Name, nameSingle.Numerical); } // Copy trackers. object[] constructorArgs = new object[] { result }; result.ageTracker = UtilityCopy.CopyExposable(source.ageTracker, constructorArgs); result.story = UtilityCopy.CopyExposable(source.story, constructorArgs); result.skills = UtilityCopy.CopyExposable(source.skills, constructorArgs); result.health = UtilityCopy.CopyExposable(source.health, constructorArgs); result.apparel = UtilityCopy.CopyExposable(source.apparel, constructorArgs); // Copy alien comps ThingComp sourceAlienComp = ProviderAlienRaces.FindAlienCompForPawn(source); ThingComp resultAlienComp = ProviderAlienRaces.FindAlienCompForPawn(result); if (sourceAlienComp != null && resultAlienComp != null) { ProviderAlienRaces.SetCrownTypeOnComp(resultAlienComp, ProviderAlienRaces.GetCrownTypeFromComp(sourceAlienComp)); ProviderAlienRaces.SetSkinColorOnComp(resultAlienComp, ProviderAlienRaces.GetSkinColorFromComp(sourceAlienComp)); ProviderAlienRaces.SetSkinColorSecondOnComp(resultAlienComp, ProviderAlienRaces.GetSkinColorSecondFromComp(sourceAlienComp)); ProviderAlienRaces.SetHairColorSecondOnComp(resultAlienComp, ProviderAlienRaces.GetHairColorSecondFromComp(sourceAlienComp)); } // Copy properties added to pawns by mods. CopyModdedProperties(source, result); // Verify the pawn health state. if (result.health.State != savedHealthState) { Log.Warning("Mismatched health state on copied pawn: " + savedHealthState + " != " + result.health.State + "; Resetting value to match."); result.SetHealthState(savedHealthState); } // Clear all of the pawn caches. source.ClearCaches(); return(result); }