context: using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Runtime.InteropServices;
  using HiToText;
  using HiToText.Utils;
  
  namespace HiGames
  {
      class djboy : Hiscore
      {
          [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
          public struct HiscoreData
          {
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Score1;
              public byte Unknown1;
              public byte Level1;
              public byte Stars1;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name1;
  
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Score2;
              public byte Unknown2;
              public byte Level2;
              public byte Stars2;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name2;
  
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Score3;
              public byte Unknown3;
              public byte Level3;
              public byte Stars3;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name3;
  
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Score4;
              public byte Unknown4;
              public byte Level4;
              public byte Stars4;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name4;
  
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Score5;
              public byte Unknown5;
              public byte Level5;
              public byte Stars5;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name5;
          }
  
          public djboy()
          {
              m_numEntries = 5;
              m_format = "RANK|SCORE|NAME|LEVEL|STARS";
              m_gamesSupported = "djboy,djboya,djboyj";
              m_extensionsRequired = ".hi";
          }
  
          public string ByteArrayToString(byte[] data)
          {
              StringBuilder sb = new StringBuilder();
  
              for (int i = 0; i < data.Length; i++)
              {
                  if (data[i] >= 0x01 && data[i] <= 0x1a)
                      sb.Append(((char)((((int)data[i])) + 65 - 0x01)));
                  else if (data[i] == 0x1b)
                      sb.Append('.');
                  else if (data[i] == 0x00)
                      sb.Append(' ');
              }
  
              return sb.ToString();
          }
  
          public byte[] StringToByteArray(string str)
          {
              byte[] data = new byte[str.Length];
  
              for (int i = 0; i < str.Length; i++)
              {
                  if (str[i] >= 'A' && str[i] <= 'Z')
                      data[i] = (byte)(((int)str[i] - 65 + 0x01));
                  else if (str[i] == '.')
                      data[i] = 0x1b;
                  else if (str[i] == ' ')
                      data[i] = 0x00;
              }
  
              return data;
          }
  
          public override void SetHiScore(string[] args)
          {
              int rankGiven = System.Convert.ToInt32(args[0]);
              int score = System.Convert.ToInt32(args[1]) / 10;
              string name = args[2];
              int level = System.Convert.ToInt32(args[3]);
              int stars = System.Convert.ToInt32(args[4]);
  
              HiscoreData hiscoreData = (HiscoreData)HiConvert.RawDeserialize(m_data, 0, typeof(HiscoreData));
  
              #region DETERMINE_RANK
              int rank = NumEntries;
              if (score > HiConvert.ByteArrayHexToInt(hiscoreData.Score1))
                  rank = 0;
              else if (score > HiConvert.ByteArrayHexToInt(hiscoreData.Score2))
                  rank = 1;
              else if (score > HiConvert.ByteArrayHexToInt(hiscoreData.Score3))
                  rank = 2;
              else if (score > HiConvert.ByteArrayHexToInt(hiscoreData.Score4))
                  rank = 3;
              else if (score > HiConvert.ByteArrayHexToInt(hiscoreData.Score5))
                  rank = 4;
              #endregion
  
              #region ADJUST
              int adjust = NumEntries - 1;
              if (rank < NumEntries - 1)
                  adjust = NumEntries - 2;
              switch (adjust)
              {
                  case 0:
                      HiConvert.ByteArrayCopy(hiscoreData.Score2, hiscoreData.Score1);
                      HiConvert.ByteArrayCopy(hiscoreData.Name2, hiscoreData.Name1);
                      hiscoreData.Level2 = hiscoreData.Level1;
                      hiscoreData.Stars2 = hiscoreData.Stars1;
                      break;
                  case 1:
                      HiConvert.ByteArrayCopy(hiscoreData.Score3, hiscoreData.Score2);
                      HiConvert.ByteArrayCopy(hiscoreData.Name3, hiscoreData.Name2);
                      hiscoreData.Level3 = hiscoreData.Level2;
                      hiscoreData.Stars3 = hiscoreData.Stars2;
                      if (rank < 1)
                          goto case 0;
                      break;
                  case 2:
                      HiConvert.ByteArrayCopy(hiscoreData.Score4, hiscoreData.Score3);
                      HiConvert.ByteArrayCopy(hiscoreData.Name4, hiscoreData.Name3);
                      hiscoreData.Level4 = hiscoreData.Level3;
                      hiscoreData.Stars4 = hiscoreData.Stars3;
                      if (rank < 2)
                          goto case 1;
                      break;
                  case 3:
                      HiConvert.ByteArrayCopy(hiscoreData.Score5, hiscoreData.Score4);
                      HiConvert.ByteArrayCopy(hiscoreData.Name5, hiscoreData.Name4);
                      hiscoreData.Level5 = hiscoreData.Level4;
                      hiscoreData.Stars5 = hiscoreData.Stars4;
                      if (rank < 3)
                          goto case 2;
                      break;
              }
              #endregion
  
              #region REPLACE_NEW
              switch (rank)
              {
                  case 0:
                      HiConvert.ByteArrayCopy(hiscoreData.Name1, StringToByteArray(name));
                      HiConvert.ByteArrayCopy(hiscoreData.Score1, HiConvert.IntToByteArrayHex(score, hiscoreData.Score1.Length));
                      hiscoreData.Stars1 = (byte)stars;
                      hiscoreData.Level1 = (byte)level;
                      break;
                  case 1:
                      HiConvert.ByteArrayCopy(hiscoreData.Name2, StringToByteArray(name));
                      HiConvert.ByteArrayCopy(hiscoreData.Score2, HiConvert.IntToByteArrayHex(score, hiscoreData.Score2.Length));
                      hiscoreData.Stars2 = (byte)stars;
                      hiscoreData.Level2 = (byte)level;
                      break;
                  case 2:
                      HiConvert.ByteArrayCopy(hiscoreData.Name3, StringToByteArray(name));
                      HiConvert.ByteArrayCopy(hiscoreData.Score3, HiConvert.IntToByteArrayHex(score, hiscoreData.Score3.Length));
                      hiscoreData.Stars3 = (byte)stars;
                      hiscoreData.Level3 = (byte)level;
                      break;
                  case 3:
                      HiConvert.ByteArrayCopy(hiscoreData.Name4, StringToByteArray(name));
                      HiConvert.ByteArrayCopy(hiscoreData.Score4, HiConvert.IntToByteArrayHex(score, hiscoreData.Score4.Length));
                      hiscoreData.Stars4 = (byte)stars;
                      hiscoreData.Level4 = (byte)level;
                      break;
                  case 4:
                      HiConvert.ByteArrayCopy(hiscoreData.Name5, StringToByteArray(name));
                      HiConvert.ByteArrayCopy(hiscoreData.Score5, HiConvert.IntToByteArrayHex(score, hiscoreData.Score4.Length));
                      hiscoreData.Stars5 = (byte)stars;
                      hiscoreData.Level5 = (byte)level;
                      break;
              }
              #endregion
  
              byte[] byteArray = HiConvert.RawSerialize(hiscoreData);
  
              HiConvert.ByteArrayCopy(m_data, byteArray);
          }
  
          public override void EmptyScores()
          {
              HiscoreData hiscoreData = (HiscoreData)HiConvert.RawDeserialize(m_data, 0, typeof(HiscoreData));
  
              HiConvert.ByteArrayCopy(hiscoreData.Score1, HiConvert.IntToByteArrayHex(0, hiscoreData.Score1.Length));
              HiConvert.ByteArrayCopy(hiscoreData.Score2, HiConvert.IntToByteArrayHex(0, hiscoreData.Score2.Length));
              HiConvert.ByteArrayCopy(hiscoreData.Score3, HiConvert.IntToByteArrayHex(0, hiscoreData.Score3.Length));
              HiConvert.ByteArrayCopy(hiscoreData.Score4, HiConvert.IntToByteArrayHex(0, hiscoreData.Score4.Length));
              HiConvert.ByteArrayCopy(hiscoreData.Score5, HiConvert.IntToByteArrayHex(0, hiscoreData.Score5.Length));
  
              byte[] byteArray = HiConvert.RawSerialize(hiscoreData);
  
              HiConvert.ByteArrayCopy(m_data, byteArray);
  
              SaveData();
          }
  
          public override string HiToString()
          {
  
gt:             string retString = m_format + Environment.NewLine;