public static Boolean TryParse(ContentManifestRegistry manifests, String s, out SpriteAnimationID value) { Contract.Require(manifests, nameof(manifests)); Contract.Require(s, nameof(s)); return(TryParseInternal(manifests, s, out value)); }
/// <summary> /// Converts the string representation of an asset identifier to an instance of the SpriteAnimationID structure. /// </summary> /// <param name="manifests">The content manifest registry that contains the currently-loaded content manifests.</param> /// <param name="s">A string containing the asset identifier to convert.</param> /// <param name="value">An instance of the SpriteAnimationID structure that is equivalent to the specified string.</param> /// <returns><see langword="true"/> if the string was successfully parsed; otherwise, <see langword="false"/>.</returns> private static Boolean TryParseInternal(ContentManifestRegistry manifests, String s, out SpriteAnimationID value) { value = default(SpriteAnimationID); var delimiterIndex = s.LastIndexOf(':'); if (delimiterIndex < 0) { return(false); } var assetID = AssetID.Invalid; if (!AssetID.TryParse(manifests, s.Substring(0, delimiterIndex), out assetID)) { return(false); } var animation = s.Substring(delimiterIndex + 1); if (String.IsNullOrEmpty(animation)) { return(false); } var animationIndex = 0; var animationIndexIsValid = Int32.TryParse(animation, out animationIndex); value = animationIndexIsValid ? new SpriteAnimationID(assetID, animationIndex) : new SpriteAnimationID(assetID, animation); return(true); }
/// <summary> /// Converts the string representation of a <see cref="SourcedAssetID"/> to an object instance. /// A return value indicates whether the conversion succeeded. /// </summary> /// <param name="manifests">The content manifest registry that contains the currently-loaded content manifests.</param> /// <param name="s">The string to convert.</param> /// <param name="v">The converted value.</param> /// <returns><see langword="true"/> if the conversion succeeded; otherwise, <see langword="false"/>.</returns> public static Boolean TryParse(ContentManifestRegistry manifests, String s, out SourcedAssetID v) { Contract.Require(manifests, nameof(manifests)); Contract.Require(s, nameof(s)); return(TryParseInternal(manifests, s, out v)); }
/// <summary> /// Reads an asset identifier from the stream. /// </summary> /// <param name="reader">The binary reader from which to read the asset identifier.</param> /// <param name="manifests">The registry that contains the application's loaded manifests.</param> /// <returns>An instance of the <see cref="AssetID"/> structure representing the /// asset identifier that was read from the stream.</returns> public static AssetID ReadAssetID(this BinaryReader reader, ContentManifestRegistry manifests) { Contract.Require(reader, "reader"); Contract.Require(manifests, "manifests"); var valid = reader.ReadBoolean(); if (valid) { var manifestName = reader.ReadString(); var manifestGroupName = reader.ReadString(); var assetName = reader.ReadString(); var manifest = manifests[manifestName]; if (manifest == null) return AssetID.Invalid; var manifestGroup = manifest[manifestGroupName]; if (manifestGroup == null) return AssetID.Invalid; var asset = manifestGroup[assetName]; if (asset == null) return AssetID.Invalid; return asset.CreateAssetID(); } return AssetID.Invalid; }
/// <summary> /// Converts the string representation of a sprite animation identifier to an instance of the SpriteAnimationID structure. /// </summary> /// <param name="manifests">The content manifest registry that contains the currently-loaded content manifests.</param> /// <param name="s">A string containing the sprite animation identifier to convert.</param> /// <returns>An instance of the SpriteAnimationID structure that is equivalent to the specified string.</returns> public static SpriteAnimationID Parse(ContentManifestRegistry manifests, String s) { Contract.Require(manifests, nameof(manifests)); Contract.Require(s, nameof(s)); if (!TryParseInternal(manifests, s, out SpriteAnimationID value)) { throw new FormatException(); } return(value); }
/// <summary> /// Reads a nullable asset identifier from the stream. /// </summary> /// <param name="reader">The binary reader from which to read the asset identifier.</param> /// <param name="manifests">The registry that contains the application's loaded manifests.</param> /// <returns>An instance of the <see cref="System.Nullable{AssetID}"/> structure representing the /// asset identifier that was read from the stream.</returns> public static AssetID? ReadNullableAssetID(this BinaryReader reader, ContentManifestRegistry manifests) { Contract.Require(reader, "reader"); Contract.Require(manifests, "manifests"); var hasValue = reader.ReadBoolean(); if (hasValue) { return reader.ReadAssetID(manifests); } return null; }
/// <summary> /// Reads a nullable asset identifier from the stream. /// </summary> /// <param name="reader">The <see cref="BinaryReader"/> from which to read the sprite animation identifier.</param> /// <param name="manifests">The <see cref="ContentManifestRegistry"/> that contains the application's loaded manifests.</param> /// <returns>The <see cref="SpriteAnimationID"/> that was read from the stream.</returns> public static SpriteAnimationID? ReadNullableSpriteAnimationID(this BinaryReader reader, ContentManifestRegistry manifests) { Contract.Require(reader, nameof(reader)); Contract.Require(manifests, nameof(manifests)); var hasValue = reader.ReadBoolean(); if (hasValue) { return reader.ReadSpriteAnimationID(manifests); } return null; }
/// <summary> /// Reads a sprite animation identifier from the stream. /// </summary> /// <param name="reader">The <see cref="BinaryReader"/> from which to read the sprite animation identifier.</param> /// <param name="manifests">The <see cref="ContentManifestRegistry"/> that contains the application's loaded manifests.</param> /// <returns>The <see cref="SpriteAnimationID"/> that was read from the stream.</returns> public static SpriteAnimationID ReadSpriteAnimationID(this BinaryReader reader, ContentManifestRegistry manifests) { Contract.Require(reader, nameof(reader)); Contract.Require(manifests, nameof(manifests)); var valid = reader.ReadBoolean(); if (valid) { var spriteAssetID = reader.ReadAssetID(); var animationName = reader.ReadString(); var animationIndex = reader.ReadInt32(); return String.IsNullOrEmpty(animationName) ? new SpriteAnimationID(spriteAssetID, animationIndex) : new SpriteAnimationID(spriteAssetID, animationName); } return SpriteAnimationID.Invalid; }
/// <summary> /// Converts the string representation of an asset identifier to an instance of the <see cref="SourcedAssetID"/> structure. /// </summary> /// <param name="manifests">The content manifest registry that contains the currently-loaded content manifests.</param> /// <param name="s">A string containing the asset identifier to convert.</param> /// <param name="value">An instance of the <see cref="SourcedAssetID"/> structure that is equivalent to the specified string.</param> /// <returns><see langword="true"/> if the string was successfully parsed; otherwise, <see langword="false"/>.</returns> private static Boolean TryParseInternal(ContentManifestRegistry manifests, String s, out SourcedAssetID value) { var parts = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length > 2) { throw new FormatException(); } // Parse the asset identifier var assetID = default(AssetID); var assetIDParsed = false; if (manifests == null) { assetIDParsed = AssetID.TryParse(parts[0], out assetID); } else { assetIDParsed = AssetID.TryParse(manifests, parts[0], out assetID); } if (!assetIDParsed) { value = default(SourcedAssetID); return(false); } // Parse the asset source AssetSource assetSource = AssetSource.Global; if (parts.Length == 2) { if (!Enum.TryParse(parts[1], true, out assetSource)) { value = default(SourcedAssetID); return(false); } } value = new SourcedAssetID(assetID, assetSource); return(true); }
/// <summary> /// Reads a nullable asset identifier from the stream. /// </summary> /// <param name="reader">The <see cref="BinaryReader"/> from which to read the sprite animation identifier.</param> /// <param name="manifests">The <see cref="ContentManifestRegistry"/> that contains the application's loaded manifests.</param> /// <returns>The <see cref="SpriteAnimationID"/> that was read from the stream.</returns> public static SpriteAnimationID?ReadNullableSpriteAnimationID(this BinaryReader reader, ContentManifestRegistry manifests) { Contract.Require(reader, nameof(reader)); Contract.Require(manifests, nameof(manifests)); var hasValue = reader.ReadBoolean(); if (hasValue) { return(reader.ReadSpriteAnimationID(manifests)); } return(null); }
/// <summary> /// Reads a sprite animation identifier from the stream. /// </summary> /// <param name="reader">The <see cref="BinaryReader"/> from which to read the sprite animation identifier.</param> /// <param name="manifests">The <see cref="ContentManifestRegistry"/> that contains the application's loaded manifests.</param> /// <returns>The <see cref="SpriteAnimationID"/> that was read from the stream.</returns> public static SpriteAnimationID ReadSpriteAnimationID(this BinaryReader reader, ContentManifestRegistry manifests) { Contract.Require(reader, nameof(reader)); Contract.Require(manifests, nameof(manifests)); var valid = reader.ReadBoolean(); if (valid) { var spriteAssetID = reader.ReadAssetID(); var animationName = reader.ReadString(); var animationIndex = reader.ReadInt32(); return(String.IsNullOrEmpty(animationName) ? new SpriteAnimationID(spriteAssetID, animationIndex) : new SpriteAnimationID(spriteAssetID, animationName)); } return(SpriteAnimationID.Invalid); }