/// <summary>Loads content packs and vanilla doors.</summary>
        /// <returns>The loaded doors.</returns>
        public IList <LoadedContentPackDoorEntry> LoadContentPacks()
        {
            IList <LoadedContentPackDoorEntry> data = new List <LoadedContentPackDoorEntry>();

            // Validate each pack and load the tile sheets referenced in the process.
            foreach (IContentPack contentPack in this.helper.ContentPacks.GetOwned())
            {
                if (contentPack.Manifest.UniqueID.Equals("vanilla", StringComparison.InvariantCultureIgnoreCase))
                {
                    this.errorQueue.AddError($"{contentPack.Manifest.Name} ({contentPack.Manifest.UniqueID}) - A content pack's unique id can't be {contentPack.Manifest.UniqueID}. This pack won't be loaded.");
                    continue;
                }

                ContentPack loadedPack = contentPack.ReadJsonFile <ContentPack>("content.json");

                if (loadedPack.Version.IsNewerThan(this.helper.ModRegistry.Get(this.helper.ModRegistry.ModID).Manifest.Version))
                {
                    this.errorQueue.AddError($"{contentPack.Manifest.Name} ({contentPack.Manifest.UniqueID}) is too new to be loaded ({loadedPack.Version}). Please update Better Doors! ");
                    continue;
                }

                if (loadedPack.Version != new SemanticVersion(1, 0, 0))
                {
                    this.errorQueue.AddError($"{contentPack.Manifest.Name} ({contentPack.Manifest.UniqueID}) - Unrecognized content pack version: {loadedPack.Version}. This pack won't be loaded. ");
                    continue;
                }

                string error = null;
                IDictionary <string, Texture2D> spriteSheets = new Dictionary <string, Texture2D>();
                ISet <string> spriteNames = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);
                foreach (ContentPackDoorEntry doorEntry in loadedPack.Doors)
                {
                    if (!File.Exists(Path.Combine(contentPack.DirectoryPath, doorEntry.ImageFilePath)))
                    {
                        error = $"{doorEntry.ImageFilePath} doesn't exist";
                    }
                    else if (!spriteNames.Add(doorEntry.Name))
                    {
                        error = $"{doorEntry.Name} is repeated more than once";
                    }
                    else
                    {
                        try
                        {
                            if (!spriteSheets.ContainsKey(doorEntry.ImageFilePath))
                            {
                                Texture2D spriteSheet = contentPack.LoadAsset <Texture2D>(doorEntry.ImageFilePath);
                                spriteSheets[doorEntry.ImageFilePath] = spriteSheet;

                                if (spriteSheet.Width % 64 != 0 || spriteSheet.Height % 48 != 0)
                                {
                                    error = $"The dimensions of the sprite sheet are invalid. Must be a multiple of 64 x 48. Instead, they are {spriteSheet.Width} x {spriteSheet.Height}";
                                }
                                else
                                {
                                    Utils.IsValidTile(spriteSheet.Width, spriteSheet.Height, 16, doorEntry.TopLeftTileIndex, out error);
                                }
                            }
                        }
                        catch (ContentLoadException)
                        {
                            error = $"{doorEntry.ImageFilePath} isn't a valid image";
                        }
                    }

                    if (error != null)
                    {
                        this.errorQueue.AddError($"{contentPack.Manifest.Name} ({contentPack.Manifest.UniqueID}) - {doorEntry.Name} - This entry is invalid. Info: {error}. This entry won't be loaded.");
                        continue;
                    }

                    data.Add(new LoadedContentPackDoorEntry(contentPack.Manifest.UniqueID, spriteSheets[doorEntry.ImageFilePath], doorEntry));
                }
            }

            this.monitor.Log($"Loaded {data.Count} door sprites from content packs.", LogLevel.Trace);

            this.errorQueue.PrintErrors("Found some errors when loading door sprites from content packs:");

            // Also load the vanilla door textures.
            const string vanillaPath    = "LooseSprites/Cursors";
            Texture2D    vanillaTexture = this.helper.Content.Load <Texture2D>(vanillaPath, ContentSource.GameContent);

            data.Add(new LoadedContentPackDoorEntry("vanilla", vanillaTexture, new ContentPackDoorEntry(vanillaPath, 428, "light")));
            data.Add(new LoadedContentPackDoorEntry("vanilla", vanillaTexture, new ContentPackDoorEntry(vanillaPath, 432, "window")));
            data.Add(new LoadedContentPackDoorEntry("vanilla", vanillaTexture, new ContentPackDoorEntry(vanillaPath, 436, "saloon")));

            return(data);
        }
