Exemplo n.º 1
0
        public static byte[] ConvertDDS(GUIDEntry value, DXGI_FORMAT targetFormat, DDSConverter.ImageFormat imageFormat, int frame)
        {
            try {
                if (GetDataType(value) != DataType.Image)
                {
                    return(null);
                }

                teTexture texture = new teTexture(IOHelper.OpenFile(value));
                if (texture.PayloadRequired)
                {
                    ulong payload = texture.GetPayloadGUID(value.GUID, 1);
                    if (IOHelper.HasFile(payload))
                    {
                        texture.LoadPayload(IOHelper.OpenFile(payload), 1);
                    }
                    else
                    {
                        return(null);
                    }
                }

                Stream ms = texture.SaveToDDS(1);

                return(DDSConverter.ConvertDDS(ms, targetFormat, imageFormat, frame));
            } catch {
                // ignored
            }

            return(null);
        }
Exemplo n.º 2
0
        private void ExportDDS(IExportContainer container, Stream destination, Stream source, long length)
        {
            DDSConvertParameters @params = new DDSConvertParameters()
            {
                DataLength          = length,
                MipMapCount         = MipCount,
                Width               = Width,
                Height              = Height,
                IsPitchOrLinearSize = DDSIsPitchOrLinearSize,
                PixelFormatFlags    = DDSPixelFormatFlags,
                FourCC              = (DDSFourCCType)DDSFourCC,
                RGBBitCount         = DDSRGBBitCount,
                RBitMask            = DDSRBitMask,
                GBitMask            = DDSGBitMask,
                BBitMask            = DDSBBitMask,
                ABitMask            = DDSABitMask,
                Caps = DDSCaps(container.Version),
            };

            EndianType endianess = IsSwapBytes(container.Platform) ? EndianType.BigEndian : EndianType.LittleEndian;

            using (EndianReader sourceReader = new EndianReader(source, endianess))
            {
                DDSConverter.ExportDDS(sourceReader, destination, @params);
            }
        }
Exemplo n.º 3
0
        private void ExportDDS(IExportContainer container, Stream destination, Stream source, long length)
        {
            DDSConvertParameters @params = new DDSConvertParameters()
            {
                DataLength          = length,
                MipMapCount         = MipCount,
                Width               = Width,
                Height              = Height,
                IsPitchOrLinearSize = DDSIsPitchOrLinearSize,
                PixelFormatFlags    = DDSPixelFormatFlags,
                FourCC              = (DDSFourCCType)DDSFourCC,
                RGBBitCount         = DDSRGBBitCount,
                RBitMask            = DDSRBitMask,
                GBitMask            = DDSGBitMask,
                BBitMask            = DDSBBitMask,
                ABitMask            = DDSABitMask,
                Caps = DDSCaps(container.Version),
            };

            if (IsSwapBytes(container.Platform))
            {
                using (ReverseStream reverse = new ReverseStream(source, source.Position, length, true))
                {
                    DDSConverter.ExportDDS(destination, reverse, @params);
                }
            }
            else
            {
                DDSConverter.ExportDDS(destination, source, @params);
            }
        }
 public static ImageSource Convert(byte[] imageData)
 {
     using (var memoryStream = new MemoryStream(imageData))
     {
         return(DDSConverter.Convert(memoryStream));
     }
 }
 public static ImageSource Convert(string filePath)
 {
     using (var fileStream = File.OpenRead(filePath))
     {
         return(DDSConverter.Convert(fileStream));
     }
 }
Exemplo n.º 6
0
        public static byte[] ConvertDDS(GUIDEntry value, DXGI_FORMAT targetFormat, DDSConverter.ImageFormat imageFormat, int frame)
        {
            try {
                if (GetDataType(value) != DataType.Image)
                {
                    return(null);
                }

                teTexture texture = LoadTexture(value);
                Stream    ms      = texture.SaveToDDS(1);

                return(DDSConverter.ConvertDDS(ms, targetFormat, imageFormat, frame));
            } catch {
                // ignored
            }

            return(null);
        }
Exemplo n.º 7
0
        public static byte[] ConvertDDS(ulong guid, DXGI_FORMAT targetFormat, WICCodecs imageFormat, int frame)
        {
            try {
                if (GetDataType(guid) != DataType.Image)
                {
                    return(null);
                }

                teTexture texture = LoadTexture(guid);
                Stream    ms      = texture.SaveToDDS(1);

                return(DDSConverter.ConvertDDS(ms, targetFormat, imageFormat, frame));
            } catch {
                // ignored
            }

            return(null);
        }
