Beispiel #1
0
        /// <summary>
        /// Converts the string representation of an asset identifier to an instance of the <see cref="AssetID"/> 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="AssetID"/> structure that is equivalent to the specified string.</param>
        /// <returns><see langword="true"/> if the string was successfully parsed; otherwise, <see langword="false"/>.</returns>
        public static Boolean TryParse(ContentManifestRegistry manifests, String s, out AssetID value)
        {
            Contract.Require(manifests, nameof(manifests));
            Contract.Require(s, nameof(s));

            return(TryParseInternal(manifests, s, out value));
        }
        /// <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, nameof(reader));
            Contract.Require(manifests, nameof(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);
        }
Beispiel #3
0
        /// <summary>
        /// Converts the string representation of an asset identifier to an instance of the <see cref="AssetID"/> 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>
        /// <returns>An instance of the <see cref="AssetID"/> structure that is equivalent to the specified string.</returns>
        public static AssetID Parse(ContentManifestRegistry manifests, String s)
        {
            Contract.Require(manifests, nameof(manifests));
            Contract.Require(s, nameof(s));

            AssetID value;

            if (!TryParseInternal(manifests, s, out 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, nameof(reader));
            Contract.Require(manifests, nameof(manifests));

            var hasValue = reader.ReadBoolean();

            if (hasValue)
            {
                return(reader.ReadAssetID(manifests));
            }
            return(null);
        }
Beispiel #5
0
        /// <summary>
        /// Converts the string representation of an asset identifier to an instance of the <see cref="AssetID"/> 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="AssetID"/> 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 AssetID value)
        {
            value = default(AssetID);

            if (!s.StartsWith("#"))
            {
                return(false);
            }

            if (s == "#INVALID")
            {
                value = AssetID.Invalid;
                return(true);
            }

            var components = s.Substring(1).Split(':');

            if (components.Length != 3)
            {
                return(false);
            }

            var manifest = manifests[components[0]];

            if (manifest == null)
            {
                throw new AssetException(UltravioletStrings.ContentManifestDoesNotExist.Format(components[0]));
            }

            var manifestGroup = manifest[components[1]];

            if (manifestGroup == null)
            {
                throw new AssetException(UltravioletStrings.ContentManifestGroupDoesNotExist.Format(components[0], components[1]));
            }

            var manifestAsset = manifestGroup[components[2]];

            if (manifestAsset == null)
            {
                throw new AssetException(UltravioletStrings.AssetDoesNotExistWithinManifest.Format(components[0], components[1], components[2]));
            }

            var manifestIndex = manifestGroup.IndexOf(manifestAsset);

            value = new AssetID(manifest.Name, manifestGroup.Name, manifestAsset.Name, manifestAsset.AbsolutePath, manifestIndex);
            return(true);
        }