Esempio n. 2
0
        /// <summary>Loads content packs and vanilla doors.</summary>
        /// <returns>The loaded doors.</returns>
        public IList <ContentPackDoor> LoadContentPacks()
        {
            IList <ContentPackDoor> data = new List <ContentPackDoor>();

            // Validate each pack and load the tile sheets referenced in the process.
            foreach (IContentPack contentPack in this.helper.ContentPacks.GetOwned())
            {
                if (contentPack.Manifest.UniqueID.Equals("vanilla", StringComparison.InvariantCultureIgnoreCase))
                {
                    this.errorQueue.AddError($"{contentPack.Manifest.Name} ({contentPack.Manifest.UniqueID}) - A content pack's unique id can't be {contentPack.Manifest.UniqueID}. This pack won't be loaded.");
                    continue;
                }

                ContentPack loadedPack = contentPack.ReadJsonFile <ContentPack>("content.json");

                if (!SemanticVersion.TryParse(loadedPack.Version, out ISemanticVersion version))
                {
                    this.errorQueue.AddError($"{contentPack.Manifest.Name} ({contentPack.Manifest.UniqueID}) - The version ({loadedPack.Version}) is invalid. This pack won't be loaded.");
                    continue;
                }

                if (version.IsNewerThan(this.helper.ModRegistry.Get(this.helper.ModRegistry.ModID).Manifest.Version))
                {
                    this.errorQueue.AddError($"{contentPack.Manifest.Name} ({contentPack.Manifest.UniqueID}) - ({loadedPack.Version}) is too new to be loaded. Please update Better Doors!");
                    continue;
                }

                if (!version.Equals(new SemanticVersion("1.0.0")))
                {
                    this.errorQueue.AddError($"{contentPack.Manifest.Name} ({contentPack.Manifest.UniqueID}) - Unrecognized content pack version: {loadedPack.Version}. This pack won't be loaded.");
                    continue;
                }

                ISet <string> doorNames = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);

                foreach (KeyValuePair <string, IList <string> > entry in loadedPack.Doors)
                {
                    if (!File.Exists(Path.Combine(contentPack.DirectoryPath, entry.Key)))
                    {
                        this.QueueError(contentPack, entry.Key, $"{entry.Key} doesn't exist", false);
                        continue;
                    }

                    string    imageError  = null;
                    Texture2D spriteSheet = null;

                    try
                    {
                        spriteSheet = contentPack.LoadAsset <Texture2D>(entry.Key);

                        if (spriteSheet.Width % 64 != 0 || spriteSheet.Height % 48 != 0)
                        {
                            imageError = $"The dimensions of the sprite sheet are invalid. Must be a multiple of 64 x 48. Instead, they are {spriteSheet.Width} x {spriteSheet.Height}";
                        }
                    }
                    catch (ContentLoadException)
                    {
                        imageError = $"{entry.Key} isn't a valid image";
                    }

                    if (imageError != null)
                    {
                        this.QueueError(contentPack, entry.Key, imageError, false);
                        continue;
                    }

                    int count = 0;
                    foreach (string doorName in entry.Value)
                    {
                        string nameError = null;

                        if (!doorNames.Add(doorName))
                        {
                            nameError = $"{doorName} is repeated more than once";
                        }
                        else if (doorName.Contains(" "))
                        {
                            nameError = $"{doorName} can't have spaces in it";
                        }

                        if (nameError != null)
                        {
                            this.QueueError(contentPack, entry.Key, nameError, true);
                            continue;
                        }

                        if (!Utils.IsValidTile(spriteSheet.Width, spriteSheet.Height, 16, count * 4, out string tileError))
                        {
                            this.QueueError(contentPack, entry.Key, tileError, true);
                            continue;
                        }

                        data.Add(new ContentPackDoor(contentPack.Manifest.UniqueID, spriteSheet, doorName, Utils.ConvertTileIndexToPosition(spriteSheet.Width, Utils.TileSize, count * 4)));

                        count++;
                    }
                }
            }

            this.monitor.Log($"Loaded {data.Count} door sprites from content packs.", LogLevel.Trace);

            this.errorQueue.PrintErrors("Found some errors when loading door sprites from content packs:");

            // Also load the vanilla door textures.
            const string vanillaPath    = "LooseSprites/Cursors";
            Texture2D    vanillaTexture = this.helper.Content.Load <Texture2D>(vanillaPath, ContentSource.GameContent);

            data.Add(new ContentPackDoor("vanilla", vanillaTexture, "light", new Point(512, 144)));
            data.Add(new ContentPackDoor("vanilla", vanillaTexture, "window", new Point(576, 144)));
            data.Add(new ContentPackDoor("vanilla", vanillaTexture, "saloon", new Point(640, 144)));

            return(data);
        }