Esempio n. 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));
            }

            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);
        }
Esempio n. 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));
            }

            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);
        }
Esempio n. 3
0
        public ActionResult ImageEditor(string virtualPath, string data, string newname, bool overwrite = false)
        {
            try
            {
                if (data == null)
                {
                    return(Json(new { result = MediaEnums.EditImageEnums.SaveFail, message = "no data" }));
                }
                if (!virtualPath.StartsWith("/"))
                {
                    virtualPath = "/" + virtualPath;
                }

                var imageFormat = FileInfoUtilities.GetImageFormatFromName(virtualPath);

                var physicalPath = _mediaFileManager.GetPhysicalPathFromVirtualPath(virtualPath);
                if (newname != null)
                {
                    var filename = Path.GetFileName(physicalPath);
                    if (filename != null)
                    {
                        physicalPath = physicalPath.Substring(0, physicalPath.Length - filename.Length) + newname;
                        if (!overwrite && System.IO.File.Exists(physicalPath))
                        {
                            return(Json(new { result = MediaEnums.EditImageEnums.OverWriteConfirm }));
                        }
                    }
                }
                ImageUtilities.SaveImageFromBase64String(data,
                                                         physicalPath,
                                                         imageFormat);
                return(Json(new { result = MediaEnums.EditImageEnums.SaveSuccess }));
            }
            catch (Exception ex)
            {
                while (ex.InnerException != null)
                {
                    ex = ex.InnerException;
                }
                return(Json(new { result = MediaEnums.EditImageEnums.SaveFail, message = ex.Message }));
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Gets the type of the referenced file.
 /// </summary>
 /// <returns>The referenced file type.</returns>
 public WarcraftFileType GetReferencedFileType()
 {
     return(FileInfoUtilities.GetFileType(this.FilePath));
 }