Example #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);
        }
        /// <summary>
        /// Loads the database which corresponds to the given database name.
        /// </summary>
        /// <param name="databaseName">The name of the database.</param>
        private void LoadDatabase(DatabaseName databaseName)
        {
            if (_databases.ContainsKey(databaseName))
            {
                return;
            }

            var genericDBCType  = typeof(DBC <>);
            var specificDBCType = genericDBCType.MakeGenericType(GetRecordTypeFromDatabaseName(databaseName));

            var databasePath = GetDatabasePackagePath(databaseName);
            var databaseData = _contentSource.ExtractFile(databasePath);

            var database = (IDBC)Activator.CreateInstance(specificDBCType, _version, databaseData);

            _databases.Add(databaseName, database);
        }