Esempio n. 1
0
        /// <summary>The method that calculates what seeds should dropped from mixed seeds. This is dependant on the config and installed mods.</summary>
        private void PopulateSeeds()
        {
            List <Seed> enabledSeeds = new List <Seed>();

            // Add seeds from Stardew Valley
            enabledSeeds = enabledSeeds
                           .Union(CheckModForEnabledCrops("StardewValley"))
                           .ToList();

            // Add seeds from integrated mods
            List <string> integratedModsInstalled = CheckIntegratedMods();

            object jaApi = null;

            if (integratedModsInstalled.Count() != 0)
            {
                jaApi = this.Helper.ModRegistry.GetApi("spacechase0.JsonAssets");

                if (jaApi != null)
                {
                    foreach (string modName in integratedModsInstalled)
                    {
                        MMonitor.Log($"{modName} loaded");

                        enabledSeeds = enabledSeeds
                                       .Union(CheckModForEnabledCrops(modName))
                                       .ToList();
                    }
                }
                else
                {
                    MMonitor.Log("Failed to retrieve Json Assets API", LogLevel.Error);
                }
            }

            // Create a new collection, so no exceptions occur from changing values in the current collection loop
            List <Seed> updatedEnabledSeeds = new List <Seed>();

            foreach (var enabledSeed in enabledSeeds)
            {
                Seed newEnabledSeed = enabledSeed;
                int  seedIndex;

                // Check if the current seedName is a number, meaning it's a Stardew seed
                if (int.TryParse(enabledSeed.Name, out seedIndex))
                {
                    newEnabledSeed.Id = seedIndex;
                }
                else
                {
                    // Get seed if the current seed is from an integrated mod
                    seedIndex = this.Helper.Reflection.GetMethod(jaApi, "GetObjectId").Invoke <int>(enabledSeed.Name);

                    if (seedIndex == -1)
                    {
                        MMonitor.Log($"Could find seed index for item: {enabledSeed.Name}", LogLevel.Error);
                        continue;
                    }

                    newEnabledSeed.Id = seedIndex;
                }

                updatedEnabledSeeds.Add(newEnabledSeed);
            }

            Seeds = updatedEnabledSeeds;
        }
Esempio n. 2
0
        /// <summary>The method for finding which seeds are currently enabled in the specified mod.</summary>
        /// <param name="modName">The internal mod name that will be used for getting the list of seeds.</param>
        /// <returns>A list of enabled seeds.</returns>
        private List <Seed> CheckModForEnabledCrops(string modName)
        {
            List <Seed> seedNames = new List <Seed>();

            // Get the CropMod object for the current mod from config
            PropertyInfo configInfo = ModConfig.GetType().GetProperty(modName);
            CropMod      mod        = (CropMod)configInfo.GetValue(ModConfig);

            // Get the 4 season properties for the current CropMod
            PropertyInfo[] modSeasonsInfo = mod.GetType().GetProperties();

            // Get the seed index for finding seed names for the crops
            PropertyInfo seedIndexInfo            = SeedIndex.GetType().GetProperty(modName);
            Dictionary <string, string> seedIndex = (Dictionary <string, string>)seedIndexInfo.GetValue(SeedIndex);

            for (int i = 0; i < 4; i++)
            {
                PropertyInfo modSeasonInfo = modSeasonsInfo[i];
                Season       season        = (Season)modSeasonInfo.GetValue(mod);
                string       seasonName    = modSeasonInfo.Name.ToLower();

                if (season == null)
                {
                    continue;
                }

                foreach (Crop crop in season.Crops)
                {
                    if (crop.Enabled)
                    {
                        string seedName = seedIndex
                                          .Where(seed => seed.Key == crop.Name)
                                          .Select(seed => seed.Value)
                                          .FirstOrDefault();

                        if (string.IsNullOrEmpty(seedName))
                        {
                            MMonitor.Log($"Seed name for {crop.Name} couldn't be found", LogLevel.Error);
                            continue;
                        }

                        // If the seed is already in the dictionary, add the season to the array
                        if (seedNames.Where(seed => seed.Name == seedName).Any())
                        {
                            // Add the season to all instances of the seed
                            seedNames
                            .Where(seed => seed.Name == seedName)
                            .Select(seed => seed.Seasons.Add(seasonName));
                        }
                        else
                        {
                            // Add the to the list [Chance] number of times, so it has an effect on the final result
                            for (int j = 0; j < crop.Chance; j++)
                            {
                                seedNames.Add(new Seed(0, seedName, new string[1] {
                                    seasonName
                                }));
                            }

                            MMonitor.Log($"{seedName} has been added to the seed list");
                        }
                    }
                }
            }

            return(seedNames);
        }