namespace ns0
{
    using ns1;
    using ns3;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Globalization;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;

    internal class Class7
    {
        public byte[] byte_0;
        public byte[] byte_1;
        public DataTable dataTable_0;
        public Dictionary<int, int> dictionary_0;
        private int[] int_0;
        public List<Class6> list_0;
        public string string_0;
        private string[] string_1;
        public uint uint_0;
        private uint uint_1;
        private uint uint_2;
        private uint uint_3;
        private uint[] uint_4;

        public Class7()
        {
            this.dataTable_0 = new DataTable();
            this.dictionary_0 = new Dictionary<int, int>();
            this.list_0 = new List<Class6>();
        }

        public Class7(string path)
        {
            this.dataTable_0 = new DataTable();
            this.dictionary_0 = new Dictionary<int, int>();
            this.list_0 = new List<Class6>();
            try
            {
                this.list_0.Clear();
                this.string_0 = path;
                Class8 class2 = new Class8(File.OpenRead(path));
                if (path.EndsWith(".shn"))
                {
                    this.byte_0 = class2.ReadBytes(0x20);
                    this.byte_1 = class2.ReadBytes(class2.ReadInt32() - 0x24);
                }
                else
                {
                    this.byte_1 = class2.ReadBytes((int) class2.Length);
                }
                class2.Close();
                this.method_20(this.byte_1, 0, this.byte_1.Length);
                class2 = new Class8(new MemoryStream(this.byte_1));
                this.uint_0 = class2.ReadUInt32();
                if ((this.uint_0 == 0xcdcdcdcd) | (this.uint_0 == 0))
                {
                }
                this.uint_1 = class2.ReadUInt32();
                this.uint_2 = class2.ReadUInt32();
                this.uint_3 = class2.ReadUInt32();
                this.string_1 = new string[this.uint_3];
                this.uint_4 = new uint[this.uint_3];
                this.int_0 = new int[this.uint_3];
                int num = 2;
                int num2 = 0;
                for (uint i = 0; i < this.uint_3; i++)
                {
                    string str = class2.method_6(0x30);
                    uint num4 = class2.ReadUInt32();
                    int num5 = class2.ReadInt32();
                    Class6 item = new Class6();
                    if (str.Length == 0)
                    {
                        str = "UnkCol" + num2.ToString();
                        num2++;
                    }
                    item.string_0 = str;
                    item.uint_0 = num4;
                    item.int_0 = num5;
                    this.list_0.Add(item);
                    this.string_1[i] = str;
                    this.uint_4[i] = num4;
                    this.int_0[i] = num5;
                    num += num5;
                }
                if (num != this.uint_2)
                {
                    throw new Exception("Wrong record lenght!");
                }
                this.method_18(this.dataTable_0, this.list_0);
                this.method_17(class2, this.dataTable_0);
            }
            catch (Exception exception)
            {
                Stream output = new FileStream("unk.dat", FileMode.OpenOrCreate);
                BinaryWriter writer = new BinaryWriter(output);
                writer.Write(this.byte_1, 0, this.byte_1.Length);
                writer.Close();
                throw new Exception("Unknown File Type -- dec to unk.dat Reason: " + exception.Message);
            }
        }

        public void method_0(string string_2)
        {
            Class8 class2 = new Class8(File.OpenRead(string_2));
            if (string_2.EndsWith(".shn"))
            {
                this.byte_0 = class2.ReadBytes(0x20);
                this.byte_1 = class2.ReadBytes(class2.ReadInt32() - 0x24);
            }
            else
            {
                this.byte_1 = class2.ReadBytes((int) class2.Length);
            }
            class2.Close();
            this.method_20(this.byte_1, 0, this.byte_1.Length);
        }

        public void method_1()
        {
            this.byte_0 = new byte[0x20];
            this.method_2("3B 02 00 00 32 30 30 35 2D 30 38 2D 32 36 20 BF C0 C8 C4 20 32 3A 33 00 6A 7F 00 00 01 00 00 00");
            this.uint_0 = 0;
            this.method_8("Empty Column", 0xff, 0x18, "");
            this.string_0 = "New File.shn";
        }

