/// <summary>
        /// Reads any Daggerfall image file to ImageData package.
        /// </summary>
        /// <param name="filename">Name of standalone file as it appears in arena2 folder.</param>
        /// <param name="record">Which image record to read for multi-image files.</param>
        /// <param name="frame">Which frame to read for multi-frame images.</param>
        /// <param name="hasAlpha">Enable this for image cutouts.</param>
        /// <param name="createTexture">Create a Texture2D.</param>
        /// <param name="createAllFrameTextures">Creates a Texture2D for every frame in a TEXTURE file (if greater than 1 frames).</param>
        /// <param name="alphaIndex">Set palette index for alpha checks (default is 0).</param>
        /// <returns>ImageData. If result.type == ImageTypes.None then read failed.</returns>
        public static ImageData GetImageData(string filename, int record = 0, int frame = 0, bool hasAlpha = false, bool createTexture = true, bool createAllFrameTextures = false, int alphaIndex = 0)
        {
            // Check API ready
            DaggerfallUnity dfUnity = DaggerfallUnity.Instance;

            if (!dfUnity.IsReady)
            {
                return(new ImageData());
            }

            // Parse image file type
            ImageTypes fileType;

            try
            {
                fileType = ParseFileType(filename);
            }
            catch
            {
                return(new ImageData());
            }

            // Create base image data
            ImageData imageData = new ImageData();

            imageData.type       = fileType;
            imageData.filename   = filename;
            imageData.record     = record;
            imageData.frame      = frame;
            imageData.hasAlpha   = hasAlpha;
            imageData.alphaIndex = alphaIndex;

            // Read supported image files
            DFBitmap dfBitmap = null;

            DFBitmap[] dfBitmapAllFrames = null;
            switch (fileType)
            {
            case ImageTypes.TEXTURE:
                TextureFile textureFile = new TextureFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                textureFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, textureFile.PaletteName));
                dfBitmap = textureFile.GetDFBitmap(record, frame);
                int frameCount = textureFile.GetFrameCount(record);
                if (createAllFrameTextures && frameCount > 1)
                {
                    dfBitmapAllFrames = new DFBitmap[frameCount];
                    for (int i = 0; i < frameCount; i++)
                    {
                        dfBitmapAllFrames[i] = textureFile.GetDFBitmap(record, i);
                    }
                }
                imageData.offset = textureFile.GetOffset(record);
                imageData.scale  = textureFile.GetScale(record);
                imageData.size   = textureFile.GetSize(record);

                // Texture pack support
                int archive = AssetInjection.TextureReplacement.FileNameToArchive(filename);
                if (createTexture && AssetInjection.TextureReplacement.TryImportTexture(archive, record, frame, out imageData.texture))
                {
                    createTexture = false;
                }
                if (createAllFrameTextures && frameCount > 1 && AssetInjection.TextureReplacement.TryImportTexture(archive, record, out imageData.animatedTextures))
                {
                    createAllFrameTextures = false;
                }

                break;

            case ImageTypes.IMG:
                ImgFile imgFile = new ImgFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                imgFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, imgFile.PaletteName));
                dfBitmap         = imgFile.GetDFBitmap();
                imageData.offset = imgFile.ImageOffset;
                imageData.scale  = new DFSize();
                imageData.size   = imgFile.GetSize(0);

                // Texture pack support
                if (createTexture && AssetInjection.TextureReplacement.TryImportImage(filename, false, out imageData.texture))
                {
                    createTexture = false;
                }

                break;

            case ImageTypes.CIF:
            case ImageTypes.RCI:
                CifRciFile cifFile = new CifRciFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                cifFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, cifFile.PaletteName));
                dfBitmap         = cifFile.GetDFBitmap(record, frame);
                imageData.offset = cifFile.GetOffset(record);
                imageData.scale  = new DFSize();
                imageData.size   = cifFile.GetSize(record);

                // Texture pack support
                if (createTexture && AssetInjection.TextureReplacement.TryImportCifRci(filename, record, frame, false, out imageData.texture))
                {
                    createTexture = false;
                }

                break;

            case ImageTypes.CFA:
                CfaFile cfaFile = new CfaFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                cfaFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, cfaFile.PaletteName));

                dfBitmap         = cfaFile.GetDFBitmap(record, frame);
                imageData.offset = new DFPosition(0, 0);
                imageData.scale  = new DFSize();
                imageData.size   = cfaFile.GetSize(record);

                // Texture pack support
                if (createTexture && AssetInjection.TextureReplacement.TryImportCifRci(filename, record, frame, false, out imageData.texture))
                {
                    createTexture = false;
                }

                break;

            case ImageTypes.BSS:
                BssFile bssFile = new BssFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                bssFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, bssFile.PaletteName));

                dfBitmap         = bssFile.GetDFBitmap(record, frame);
                imageData.offset = new DFPosition(0, 0);
                imageData.scale  = new DFSize();
                imageData.size   = bssFile.GetSize(record);

                // Texture pack support
                if (createTexture && AssetInjection.TextureReplacement.TryImportCifRci(filename, record, frame, false, out imageData.texture))
                {
                    createTexture = false;
                }

                break;

            case ImageTypes.GFX:
                GfxFile gfxFile = new GfxFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                gfxFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, gfxFile.PaletteName));

                dfBitmap         = gfxFile.GetDFBitmap(record, frame);
                imageData.offset = new DFPosition(0, 0);
                imageData.scale  = new DFSize();
                imageData.size   = gfxFile.GetSize(record);

                // Texture pack support
                if (createTexture && AssetInjection.TextureReplacement.TryImportCifRci(filename, record, frame, false, out imageData.texture))
                {
                    createTexture = false;
                }

                break;

            default:
                return(new ImageData());
            }

            // Store bitmap
            imageData.dfBitmap = dfBitmap;
            imageData.width    = dfBitmap.Width;
            imageData.height   = dfBitmap.Height;

            // Create Texture2D
            if (createTexture)
            {
                // Get colors array
                Color32[] colors = GetColors(imageData);
                if (colors == null)
                {
                    return(new ImageData());
                }

                // Create new Texture2D
                imageData.texture = GetTexture(colors, imageData.width, imageData.height);
            }

            // Create animated Texture2D frames
            if (createAllFrameTextures && dfBitmapAllFrames != null)
            {
                imageData.animatedTextures = new Texture2D[dfBitmapAllFrames.Length];
                for (int i = 0; i < dfBitmapAllFrames.Length; i++)
                {
                    ImageData curFrame = imageData;
                    curFrame.dfBitmap = dfBitmapAllFrames[i];
                    Color32[] colors = GetColors(curFrame);
                    imageData.animatedTextures[i] = GetTexture(colors, imageData.width, imageData.height);
                }
            }

            return(imageData);
        }
