context: using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Runtime.InteropServices;
  using HiToText;
  using HiToText.Utils;
  
  namespace HiGames
  {
      class tmnt2 : Hiscore
      {
          [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
          public struct HiscoreData
          {
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
              public byte[] Score1;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
              public byte[] Score2;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
              public byte[] Score3;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
              public byte[] Score4;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
              public byte[] Score5;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
              public byte[] Score6;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
              public byte[] Score7;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
              public byte[] Score8;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
              public byte[] Score9;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
              public byte[] Score10;
  
              //These are used so that if you were to score 33rd, it would tell you, but you cannot enter initials.
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 180)]
              public byte[] UnusedScores;
  
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name1;
              public byte Seperator1;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name2;
              public byte Seperator2;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name3;
              public byte Seperator3;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name4;
              public byte Seperator4;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name5;
              public byte Seperator5;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name6;
              public byte Seperator6;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name7;
              public byte Seperator7;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name8;
              public byte Seperator8;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name9;
              public byte Seperator9;
              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
              public byte[] Name10;
              public byte Seperator10;
  
              public byte Unknown;
          }
  
          public tmnt2()
          {
              m_numEntries = 10;
              m_format = "RANK|SCORE|NAME";
              m_gamesSupported = "tmnt2,tmnt2a";
              m_extensionsRequired = ".hi";
          }
  
          public string ByteArrayToString(byte[] data)
          {
              StringBuilder sb = new StringBuilder();
  
              for (int i = 0; i < data.Length; i++)
              {
                  if (data[i] == 0x5f)
                      sb.Append('.');
                  else if (data[i] == 0x5e)
                      sb.Append('!');
                  else if (data[i] == 0x5d)
                      sb.Append('?');
                  else if (data[i] == 0x40)
                      sb.Append(' ');
                  else if (data[i] == 0x5b)
                      sb.Append(',');
                  else if (data[i] == 0x5c)
                      sb.Append('\'');
                  else
                      sb.Append((char)(int)data[i]);
              }
  
              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] == '.')
                      data[i] = 0x5f;
                  else if (str[i] == '!')
                      data[i] = 0x5e;
                  else if (str[i] == '?')
                      data[i] = 0x5d;
                  else if (str[i] == ' ')
                      data[i] = 0x40;
                  else if (str[i] == ',')
                      data[i] = 0x5b;
                  else if (str[i] == '\'')
                      data[i] = 0x5c;
                  else
                      data[i] = (byte)((int)str[i]);
              }
  
              return data;
          }
  
          public override void SetHiScore(string[] args)
          {
              int rankGiven = Convert.ToInt32(args[0]);
              int score = System.Convert.ToInt32(args[1]);
              string name = args[2];
  
              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;
              else if (score > HiConvert.ByteArrayHexToInt(hiscoreData.Score6))
                  rank = 5;
              else if (score > HiConvert.ByteArrayHexToInt(hiscoreData.Score7))
                  rank = 6;
              else if (score > HiConvert.ByteArrayHexToInt(hiscoreData.Score8))
                  rank = 7;
              else if (score > HiConvert.ByteArrayHexToInt(hiscoreData.Score9))
                  rank = 8;
              else if (score > HiConvert.ByteArrayHexToInt(hiscoreData.Score10))
                  rank = 9;
              #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);
                      break;
                  case 1:
                      HiConvert.ByteArrayCopy(hiscoreData.Score3, hiscoreData.Score2);
                      HiConvert.ByteArrayCopy(hiscoreData.Name3, hiscoreData.Name2);
                      if (rank < 1)
                          goto case 0;
                      break;
                  case 2:
                      HiConvert.ByteArrayCopy(hiscoreData.Score4, hiscoreData.Score3);
                      HiConvert.ByteArrayCopy(hiscoreData.Name4, hiscoreData.Name3);
                      if (rank < 2)
                          goto case 1;
                      break;
                  case 3:
                      HiConvert.ByteArrayCopy(hiscoreData.Score5, hiscoreData.Score4);
                      HiConvert.ByteArrayCopy(hiscoreData.Name5, hiscoreData.Name4);
                      if (rank < 3)
                          goto case 2;
                      break;
                  case 4:
                      HiConvert.ByteArrayCopy(hiscoreData.Score6, hiscoreData.Score5);
                      HiConvert.ByteArrayCopy(hiscoreData.Name6, hiscoreData.Name5);
                      if (rank < 4)
                          goto case 3;
                      break;
                  case 5:
                      HiConvert.ByteArrayCopy(hiscoreData.Score7, hiscoreData.Score6);
                      HiConvert.ByteArrayCopy(hiscoreData.Name7, hiscoreData.Name6);
  
gt:                     if (rank < 5)