        public DataColumn method_10(string string_2)
        {
            for (int i = 0; i < this.dataTable_0.Columns.Count; i++)
            {
                if (this.dataTable_0.Columns[i].ColumnName == string_2)
                {
                    return this.dataTable_0.Columns[i];
                }
            }
            return null;
        }

        public Class6 method_11(string string_2)
        {
            foreach (Class6 class2 in this.list_0)
            {
                if (class2.string_0.ToLower() == string_2.ToLower())
                {
                    return class2;
                }
            }
            return null;
        }

        public int method_12(int int_1, string string_2)
        {
            for (int i = 0; i < this.dataTable_0.Rows.Count; i++)
            {
                if (this.dataTable_0.Rows[i][int_1].ToString().ToLower() == string_2.ToLower())
                {
                    return i;
                }
            }
            return -1;
        }

        public void method_13(string string_2)
        {
        }

        private void method_14(BinaryWriter binaryWriter_0)
        {
            for (int i = 0; i < this.dataTable_0.Rows.Count; i++)
            {
                DataRow row = this.dataTable_0.Rows[i];
                long position = binaryWriter_0.BaseStream.Position;
                binaryWriter_0.Write((ushort) 0);
                for (int j = 0; j < this.dataTable_0.Columns.Count; j++)
                {
                    object obj2 = row.ItemArray[this.dictionary_0[j]];
                    if (obj2 == null)
                    {
                        obj2 = "0";
                    }
                    Class6 class2 = this.method_11(this.dataTable_0.Columns[this.dictionary_0[j]].ColumnName);
                    switch (class2.uint_0)
                    {
                        case 1:
                            if (obj2 is string)
                            {
                                obj2 = byte.Parse((string) obj2);
                            }
                            binaryWriter_0.Write((byte) obj2);
                            break;

                        case 2:
                            if (obj2 is string)
                            {
                                obj2 = ushort.Parse((string) obj2);
                            }
                            binaryWriter_0.Write((ushort) obj2);
                            break;

                        case 3:
                            if (obj2 is string)
                            {
                                obj2 = uint.Parse((string) obj2);
                            }
                            binaryWriter_0.Write((uint) obj2);
                            break;

                        case 5:
                            if (obj2 is string)
                            {
                                obj2 = float.Parse((string) obj2);
                            }
                            binaryWriter_0.Write((float) obj2);
                            break;

                        case 9:
                            this.method_16(binaryWriter_0, (string) obj2, class2.int_0);
                            break;

                        case 11:
                            if (obj2 is string)
                            {
                                obj2 = uint.Parse((string) obj2);
                            }
                            binaryWriter_0.Write((uint) obj2);
                            break;

                        case 12:
                            if (obj2 is string)
                            {
                                obj2 = byte.Parse((string) obj2);
                            }
                            binaryWriter_0.Write((byte) obj2);
                            break;

                        case 13:
                            if (obj2 is string)
                            {
                                obj2 = short.Parse((string) obj2);
                            }
                            binaryWriter_0.Write((short) obj2);
                            break;

                        case 0x10:
                            if (obj2 is string)
                            {
                                obj2 = byte.Parse((string) obj2);
                            }
                            binaryWriter_0.Write((byte) obj2);
                            break;

                        case 0x12:
                            if (obj2 is string)
                            {
                                obj2 = uint.Parse((string) obj2);
                            }
                            binaryWriter_0.Write((uint) obj2);
                            break;

                        case 20:
                            if (obj2 is string)
                            {
                                obj2 = sbyte.Parse((string) obj2);
                            }
                            binaryWriter_0.Write((sbyte) obj2);
                            break;

                        case 0x15:
                            if (obj2 is string)
                            {
                                obj2 = short.Parse((string) obj2);
                            }
                            binaryWriter_0.Write((short) obj2);
                            break;

                        case 0x16:
                            if (obj2 is string)
                            {
                                obj2 = int.Parse((string) obj2);
                            }
                            binaryWriter_0.Write((int) obj2);
                            break;

                        case 0x18:
                            this.method_16(binaryWriter_0, (string) obj2, class2.int_0);
                            break;

                        case 0x1a:
                            this.method_16(binaryWriter_0, (string) obj2, -1);
                            break;

                        case 0x1b:
                            if (obj2 is string)
                            {
                                obj2 = uint.Parse((string) obj2);
                            }
                            binaryWriter_0.Write((uint) obj2);
                            break;
                    }
                }
                long num5 = binaryWriter_0.BaseStream.Position - position;
                long offset = binaryWriter_0.BaseStream.Position;
                binaryWriter_0.BaseStream.Seek(position, SeekOrigin.Begin);
                binaryWriter_0.Write((ushort) num5);
                binaryWriter_0.BaseStream.Seek(offset, SeekOrigin.Begin);
            }
        }

