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); } }
public CustomPawn Load(PrepareCarefully loadout, string name) { SaveRecordPawnV5 pawnRecord = new SaveRecordPawnV5(); 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 <SaveRecordPawnV5>(ref pawnRecord, "pawn", 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); } CustomPawn pawn = ConvertSaveRecordToPawn(pawnRecord); return(pawn); }
public bool Load(PrepareCarefully loadout, string presetName) { List <SaveRecordPawnV3> pawns = new List <SaveRecordPawnV3>(); List <SaveRecordPawnV3> hiddenPawns = new List <SaveRecordPawnV3>(); List <SaveRecordRelationshipV3> savedRelationships = new List <SaveRecordRelationshipV3>(); List <SaveRecordParentChildGroupV3> parentChildGroups = new List <SaveRecordParentChildGroupV3>(); Failed = false; int startingPoints = 0; bool usePoints = false; try { Scribe.loader.InitLoading(PresetFiles.FilePathForSavedPreset(presetName)); Scribe_Values.Look <bool>(ref usePoints, "usePoints", true, false); Scribe_Values.Look <int>(ref startingPoints, "startingPoints", 0, false); Scribe_Values.Look <string>(ref ModString, "mods", "", false); try { Scribe_Collections.Look <SaveRecordPawnV3>(ref pawns, "colonists", LookMode.Deep, null); } catch (Exception e) { Messages.Message("EdB.PC.Dialog.Preset.Error.Failed".Translate(), MessageTypeDefOf.ThreatBig); Log.Warning(e.ToString()); Log.Warning("Preset was created with the following mods: " + ModString); return(false); } try { Scribe_Collections.Look <SaveRecordPawnV3>(ref hiddenPawns, "hiddenPawns", LookMode.Deep, null); } catch (Exception e) { Messages.Message("EdB.PC.Dialog.Preset.Error.Failed".Translate(), MessageTypeDefOf.ThreatBig); Log.Warning(e.ToString()); Log.Warning("Preset was created with the following mods: " + ModString); return(false); } try { Scribe_Collections.Look <SaveRecordRelationshipV3>(ref savedRelationships, "relationships", LookMode.Deep, null); } catch (Exception e) { Messages.Message("EdB.PC.Dialog.Preset.Error.Failed".Translate(), MessageTypeDefOf.ThreatBig); Log.Warning(e.ToString()); Log.Warning("Preset was created with the following mods: " + ModString); return(false); } try { Scribe_Collections.Look <SaveRecordParentChildGroupV3>(ref parentChildGroups, "parentChildGroups", LookMode.Deep, null); } catch (Exception e) { Messages.Message("EdB.PC.Dialog.Preset.Error.Failed".Translate(), MessageTypeDefOf.ThreatBig); Log.Warning(e.ToString()); Log.Warning("Preset was created with the following mods: " + ModString); return(false); } List <SaveRecordEquipmentV3> tempEquipment = new List <SaveRecordEquipmentV3>(); Scribe_Collections.Look <SaveRecordEquipmentV3>(ref tempEquipment, "equipment", LookMode.Deep, null); loadout.Equipment.Clear(); if (tempEquipment != null) { List <EquipmentSelection> equipment = new List <EquipmentSelection>(tempEquipment.Count); foreach (var e in tempEquipment) { ThingDef thingDef = DefDatabase <ThingDef> .GetNamedSilentFail(e.def); if (thingDef == null) { string replacementDefName; if (thingDefReplacements.TryGetValue(e.def, out replacementDefName)) { thingDef = DefDatabase <ThingDef> .GetNamedSilentFail(replacementDefName); } } ThingDef stuffDef = null; Gender gender = Gender.None; if (!string.IsNullOrEmpty(e.stuffDef)) { stuffDef = DefDatabase <ThingDef> .GetNamedSilentFail(e.stuffDef); } if (!string.IsNullOrEmpty(e.gender)) { try { gender = (Gender)Enum.Parse(typeof(Gender), e.gender); } catch (Exception) { Log.Warning("Failed to load gender value for animal."); Failed = true; continue; } } if (thingDef != null) { if (string.IsNullOrEmpty(e.stuffDef)) { EquipmentKey key = new EquipmentKey(thingDef, null, gender); EquipmentRecord record = PrepareCarefully.Instance.EquipmentDatabase.LookupEquipmentRecord(key); if (record != null) { equipment.Add(new EquipmentSelection(record, e.count)); } else { Log.Warning("Could not find equipment in equipment database: " + key); Failed = true; continue; } } else { if (stuffDef != null) { EquipmentKey key = new EquipmentKey(thingDef, stuffDef, gender); EquipmentRecord record = PrepareCarefully.Instance.EquipmentDatabase.LookupEquipmentRecord(key); if (record == null) { string thing = thingDef != null ? thingDef.defName : "null"; string stuff = stuffDef != null ? stuffDef.defName : "null"; Log.Warning(string.Format("Could not load equipment/resource from the preset. This may be caused by an invalid thing/stuff combination: " + key)); Failed = true; continue; } else { equipment.Add(new EquipmentSelection(record, e.count)); } } else { Log.Warning("Could not load stuff definition \"" + e.stuffDef + "\" for item \"" + e.def + "\""); Failed = true; } } } else { Log.Warning("Could not load thing definition \"" + e.def + "\""); Failed = true; } } loadout.Equipment.Clear(); foreach (var e in equipment) { loadout.Equipment.Add(e); } } else { Messages.Message("EdB.PC.Dialog.Preset.Error.EquipmentFailed".Translate(), MessageTypeDefOf.ThreatBig); Log.Warning("Failed to load equipment from preset"); Failed = true; } //PrepareCarefully.Instance.Config.pointsEnabled = usePoints; } catch (Exception e) { Log.Error("Failed to load preset file"); throw e; } finally { PresetLoader.ClearSaveablesAndCrossRefs(); } List <CustomPawn> allPawns = new List <CustomPawn>(); List <CustomPawn> colonistCustomPawns = new List <CustomPawn>(); try { foreach (SaveRecordPawnV3 p in pawns) { CustomPawn pawn = LoadPawn(p); if (pawn != null) { allPawns.Add(pawn); colonistCustomPawns.Add(pawn); } else { Messages.Message("EdB.PC.Dialog.Preset.Error.NoCharacter".Translate(), MessageTypeDefOf.ThreatBig); Log.Warning("Preset was created with the following mods: " + ModString); } } } catch (Exception e) { Messages.Message("EdB.PC.Dialog.Preset.Error.Failed".Translate(), MessageTypeDefOf.ThreatBig); Log.Warning(e.ToString()); Log.Warning("Preset was created with the following mods: " + ModString); return(false); } List <CustomPawn> hiddenCustomPawns = new List <CustomPawn>(); try { if (hiddenPawns != null) { foreach (SaveRecordPawnV3 p in hiddenPawns) { CustomPawn pawn = LoadPawn(p); if (pawn != null) { allPawns.Add(pawn); hiddenCustomPawns.Add(pawn); } else { Log.Warning("Prepare Carefully failed to load a hidden character from the preset"); } } } } catch (Exception e) { Messages.Message("EdB.PC.Dialog.Preset.Error.Failed".Translate(), MessageTypeDefOf.ThreatBig); Log.Warning(e.ToString()); Log.Warning("Preset was created with the following mods: " + ModString); return(false); } loadout.ClearPawns(); foreach (CustomPawn p in colonistCustomPawns) { loadout.AddPawn(p); } loadout.RelationshipManager.Clear(); loadout.RelationshipManager.InitializeWithCustomPawns(colonistCustomPawns.AsEnumerable().Concat(hiddenCustomPawns)); bool atLeastOneRelationshipFailed = false; List <CustomRelationship> allRelationships = new List <CustomRelationship>(); if (savedRelationships != null) { try { foreach (SaveRecordRelationshipV3 r in savedRelationships) { if (string.IsNullOrEmpty(r.source) || string.IsNullOrEmpty(r.target) || string.IsNullOrEmpty(r.relation)) { atLeastOneRelationshipFailed = true; Log.Warning("Prepare Carefully failed to load a custom relationship from the preset: " + r); continue; } CustomRelationship relationship = LoadRelationship(r, allPawns); if (relationship == null) { atLeastOneRelationshipFailed = true; Log.Warning("Prepare Carefully failed to load a custom relationship from the preset: " + r); } else { allRelationships.Add(relationship); } } } catch (Exception e) { Messages.Message("EdB.PC.Dialog.Preset.Error.RelationshipFailed".Translate(), MessageTypeDefOf.ThreatBig); Log.Warning(e.ToString()); Log.Warning("Preset was created with the following mods: " + ModString); return(false); } if (atLeastOneRelationshipFailed) { Messages.Message("EdB.PC.Dialog.Preset.Error.RelationshipFailed".Translate(), MessageTypeDefOf.ThreatBig); } } loadout.RelationshipManager.AddRelationships(allRelationships); if (parentChildGroups != null) { foreach (var groupRecord in parentChildGroups) { ParentChildGroup group = new ParentChildGroup(); if (groupRecord.parents != null) { foreach (var id in groupRecord.parents) { CustomPawn parent = FindPawnById(id, colonistCustomPawns, hiddenCustomPawns); if (parent != null) { var pawn = parent; if (pawn != null) { group.Parents.Add(pawn); } else { Log.Warning("Prepare Carefully could not load a custom parent relationship because it could not find a matching pawn in the relationship manager."); } } else { Log.Warning("Prepare Carefully could not load a custom parent relationship because it could not find a pawn with the saved identifer."); } } } if (groupRecord.children != null) { foreach (var id in groupRecord.children) { CustomPawn child = FindPawnById(id, colonistCustomPawns, hiddenCustomPawns); if (child != null) { var pawn = child; if (pawn != null) { group.Children.Add(pawn); } else { Log.Warning("Prepare Carefully could not load a custom child relationship because it could not find a matching pawn in the relationship manager."); } } else { Log.Warning("Prepare Carefully could not load a custom child relationship because it could not find a pawn with the saved identifer."); } } } loadout.RelationshipManager.ParentChildGroups.Add(group); } } loadout.RelationshipManager.ReassignHiddenPawnIndices(); if (Failed) { Messages.Message(ModString, MessageTypeDefOf.SilentInput); Messages.Message("EdB.PC.Dialog.Preset.Error.ThingDefFailed".Translate(), MessageTypeDefOf.ThreatBig); Log.Warning("Preset was created with the following mods: " + ModString); return(false); } return(true); }