Example #1
0
        internal static void LoadData(object s, EventArgs e)
        {
            // Only allow the host player to load Adopt & Skin data
            if (Context.IsMainPlayer)
            {
                //SMonitor.Log("Multiplayer Farmhand detected. Adopt & Skin has been disabled.", LogLevel.Debug);
                SMonitor.Log("Host detected! Let's WRECK IT", LogLevel.Debug);

                // Load skin and category maps
                ModEntry.SkinMap      = SHelper.Data.ReadSaveData <Dictionary <long, int> >("skin-map") ?? new Dictionary <long, int>();
                ModEntry.IDToCategory = SHelper.Data.ReadSaveData <Dictionary <long, ModEntry.CreatureCategory> >("id-to-category") ?? new Dictionary <long, ModEntry.CreatureCategory>();

                // Load Short ID maps
                ModEntry.AnimalLongToShortIDs = SHelper.Data.ReadSaveData <Dictionary <long, int> >("animal-long-to-short-ids") ?? new Dictionary <long, int>();
                ModEntry.AnimalShortToLongIDs = SHelper.Data.ReadSaveData <Dictionary <int, long> >("animal-short-to-long-ids") ?? new Dictionary <int, long>();

                // Set up maps if save data is from an older data format of A&S
                if (ModEntry.SkinMap.Count == 0)
                {
                    LoadSkinsOldVersion();
                }

                // Load Pet ownership map
                // TODO: Similar to horse ownership loading

                // Load Horse ownership map
                Dictionary <long, long> horseOwnerMap = SHelper.Data.ReadSaveData <Dictionary <long, long> >("horse-ownership-map") ?? new Dictionary <long, long>();
                ModEntry.HorseOwnershipMap = new Dictionary <long, Farmer>();

                foreach (KeyValuePair <long, long> pair in horseOwnerMap)
                {
                    foreach (Farmer farmer in Game1.getAllFarmers())
                    {
                        if (pair.Key == ModApi.FarmerToID(farmer))
                        {
                            ModEntry.HorseOwnershipMap[pair.Key] = farmer;
                        }
                    }
                }

                // If no horses are mapped to owners, or there are some unmapped horses, assign them all to the host player
                if (ModEntry.HorseOwnershipMap.Count != ModApi.GetHorses().Count())
                {
                    foreach (Horse horse in ModApi.GetHorses())
                    {
                        long horseID = ModEntry.GetLongID(horse);
                        if (horseID != 0 && !ModEntry.HorseOwnershipMap.ContainsKey(horseID))
                        {
                            ModEntry.HorseOwnershipMap.Add(horseID, Game1.MasterPlayer);
                        }
                    }
                }
                // If there are extra horses on the mapping, remove them. This deal with a bug in older versions of A&S
                if (ModEntry.HorseOwnershipMap.Count > ModApi.GetHorses().Count())
                {
                    List <Horse> horses         = ModApi.GetHorses().ToList();
                    List <long>  existingHorses = new List <long>();
                    // Find the IDs of all existing, owned Horses
                    foreach (Horse horse in horses)
                    {
                        existingHorses.Add(ModEntry.GetLongID(horse));
                    }

                    // Find the stowaways.
                    List <long> idsToRemove = new List <long>();
                    foreach (long horseID in ModEntry.HorseOwnershipMap.Keys)
                    {
                        if (!existingHorses.Contains(horseID))
                        {
                            idsToRemove.Add(horseID);
                        }
                    }

                    // Yeet them from the mapping.
                    foreach (long id in idsToRemove)
                    {
                        ModEntry.HorseOwnershipMap.Remove(id);
                    }
                }

                // Load received first pet/horse status
                ModEntry.Creator.FirstHorseReceived = bool.Parse(SHelper.Data.ReadSaveData <string>("first-horse-received") ?? "false");
                ModEntry.Creator.FirstPetReceived   = bool.Parse(SHelper.Data.ReadSaveData <string>("first-pet-received") ?? "false");
                Entry.CheckForFirstPet(null, null);
                Entry.CheckForFirstHorse(null, null);
                //return;
            }


            // Refresh skins via skinmap
            LoadCreatureSkins();

            // Make sure Marnie's cows put some clothes on
            foreach (GameLocation loc in Game1.locations)
            {
                if (loc is Forest forest)
                {
                    foreach (FarmAnimal animal in forest.marniesLivestock)
                    {
                        string type = ModApi.GetInternalType(animal);
                        if (ModApi.HasSkins(type))
                        {
                            int[]      spriteInfo = ModApi.GetSpriteInfo(animal);
                            AnimalSkin skin       = ModEntry.GetSkin(type, ModEntry.GetRandomSkin(type));
                            animal.Sprite = new AnimatedSprite(skin.AssetKey, spriteInfo[0], spriteInfo[1], spriteInfo[2]);
                        }
                    }
                }
            }

            // Set configuration for walk-through pets
            foreach (Pet pet in ModApi.GetPets())
            {
                if (ModEntry.Config.WalkThroughPets)
                {
                    pet.farmerPassesThrough = true;
                }
                else
                {
                    pet.farmerPassesThrough = false;
                }
            }

            // Set last known animal count
            ModEntry.AnimalCount = Game1.getFarm().getAllFarmAnimals().Count;

            // Add Adopt & Skin to the update loop
            StartUpdateChecks();
        }
        internal static void LoadData(object s, EventArgs e)
        {
            // Only allow the host player to load Adopt & Skin data
            if (!Context.IsMainPlayer)
            {
                SMonitor.Log("Multiplayer Farmhand detected. Adopt & Skin has been disabled.", LogLevel.Debug);
                return;
            }

            // Load skin and category maps
            ModEntry.SkinMap      = SHelper.Data.ReadSaveData <Dictionary <long, int> >("skin-map") ?? new Dictionary <long, int>();
            ModEntry.IDToCategory = SHelper.Data.ReadSaveData <Dictionary <long, ModEntry.CreatureCategory> >("id-to-category") ?? new Dictionary <long, ModEntry.CreatureCategory>();

            // Load Short ID maps
            ModEntry.AnimalLongToShortIDs = SHelper.Data.ReadSaveData <Dictionary <long, int> >("animal-long-to-short-ids") ?? new Dictionary <long, int>();
            ModEntry.AnimalShortToLongIDs = SHelper.Data.ReadSaveData <Dictionary <int, long> >("animal-short-to-long-ids") ?? new Dictionary <int, long>();

            // Set up maps if save data is from an older A&S
            if (ModEntry.SkinMap.Count == 0)
            {
                LoadSkinsOldVersion();
            }

            // Load received first pet/horse status
            ModEntry.Creator.FirstHorseReceived = bool.Parse(SHelper.Data.ReadSaveData <string>("first-horse-received") ?? "false");
            ModEntry.Creator.FirstPetReceived   = bool.Parse(SHelper.Data.ReadSaveData <string>("first-pet-received") ?? "false");
            Entry.CheckForFirstPet(null, null);
            Entry.CheckForFirstHorse(null, null);

            // Refresh skins via skinmap
            LoadCreatureSkins();

            // Make sure Marnie's cows put some clothes on
            foreach (GameLocation loc in Game1.locations)
            {
                if (loc is Forest forest)
                {
                    foreach (FarmAnimal animal in forest.marniesLivestock)
                    {
                        string type = ModApi.GetInternalType(animal);
                        if (ModApi.HasSkins(type))
                        {
                            int[]      spriteInfo = ModApi.GetSpriteInfo(animal);
                            AnimalSkin skin       = ModEntry.GetSkin(type, ModEntry.GetRandomSkin(type));
                            animal.Sprite = new AnimatedSprite(skin.AssetKey, spriteInfo[0], spriteInfo[1], spriteInfo[2]);
                        }
                    }
                }
            }

            // Set configuration for walk-through pets
            foreach (Pet pet in ModApi.GetPets())
            {
                if (!ModApi.IsStray(pet))
                {
                    if (ModEntry.Config.WalkThroughPets)
                    {
                        pet.farmerPassesThrough = true;
                    }
                    else
                    {
                        pet.farmerPassesThrough = false;
                    }
                }
            }

            // Set last known animal count
            ModEntry.AnimalCount = Game1.getFarm().getAllFarmAnimals().Count;

            // Add Adopt & Skin to the update loop
            StartUpdateChecks();
        }