        public int method_15(string string_2)
        {
            for (int i = 0; i < this.dataTable_0.Columns.Count; i++)
            {
                if (this.dataTable_0.Columns[i].ColumnName == string_2)
                {
                    return i;
                }
            }
            return -1;
        }

        private void method_16(BinaryWriter binaryWriter_0, string string_2, int int_1)
        {
            byte[] bytes = Encoding.Default.GetBytes(string_2);
            if (int_1 == -1)
            {
                int_1 = bytes.Length + 1;
            }
            byte[] destinationArray = new byte[int_1];
            Array.Copy(bytes, destinationArray, Math.Min(int_1, bytes.Length));
            binaryWriter_0.Write(destinationArray);
        }

        private void method_17(Class8 class8_0, DataTable dataTable_1)
        {
            object[] values = new object[this.list_0.Count];
            for (uint i = 0; i < this.uint_1; i++)
            {
                class8_0.ReadUInt16();
                for (int j = 0; j < this.list_0.Count; j++)
                {
                    switch (this.list_0[j].uint_0)
                    {
                        case 1:
                            values[j] = class8_0.ReadByte();
                            break;

                        case 2:
                            values[j] = class8_0.ReadUInt16();
                            break;

                        case 3:
                            values[j] = class8_0.ReadUInt32();
                            break;

                        case 5:
                            values[j] = class8_0.ReadSingle();
                            break;

                        case 9:
                            values[j] = class8_0.method_6(this.int_0[j]);
                            break;

                        case 11:
                            values[j] = class8_0.ReadUInt32();
                            break;

                        case 12:
                            values[j] = class8_0.ReadByte();
                            break;

                        case 13:
                            values[j] = class8_0.ReadInt16();
                            break;

                        case 0x10:
                            values[j] = class8_0.ReadByte();
                            break;

                        case 0x12:
                            values[j] = class8_0.ReadUInt32();
                            break;

                        case 20:
                            values[j] = class8_0.ReadSByte();
                            break;

                        case 0x15:
                            values[j] = class8_0.ReadInt16();
                            break;

                        case 0x16:
                            values[j] = class8_0.ReadInt32();
                            break;

                        case 0x18:
                            values[j] = class8_0.method_6(this.int_0[j]);
                            break;

                        case 0x1a:
                            values[j] = class8_0.ReadString();
                            break;

                        case 0x1b:
                            values[j] = class8_0.ReadUInt32();
                            break;
                    }
                }
                dataTable_1.Rows.Add(values);
            }
        }

        private void method_18(DataTable dataTable_1, List<Class6> list_1)
        {
            for (int i = 0; i < list_1.Count; i++)
            {
                DataColumn column = new DataColumn {
                    ColumnName = list_1[i].string_0,
                    DataType = this.method_19(list_1[i])
                };
                dataTable_1.Columns.Add(column);
            }
        }

        public System.Type method_19(Class6 class6_0)
        {
            switch (class6_0.uint_0)
            {
                case 1:
                case 12:
                    return typeof(byte);

                case 2:
                    return typeof(ushort);

                case 3:
                case 11:
                    return typeof(uint);

                case 5:
                    return typeof(float);

                case 9:
                case 0x18:
                case 0x1a:
                    return typeof(string);

                case 13:
                case 0x15:
                    return typeof(short);

                case 0x10:
                    return typeof(byte);

                case 0x12:
                case 0x1b:
                    return typeof(uint);

                case 20:
                    return typeof(sbyte);

                case 0x16:
                    return typeof(int);
            }
            return typeof(object);
        }