Esempio n. 2
0
        /// <summary>
        /// Reads any Daggerfall image file to ImageData package.
        /// </summary>
        /// <param name="filename">Name of standalone file as it appears in arena2 folder.</param>
        /// <param name="record">Which image record to read for multi-image files.</param>
        /// <param name="frame">Which frame to read for multi-frame images.</param>
        /// <param name="hasAlpha">Enable this for image cutouts.</param>
        /// <param name="createTexture">Create a Texture2D.</param>
        /// <returns>ImageData. If result.type == ImageTypes.None then read failed.</returns>
        public static ImageData GetImageData(string filename, int record = 0, int frame = 0, bool hasAlpha = false, bool createTexture = true)
        {
            // Check API ready
            DaggerfallUnity dfUnity = DaggerfallUnity.Instance;

            if (!dfUnity.IsReady)
            {
                return(new ImageData());
            }

            // Parse image file type
            ImageTypes fileType;

            try
            {
                fileType = ParseFileType(filename);
            }
            catch
            {
                return(new ImageData());
            }

            // Create base image data
            ImageData imageData = new ImageData();

            imageData.type     = fileType;
            imageData.filename = filename;
            imageData.record   = record;
            imageData.frame    = frame;
            imageData.hasAlpha = hasAlpha;

            // Read supported image files
            DFBitmap dfBitmap = null;

            switch (fileType)
            {
            case ImageTypes.TEXTURE:
                TextureFile textureFile = new TextureFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                textureFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, textureFile.PaletteName));
                dfBitmap         = textureFile.GetDFBitmap(record, frame);
                imageData.offset = textureFile.GetOffset(record);
                imageData.scale  = textureFile.GetScale(record);
                imageData.size   = textureFile.GetSize(record);
                break;

            case ImageTypes.IMG:
                ImgFile imgFile = new ImgFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                imgFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, imgFile.PaletteName));
                dfBitmap         = imgFile.GetDFBitmap();
                imageData.offset = imgFile.ImageOffset;
                imageData.scale  = new DFSize();
                imageData.size   = imgFile.GetSize(0);
                break;

            case ImageTypes.CIF:
            case ImageTypes.RCI:
                CifRciFile cifFile = new CifRciFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                cifFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, cifFile.PaletteName));
                dfBitmap         = cifFile.GetDFBitmap(record, frame);
                imageData.offset = cifFile.GetOffset(record);
                imageData.scale  = new DFSize();
                imageData.size   = cifFile.GetSize(record);
                break;

            default:
                return(new ImageData());
            }

            // Store bitmap
            imageData.dfBitmap = dfBitmap;
            imageData.width    = dfBitmap.Width;
            imageData.height   = dfBitmap.Height;

            // Create Texture2D
            if (createTexture)
            {
                // Get colors array
                Color32[] colors = GetColors(imageData);
                if (colors == null)
                {
                    return(new ImageData());
                }

                // Create new Texture2D
                imageData.texture = GetTexture(colors, imageData.width, imageData.height);
            }

            return(imageData);
        }