Exemple #1
0
        public static async Task Ddt2PngAsync(string ddtFile)
        {
            var outname = ddtFile.ToLower().Replace(".ddt", ".png");

            using (var fileStream = new FileStream(outname, FileMode.Create))
            {
                // DdtFile ddt = new DdtFile(File.ReadAllBytes(ddtFile));
                BitmapEncoder encoder = new PngBitmapEncoder();
                var           data    = await File.ReadAllBytesAsync(ddtFile);

                if (Alz4Utils.IsAlz4File(data))
                {
                    data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                }
                else
                {
                    if (L33TZipUtils.IsL33TZipFile(data))
                    {
                        data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                    }
                }


                //await File.WriteAllBytesAsync("test", data);

                encoder.Frames.Add(BitmapFrame.Create(new DdtFile(data, false).Bitmap));
                encoder.Save(fileStream);
            }
        }
Exemple #2
0
        public static async Task DdtBytes2TgaAsync(byte[] source, string path)
        {
            var data = source;

            if (Alz4Utils.IsAlz4File(data))
            {
                data = await Alz4Utils.ExtractAlz4BytesAsync(data);
            }
            else
            {
                if (L33TZipUtils.IsL33TZipFile(data))
                {
                    data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                }
            }
            DdtFile          ddt     = new DdtFile(data, false);
            PngBitmapEncoder encoder = new PngBitmapEncoder();

            byte[] raw_image = new byte[4 * ddt.BaseWidth * ddt.BaseHeight];
            ddt.Bitmap.CopyPixels(raw_image, 4 * ddt.BaseWidth, 0);
            var tga = new TGAImage(ddt.BaseWidth, ddt.BaseHeight, (byte)ddt.Usage, (byte)ddt.Alpha, (byte)ddt.Format, ddt.MipmapLevels, raw_image);

            var file_name = Path.GetFileNameWithoutExtension(path);

            file_name = file_name + ".(" + tga.image_id[0].ToString() + "," + tga.image_id[1].ToString() + "," + tga.image_id[2].ToString() + "," + tga.image_id[3].ToString() + ").tga";

            await File.WriteAllBytesAsync(Path.Combine(Path.GetDirectoryName(path), file_name), tga.ToByteArray());
        }
        public async Task saveFiles(List <BarEntry> files, string savePath, bool Decompress, CancellationToken token, ExportDDT settings)
        {
            ResetProgress();
            if (files.Count == 0)
            {
                return;
            }

            using var input = File.OpenRead(barFilePath);

            long filesSize = files.Sum(x => (long)x.FileSize2);

            foreach (var file in files)
            {
                if (token.IsCancellationRequested)
                {
                    while (extractingState == 1)
                    {
                        await Task.Delay(1000);
                    }
                }
                if (token.IsCancellationRequested && extractingState == 2)
                {
                    ResetProgress();
                    return;
                }
                // Locate the file within the BAR file.
                input.Seek(file.Offset, SeekOrigin.Begin);

                Directory.CreateDirectory(Path.Combine(savePath, Path.GetDirectoryName(file.FileNameWithRoot)));



                var data = new byte[file.FileSize2];
                await input.ReadAsync(data, 0, data.Length);

                //
                if (file.Extension != ".XMB" && (L33TZipUtils.IsL33TZipFile(data) || Alz4Utils.IsAlz4File(data)) && Decompress)
                {
                    if (Alz4Utils.IsAlz4File(data))
                    {
                        data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                    }
                    else
                    {
                        if (L33TZipUtils.IsL33TZipFile(data))
                        {
                            data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                        }
                    }
                }

                await File.WriteAllBytesAsync(Path.Combine(savePath, file.FileNameWithRoot), data);

                CurrentProgress += (double)file.FileSize2 / filesSize;
            }
            ResetProgress();
        }
        private async void convertFiles(object sender, RoutedEventArgs e)
        {
            mainMenu.IsEnabled        = false;
            SpinnerConvert.Visibility = Visibility.Visible;
            tbConvert.Text            = "Converting";
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = Settings.Default.lastConvertedPath;
            openFileDialog.Multiselect      = true;

            string operationType = (sender as MenuItem).Tag.ToString();

            //await DdtFileUtils.Ddt2PngAsync(@"D:\Development\Resource Manager\Resource Manager\bin\Release\netcoreapp3.1\Art\ui\alerts\alert_treatyend_bump.ddt");



            if (operationType == "totga")
            {
                openFileDialog.Filter = "Age of Empires 3 ddt files (*.ddt)|*.ddt";
                if (openFileDialog.ShowDialog() == true)
                {
                    Settings.Default.lastConvertedPath = Path.GetDirectoryName(openFileDialog.FileName);
                    Settings.Default.Save();
                    foreach (var file in openFileDialog.FileNames)
                    {
                        try
                        {
                            await DdtFileUtils.Ddt2TgaAsync(file);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
            if (operationType == "topng")
            {
                openFileDialog.Filter = "Age of Empires 3 ddt files (*.ddt)|*.ddt";
                if (openFileDialog.ShowDialog() == true)
                {
                    Settings.Default.lastConvertedPath = Path.GetDirectoryName(openFileDialog.FileName);
                    Settings.Default.Save();
                    foreach (var file in openFileDialog.FileNames)
                    {
                        try
                        {
                            await DdtFileUtils.Ddt2PngAsync(file);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
            if (operationType == "toxml")
            {
                openFileDialog.Filter = "Age of Empires 3 xmb files (*.xmb)|*.xmb";

                if (openFileDialog.ShowDialog() == true)
                {
                    Settings.Default.lastConvertedPath = Path.GetDirectoryName(openFileDialog.FileName);
                    Settings.Default.Save();
                    foreach (var file in openFileDialog.FileNames)
                    {
                        try
                        {
                            var data = await File.ReadAllBytesAsync(file);


                            if (Alz4Utils.IsAlz4File(data))
                            {
                                data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                            }
                            else
                            {
                                if (L33TZipUtils.IsL33TZipFile(data))
                                {
                                    data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                                }
                            }


                            using MemoryStream stream = new MemoryStream(data);
                            XMBFile xmb = await XMBFile.LoadXMBFile(stream);

                            var newName = Path.ChangeExtension(file, "");

                            xmb.file.Save(newName);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
            if (operationType == "jsontoxml")
            {
                openFileDialog.Filter = "JSON files (*.json)|*.json";

                if (openFileDialog.ShowDialog() == true)
                {
                    Settings.Default.lastConvertedPath = Path.GetDirectoryName(openFileDialog.FileName);
                    Settings.Default.Save();
                    foreach (var file in openFileDialog.FileNames)
                    {
                        try
                        {
                            var data = await File.ReadAllBytesAsync(file);


                            if (Alz4Utils.IsAlz4File(data))
                            {
                                data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                            }
                            else
                            {
                                if (L33TZipUtils.IsL33TZipFile(data))
                                {
                                    data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                                }
                            }
                            string json = await File.ReadAllTextAsync(file);

                            var xml = JsonConvert.DeserializeXmlNode(json);

                            var newName = Path.ChangeExtension(file, "");



                            xml.Save(newName);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
            if (operationType == "xmbtojson")
            {
                openFileDialog.Filter = "Age of Empires 3 xmb files (*.xmb)|*.xmb";

                if (openFileDialog.ShowDialog() == true)
                {
                    Settings.Default.lastConvertedPath = Path.GetDirectoryName(openFileDialog.FileName);
                    Settings.Default.Save();
                    foreach (var file in openFileDialog.FileNames)
                    {
                        try
                        {
                            var data = await File.ReadAllBytesAsync(file);


                            if (Alz4Utils.IsAlz4File(data))
                            {
                                data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                            }
                            else
                            {
                                if (L33TZipUtils.IsL33TZipFile(data))
                                {
                                    data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                                }
                            }


                            using MemoryStream stream = new MemoryStream(data);

                            XMBFile xmb = await XMBFile.LoadXMBFile(stream);

                            string json = JsonConvert.SerializeXmlNode(xmb.file);
                            await File.WriteAllTextAsync(Path.ChangeExtension(file, "json"), json);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
            if (operationType == "xmltojson")
            {
                openFileDialog.Filter = "Age of Empires 3 xml files (*.*)|*.*";

                if (openFileDialog.ShowDialog() == true)
                {
                    Settings.Default.lastConvertedPath = Path.GetDirectoryName(openFileDialog.FileName);
                    Settings.Default.Save();
                    foreach (var file in openFileDialog.FileNames)
                    {
                        try
                        {
                            XmlDocument xml = new XmlDocument();
                            xml.Load(file);
                            string json = JsonConvert.SerializeXmlNode(xml);
                            await File.WriteAllTextAsync(Path.ChangeExtension(file, "json"), json);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
            if (operationType == "jsontoxmblegacy")
            {
                openFileDialog.Filter = "JSON files (*.json)|*.json";

                if (openFileDialog.ShowDialog() == true)
                {
                    Settings.Default.lastConvertedPath = Path.GetDirectoryName(openFileDialog.FileName);
                    Settings.Default.Save();
                    foreach (var file in openFileDialog.FileNames)
                    {
                        try
                        {
                            string json = await File.ReadAllTextAsync(file);

                            var xml = JsonConvert.DeserializeXmlNode(json);
                            await XMBFile.CreateXMBFileL33T(xml, file);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
            if (operationType == "jsontoxmbde")
            {
                openFileDialog.Filter = "JSON files (*.json)|*.json";

                if (openFileDialog.ShowDialog() == true)
                {
                    Settings.Default.lastConvertedPath = Path.GetDirectoryName(openFileDialog.FileName);
                    Settings.Default.Save();
                    foreach (var file in openFileDialog.FileNames)
                    {
                        try
                        {
                            string json = await File.ReadAllTextAsync(file);

                            var xml = JsonConvert.DeserializeXmlNode(json);
                            await XMBFile.CreateXMBFileALZ4(xml, file);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
            if (operationType == "toxmbde")
            {
                openFileDialog.Filter = "Age of Empires 3 xml files (*.*)|*.*";

                if (openFileDialog.ShowDialog() == true)
                {
                    Settings.Default.lastConvertedPath = Path.GetDirectoryName(openFileDialog.FileName);
                    Settings.Default.Save();
                    foreach (var file in openFileDialog.FileNames)
                    {
                        try
                        {
                            await XMBFile.CreateXMBFileALZ4(file);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
            if (operationType == "toxmbcc")
            {
                openFileDialog.Filter = "Age of Empires 3 xml files (*.*)|*.*";
                if (openFileDialog.ShowDialog() == true)
                {
                    foreach (var file in openFileDialog.FileNames)
                    {
                        Settings.Default.lastConvertedPath = Path.GetDirectoryName(openFileDialog.FileName);
                        Settings.Default.Save();
                        try
                        {
                            await XMBFile.CreateXMBFileL33T(file);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }



            tbConvert.Text            = "Convert";
            SpinnerConvert.Visibility = Visibility.Collapsed;
            mainMenu.IsEnabled        = true;
        }
        public async Task readFile(BarEntry file)
        {
            // Firstly, is the file parameter null?

            PreviewDdt   = null;
            Preview      = null;
            PreviewImage = null;
            if (file == null)
            {
                return;
            }

            if (file.Extension == ".WAV" /* || file.Extension == ".MP3"*/)
            {
                using FileStream input = File.OpenRead(barFilePath);
                // Locate the file within the BAR file.
                input.Seek(file.Offset, SeekOrigin.Begin);
                var data = new byte[file.FileSize2];
                await input.ReadAsync(data, 0, data.Length);

                //File.WriteAllBytes(file.fileNameWithoutPath, data);
                audio = new MemoryStream(data);
                if (file.isCompressed)
                {
                    //   AudioFileReader audioFileReader = new AudioFileReader();
                    //  DirectSoundOut directSoundOut = new DirectSoundOut();

                    // audioFileReader.
                    audio = new MemoryStream(data);
                }
                else
                {
                    audio = new MemoryStream(data);
                }

                return;
            }
            if (file.Extension == ".DDT")
            {
                using FileStream input = File.OpenRead(barFilePath);
                // Locate the file within the BAR file.
                input.Seek(file.Offset, SeekOrigin.Begin);
                var data = new byte[file.FileSize2];
                await input.ReadAsync(data, 0, data.Length);


                if (Alz4Utils.IsAlz4File(data))
                {
                    data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                }
                else
                {
                    if (L33TZipUtils.IsL33TZipFile(data))
                    {
                        data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                    }
                }

                PreviewDdt = new DdtFile(data, true);
                return;
            }
            if (file.Extension == ".BMP" || file.Extension == ".PNG" || file.Extension == ".CUR" || file.Extension == ".JPG")
            {
                using FileStream input = File.OpenRead(barFilePath);
                // Locate the file within the BAR file.
                input.Seek(file.Offset, SeekOrigin.Begin);
                var data = new byte[file.FileSize2];
                await input.ReadAsync(data, 0, data.Length);

                if (Alz4Utils.IsAlz4File(data))
                {
                    data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                }
                else
                {
                    if (L33TZipUtils.IsL33TZipFile(data))
                    {
                        data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                    }
                }
                var bitmap = new BitmapImage();
                using (var stream = new MemoryStream(data))
                {
                    bitmap.BeginInit();
                    bitmap.StreamSource = stream;
                    bitmap.CacheOption  = BitmapCacheOption.OnLoad;
                    bitmap.EndInit();
                    bitmap.Freeze();
                }

                PreviewImage = bitmap;
                return;
            }

            if (file.Extension == ".XMB")
            {
                using FileStream input = File.OpenRead(barFilePath);
                // Locate the file within the BAR file.
                input.Seek(file.Offset, SeekOrigin.Begin);
                var data = new byte[file.FileSize2];
                await input.ReadAsync(data, 0, data.Length);

                if (Alz4Utils.IsAlz4File(data))
                {
                    data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                }
                else
                {
                    if (L33TZipUtils.IsL33TZipFile(data))
                    {
                        data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                    }
                }

                //File.WriteAllBytes(file.fileNameWithoutPath, data);

                Preview = new Document();
                Preview.SyntaxHighlighting = "XML";
                Preview.Text = await XMBFile.XmbToXmlAsync(data);

                NotifyPropertyChanged("Preview");
                return;
            }
            if (file.Extension == ".XAML" || file.Extension == ".XML" || file.Extension == ".SHP" || file.Extension == ".LGT" || file.Extension == ".XS" || file.Extension == ".TXT" || file.Extension == ".CFG" || file.Extension == ".PY")
            {
                using FileStream input = File.OpenRead(barFilePath);
                // Locate the file within the BAR file.
                input.Seek(file.Offset, SeekOrigin.Begin);
                var data = new byte[file.FileSize2];
                await input.ReadAsync(data, 0, data.Length);

                if (Alz4Utils.IsAlz4File(data))
                {
                    data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                }
                else
                {
                    if (L33TZipUtils.IsL33TZipFile(data))
                    {
                        data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                    }
                }
                Preview      = new Document();
                Preview.Text = System.Text.Encoding.UTF8.GetString(data);
                if (file.Extension == ".XS")
                {
                    Preview.SyntaxHighlighting = "C++";
                }
                else
                {
                    Preview.SyntaxHighlighting = "XML";
                }
                NotifyPropertyChanged("Preview");
                return;
            }

            return;
        }
        public async Task readFile(BarEntry file)
        {
            // Firstly, is the file parameter null?

            PreviewDdt   = null;
            Preview      = null;
            PreviewImage = null;
            if (file == null)
            {
                return;
            }

            if (file.Extension == ".WAV" || file.Extension == ".MP3")
            {
                using FileStream input = File.OpenRead(barFilePath);
                // Locate the file within the BAR file.
                input.Seek(file.Offset, SeekOrigin.Begin);
                var data = new byte[file.FileSize2];
                await input.ReadAsync(data, 0, data.Length);

                if (file.isCompressed == 2)
                {
                    audio = new MemoryStream(await soundUtils.DecryptSound(data));
                }
                else
                {
                    audio = new MemoryStream(data);
                }
                return;
            }
            if (file.Extension == ".DDT")
            {
                using FileStream input = File.OpenRead(barFilePath);
                // Locate the file within the BAR file.
                input.Seek(file.Offset, SeekOrigin.Begin);
                var data = new byte[file.FileSize2];
                await input.ReadAsync(data, 0, data.Length);


                if (Alz4Utils.IsAlz4File(data))
                {
                    data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                }
                else
                {
                    if (L33TZipUtils.IsL33TZipFile(data))
                    {
                        data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                    }
                }



                PreviewDdt = new DdtFile(data, true);
                return;
            }
            if (file.Extension == ".BMP" || file.Extension == ".TGA" || file.Extension == ".PNG" || file.Extension == ".CUR" || file.Extension == ".JPG")
            {
                using FileStream input = File.OpenRead(barFilePath);
                // Locate the file within the BAR file.
                input.Seek(file.Offset, SeekOrigin.Begin);
                var data = new byte[file.FileSize2];
                await input.ReadAsync(data, 0, data.Length);

                if (Alz4Utils.IsAlz4File(data))
                {
                    data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                }
                else
                {
                    if (L33TZipUtils.IsL33TZipFile(data))
                    {
                        data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                    }
                }
                var bitmap = new BitmapImage();

                using (var stream = new MemoryStream(data))
                {
                    if (file.Extension == ".TGA")
                    {
                        IImage image = await Task.Run(() => Pfim.Pfim.FromStream(stream));

                        var pinnedArray = GCHandle.Alloc(image.Data, GCHandleType.Pinned);
                        var addr        = pinnedArray.AddrOfPinnedObject();
                        var bsource     = BitmapSource.Create(image.Width, image.Height, 96.0, 96.0,
                                                              PixelFormat(image), null, addr, image.DataLen, image.Stride);
                        PngBitmapEncoder encoder      = new PngBitmapEncoder();
                        MemoryStream     memoryStream = new MemoryStream();

                        encoder.Frames.Add(BitmapFrame.Create(bsource));
                        encoder.Save(memoryStream);
                        memoryStream.Position = 0;
                        bitmap.BeginInit();
                        bitmap.StreamSource = memoryStream;
                        bitmap.CacheOption  = BitmapCacheOption.OnLoad;
                        bitmap.EndInit();
                        bitmap.Freeze();
                        memoryStream.Close();
                    }

                    /*else if (file.Extension == ".PNG")
                     * {
                     *  System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
                     *  using var memory = new MemoryStream();
                     *  image.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
                     *  memory.Seek(0, SeekOrigin.Begin);
                     *  bitmap.BeginInit();
                     *  bitmap.CacheOption = BitmapCacheOption.OnLoad;
                     *  bitmap.StreamSource = memory;
                     *  bitmap.EndInit();
                     * }*/
                    else
                    {
                        bitmap.BeginInit();
                        bitmap.StreamSource = stream;
                        bitmap.CacheOption  = BitmapCacheOption.OnLoad;
                        bitmap.EndInit();
                        bitmap.Freeze();
                    }
                }


                PreviewImage = bitmap;
                return;
            }

            if (file.Extension == ".XMB")
            {
                using FileStream input = File.OpenRead(barFilePath);
                // Locate the file within the BAR file.
                input.Seek(file.Offset, SeekOrigin.Begin);
                var data = new byte[file.FileSize2];
                await input.ReadAsync(data, 0, data.Length);

                if (Alz4Utils.IsAlz4File(data))
                {
                    data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                }
                else
                {
                    if (L33TZipUtils.IsL33TZipFile(data))
                    {
                        data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                    }
                }

                //File.WriteAllBytes(file.fileNameWithoutPath, data);

                Preview = new Document();
                Preview.SyntaxHighlighting = "XML";
                Preview.Text = await XMBFile.XmbToXmlAsync(data);

                NotifyPropertyChanged("Preview");
                return;
            }
            if (file.Extension == ".XAML" || file.Extension == ".XML" || file.Extension == ".SHP" || file.Extension == ".LGT" || file.Extension == ".XS" || file.Extension == ".TXT" || file.Extension == ".CFG" || file.Extension == ".PY" || file.Extension == ".TACTICS")
            {
                using FileStream input = File.OpenRead(barFilePath);
                // Locate the file within the BAR file.
                input.Seek(file.Offset, SeekOrigin.Begin);
                var data = new byte[file.FileSize2];
                await input.ReadAsync(data, 0, data.Length);

                if (Alz4Utils.IsAlz4File(data))
                {
                    data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                }
                else
                {
                    if (L33TZipUtils.IsL33TZipFile(data))
                    {
                        data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                    }
                }
                Preview      = new Document();
                Preview.Text = System.Text.Encoding.UTF8.GetString(data);
                if (file.Extension == ".XS")
                {
                    Preview.SyntaxHighlighting = "C++";
                }
                else
                {
                    Preview.SyntaxHighlighting = "XML";
                }
                NotifyPropertyChanged("Preview");
                return;
            }

            return;
        }
        public async Task saveFiles(List <BarEntry> files, string savePath, bool Decompress, CancellationToken token, bool convertDDTToPNG, bool convertDDTToTGA, bool convertXMB, bool OneFolder, bool SavePNGasBMP, bool AutoJSONConversion, Color OverlayColor)
        {
            ResetProgress();
            if (files.Count == 0)
            {
                return;
            }

            using var input = File.OpenRead(barFilePath);

            long filesSize = files.Sum(x => (long)x.FileSize2);

            foreach (var file in files)
            {
                if (token.IsCancellationRequested)
                {
                    while (extractingState == 1)
                    {
                        await Task.Delay(1000);
                    }
                }
                if (token.IsCancellationRequested && extractingState == 2)
                {
                    ResetProgress();
                    return;
                }
                // Locate the file within the BAR file.
                input.Seek(file.Offset, SeekOrigin.Begin);


                string ExtractPath = Path.Combine(savePath, file.FileNameWithRoot);
                if (OneFolder)
                {
                    ExtractPath = Path.Combine(savePath, file.fileNameWithoutPath);
                }

                Directory.CreateDirectory(Path.GetDirectoryName(ExtractPath));


                var data = new byte[file.FileSize2];
                await input.ReadAsync(data, 0, data.Length);

                // XMB and decompress
                if (file.Extension != ".XMB" && (L33TZipUtils.IsL33TZipFile(data) || Alz4Utils.IsAlz4File(data)) && Decompress)
                {
                    if (Alz4Utils.IsAlz4File(data))
                    {
                        data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                    }
                    else
                    {
                        if (L33TZipUtils.IsL33TZipFile(data))
                        {
                            data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                        }
                    }
                }

                // WAV or MP3
                if (file.Extension == ".WAV" || file.Extension == ".MP3")
                {
                    if (file.isCompressed == 2)
                    {
                        data = await soundUtils.DecryptSound(data);
                    }
                }

                // Save PNG as BMP, skip saving as PNG
                if (file.Extension == ".PNG" && SavePNGasBMP)
                {
                    using var memory = new MemoryStream(data);
                    Bitmap img = new Bitmap(memory);


                    PixelFormat fmt1 = img.PixelFormat;
                    byte        bpp1 = 4;

                    Rectangle  rect    = new Rectangle(Point.Empty, new Size(img.Width, img.Height));
                    BitmapData bmpData = img.LockBits(rect, ImageLockMode.ReadWrite, fmt1);

                    int    size1  = bmpData.Stride * bmpData.Height;
                    byte[] pixels = new byte[size1];
                    Marshal.Copy(bmpData.Scan0, pixels, 0, size1);

                    for (int y = 0; y < img.Height; y++)
                    {
                        for (int x = 0; x < img.Width; x++)
                        {
                            int index = y * bmpData.Stride + x * bpp1;
                            var alpha = pixels[index + 3];
                            if (alpha < 255)
                            {
                                pixels[index]     = (byte)(pixels[index] * OverlayColor.B / 255);     //b
                                pixels[index + 1] = (byte)(pixels[index + 1] * OverlayColor.G / 255); //g
                                pixels[index + 2] = (byte)(pixels[index + 2] * OverlayColor.R / 255); //r
                                pixels[index + 3] = 255;
                            }
                        }
                    }

                    Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
                    img.UnlockBits(bmpData);
                    using (Graphics g = Graphics.FromImage(img))
                    {
                        g.DrawImage(new Bitmap(memory), Point.Empty);
                    }
                    img.Save(ExtractPath, System.Drawing.Imaging.ImageFormat.Png);
                    CurrentProgress += (double)file.FileSize2 / filesSize;
                    continue;
                }


                // Save data
                await File.WriteAllBytesAsync(ExtractPath, data);


                // Additionaly convert xmb
                if (file.Extension == ".XMB" && convertXMB)
                {
                    if (Alz4Utils.IsAlz4File(data))
                    {
                        data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                    }
                    else
                    {
                        if (L33TZipUtils.IsL33TZipFile(data))
                        {
                            data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                        }
                    }

                    using MemoryStream stream = new MemoryStream(data);
                    XMBFile xmb = await XMBFile.LoadXMBFile(stream);

                    xmb.file.Save(Path.ChangeExtension(ExtractPath, ""));
                }

                // Additionaly convert xmb -> json
                if (file.Extension == ".XMB" && AutoJSONConversion)
                {
                    if (Alz4Utils.IsAlz4File(data))
                    {
                        data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                    }
                    else
                    {
                        if (L33TZipUtils.IsL33TZipFile(data))
                        {
                            data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                        }
                    }

                    using MemoryStream stream = new MemoryStream(data);
                    XMBFile xmb = await XMBFile.LoadXMBFile(stream);

                    string json = JsonConvert.SerializeXmlNode(xmb.file);
                    await File.WriteAllTextAsync(Path.ChangeExtension(ExtractPath, "json"), json);
                }

                // Additionaly convert xml -> json
                if (file.Extension == ".XML" && AutoJSONConversion)
                {
                    if (Alz4Utils.IsAlz4File(data))
                    {
                        data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                    }
                    else
                    {
                        if (L33TZipUtils.IsL33TZipFile(data))
                        {
                            data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                        }
                    }

                    using MemoryStream stream = new MemoryStream(data);
                    XmlDocument xml = new XmlDocument();
                    xml.Load(stream);
                    string json = JsonConvert.SerializeXmlNode(xml);
                    await File.WriteAllTextAsync(Path.ChangeExtension(ExtractPath, "json"), json);
                }



                // Additionaly convert ddt to png
                if (file.Extension == ".DDT" && convertDDTToPNG)
                {
                    await DdtFileUtils.DdtBytes2PngAsync(data, ExtractPath);
                }
                // Additionaly convert ddt to tga
                if (file.Extension == ".DDT" && convertDDTToTGA)
                {
                    await DdtFileUtils.DdtBytes2TgaAsync(data, ExtractPath);
                }



                CurrentProgress += (double)file.FileSize2 / filesSize;
            }
            ResetProgress();
        }
        public async void GetCustomValues(string filename)
        {
            if (!File.Exists(filename))
            {
                throw new Exception("BAR file does not exist!");
            }
            using var file = File.OpenRead(filename);
            var reader = new BinaryReader(file);

            reader.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
            byte[] data = reader.ReadBytes(entry.FileSize2);
            await Task.Run(() =>
            {
                entry.CRC32 = Crc32Algorithm.Compute(data);
            });

            reader.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
            HeaderText = new string(reader.ReadChars(4));
            reader.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
            Header = reader.ReadInt32();
            if (entry.Extension == ".DDT")
            {
                if (Alz4Utils.IsAlz4File(data))
                {
                    data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                }
                else
                {
                    if (L33TZipUtils.IsL33TZipFile(data))
                    {
                        data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                    }
                }



                PreviewDdt = new DdtFile(data, true);
                var flagList = new List <string>();
                if (PreviewDdt.Usage.HasFlag(DdtFileTypeUsage.AlphaTest))
                {
                    flagList.Add(DdtFileTypeUsage.AlphaTest.ToString());
                }
                if (PreviewDdt.Usage.HasFlag(DdtFileTypeUsage.LowDetail))
                {
                    flagList.Add(DdtFileTypeUsage.LowDetail.ToString());
                }
                if (PreviewDdt.Usage.HasFlag(DdtFileTypeUsage.Bump))
                {
                    flagList.Add(DdtFileTypeUsage.Bump.ToString());
                }
                if (PreviewDdt.Usage.HasFlag(DdtFileTypeUsage.Cube))
                {
                    flagList.Add(DdtFileTypeUsage.Cube.ToString());
                }
                if (flagList.Count > 0)
                {
                    DdtUsage = ((byte)PreviewDdt.Usage).ToString() + " (" + string.Join('+', flagList) + ")";
                }
                else
                {
                    DdtUsage = ((byte)PreviewDdt.Usage).ToString();
                }
                DdtAlpha         = ((byte)PreviewDdt.Alpha).ToString() + " (" + PreviewDdt.Alpha.ToString() + ")";
                DdtFormat        = ((byte)PreviewDdt.Format).ToString() + " (" + PreviewDdt.Format.ToString() + ")";
                gpDDT.Visibility = Visibility.Visible;
            }
        }
        private async void convertFiles(object sender, RoutedEventArgs e)
        {
            mainMenu.IsEnabled        = false;
            SpinnerConvert.Visibility = Visibility.Visible;
            tbConvert.Text            = "Converting";
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Multiselect = true;

            string operationType = (sender as MenuItem).Tag.ToString();

            //await DdtFileUtils.Ddt2PngAsync(@"D:\Development\Resource Manager\Resource Manager\bin\Release\netcoreapp3.1\Art\ui\alerts\alert_treatyend_bump.ddt");

            if (operationType == "topng")
            {
                openFileDialog.Filter = "Age of Empires 3 ddt files (*.ddt)|*.ddt";
                if (openFileDialog.ShowDialog() == true)
                {
                    foreach (var file in openFileDialog.FileNames)
                    {
                        try
                        {
                            await DdtFileUtils.Ddt2PngAsync(file);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
            if (operationType == "toxml")
            {
                openFileDialog.Filter = "Age of Empires 3 xmb files (*.xmb)|*.xmb";

                if (openFileDialog.ShowDialog() == true)
                {
                    foreach (var file in openFileDialog.FileNames)
                    {
                        try
                        {
                            var data = await File.ReadAllBytesAsync(file);


                            if (Alz4Utils.IsAlz4File(data))
                            {
                                data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                            }
                            else
                            {
                                if (L33TZipUtils.IsL33TZipFile(data))
                                {
                                    data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                                }
                            }


                            using MemoryStream stream = new MemoryStream(data);
                            XMBFile xmb = await XMBFile.LoadXMBFile(stream);

                            var newName = Path.ChangeExtension(file, "");

                            xmb.file.Save(newName);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
            if (operationType == "toxmbde")
            {
                openFileDialog.Filter = "Age of Empires 3 xml files (*.xml)|*.xml";

                if (openFileDialog.ShowDialog() == true)
                {
                    foreach (var file in openFileDialog.FileNames)
                    {
                        try
                        {
                            await XMBFile.CreateXMBFileALZ4(file);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
            if (operationType == "toxmbcc")
            {
                openFileDialog.Filter = "Age of Empires 3 xml files (*.xml)|*.xml";
                if (openFileDialog.ShowDialog() == true)
                {
                    foreach (var file in openFileDialog.FileNames)
                    {
                        try
                        {
                            await XMBFile.CreateXMBFileL33T(file);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Conversion error - " + Path.GetFileName(file), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }



            tbConvert.Text            = "Convert";
            SpinnerConvert.Visibility = Visibility.Collapsed;
            mainMenu.IsEnabled        = true;
        }
        public async Task saveFiles(List <BarEntry> files, string savePath, bool Decompress, CancellationToken token, bool convertDDT, bool convertXMB)
        {
            ResetProgress();
            if (files.Count == 0)
            {
                return;
            }

            using var input = File.OpenRead(barFilePath);

            long filesSize = files.Sum(x => (long)x.FileSize2);

            foreach (var file in files)
            {
                if (token.IsCancellationRequested)
                {
                    while (extractingState == 1)
                    {
                        await Task.Delay(1000);
                    }
                }
                if (token.IsCancellationRequested && extractingState == 2)
                {
                    ResetProgress();
                    return;
                }
                // Locate the file within the BAR file.
                input.Seek(file.Offset, SeekOrigin.Begin);

                Directory.CreateDirectory(Path.Combine(savePath, Path.GetDirectoryName(file.FileNameWithRoot)));



                var data = new byte[file.FileSize2];
                await input.ReadAsync(data, 0, data.Length);


                if (file.Extension != ".XMB" && (L33TZipUtils.IsL33TZipFile(data) || Alz4Utils.IsAlz4File(data)) && Decompress)
                {
                    if (Alz4Utils.IsAlz4File(data))
                    {
                        data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                    }
                    else
                    {
                        if (L33TZipUtils.IsL33TZipFile(data))
                        {
                            data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                        }
                    }
                }

                if (file.Extension == ".WAV" || file.Extension == ".MP3")
                {
                    if (file.isCompressed == 2)
                    {
                        data = await soundUtils.DecryptSound(data);
                    }
                }

                await File.WriteAllBytesAsync(Path.Combine(savePath, file.FileNameWithRoot), data);



                if (file.Extension == ".XMB" && convertXMB)
                {
                    if (Alz4Utils.IsAlz4File(data))
                    {
                        data = await Alz4Utils.ExtractAlz4BytesAsync(data);
                    }
                    else
                    {
                        if (L33TZipUtils.IsL33TZipFile(data))
                        {
                            data = await L33TZipUtils.ExtractL33TZippedBytesAsync(data);
                        }
                    }

                    using MemoryStream stream = new MemoryStream(data);
                    XMBFile xmb = await XMBFile.LoadXMBFile(stream);

                    var newName = Path.ChangeExtension(Path.Combine(savePath, file.FileNameWithRoot), "");

                    xmb.file.Save(newName);
                }

                if (file.Extension == ".DDT" && convertDDT)
                {
                    await DdtFileUtils.DdtBytes2PngAsync(data, Path.Combine(savePath, file.FileNameWithRoot));
                }


                CurrentProgress += (double)file.FileSize2 / filesSize;
            }
            ResetProgress();
        }