// Decompiled with JetBrains decompiler
// Type: FiestaLauncher.Services.GDPService
// Assembly: FiestaOnline, Version=2019.5.22.0, Culture=neutral, PublicKeyToken=null
// MVID: 2FD5EB61-BC25-469D-997E-88998E849F6A
// Assembly location: C:\Users\marbo\Documents\Gamigo 2020 Client\FiestaOnline.exe

using FiestaLauncher.gClasses;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FiestaLauncher.Services
{
  public static class GDPService
  {
    internal static void ExtractGDP(string path)
    {
      try
      {
        using (BinaryReaderEx binaryReaderEx = new BinaryReaderEx((Stream) File.Open(path, FileMode.Open)))
        {
          if (Encoding.ASCII.GetString(binaryReaderEx.ReadBytes(3)) == "GDP")
          {
            binaryReaderEx.ReadBytes(5);
            binaryReaderEx.ReadString(260);
            int num1 = (int) binaryReaderEx.ReadUInt32();
            int num2 = binaryReaderEx.ReadInt32();
            binaryReaderEx.ReadBytes(40);
            Dictionary<int, GDPFile> dictionary = new Dictionary<int, GDPFile>();
            for (int key = 0; key < num2; ++key)
            {
              long num3 = binaryReaderEx.ReadInt64();
              if (num3 == 2L)
              {
                GDPFile gdpFile = new GDPFile()
                {
                  Unk1 = num3,
                  ApplyLocation = binaryReaderEx.ReadString(264),
                  GDPPosition = binaryReaderEx.ReadInt64(),
                  DataLenA = binaryReaderEx.ReadInt64(),
                  DataLenB = binaryReaderEx.ReadInt64(),
                  Unk2 = binaryReaderEx.ReadBytes(20)
                };
                dictionary.Add(key, gdpFile);
                long position = binaryReaderEx.BaseStream.Position;
                binaryReaderEx.BaseStream.Position = dictionary[key].GDPPosition;
                string directoryName = Path.GetDirectoryName(Directory.GetCurrentDirectory() + "\\" + dictionary[key].ApplyLocation);
                if (!Directory.Exists(directoryName))
                  Directory.CreateDirectory(directoryName);
                int int32 = Convert.ToInt32((double) key / (double) num2 * 100.0);
                int pROGRESSBARMAXSIZE = 0;
                string fileName = Path.GetFileName(dictionary[key].ApplyLocation);
                pROGRESSBARMAXSIZE = int32;
                gFiestaLauncher.MainWindow.RunOnUiThread((Main.RunOnUiThreadDelegate) (() =>
                {
                  gFiestaLauncher.MainWindow.SetStatus2(fileName);
                  gFiestaLauncher.MainWindow.SetProgressWidth(pROGRESSBARMAXSIZE);
                }));
                using (BinaryWriter binaryWriter = new BinaryWriter((Stream) File.Create(Directory.GetCurrentDirectory() + "\\" + dictionary[key].ApplyLocation)))
                {
                  long num4 = dictionary[key].DataLenA;
                  while (num4 > 0L)
                  {
                    if (num4 <= (long) int.MaxValue)
                    {
                      binaryWriter.Write(binaryReaderEx.ReadBytes((int) num4));
                      num4 = 0L;
                    }
                    else
                    {
                      binaryWriter.Write(binaryReaderEx.ReadBytes(int.MaxValue));
                      num4 -= (long) int.MaxValue;
                    }
                  }
                }
                binaryReaderEx.BaseStream.Position = position;
              }
            }
          }
        }
        gFiestaLauncher.MainWindow.RunOnUiThread((Main.RunOnUiThreadDelegate) (() => gFiestaLauncher.MainWindow.GdpExtractComplete()));
      }
      catch (Exception ex)
      {
        FeedbackService.Exception(ex);
      }
    }
  }
}
