// Decompiled with JetBrains decompiler
// Type: FiestaLauncher.Services.VersionService
// 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 System;
using System.IO;

namespace FiestaLauncher.Services
{
  public static class VersionService
  {
    internal static void GdpUpdateComplete()
    {
      string[] strArray = gFiestaLauncher.FUTURE_PATCH.Split('.');
      gFiestaLauncher.YEAR = (ushort) 2013;
      gFiestaLauncher.VERSION = ushort.Parse(strArray[0]);
      gFiestaLauncher.SUBVERSION = ushort.Parse(strArray[1]);
      gFiestaLauncher.PATCHNO = ushort.Parse(strArray[2]);
      VersionService.SetVersionHashFile();
      VersionService.SetCurPatchInfo();
      VersionService.WriteVersionFile("Fiesta.vdt");
    }

    public static void ParseGdpFile(string file)
    {
      char[] chArray = new char[1]{ '_' };
      gFiestaLauncher.FUTURE_PATCH = file.Split(chArray)[2];
    }

    private static void ReadVersionFile(string path)
    {
      try
      {
        if (!File.Exists(path))
          return;
        byte[] numArray = new byte[8];
        using (BinaryReader binaryReader = new BinaryReader((Stream) File.OpenRead(path)))
          numArray = binaryReader.ReadBytes(8);
        numArray[0] ^= (byte) 117;
        numArray[1] ^= (byte) 208;
        numArray[2] ^= (byte) 117;
        numArray[3] ^= (byte) 208;
        numArray[4] ^= (byte) 117;
        numArray[5] ^= (byte) 208;
        numArray[6] ^= (byte) 117;
        numArray[7] ^= (byte) 208;
        gFiestaLauncher.YEAR = BitConverter.ToUInt16(numArray, 0);
        gFiestaLauncher.VERSION = BitConverter.ToUInt16(numArray, 2);
        gFiestaLauncher.SUBVERSION = BitConverter.ToUInt16(numArray, 4);
        gFiestaLauncher.PATCHNO = BitConverter.ToUInt16(numArray, 6);
        VersionService.SetVersionHashFile();
        VersionService.SetCurPatchInfo();
      }
      catch (Exception ex)
      {
        gFiestaLauncher.onCrash(ex);
      }
    }

    private static void SetCurPatchInfo()
    {
      gFiestaLauncher.CURRENT_PATCH = gFiestaLauncher.VERSION.ToString() + "." + gFiestaLauncher.SUBVERSION.ToString("D2") + "." + gFiestaLauncher.PATCHNO.ToString("D3");
    }

    internal static void SetDefaultValues()
    {
      string firstPatch = gFiestaLauncher.FirstPatch;
      if (firstPatch == "")
      {
        FeedbackService.PopError("Invalid config file.", true);
      }
      else
      {
        string[] strArray = firstPatch.Split('.');
        gFiestaLauncher.YEAR = (ushort) 2013;
        gFiestaLauncher.VERSION = ushort.Parse(strArray[0]);
        gFiestaLauncher.SUBVERSION = ushort.Parse(strArray[1]);
        gFiestaLauncher.PATCHNO = ushort.Parse(strArray[2]);
        VersionService.SetVersionHashFile();
        VersionService.SetCurPatchInfo();
        gFiestaLauncher.MainWindow.SetVersion(gFiestaLauncher.CURRENT_PATCH);
      }
    }

    public static void SetVersionHashFile()
    {
      gFiestaLauncher.VERSIONHASH = (ulong) gFiestaLauncher.VERSION << 32;
      gFiestaLauncher.VERSIONHASH ^= (ulong) gFiestaLauncher.SUBVERSION << 16;
      gFiestaLauncher.VERSIONHASH ^= (ulong) gFiestaLauncher.PATCHNO;
    }

    public static void VersionControl(string path, byte type)
    {
      switch (type)
      {
        case 0:
          VersionService.ReadVersionFile(path);
          break;
        case 1:
          VersionService.WriteVersionFile(path);
          break;
      }
    }

    private static void WriteVersionFile(string path)
    {
      byte[] buffer = new byte[8];
      using (BinaryWriter binaryWriter = new BinaryWriter((Stream) new MemoryStream(buffer)))
      {
        binaryWriter.Write(gFiestaLauncher.YEAR);
        binaryWriter.Write(gFiestaLauncher.VERSION);
        binaryWriter.Write(gFiestaLauncher.SUBVERSION);
        binaryWriter.Write(gFiestaLauncher.PATCHNO);
      }
      buffer[0] ^= (byte) 117;
      buffer[1] ^= (byte) 208;
      buffer[2] ^= (byte) 117;
      buffer[3] ^= (byte) 208;
      buffer[4] ^= (byte) 117;
      buffer[5] ^= (byte) 208;
      buffer[6] ^= (byte) 117;
      buffer[7] ^= (byte) 208;
      using (BinaryWriter binaryWriter = new BinaryWriter((Stream) File.OpenWrite(path)))
        binaryWriter.Write(buffer);
    }
  }
}