Exemplo n.º 8
0
        private byte[] ExportDDS(byte[] data, int offset, int length)
        {
            PrepareForDDSExport(ref data, ref offset, ref length);

            DDSConvertParameters @params = new DDSConvertParameters()
            {
                DataLength          = length,
                MipMapCount         = MipCount,
                Width               = Width,
                Height              = Height,
                IsPitchOrLinearSize = DDSIsPitchOrLinearSize,
                PixelFormatFlags    = DDSPixelFormatFlags,
                FourCC              = DDSFourCC,
                RGBBitCount         = DDSRGBBitCount,
                RBitMask            = DDSRBitMask,
                GBitMask            = DDSGBitMask,
                BBitMask            = DDSBBitMask,
                ABitMask            = DDSABitMask,
                Caps = DDSCaps,
            };

            return(DDSConverter.WrapToDDS(data, offset, @params));
        }
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     if (value == null)
     {
         throw new ArgumentNullException("value");
     }
     else if (value is Stream)
     {
         return(DDSConverter.Convert((Stream)value));
     }
     else if (value is string)
     {
         return(DDSConverter.Convert((string)value));
     }
     else if (value is byte[])
     {
         return(DDSConverter.Convert((byte[])value));
     }
     else
     {
         throw new NotSupportedException(string.Format("{0} cannot convert from {1}.", this.GetType().FullName, value.GetType().FullName));
     }
 }
Exemplo n.º 10
0
        private static async Task SaveTextureTask(ICLIFlags flags, string path, SaveContext info, ulong textureGUID, string name = null)
        {
            bool   convertTextures         = true;
            string convertType             = "tif";
            string multiSurfaceConvertType = "tif";
            bool   createMultiSurfaceSheet = false;
            bool   lossless = false;
            int    maxMips  = 1;

            if (flags is ExtractFlags extractFlags)
            {
                if (extractFlags.SkipTextures)
                {
                    return;
                }
                createMultiSurfaceSheet = extractFlags.SheetMultiSurface;
                convertTextures         = !extractFlags.RawTextures && !extractFlags.Raw;
                convertType             = extractFlags.ConvertTexturesType.ToLowerInvariant();
                lossless = extractFlags.ConvertTexturesLossless;

                multiSurfaceConvertType = convertType;
                if (extractFlags.ForceDDSMultiSurface)
                {
                    multiSurfaceConvertType = "dds";
                }

                if (convertType == "dds" && extractFlags.SaveMips)
                {
                    maxMips = 0xF;
                }
            }

            if (!path.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                path += Path.DirectorySeparatorChar;
            }


            FindLogic.Combo.TextureAsset textureInfo = info.m_info.m_textures[textureGUID];
            string filePath = Path.Combine(path, name ?? $"{textureInfo.GetNameIndex()}");

            if (teResourceGUID.Type(textureGUID) != 0x4)
            {
                filePath += $".{teResourceGUID.Type(textureGUID):X3}";
            }

            if (Program.Flags != null && Program.Flags.Deduplicate)
            {
                if (ScratchDBInstance.HasRecord(textureGUID))
                {
                    return;
                }

                ScratchDBInstance[textureGUID] = new ScratchDB.ScratchPath($"{filePath}.{convertType}", true);
            }

            CreateDirectoryFromFile(path);

            await s_texurePrepareSemaphore.WaitAsync();

            try {
                if (!convertTextures)
                {
                    teTexture texture;
                    using (Stream textureStream = OpenFile(textureGUID)) {
                        texture = new teTexture(textureStream, true);
                        textureStream.Position = 0;
                        WriteFile(textureStream, $"{filePath}.004");
                    }

                    if (!texture.PayloadRequired)
                    {
                        return;
                    }
                    for (int i = 0; i < texture.Payloads.Length; ++i)
                    {
                        using (Stream texturePayloadStream = OpenFile(texture.GetPayloadGUID(textureGUID, i)))
                            WriteFile(texturePayloadStream, $"{filePath}_{i}.04D");
                    }
                }
                else
                {
                    teTexture texture;
                    using (Stream textureStream = OpenFile(textureGUID)) {
                        if (textureStream == null)
                        {
                            return;
                        }

                        texture = new teTexture(textureStream);
                    }

                    //if (texture.Header.Flags.HasFlag(teTexture.Flags.CUBEMAP)) return;
                    // for diffing when they add/regen loads of cubemaps

                    if (texture.PayloadRequired)
                    {
                        for (int i = 0; i < texture.Payloads.Length; ++i)
                        {
                            using (var payloadStream = OpenFile(texture.GetPayloadGUID(textureGUID, i)))
                                texture.LoadPayload(payloadStream, i);
                            if (maxMips == 1)
                            {
                                break;
                            }
                        }
                    }

                    uint?width    = null;
                    uint?height   = null;
                    uint?surfaces = null;
                    if (texture.Header.IsCubemap || texture.Header.IsArray || texture.HasMultipleSurfaces)
                    {
                        if (createMultiSurfaceSheet)
                        {
                            Logger.Debug("Combo", $"Saving {Path.GetFileName(filePath)} as a sheet because it has more than one surface");
                            height               = (uint)(texture.Header.Height * texture.Header.Surfaces);
                            surfaces             = 1;
                            texture.Header.Flags = 0;
                        }
                        else if (convertType != "tif" && convertType != "dds")
                        {
                            Logger.Debug("Combo", $"Saving {Path.GetFileName(filePath)} as {multiSurfaceConvertType} because it has more than one surface");
                            convertType = multiSurfaceConvertType;
                        }
                    }


                    WICCodecs?imageFormat = null;
                    switch (convertType)
                    {
                    case "tif":
                        imageFormat = WICCodecs.TIFF;
                        break;

                    case "png":
                        imageFormat = WICCodecs.PNG;
                        break;

                    case "jpg":
                        imageFormat = WICCodecs.JPEG;
                        break;
                    }

                    // if (convertType == "tga") imageFormat = Im.... oh
                    // so there is no TGA image format.
                    // sucks to be them

                    if (convertType == "dds")
                    {
                        using (Stream convertedStream = texture.SaveToDDS(maxMips == 1 ? 1 : texture.Header.MipCount, width, height, surfaces)) {
                            WriteFile(convertedStream, $"{filePath}.dds");
                        }

                        return;
                    }

                    Process pProcess;

                    using (Stream convertedStream = texture.SaveToDDS(maxMips == 1 ? 1 : texture.Header.MipCount, width, height, surfaces)) {
                        var data = DDSConverter.ConvertDDS(convertedStream, DXGI_FORMAT.R8G8B8A8_UNORM, imageFormat.Value, 0);
                        if (data != null)
                        {
                            WriteFile(data, $"{filePath}.{convertType}");
                        }
                        else
                        {
                            convertedStream.Position = 0;
                            WriteFile(convertedStream, $"{filePath}.dds");
                            Logger.Error("Combo", $"Unable to save {Path.GetFileName(filePath)} as {convertType} because DirectXTex failed.");
                        }
                    }
                }
            } finally {
                s_texurePrepareSemaphore.Release();
            }
        }
