public bool Load(PrepareCarefully loadout, string presetName) { List <SaveRecordPawnV3> pawns = new List <SaveRecordPawnV3>(); List <SaveRecordRelationshipV3> savedRelationships = new List <SaveRecordRelationshipV3>(); Failed = false; int startingPoints = 0; bool usePoints = false; try { Scribe.InitLoading(PresetFiles.FilePathForSavedPreset(presetName)); Scribe_Values.LookValue <bool>(ref usePoints, "usePoints", true, false); Scribe_Values.LookValue <int>(ref startingPoints, "startingPoints", 0, false); Scribe_Values.LookValue <string>(ref ModString, "mods", "", false); try { Scribe_Collections.LookList <SaveRecordPawnV3>(ref pawns, "colonists", LookMode.Deep, null); } catch (Exception e) { Messages.Message(ModString, MessageSound.Silent); Messages.Message("EdB.PresetPawnLoadFailed".Translate(), MessageSound.SeriousAlert); Log.Warning(e.ToString()); Log.Warning("Preset was created with the following mods: " + ModString); return(false); } try { Scribe_Collections.LookList <SaveRecordRelationshipV3>(ref savedRelationships, "relationships", LookMode.Deep, null); } catch (Exception e) { Messages.Message(ModString, MessageSound.Silent); Messages.Message("EdB.PresetPawnLoadFailed".Translate(), MessageSound.SeriousAlert); Log.Warning(e.ToString()); Log.Warning("Preset was created with the following mods: " + ModString); return(false); } List <LoadableEquipment> tempEquipment = new List <LoadableEquipment>(); Scribe_Collections.LookList <LoadableEquipment>(ref tempEquipment, "equipment", LookMode.Deep, null); List <SelectedEquipment> equipment = new List <SelectedEquipment>(tempEquipment.Count); foreach (var e in tempEquipment) { ThingDef thingDef = DefDatabase <ThingDef> .GetNamedSilentFail(e.def); 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)) { equipment.Add(new SelectedEquipment(thingDef, null, gender, e.count)); } else { if (stuffDef != null) { EquipmentDatabaseEntry entry = PrepareCarefully.Instance.EquipmentEntries[new EquipmentKey(thingDef, stuffDef, gender)]; if (entry == 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. (thing = {0}, stuff={1})", thing, stuff)); Failed = true; continue; } else { equipment.Add(new SelectedEquipment(thingDef, stuffDef, gender, 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); } // After loading items using the Scribe methods, the saveables that were loaded get // put into this saveablesToPostLoad map. This post-load initialization is only // applicable for when we load a save game. We need to clear our saveables out of // there so that they don't cause errors later. HashSet <IExposable> saveables = (HashSet <IExposable>)(typeof(PostLoadInitter).GetField("saveablesToPostLoad", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null)); saveables.Clear(); //PrepareCarefully.Instance.Config.pointsEnabled = usePoints; } catch (Exception e) { Log.Error("Failed to load preset file"); throw e; } finally { Scribe.mode = LoadSaveMode.Inactive; } List <CustomPawn> pawnModels = new List <CustomPawn>(); try { foreach (SaveRecordPawnV3 p in pawns) { pawnModels.Add(LoadPawn(p)); } } catch (Exception e) { Messages.Message(ModString, MessageSound.Silent); Messages.Message("EdB.PresetPawnLoadFailed".Translate(), MessageSound.SeriousAlert); Log.Warning(e.ToString()); Log.Warning("Preset was created with the following mods: " + ModString); return(false); } List <CustomRelationship> relationships = new List <CustomRelationship>(); try { foreach (SaveRecordRelationshipV3 r in savedRelationships) { CustomRelationship relationship = LoadRelationship(r, pawnModels); if (relationship == null) { Messages.Message(ModString, MessageSound.Silent); Messages.Message("EdB.PresetRelationshipLoadFailed".Translate(), MessageSound.SeriousAlert); Log.Warning("Failed to load relationship: " + r.relation); Log.Warning("Preset was created with the following mods: " + ModString); } else { relationships.Add(relationship); } } } catch (Exception e) { Messages.Message(ModString, MessageSound.Silent); Messages.Message("EdB.PresetRelationshipLoadFailed".Translate(), MessageSound.SeriousAlert); Log.Warning(e.ToString()); Log.Warning("Preset was created with the following mods: " + ModString); return(false); } loadout.ClearPawns(); foreach (CustomPawn p in pawnModels) { loadout.AddPawn(p); } loadout.RelationshipManager.Clear(); foreach (CustomRelationship r in relationships) { loadout.RelationshipManager.AddRelationship(r.def, r.source, r.target); } if (Failed) { Messages.Message(ModString, MessageSound.Silent); Messages.Message("EdB.PresetThingDefFailed".Translate(), MessageSound.SeriousAlert); Log.Warning("Preset was created with the following mods: " + ModString); return(false); } return(true); }
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); }
public bool Load(PrepareCarefully loadout, string presetName) { SaveRecordPresetV4 preset = new SaveRecordPresetV4(); Failed = false; try { Scribe.loader.InitLoading(PresetFiles.FilePathForSavedPreset(presetName)); preset.ExposeData(); if (preset.equipment != null) { List <EquipmentSelection> equipment = new List <EquipmentSelection>(preset.equipment.Count); foreach (var e in preset.equipment) { 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; } } 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)); if (saveables != null) { saveables.Clear(); } List <IExposable> crossReferencingExposables = (List <IExposable>)(typeof(CrossRefHandler).GetField("crossReferencingExposables", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Scribe.loader.crossRefs)); if (crossReferencingExposables != null) { crossReferencingExposables.Clear(); } Scribe.loader.FinalizeLoading(); } List <CustomPawn> allPawns = new List <CustomPawn>(); List <CustomPawn> colonistCustomPawns = new List <CustomPawn>(); List <CustomPawn> hiddenCustomPawns = new List <CustomPawn>(); try { foreach (SaveRecordPawnV4 p in preset.pawns) { CustomPawn pawn = LoadPawn(p); if (pawn != null) { allPawns.Add(pawn); if (!pawn.Hidden) { colonistCustomPawns.Add(pawn); } else { hiddenCustomPawns.Add(pawn); } } else { Messages.Message("EdB.PC.Dialog.Preset.Error.NoCharacter".Translate(), MessageTypeDefOf.ThreatBig); Log.Warning("Preset was created with the following mods: " + preset.mods); } } } 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: " + preset.mods); 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 (preset.relationships != null) { try { foreach (SaveRecordRelationshipV3 r in preset.relationships) { 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: " + preset.mods); return(false); } if (atLeastOneRelationshipFailed) { Messages.Message("EdB.PC.Dialog.Preset.Error.RelationshipFailed".Translate(), MessageTypeDefOf.ThreatBig); } } loadout.RelationshipManager.AddRelationships(allRelationships); if (preset.parentChildGroups != null) { foreach (var groupRecord in preset.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(preset.mods, MessageTypeDefOf.SilentInput); Messages.Message("EdB.PC.Dialog.Preset.Error.ThingDefFailed".Translate(), MessageTypeDefOf.ThreatBig); Log.Warning("Preset was created with the following mods: " + preset.mods); return(false); } return(true); }