Beispiel #1
0
        /// <summary>
        /// Gets a <see cref="Texture2D"/> instance from the cache. If the texture is not already cached, it is
        /// extracted from the given <see cref="IPackage"/>. If it is cached, the cached version is returned. If no
        /// texture can be extracted, a fallback texture is returned.
        /// </summary>
        /// <param name="texturePath">The path to the texture in the package.</param>
        /// <param name="package">The package where the texture is stored.</param>
        /// <param name="wrappingModeS">The wrapping mode to use for the texture on the S axis.</param>
        /// <param name="wrappingModeT">The wrapping mode to use for the texture on the T axis.</param>
        /// <returns>A <see cref="Texture2D"/> object.</returns>
        public Texture2D GetTexture(string texturePath, IPackage package, TextureWrapMode wrappingModeS = TextureWrapMode.Repeat, TextureWrapMode wrappingModeT = TextureWrapMode.Repeat)
        {
            ThrowIfDisposed();

            if (HasCachedTextureForPath(texturePath))
            {
                return(GetCachedTexture(texturePath));
            }

            try
            {
                WarcraftFileType textureType = FileInfoUtilities.GetFileType(texturePath);
                switch (textureType)
                {
                case WarcraftFileType.BinaryImage:
                {
                    var textureData = package.ExtractFile(texturePath);

                    if (textureData == null)
                    {
                        return(this.FallbackTexture);
                    }

                    BLP texture = new BLP(textureData);
                    return(CreateCachedTexture(texture, texturePath, wrappingModeS, wrappingModeT));
                }

                case WarcraftFileType.BitmapImage:
                case WarcraftFileType.GIFImage:
                case WarcraftFileType.IconImage:
                case WarcraftFileType.PNGImage:
                case WarcraftFileType.JPGImage:
                case WarcraftFileType.TargaImage:
                {
                    using (MemoryStream ms = new MemoryStream(package.ExtractFile(texturePath)))
                    {
                        Bitmap texture = new Bitmap(ms);
                        return(CreateCachedTexture(texture, texturePath));
                    }
                }
                }
            }
            catch (InvalidFileSectorTableException fex)
            {
                Log.Warn
                (
                    $"Failed to load the texture \"{texturePath}\" due to an invalid sector table (\"{fex.Message}\").\nA fallback texture has been loaded instead."
                );
            }

            return(this.FallbackTexture);
        }
Beispiel #2
0
        /// <summary>
        /// Gets a <see cref="Texture2D"/> instance from the cache. If the texture is not already cached, it is
        /// extracted from the given <see cref="IPackage"/>. If it is cached, the cached version is returned. If no
        /// texture can be extracted, a fallback texture is returned.
        /// </summary>
        /// <param name="texturePath">The path to the texture in the package.</param>
        /// <param name="package">The package where the texture is stored.</param>
        /// <param name="wrappingModeS">The wrapping mode to use for the texture on the S axis.</param>
        /// <param name="wrappingModeT">The wrapping mode to use for the texture on the T axis.</param>
        /// <returns>A <see cref="Texture2D"/> object.</returns>
        public Texture2D GetTexture
        (
            string texturePath,
            IPackage package,
            TextureWrapMode wrappingModeS = TextureWrapMode.Repeat,
            TextureWrapMode wrappingModeT = TextureWrapMode.Repeat
        )
        {
            ThrowIfDisposed();

            if (HasCachedTextureForPath(texturePath))
            {
                return(GetCachedTexture(texturePath));
            }

            var textureType = FileInfoUtilities.GetFileType(texturePath);

            switch (textureType)
            {
            case WarcraftFileType.BinaryImage:
            {
                if (!package.TryExtractFile(texturePath, out var textureData))
                {
                    return(this.FallbackTexture);
                }

                var texture = new BLP(textureData);
                return(CreateCachedTexture(texture, texturePath, wrappingModeS, wrappingModeT));
            }

            case WarcraftFileType.BitmapImage:
            case WarcraftFileType.GIFImage:
            case WarcraftFileType.IconImage:
            case WarcraftFileType.PNGImage:
            case WarcraftFileType.JPGImage:
            case WarcraftFileType.TargaImage:
            {
                if (!package.TryExtractFile(texturePath, out var data))
                {
                    return(this.FallbackTexture);
                }

                using (var ms = new MemoryStream(data))
                {
                    var texture = new Bitmap(ms);
                    return(CreateCachedTexture(texture, texturePath));
                }
            }
            }

            return(this.FallbackTexture);
        }
Beispiel #3
0
 /// <summary>
 /// Gets the type of the referenced file.
 /// </summary>
 /// <returns>The referenced file type.</returns>
 public WarcraftFileType GetReferencedFileType()
 {
     return(FileInfoUtilities.GetFileType(this.FilePath));
 }