private bool ProcessFarmType(IContentPack contentPack, out CustomFarm farm) { Dictionary <string, object> Extra; bool results; if (contentPack.Manifest.ExtraFields != null && contentPack.Manifest.ExtraFields.ContainsKey("ContentPackType")) { Extra = (Dictionary <string, object>)ObjectToDictionaryHelper.ToDictionary(contentPack.Manifest.ExtraFields["ContentPackType"]); if (Extra.ContainsKey("Farm") && bool.Parse(Extra["Farm"].ToString())) { farm = contentPack.ReadJsonFile <CustomFarm>("farmType.json"); results = true; } else { farm = null; results = false; } } else { farm = contentPack.ReadJsonFile <CustomFarm>("farmType.json"); results = true; } return(results); }
/// <summary> /// Converts a content pack with a farmType.json that is verison 1.0 or 1.1 to /// version 2.0 /// </summary> /// <param name="contentPack">A SMAPI Content Pack</param> /// <param name="monitor">SMAPI's IMonitor, to print useful information</param> /// <returns></returns> private CustomFarm PopulateOld(IContentPack contentPack, IMonitor monitor) { CustomFarmVer1 oldVersion; CustomFarm convertedFarm; monitor.Log("\t - Content Pack is for MTN1. Using Backwards Compatibility."); oldVersion = contentPack.ReadJsonFile <CustomFarmVer1>("farmType.json"); convertedFarm = new CustomFarm(); CustomFarmVer1.Convert(convertedFarm, oldVersion); return(convertedFarm); }
/// <summary> /// Checks each field containing a <see cref="Structure"/> type. Implements the default (canon) values if /// the field is omitted. /// </summary> /// <param name="farm"></param> private void Validate(CustomFarm farm) { farm.FarmHouse = (farm.FarmHouse == null) ? new Structure(new Placement(3712f / 64f, 520f / 64f), new Interaction(64, 15)) : farm.FarmHouse; farm.GreenHouse = (farm.GreenHouse == null) ? new Structure(new Placement(1600f / 64f, 384f / 64f), new Interaction(28, 17)) : farm.GreenHouse; farm.FarmCave = (farm.FarmCave == null) ? new Structure(new Placement(), new Interaction(34, 7)) : farm.FarmCave; farm.ShippingBin = (farm.ShippingBin == null) ? new Structure(new Placement(), new Interaction(71, 13)) : farm.ShippingBin; farm.MailBox = (farm.MailBox == null) ? new Structure(new Placement(), new Interaction(68, 16)) : farm.MailBox; farm.GrandpaShrine = (farm.GrandpaShrine == null) ? new Structure(new Placement(), new Interaction(9, 7)) : farm.GrandpaShrine; farm.RabbitShrine = (farm.RabbitShrine == null) ? new Structure(new Placement(), new Interaction(48, 7)) : farm.RabbitShrine; farm.PetWaterBowl = (farm.PetWaterBowl == null) ? new Structure(new Placement(), new Interaction(54, 7)) : farm.PetWaterBowl; }
private void NewMtnSave(object sender, SaveCreatingEventArgs e) { MtnFarmData data = Helper.Data.ReadSaveData <MtnFarmData>("MtnFarmData"); if (data == null && !CustomManager.Canon) { CustomFarm farm = CustomManager.SelectedFarm; MtnFarmData customData = new MtnFarmData { FarmTypeName = farm.Name }; Helper.Data.WriteSaveData("MtnFarmData", customData); } }
private void LoadIcon(IModHelper helper, IContentPack contentPack, CustomFarm farm) { string IconFile; IconFile = Path.Combine(contentPack.DirectoryPath, "icon.png"); if (File.Exists(IconFile)) { farm.IconSource = contentPack.LoadAsset <Texture2D>("icon.png"); } else { farm.IconSource = helper.Content.Load <Texture2D>(Path.Combine("res", "missingIcon.png")); } }
private void TryToResolveFarmId(object sender, LoadStageChangedEventArgs e) { if (e.NewStage == StardewModdingAPI.Enums.LoadStage.SaveLoadedBasicInfo) { if (Game1.whichFarm >= 5) { MethodInfo keyReader = Helper.Data.GetType().GetMethod("GetSaveFileKey", BindingFlags.NonPublic | BindingFlags.Instance); string key = (string)keyReader.Invoke(Helper.Data, new object[] { "MtnFarmData" }); MtnFarmData farmData = JsonConvert.DeserializeObject <MtnFarmData>(SaveGame.loaded.CustomData[key]); if (farmData != null) { return; } CustomFarm farm = CustomManager.FarmList.Find(x => x.ID == Game1.whichFarm); MtnFarmData customData = new MtnFarmData { FarmTypeName = farm.Name }; Helper.Data.WriteSaveData("MtnFarmData", customData); Game1.whichFarm = 200; CustomManager.LoadCustomFarmByMtnData(); int farmIndex; Map map; string mapAssetKey; Game1.removeLocationFromLocationLookup("Farm"); for (farmIndex = 0; farmIndex < Game1.locations.Count; farmIndex++) { if (Game1.locations[farmIndex].Name == "Farm") { break; } } mapAssetKey = CustomManager.GetAssetKey(out map, "Farm"); Game1.locations[farmIndex] = new Farm(mapAssetKey, "Farm"); Game1.locations[farmIndex].reloadMap(); } } }
/// <summary> /// Populates the List<CustomFarm> FarmList variable with all the Content Packs /// registered to MTN. /// </summary> /// <param name="Helper">SMAPI's IModHelper, to load in the Content Packs</param> /// <param name="Monitor">SMAPI's IMonitor, to print useful information</param> public void Populate(IModHelper Helper, IMonitor Monitor) { CustomFarm FarmData; CustomGreenHouse GreenHouseData; bool ContainsFarm = false; bool ContainsGreenHouse = false; FarmList = new List <CustomFarm>(); foreach (IContentPack ContentPack in Helper.ContentPacks.GetOwned()) { FarmData = new CustomFarm(); GreenHouseData = new CustomGreenHouse(); Monitor.Log($"Reading content pack: {ContentPack.Manifest.Name} {ContentPack.Manifest.Version}."); ContainsFarm = ProcessFarmType(ContentPack, out FarmData); ContainsGreenHouse = ProcessGreenHouseType(ContentPack, out GreenHouseData); if (FarmData.Version < 2.1) { FarmData = PopulateOld(ContentPack, Monitor, FarmData.Version); } if (ContainsFarm) { Monitor.Log($"\t + Contains a custom farm.", LogLevel.Trace); LoadIcon(Helper, ContentPack, FarmData); Validate(FarmData); } if (FarmData != null) { FarmData.ContentPack = ContentPack; FarmList.Add(FarmData); } if (GreenHouseData != null) { GreenHouseData.ContentPack = ContentPack; GreenHouseList.Add(GreenHouseData); } } return; }
private void CreateFarmTemplate(IModHelper helper) { CustomFarm template = new CustomFarm(); template.ID = 25; template.Name = "Example"; template.DescriptionName = "Example Farm"; template.DescriptionDetails = "A description that appears when the player hovers over the farm icon, as they are creating a new game."; template.Folder = "Example"; template.Icon = "fileNameOfIcon.png"; template.Version = 2.1f; template.CabinCapacity = 3; template.AllowClose = true; template.AllowSeperate = true; template.FarmMap = new MapFile("Farm_Example"); //template.AdditionalMaps template.FarmHouse = new Structure(new Placement(3712.00f, 520.00f), new Interaction(64, 14)); template.GreenHouse = new Structure(new Placement(1600.00f, 384.00f), new Interaction(28, 15)); template.FarmCave = new Structure(new Placement(), new Interaction(34, 5)) { Coordinates = null }; template.ShippingBin = new Structure(new Placement(), new Interaction(71, 14)) { Coordinates = null }; template.MailBox = new Structure(new Placement(), new Interaction(68, 16)) { Coordinates = null }; template.GrandpaShrine = new Structure(new Placement(), new Interaction(8, 7)) { Coordinates = null }; template.RabbitShrine = new Structure(new Placement(), new Interaction(48, 6)) { Coordinates = null }; template.PetWaterBowl = new Structure(new Placement(), new Interaction(54, 7)) { Coordinates = null }; template.Neighbors = new List <Neighbor> { new Neighbor("Backwoods") { WarpPoints = { new MTNWarp(13, 40, 117, 0) } }, new Neighbor("BusStop") { WarpPoints = { new MTNWarp(-1, 22, 155, 24), new MTNWarp(-1, 23, 155, 25) } }, new Neighbor("Forest") { WarpPoints = { new MTNWarp(67, -1, 116, 154) } } }; template.ResourceClumps = new LargeDebris() { ResourceList = new List <Spawn> { new Spawn() { } } }; template.Foraging = new Forage() { ResourceList = new List <Spawn> { new Spawn() { } } }; template.Ores = new Ore() { ResourceList = new List <Spawn> { new Spawn { } } }; helper.Data.WriteJsonFile("farmType.json", template); }