        public void method_2(string string_2)
        {
            string[] strArray = string_2.Split(new char[] { ' ' });
            if (strArray.Length != 0x20)
            {
                throw new Exception("Incorrect header lenght!");
            }
            for (int i = 0; i < strArray.Length; i++)
            {
                this.byte_0[i] = byte.Parse(strArray[i], NumberStyles.HexNumber);
            }
        }

        private void method_20(byte[] byte_2, int int_1, int int_2)
        {
            if (((int_1 < 0) | (int_2 < 1)) | ((int_1 + int_2) > byte_2.Length))
            {
                throw new IndexOutOfRangeException();
            }
            byte num = (byte) int_2;
            for (int i = int_2 - 1; i >= 0; i--)
            {
                byte_2[i] = (byte) (byte_2[i] ^ num);
                byte num3 = (byte) i;
                num3 = (byte) (num3 & 15);
                num3 = (byte) (num3 + 0x55);
                num3 = (byte) (num3 ^ ((byte) (((byte) i) * 11)));
                num3 = (byte) (num3 ^ num);
                num3 = (byte) (num3 ^ 170);
                num = num3;
            }
        }

        public string method_3()
        {
            string str = "";
            for (int i = 0; i < this.byte_0.Length; i++)
            {
                if (i != (this.byte_0.Length - 1))
                {
                    str = str + this.byte_0[i].ToString("X2") + " ";
                }
                else
                {
                    str = str + this.byte_0[i].ToString("X2");
                }
            }
            return str;
        }

        public void method_4()
        {
            this.dataTable_0 = null;
            this.byte_0 = null;
        }

        public uint method_5()
        {
            uint num = 2;
            foreach (DataColumn column in this.dataTable_0.Columns)
            {
                Class6 class2 = this.method_11(column.ColumnName);
                num += (uint) class2.int_0;
            }
            return num;
        }

        public bool method_6(string string_2)
        {
            MemoryStream output = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(output);
            try
            {
                writer.Write(this.uint_0);
                writer.Write(this.dataTable_0.Rows.Count);
                writer.Write(this.method_5());
                writer.Write(this.dataTable_0.Columns.Count);
                for (int i = 0; i < this.dataTable_0.Columns.Count; i++)
                {
                    Class6 class2 = this.method_11(this.dataTable_0.Columns[this.dictionary_0[i]].ColumnName);
                    if (class2.string_0.Contains("UnkCol"))
                    {
                        writer.Write(new byte[0x30]);
                    }
                    else
                    {
                        this.method_16(writer, class2.string_0, 0x30);
                    }
                    writer.Write(class2.uint_0);
                    writer.Write(class2.int_0);
                }
                this.method_14(writer);
                byte[] sourceArray = output.GetBuffer();
                long length = output.Length;
                byte[] destinationArray = new byte[length];
                Array.Copy(sourceArray, destinationArray, length);
                this.method_20(destinationArray, 0, destinationArray.Length);
                writer.Close();
                writer = new BinaryWriter(File.Create(string_2));
                writer.Write(this.byte_0);
                writer.Write((int) (destinationArray.Length + 0x24));
                writer.Write(destinationArray);
                writer.Close();
                this.string_0 = string_2;
                return true;
            }
            catch (Exception exception)
            {
                writer.Close();
                MessageBox.Show(exception.Message);
                return false;
            }
        }

        public void method_7(string string_2)
        {
            this.list_0.Remove(this.method_11(string_2));
            this.dataTable_0.Columns.Remove(this.method_10(string_2));
        }

        public void method_8(string string_2, int int_1, uint uint_5, string string_3)
        {
            Class6 item = new Class6 {
                string_0 = string_2,
                int_0 = int_1,
                uint_0 = uint_5
            };
            this.list_0.Add(item);
            DataColumn column = new DataColumn {
                ColumnName = string_2,
                DefaultValue = string_3,
                DataType = this.method_19(item)
            };
            this.dataTable_0.Columns.Add(column);
        }

        public void method_9(string string_2, string string_3)
        {
            this.method_10(string_2).ColumnName = string_3;
            this.method_11(string_2).string_0 = string_3;
        }
    }
}

