/// <summary>Try to parse an instance from a string.</summary> /// <param name="propertyString">The string to parse.</param> /// <param name="error">The error while parsing, if any.</param> /// <param name="property">The resulting property, if no errors.</param> /// <returns>If parsing was successful.</returns> public static bool TryParseString(string propertyString, out string error, out MapDoorVersion property) { error = null; property = null; string[] parts = propertyString.Split(' '); if (parts.Length == 0) { error = "No property version specified"; return(false); } if (parts.Length > 1) { error = "Invalid version format"; return(false); } if (!SemanticVersion.TryParse(parts[0], out ISemanticVersion version)) { error = $"Invalid property version {parts[0]}"; return(false); } if (!version.Equals(new SemanticVersion("1.0.0"))) { error = $"Property version {version} is not recognized"; return(false); } property = new MapDoorVersion(version); return(true); }
/// <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); }