コード例 #1
0
        /// <summary>Loads all the sprites from the specified directory into the correct sprite pool.</summary>
        /// <param name="directory">The absolute directory containing the sprites.</param>
        /// <param name="contentPack">The content pack currently being loaded.</param>
        /// <param name="season">The season to load the images into.</param>
        private void LoadSpritesFromDirectory(string directory, IContentPack contentPack, ContentPackConfig contentPackConfig, string season)
        {
            foreach (var file in Directory.GetFiles(directory))
            {
                // ensure file is an image file
                if (!file.EndsWith(".png"))
                {
                    this.Monitor.Log($"Invalid file in folder: {file}");
                    continue;
                }

                // get the grass texture
                var relativePath = Path.Combine(season, Path.GetFileName(file));

                // a rare bug on Unix causes Directory.GetFiles(string) to return invalid files, it'll return a list of the expected files as well as a copy of each file but prefixed with "._"
                // these files don't actually exist and cause the below to throw an exception, I tried checking if the files started with "._" but that didn't work, in the end silently
                // catching the exception and ignoring it seemed to be the only way for it to work. silently catching shouldn't be a problem here as that shouldn't throw any other exception anyway
                Texture2D grassTexture;
                try { grassTexture = contentPack.LoadAsset <Texture2D>(relativePath); }
                catch { continue; }

                if (grassTexture == null)
                {
                    this.Monitor.Log($"Failed to get grass sprite. Path expected: {relativePath}");
                    continue;
                }

                // add the texture to the correct sprite pool
                var whiteListedLocations = contentPackConfig.WhiteListedLocations ?? new List <string>();
                whiteListedLocations.AddRange(contentPackConfig.WhiteListedGrass.FirstOrDefault(whiteListedGrass => whiteListedGrass.Key.ToLower() == new FileInfo(file).Name.ToLower()).Value ?? new List <string>());

                var blackListedLocations = contentPackConfig.BlackListedLocations ?? new List <string>();
                blackListedLocations.AddRange(contentPackConfig.BlackListedGrass.FirstOrDefault(blackListedGrass => blackListedGrass.Key.ToLower() == new FileInfo(file).Name.ToLower()).Value ?? new List <string>());

                switch (season)
                {
                case "spring": SpringSpritePool.AddCustomGrass(grassTexture, whiteListedLocations, blackListedLocations); break;

                case "summer": SummerSpritePool.AddCustomGrass(grassTexture, whiteListedLocations, blackListedLocations); break;

                case "fall": FallSpritePool.AddCustomGrass(grassTexture, whiteListedLocations, blackListedLocations); break;

                case "winter": WinterSpritePool.AddCustomGrass(grassTexture, whiteListedLocations, blackListedLocations); break;
                }
            }
        }