Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Serializable]
- public class CodeplugCommFile
- {
- // Fields
- private bool IsCrcBigEndian;
- // Methods
- public CodeplugCommFile(bool isCrcBigEndian)
- {
- this.FileData = null;
- this.IsCrcBigEndian = isCrcBigEndian;
- }
- public PbaObject Deserialize(string ishFilePath)
- {
- FileStream stream = new FileStream(ishFilePath, FileMode.Open, FileAccess.Read);
- byte[] buffer = new byte[stream.Length];
- int length = (int) stream.Length;
- int offset = 0;
- while (length > 0)
- {
- int num3 = stream.Read(buffer, offset, length);
- if (num3 == 0)
- {
- break;
- }
- offset += num3;
- length -= num3;
- }
- stream.Close();
- if (!smethod_4(buffer))
- {
- throw new CommonException(CommonErrorCode.CommonFileInvalidCrc32);
- }
- CodeplugCommFileHeader header = smethod_5(buffer);
- return this.method_2(header, buffer);
- }
- public PbaObject Deserialize(byte[] fileData)
- {
- if (!smethod_4(fileData))
- {
- throw new CommonException(CommonErrorCode.CommonFileInvalidCrc32);
- }
- CodeplugCommFileHeader header = smethod_5(fileData);
- return this.method_2(header, fileData);
- }
- private byte[] method_0(CodeplugCommFileHeader codeplugCommFileHeader_0, PbaObject pbaObject_0)
- {
- switch (codeplugCommFileHeader_0.Level)
- {
- case InfoLevel.TypeLevelChangeDisc:
- return smethod_2(pbaObject_0);
- case InfoLevel.ItemLevelChangeDisc:
- return smethod_3(pbaObject_0);
- case InfoLevel.CodeplugData:
- return this.method_1(pbaObject_0);
- }
- return null;
- }
- private byte[] method_1(PbaObject pbaObject_0)
- {
- IshItemCollection codeplugInIshItemCollection = pbaObject_0.GetCodeplugInIshItemCollection();
- UtilityMethods.CheckIfParameterIsNull(codeplugInIshItemCollection, CommonErrorCode.ArgumentNull);
- int num = 0;
- foreach (KeyValuePair<IshHeader, IshItem> pair2 in codeplugInIshItemCollection)
- {
- if (pair2.Value.ItemSize > 0)
- {
- num += 6 + pair2.Value.ItemSize;
- }
- }
- byte[] buffer2 = new byte[num];
- using (MemoryStream stream = new MemoryStream(buffer2))
- {
- byte[] array = null;
- foreach (KeyValuePair<IshHeader, IshItem> pair in codeplugInIshItemCollection)
- {
- if (pair.Value.ItemSize > 0)
- {
- IshHeader key = pair.Key;
- IshItem item = pair.Value;
- array = BitConverter.GetBytes(key.IshType);
- if (!BitConverter.IsLittleEndian)
- {
- Array.Reverse(array);
- }
- stream.Write(array, 0, 2);
- array = BitConverter.GetBytes(key.IshID);
- if (!BitConverter.IsLittleEndian)
- {
- Array.Reverse(array);
- }
- stream.Write(array, 0, 2);
- stream.WriteByte((byte) (item.ItemSize & 0xff));
- stream.WriteByte((byte) (item.ItemSize >> 8));
- if (!this.IsCrcBigEndian)
- {
- byte num2 = item.Data[item.Data.Length - 1];
- item.Data[item.Data.Length - 1] = item.Data[item.Data.Length - 2];
- item.Data[item.Data.Length - 2] = num2;
- }
- stream.Write(item.Data, 0, item.ItemSize);
- }
- }
- }
- return buffer2;
- }
- private PbaObject method_2(CodeplugCommFileHeader codeplugCommFileHeader_0, byte[] byte_0)
- {
- UtilityMethods.CheckIfParameterIsNull(byte_0, CommonErrorCode.ArgumentNull);
- PbaObject obj2 = new PbaObject();
- switch (codeplugCommFileHeader_0.Level)
- {
- case InfoLevel.TypeLevelChangeDisc:
- smethod_6(obj2, byte_0);
- return obj2;
- case InfoLevel.ItemLevelChangeDisc:
- smethod_7(obj2, byte_0);
- return obj2;
- case InfoLevel.CodeplugData:
- this.method_3(obj2, byte_0);
- return obj2;
- }
- return obj2;
- }
- private void method_3(PbaObject pbaObject_0, byte[] byte_0)
- {
- IshItem item;
- int length = byte_0.GetLength(0);
- IshItemCollection ishItemCollection = new IshItemCollection();
- for (int i = 3; i < (length - 4); i += item.ItemSize)
- {
- if ((((i + 2) + 2) + 2) >= (length - 4))
- {
- throw new CommonException(CommonErrorCode.CommonFileInvalidCodeplugDataLength);
- }
- IshHeader headers = new IshHeader();
- ushort num4 = byte_0[i];
- i++;
- ushort num5 = byte_0[i];
- i++;
- headers.IshType = (ushort) ((num5 << 8) | num4);
- num4 = byte_0[i];
- i++;
- num5 = byte_0[i];
- i++;
- headers.IshID = (ushort) ((num5 << 8) | num4);
- item = new IshItem();
- num4 = byte_0[i];
- i++;
- num5 = byte_0[i];
- i++;
- item.ItemSize = (num5 << 8) | num4;
- if ((i + item.ItemSize) > (length - 4))
- {
- throw new CommonException(CommonErrorCode.CommonFileInvalidItemDataLength);
- }
- byte[] destinationArray = new byte[item.ItemSize];
- Array.Copy(byte_0, i, destinationArray, 0, item.ItemSize);
- item.Data = destinationArray;
- if (!this.IsCrcBigEndian)
- {
- byte num3 = item.Data[item.Data.Length - 1];
- item.Data[item.Data.Length - 1] = item.Data[item.Data.Length - 2];
- item.Data[item.Data.Length - 2] = num3;
- }
- ishItemCollection.Add(headers, item);
- }
- pbaObject_0.SetCodeplug(ishItemCollection);
- }
- public byte[] Serialize(CodeplugCommFileHeader fileHeader, PbaObject pbaObject)
- {
- byte[] c = smethod_0(fileHeader);
- byte[] buffer2 = this.method_0(fileHeader, pbaObject);
- ArrayList list = new ArrayList();
- list.AddRange(c);
- list.AddRange(buffer2);
- byte[] buffer3 = (byte[]) list.ToArray(typeof(byte));
- byte[] buffer4 = smethod_1(buffer3);
- ArrayList list2 = new ArrayList();
- list2.AddRange(buffer3);
- list2.AddRange(buffer4);
- this.FileData = (byte[]) list2.ToArray(typeof(byte));
- return this.FileData;
- }
- public void Serialize(ComponentType compType, ComponentInstance compInst, CodeplugCommFileHeader fileHeader, PbaObject pbaObject)
- {
- byte[] c = smethod_0(fileHeader);
- byte[] buffer2 = this.method_0(fileHeader, pbaObject);
- ArrayList list = new ArrayList();
- list.AddRange(c);
- list.AddRange(buffer2);
- byte[] buffer3 = (byte[]) list.ToArray(typeof(byte));
- byte[] buffer4 = smethod_1(buffer3);
- ArrayList list2 = new ArrayList();
- list2.AddRange(buffer3);
- list2.AddRange(buffer4);
- this.FileData = (byte[]) list2.ToArray(typeof(byte));
- FileStream stream = new FileStream(string.Format(CultureInfo.InvariantCulture, Path.GetTempPath() + "{0}_{1}.ish", new object[] { (byte) compType, (byte) compInst }), FileMode.Create, FileAccess.Write, FileShare.None);
- stream.Write(this.FileData, 0, this.FileData.Length);
- stream.Close();
- }
- private static byte[] smethod_0(CodeplugCommFileHeader codeplugCommFileHeader_0)
- {
- return new byte[] { codeplugCommFileHeader_0.HeaderVer, codeplugCommFileHeader_0.Level, codeplugCommFileHeader_0.FullDelta };
- }
- private static byte[] smethod_1(byte[] byte_0)
- {
- byte[] bytes = BitConverter.GetBytes(CheckSumCRC32.GetCRC32CheckSum(byte_0));
- if (!BitConverter.IsLittleEndian)
- {
- Array.Reverse(bytes);
- }
- return bytes;
- }
- private static byte[] smethod_2(PbaObject pbaObject_0)
- {
- UtilityMethods.CheckIfParameterIsNull(pbaObject_0.CodeplugData.TypeCounters, CommonErrorCode.ArgumentNull);
- int num = pbaObject_0.CodeplugData.TypeCounters.Count * 6;
- byte[] buffer = new byte[num];
- using (MemoryStream stream = new MemoryStream(buffer))
- {
- byte[] array = null;
- foreach (TypeLevelChangeDiscovery discovery in pbaObject_0.CodeplugData.TypeCounters)
- {
- if (discovery != null)
- {
- array = BitConverter.GetBytes(discovery.IshType);
- if (!BitConverter.IsLittleEndian)
- {
- Array.Reverse(array);
- }
- stream.Write(array, 0, 2);
- array = BitConverter.GetBytes(discovery.Counter);
- if (!BitConverter.IsLittleEndian)
- {
- Array.Reverse(array);
- }
- stream.Write(array, 0, 4);
- }
- }
- }
- return buffer;
- }
- private static byte[] smethod_3(PbaObject pbaObject_0)
- {
- UtilityMethods.CheckIfParameterIsNull(pbaObject_0.CodeplugData.IDCounters, CommonErrorCode.ArgumentNull);
- int num = pbaObject_0.CodeplugData.IDCounters.Count * 8;
- byte[] buffer = new byte[num];
- using (MemoryStream stream = new MemoryStream(buffer))
- {
- byte[] array = null;
- foreach (ItemLevelChangeDiscovery discovery in pbaObject_0.CodeplugData.IDCounters)
- {
- if (discovery != null)
- {
- array = BitConverter.GetBytes(discovery.IshType);
- if (!BitConverter.IsLittleEndian)
- {
- Array.Reverse(array);
- }
- stream.Write(array, 0, 2);
- array = BitConverter.GetBytes(discovery.IshID);
- if (!BitConverter.IsLittleEndian)
- {
- Array.Reverse(array);
- }
- stream.Write(array, 0, 2);
- array = BitConverter.GetBytes(discovery.Counter);
- if (!BitConverter.IsLittleEndian)
- {
- Array.Reverse(array);
- }
- stream.Write(array, 0, 4);
- }
- }
- }
- return buffer;
- }
- private static bool smethod_4(byte[] byte_0)
- {
- UtilityMethods.CheckIfParameterIsNull(byte_0, CommonErrorCode.ArgumentNull);
- bool flag = true;
- int length = byte_0.Length - 4;
- if (0 >= length)
- {
- throw new CommonException(CommonErrorCode.CommonFileInvalidFileLength);
- }
- byte[] destinationArray = new byte[length];
- Array.Copy(byte_0, 0, destinationArray, 0, length);
- uint num2 = CheckSumCRC32.GetCRC32CheckSum(destinationArray);
- uint num3 = byte_0[byte_0.Length - 4];
- uint num4 = byte_0[(byte_0.Length - 4) + 1];
- uint num5 = byte_0[(byte_0.Length - 4) + 2];
- uint num6 = byte_0[(byte_0.Length - 4) + 3];
- uint num7 = (((num6 << 0x18) | (num5 << 0x10)) | (num4 << 8)) | num3;
- if (num2 != num7)
- {
- flag = false;
- }
- return flag;
- }
- private static CodeplugCommFileHeader smethod_5(byte[] byte_0)
- {
- UtilityMethods.CheckIfParameterIsNull(byte_0, CommonErrorCode.ArgumentNull);
- CodeplugCommFileHeader header = new CodeplugCommFileHeader();
- int index = 0;
- header.HeaderVer = byte_0[0];
- if (0x10 != header.HeaderVer)
- {
- throw new CommonException(CommonErrorCode.CommonFileInvalidCommonFileVersion);
- }
- index++;
- header.Level = (InfoLevel) byte_0[index];
- index++;
- header.FullDelta = (FullDeltaCodeplug) byte_0[index];
- return header;
- }
- private static void smethod_6(PbaObject pbaObject_0, byte[] byte_0)
- {
- int length = byte_0.GetLength(0);
- if ((((length - 3) - 4) % 6) != 0)
- {
- throw new CommonException(CommonErrorCode.CommonFileInvalidTypeLevelDataLength);
- }
- List<TypeLevelChangeDiscovery> typeCounters = new List<TypeLevelChangeDiscovery>();
- int index = 3;
- while (index < (length - 4))
- {
- TypeLevelChangeDiscovery item = new TypeLevelChangeDiscovery();
- ushort num3 = byte_0[index];
- index++;
- ushort num4 = byte_0[index];
- item.IshType = (ushort) ((num4 << 8) | num3);
- index++;
- int num5 = byte_0[index];
- index++;
- int num6 = byte_0[index];
- index++;
- int num7 = byte_0[index];
- index++;
- int num8 = byte_0[index];
- item.Counter = (uint) ((((num8 << 0x18) | (num7 << 0x10)) | (num6 << 8)) | num5);
- index++;
- typeCounters.Add(item);
- }
- pbaObject_0.CodeplugData.SetTypeCounters(typeCounters);
- }
- private static void smethod_7(PbaObject pbaObject_0, byte[] byte_0)
- {
- int length = byte_0.GetLength(0);
- if ((((length - 3) - 4) % 8) != 0)
- {
- throw new CommonException(CommonErrorCode.CommonFileInvalidItemLevelDataLength);
- }
- List<ItemLevelChangeDiscovery> idCounters = new List<ItemLevelChangeDiscovery>();
- int index = 3;
- while (index < (length - 4))
- {
- ItemLevelChangeDiscovery item = new ItemLevelChangeDiscovery();
- ushort num3 = byte_0[index];
- index++;
- ushort num4 = byte_0[index];
- item.IshType = (ushort) ((num4 << 8) | num3);
- index++;
- num3 = byte_0[index];
- index++;
- num4 = byte_0[index];
- item.IshID = (ushort) ((num4 << 8) | num3);
- index++;
- int num5 = byte_0[index];
- index++;
- int num6 = byte_0[index];
- index++;
- int num7 = byte_0[index];
- index++;
- int num8 = byte_0[index];
- item.Counter = (uint) ((((num8 << 0x18) | (num7 << 0x10)) | (num6 << 8)) | num5);
- index++;
- idCounters.Add(item);
- }
- pbaObject_0.CodeplugData.SetIDCounters(idCounters);
- }
- // Properties
- public byte[] FileData { get; set; }
- }
- Collapse Methods
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement