public void DeleteRelationship(PawnRelationDef def, CustomPawn source, CustomPawn target)
        {
            CustomRelationship toRemove = relationships.Find(def, source, target);

            if (toRemove != null)
            {
                deletionList.Add(toRemove);
            }
            dirty = true;
        }
        public CustomRelationship LoadRelationship(SaveRecordRelationshipV3 saved, List <CustomPawn> pawns)
        {
            CustomRelationship result = new CustomRelationship();

            foreach (var p in pawns)
            {
                if (p.Id == saved.source || p.Name.ToStringFull == saved.source)
                {
                    result.source = p;
                }
                if (p.Id == saved.target || p.Name.ToStringFull == saved.target)
                {
                    result.target = p;
                }
            }

            result.def = DefDatabase <PawnRelationDef> .GetNamedSilentFail(saved.relation);

            if (result.def != null)
            {
                result.inverseDef = PrepareCarefully.Instance.RelationshipManager.FindInverseRelationship(result.def);
            }
            if (result.def == null)
            {
                Log.Warning("Couldn't find relationship definition: " + saved.relation);
                return(null);
            }
            else if (result.source == null)
            {
                Log.Warning("Couldn't find relationship source pawn: " + saved.source);
                return(null);
            }
            else if (result.target == null)
            {
                Log.Warning("Couldn't find relationship target pawn: " + saved.source);
                return(null);
            }
            else if (result.inverseDef == null)
            {
                Log.Warning("Couldn't determine inverse relationship: " + saved.relation);
                return(null);
            }
            return(result);
        }
Пример #3
0
 public void RemoveRelationship(CustomRelationship relationship)
 {
     PrepareCarefully.Instance.RelationshipManager.DeleteRelationship(relationship);
 }
        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);
        }
