protected override void LoadSettings(BagInstance Data) { if (Data != null) { this.Size = Data.Size; this.Autofill = Data.Autofill; this.BaseName = ItemBagsMod.Translate("BundleBagName"); this.DescriptionAlias = ItemBagsMod.Translate("BundleBagDescription"); Contents.Clear(); foreach (BagItem Item in Data.Contents) { this.Contents.Add(Item.ToObject()); } if (Data.IsCustomIcon) { this.CustomIconSourceTexture = BagType.SourceTexture.SpringObjects; this.CustomIconTexturePosition = Data.OverriddenIcon; } else { ResetIcon(); } } }
private static void TryMigrateData(Item item, Action <Item> insertReplacement) { try { if (item is Chest chest) { for (int i = 0; i < chest.items.Count; i++) { TryMigrateData(chest.items[i], replacement => chest.items[i] = replacement); } } else if (item is SObject obj) { if (BagInstance.TryDeserializePyTKData(item, out BagInstance bagInstance) && bagInstance.TryDecode(out ItemBag bag)) { insertReplacement(bag); } } } catch (Exception ex) { string errorMsg = $"Error while attempting to migrate a PyTK saved item. Your bag saved in an older version of ItemBags 1.X.X could not be converted to newer data format, and will remain as a junk item. Error message: {ex}"; ItemBagsMod.ModInstance.Monitor.Log(errorMsg, LogLevel.Warn); } }
public BundleBag(BagInstance SavedData) : this(SavedData.Size, SavedData.Autofill) { foreach (BagItem Item in SavedData.Contents) { this.Contents.Add(Item.ToObject()); } if (SavedData.IsCustomIcon) { this.CustomIconSourceTexture = BagType.SourceTexture.SpringObjects; this.CustomIconTexturePosition = SavedData.OverriddenIcon; } }
public BundleBag(BagInstance SavedData) : this(SavedData.Size, SavedData.Autofill) { foreach (BagItem Item in SavedData.Contents) { this.Contents.Add(Item.ToObject()); } if (SavedData.IsCustomIcon) { this.Icon = Game1.objectSpriteSheet; this.IconTexturePosition = SavedData.OverriddenIcon; } }
protected override void LoadSettings(BagInstance Data) { if (Data != null) { this.Size = Data.Size; string SizeName = ItemBagsMod.Translate(string.Format("Size{0}Name", Size.GetDescription())); DescriptionAlias = string.Format("{0}\n({1})\n({2})", ItemBagsMod.Translate("OmniBagDescription"), ItemBagsMod.Translate("CapacityDescription", new Dictionary <string, string>() { { "count", MaxStackSize.ToString() } }), ItemBagsMod.Translate("OmniBagCapacityDescription", new Dictionary <string, string>() { { "size", SizeName } }) ); this.NestedBags.Clear(); foreach (BagInstance NestedInstance in Data.NestedBags) { if (NestedInstance.TryDecode(out ItemBag NestedBag)) { this.NestedBags.Add(NestedBag); } } Contents.Clear(); foreach (BagItem Item in Data.Contents) { this.Contents.Add(Item.ToObject()); } if (Data.IsCustomIcon) { this.CustomIconSourceTexture = BagType.SourceTexture.SpringObjects; this.CustomIconTexturePosition = Data.OverriddenIcon; } else { ResetIcon(); } } }
public OmniBag(BagInstance SavedData) : this(SavedData.Size) { this.NestedBags = new List <ItemBag>(); foreach (BagInstance NestedInstance in SavedData.NestedBags) { if (NestedInstance.TryDecode(out ItemBag NestedBag)) { this.NestedBags.Add(NestedBag); } } if (SavedData.IsCustomIcon) { this.CustomIconSourceTexture = BagType.SourceTexture.SpringObjects; this.CustomIconTexturePosition = SavedData.OverriddenIcon; } }
public OmniBag(BagInstance SavedData) : this(SavedData.Size) { this.NestedBags = new List <ItemBag>(); foreach (BagInstance NestedInstance in SavedData.NestedBags) { if (NestedInstance.TryDecode(out ItemBag NestedBag)) { this.NestedBags.Add(NestedBag); } } if (SavedData.IsCustomIcon) { this.Icon = Game1.objectSpriteSheet; this.IconTexturePosition = SavedData.OverriddenIcon; } }
public BoundedBag(BagType TypeInfo, BagInstance SavedData) : this(TypeInfo, SavedData.Size, SavedData.Autofill) { foreach (BagItem Item in SavedData.Contents) { this.Contents.Add(Item.ToObject()); } if (SavedData.IsCustomIcon) { this.Icon = Game1.objectSpriteSheet; this.IconTexturePosition = SavedData.OverriddenIcon; } this.ExcludedAutofillItems = new Dictionary <string, HashSet <ObjectQuality> >(); foreach (var KVP in SavedData.ExcludedAutofillItems) { this.ExcludedAutofillItems.Add(KVP.Key, KVP.Value); } }
protected override void LoadSettings(BagInstance Data) { if (Data != null) { this.Size = Data.Size; this.Autofill = Data.Autofill; this.ExcludedAutofillItems = new Dictionary <string, HashSet <ObjectQuality> >(); foreach (var KVP in Data.ExcludedAutofillItems) { this.ExcludedAutofillItems.Add(KVP.Key, KVP.Value); } // Load the type this.TypeInfo = ItemBagsMod.BagConfig.BagTypes.FirstOrDefault(x => x.Id == Data.TypeId); if (TypeInfo == null) { string Warning = string.Format("Warning - no BagType with Id = {0} was found. Did you manually edit your {1} json file or delete a .json file from 'Modded Bags' folder? The saved bag cannot be properly loaded without a corresponding type!" + " To prevent crashes, this bag will be automatically converted to a default BagType.", Data.TypeId, ItemBagsMod.BagConfigDataKey); ItemBagsMod.ModInstance.Monitor.Log(Warning, LogLevel.Warn); // To prevent crashes, convert this bag into a different bag type that exists this.TypeInfo = ItemBagsMod.BagConfig.GetDefaultBoundedBagType(); } // Load the size configuration this.SizeInfo = TypeInfo.SizeSettings.FirstOrDefault(x => x.Size == Size); if (SizeInfo == null) { string Warning = string.Format("Warning - BagType with Id = {0} does not contain any settings for Size={1}. Did you manually edit your {2} json file?" + " The saved bag cannot be properly loaded without the corresponding settings for this size! To prevent crashes, this bag will be automatically converted to a default size for this BagType.", this.TypeInfo.Id, this.Size.ToString(), ItemBagsMod.BagConfigDataKey); ItemBagsMod.ModInstance.Monitor.Log(Warning, LogLevel.Warn); this.SizeInfo = TypeInfo.SizeSettings.First(); } _MaxStackSize = ItemBagsMod.UserConfig.GetStandardBagCapacity(Size, TypeInfo); this.BaseName = BagType.GetTranslatedName(TypeInfo); DescriptionAlias = string.Format("{0}\n({1})", BagType.GetTranslatedDescription(TypeInfo), ItemBagsMod.Translate("CapacityDescription", new Dictionary <string, string>() { { "count", MaxStackSize.ToString() } })); if (SizeInfo.Size != Size) { this.AllowedObjects = new ReadOnlyCollection <AllowedObject>(new List <AllowedObject>()); } else { this.AllowedObjects = new ReadOnlyCollection <AllowedObject>(SizeInfo.Items.Select(x => new AllowedObject(x)).ToList()); } this.Contents.Clear(); foreach (BagItem Item in Data.Contents) { this.Contents.Add(Item.ToObject()); } if (Data.IsCustomIcon) { this.Icon = Game1.objectSpriteSheet; this.IconTexturePosition = Data.OverriddenIcon; } else { ResetIcon(); } } }
public void rebuild(Dictionary <string, string> additionalSaveData, object replacement) { BagInstance Data = BagInstance.FromPyTKAdditionalSaveData(additionalSaveData); LoadSettings(Data); }
/// <summary>Converts all custom items used by this mod into items that actually exist in the vanilla StardewValley game, so that the game won't crash while trying to save.<para/> /// The custom items are saved to a separate file using <see cref="StardewModdingAPI.IDataHelper.WriteSaveData{TModel}(string, TModel)"/></summary> internal static void OnSaving() { if (Constants.TargetPlatform == GamePlatform.Android) { ItemBagsMod.ModInstance.Monitor.Log("ItemBags OnSaving started.", LogLevel.Debug); int CurrentBagId = 0; List <BagInstance> BagInstances = new List <BagInstance>(); // Pre-emptive error-handling - try to find any encoded bags that, for whatever unknown reason, weren't able to be converted back into ItemBags during a Load. // If any were found, then we need to retain the SaveData's BagInstance associated with that item (and not re-use it's Bag InstanceId when assigning to the bags that didn't have any issues during loading) // so that the mod can still try again to load that bag during the next load. // If someone mysteriously loses a bag, I can at least do some manual save editing to restore it, as the data will still be there. List <Item> CorruptedBags = new List <Item>(); ReplaceAllInstances(x => IsEncodedCustomItem(x), x => { CorruptedBags.Add(x); return(x); }); HashSet <int> CorruptedBagIds = new HashSet <int>(CorruptedBags.Select(x => x.ParentSheetIndex - EncodedItemStartIndex)); PlayerBags PreviousBagData = null; if (CorruptedBagIds.Any()) { PreviousBagData = PlayerBags.DeserializeFromCurrentSaveFile(); if (PreviousBagData != null && PreviousBagData.Bags != null) { Dictionary <int, BagInstance> IndexedInstances = new Dictionary <int, BagInstance>(); foreach (BagInstance Instance in PreviousBagData.Bags) { if (!IndexedInstances.ContainsKey(Instance.InstanceId)) { IndexedInstances.Add(Instance.InstanceId, Instance); } } foreach (int CorruptedId in CorruptedBagIds) { if (IndexedInstances.TryGetValue(CorruptedId, out BagInstance CorruptedInstance)) { BagInstances.Add(CorruptedInstance); } } } } // Encode all bags as a regular non-modded item ReplaceAllInstances(IsCustomItem, CustomItem => { if (CustomItem is ItemBag IB) { try { while (CorruptedBagIds.Contains(CurrentBagId)) { CurrentBagId++; } BagInstance Instance = new BagInstance(CurrentBagId, IB); BagInstances.Add(Instance); // Replace the Bag with an arbitrary low-value/non-stackable item (in this case, a Rusty Sword) and store the bag instance's Id in the replacement item's ParentSheetIndex MeleeWeapon Replacement = new MeleeWeapon(0); Replacement.ParentSheetIndex = EncodedItemStartIndex + CurrentBagId; return(Replacement); } finally { CurrentBagId++; } } else { return(CustomItem); } }); PlayerBags OwnedBags = new PlayerBags(); OwnedBags.Bags = BagInstances.ToArray(); OwnedBags.SerializeToCurrentSaveFile(); ItemBagsMod.ModInstance.Monitor.Log("ItemBags OnSaving finished.", LogLevel.Debug); } }