Exemplo n.º 11
0
        public static Task <Control> Get(ProgressWorker a1, SynchronizationContext context, Window window, bool npc)
        {
            var source = new TaskCompletionSource <Control>();

            context.Send(obj => {
                var control = new ImageGridView();
                var t       = new Thread(() => {
                    if (!(obj is Tuple <ProgressWorker, TaskCompletionSource <Control> > tuple))
                    {
                        return;
                    }
                    var worker = tuple.Item1;
                    var tcs    = tuple.Item2;
                    try {
                        var i = 0;
                        worker.ReportProgress(0, "Loading heroes...");
                        if (TrackedFiles == null || !TrackedFiles.ContainsKey(0x75))
                        {
                            throw new DataToolWpfException("Open storage first");
                        }

                        var max = TrackedFiles[0x75].Count;

                        foreach (var key in TrackedFiles[0x75])
                        {
                            try {
                                var hero = GetInstance <STUHero>(key);
                                if (hero == null)
                                {
                                    continue;
                                }
                                string heroNameActual = GetString(hero.m_0EDCE350) ?? teResourceGUID.Index(key).ToString("X");

                                heroNameActual = heroNameActual.TrimEnd(' ');

                                ProgressionUnlocks progressionUnlocks = new ProgressionUnlocks(hero);
                                if (progressionUnlocks.LevelUnlocks == null && !npc)
                                {
                                    continue;
                                }
                                if (progressionUnlocks.LootBoxesUnlocks != null && npc)
                                {
                                    continue;
                                }

                                var tex = hero.m_8203BFE1.FirstOrDefault(x => teResourceGUID.Index(x.m_id) == 0x40C9 || teResourceGUID.Index(x.m_id) == 0x40CA)?.m_texture;

                                if (tex == 0)
                                {
                                    tex = hero.m_8203BFE1.FirstOrDefault()?.m_texture;
                                }

                                var image = new byte[] { };

                                var width  = 128;
                                var height = 128;

                                if (tex != 0)
                                {
                                    teTexture texture = new teTexture(OpenFile(tex));
                                    if (texture.PayloadRequired)
                                    {
                                        ulong payload        = texture.GetPayloadGUID(tex);
                                        Stream payloadStream = OpenFile(payload);
                                        if (payloadStream != null)
                                        {
                                            texture.LoadPayload(payloadStream);
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }

                                    width  = texture.Header.Width;
                                    height = texture.Header.Height;

                                    Stream ms = texture.SaveToDDS();

                                    image = DDSConverter.ConvertDDS(ms, DXGI_FORMAT.R8G8B8A8_UNORM, DDSConverter.ImageFormat.PNG, 0);
                                }

                                var entry      = control.Add(heroNameActual, image, 128, (int)ImagingHelper.CalculateSizeAS(height, width, 128));
                                entry.Payload  = progressionUnlocks;
                                entry.OnClick += (sender, args) => {
                                    window.Close();
                                };
                            } catch {
                                // ignored
                            } finally {
                                i += 1;
                                worker.ReportProgress((int)(i / (float)max * 100));
                            }
                        }

                        tcs.SetResult(control);
                    } catch (Exception e) {
                        tcs.SetException(e);
                    } finally {
                        worker.ReportProgress(100);
                    }
                });
                t.SetApartmentState(ApartmentState.STA);
                t.Start();
            }, new Tuple <ProgressWorker, TaskCompletionSource <Control> >(a1, source));
            return(source.Task);
        }