Пример #5
0
 public SaveRecordRelationshipV3(CustomRelationship relationship)
 {
     this.source   = relationship.source.Id;
     this.target   = relationship.target.Id;
     this.relation = relationship.def.defName;
 }
        protected Vector2 DrawRelationship(Vector2 cursor, CustomRelationship relationship)
        {
            if (cursor.x + SizeRelationship.x > RectScrollView.width)
            {
                cursor.x  = 0;
                cursor.y += (SizeRelationship.y + SizeRelationshipSpacing.y);
            }

            Rect relationshipRect = new Rect(cursor, SizeRelationship);
            Rect sourcePawnRect   = new Rect(cursor, SizePawn);

            GUI.color = ColorPawnSource;
            GUI.DrawTexture(sourcePawnRect, BaseContent.WhiteTex);
            GUI.color = Color.white;

            Rect sourcePawnName = new Rect(sourcePawnRect.x, sourcePawnRect.yMax - 34, sourcePawnRect.width, 26);

            Text.Font   = GameFont.Small;
            Text.Anchor = TextAnchor.UpperCenter;
            GUI.color   = Style.ColorText;
            Widgets.Label(sourcePawnName, relationship.source.Pawn.LabelShort);
            GUI.color = Color.white;

            Rect sourceProfessionName = new Rect(sourcePawnRect.x, sourcePawnRect.yMax - 18, sourcePawnRect.width, 18);

            Text.Font   = GameFont.Tiny;
            Text.Anchor = TextAnchor.LowerCenter;
            GUI.color   = Style.ColorText;
            Widgets.Label(sourceProfessionName, relationship.source.ProfessionLabelShort);
            GUI.color = Color.white;

            Rect sourcePortraitRect = sourcePawnRect.InsetBy(6);

            sourcePortraitRect.y -= 8;
            var sourcePortraitTexture = relationship.source.GetPortrait(sourcePortraitRect.size);

            GUI.DrawTexture(sourcePortraitRect.OffsetBy(0, -4), sourcePortraitTexture);

            TooltipHandler.TipRegion(sourcePawnRect, GetTooltipText(relationship.source));

            // Delete button.
            Rect deleteRect = new Rect(sourcePawnRect.xMax - 16, sourcePawnRect.y + 4, 12, 12);

            Style.SetGUIColorForButton(deleteRect);
            GUI.DrawTexture(deleteRect, Textures.TextureButtonDelete);
            if (Widgets.ButtonInvisible(deleteRect))
            {
                relationshipsToDelete.Add(relationship);
            }

            Rect targetPawnRect = new Rect(cursor.x + SizeRelationship.x - SizePawn.x, cursor.y, SizePawn.x, SizePawn.y);

            GUI.color = ColorPawnTarget;
            GUI.DrawTexture(targetPawnRect, BaseContent.WhiteTex);
            GUI.color = Color.white;

            Rect targetPawnName = new Rect(targetPawnRect.x, targetPawnRect.yMax - 34, targetPawnRect.width, 26);

            Text.Font   = GameFont.Small;
            Text.Anchor = TextAnchor.UpperCenter;
            GUI.color   = Style.ColorText;
            Widgets.Label(targetPawnName, relationship.target.Pawn.LabelShort);
            GUI.color = Color.white;

            Rect targetProfessionName = new Rect(targetPawnRect.x, targetPawnRect.yMax - 18, targetPawnRect.width, 18);

            Text.Font   = GameFont.Tiny;
            Text.Anchor = TextAnchor.LowerCenter;
            GUI.color   = Style.ColorText;
            Widgets.Label(targetProfessionName, relationship.target.ProfessionLabelShort);
            GUI.color = Color.white;

            Rect targetPortraitRect = targetPawnRect.InsetBy(6);

            targetPortraitRect.y -= 8;
            var targetPortraitTexture = relationship.target.GetPortrait(targetPortraitRect.size);

            GUI.DrawTexture(targetPortraitRect.OffsetBy(0, -4), targetPortraitTexture);

            TooltipHandler.TipRegion(targetPawnRect, GetTooltipText(relationship.target));

            Rect sourceRelLabelRect = new Rect(sourcePawnRect.xMax, sourcePawnRect.y + SizeLabelSpacing.y, targetPawnRect.x - sourcePawnRect.xMax, HeightLabel);

            sourceRelLabelRect.width -= (SizeArrow.x + SizeLabelSpacing.x);
            GUI.color = ColorPawnSource;
            GUI.DrawTexture(sourceRelLabelRect, BaseContent.WhiteTex);
            Rect sourceRelArrowRect = new Rect(sourceRelLabelRect.xMax, sourceRelLabelRect.MiddleY() - SizeArrow.HalfY(), SizeArrow.x, SizeArrow.y);

            GUI.DrawTexture(sourceRelArrowRect, Textures.TextureArrowRight);
            Text.Font   = GameFont.Tiny;
            Text.Anchor = TextAnchor.MiddleCenter;
            GUI.color   = Style.ColorText;
            Widgets.Label(sourceRelLabelRect.OffsetBy(0, 1), relationship.inverseDef.GetGenderSpecificLabelCap(relationship.source.Pawn));

            Rect targetRelLabelRect = new Rect(sourcePawnRect.xMax, targetPawnRect.yMax - SizeLabelSpacing.y - HeightLabel, targetPawnRect.x - sourcePawnRect.xMax, HeightLabel);

            targetRelLabelRect.width -= (SizeArrow.x + SizeLabelSpacing.x);
            targetRelLabelRect.x     += (SizeArrow.x + SizeLabelSpacing.x);
            GUI.color = ColorPawnTarget;
            GUI.DrawTexture(targetRelLabelRect, BaseContent.WhiteTex);
            Rect targetRelArrowRect = new Rect(targetRelLabelRect.xMin - SizeArrow.x, targetRelLabelRect.MiddleY() - SizeArrow.HalfY(), SizeArrow.x, SizeArrow.y);

            GUI.DrawTexture(targetRelArrowRect, Textures.TextureArrowLeft);
            Text.Font   = GameFont.Tiny;
            Text.Anchor = TextAnchor.MiddleCenter;
            GUI.color   = Style.ColorText;
            Widgets.Label(targetRelLabelRect.OffsetBy(0, 1), relationship.def.GetGenderSpecificLabelCap(relationship.target.Pawn));

            Text.Anchor = TextAnchor.UpperLeft;
            Text.Font   = GameFont.Small;
            GUI.color   = Color.white;

            cursor.x += SizeRelationship.x + SizeRelationshipSpacing.x;
            return(cursor);
        }
Пример #7
0
        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);
        }
Пример #8
0
        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);
        }
Пример #9
0
 public void DeleteRelationship(CustomRelationship relationship)
 {
     this.relationships.Remove(relationship);
 }
 public SaveRecordRelationshipV3(CustomRelationship relationship)
 {
     this.source   = relationship.source.Name.ToStringFull;
     this.target   = relationship.target.Name.ToStringFull;
     this.relation = relationship.def.defName;
 }