using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LibXom.Data { public class XomContainer : XomFileComponent { private string typeBelongs; internal byte[] data; internal XomContainer(XomFile fromFile, string fromType, byte[] data) { fileBelongs = fromFile; typeBelongs = fromType; this.data = data; } public XomType Type { get { return this.fileBelongs.GetTypeByName(typeBelongs); } } public override int Id { get { return this.fileBelongs.calculateIdForXomFileComponent(this.Uuid, fileBelongs.XomContainers); } } public byte[] GetData() { return data; } public void SetData(byte[] data) { this.Type.ReplaceContainerData(this, data); } // Compress method, convert bytes to int array, then to compressed byte array. public byte[] Compress(byte[] bytes) { return this.Compress(byteArrayToIntArray(bytes)); } // Compress method, convert nums to compressed byte array. public byte[] Compress(int[] nums) { byte[] buffer = XomCompressor.compressBuffer(nums); byte[] compressedData = new byte[buffer.Length + 3]; Array.ConstrainedCopy(buffer, 0, compressedData, 3, buffer.Length); return compressedData; } public void CompressAndUpdate(byte[] bytes) { this.SetData(this.Compress(bytes)); } public void CompressAndUpdate(int[] nums) { this.SetData(this.Compress(nums)); } // Decompress methods, decompress all data, (eg for WeaponFactoryCollective, or StoredStatsCollective) public int[] Decompress() { byte[] compressedData = new byte[data.Length - 3]; Array.ConstrainedCopy(data, 3, compressedData, 0, compressedData.Length); return XomCompressor.decompressBuffer(compressedData); } // Decompress to a byte array. public byte[] DecompressToBytes() { return intArrayToByteArray(this.Decompress()); } private int[] byteArrayToIntArray(byte[] byteArray) { int[] intArray = new int[byteArray.Length / 4]; for (int i = 0; i < intArray.Length; i++) intArray[i] = BitConverter.ToInt32(byteArray, i * 4); return intArray; } public void Delete() { this.Type.DeleteContainer(this); } private byte[] intArrayToByteArray(int[] intArray) { using (MemoryStream ms = new MemoryStream()) { foreach (int i in intArray) { byte[] buf = BitConverter.GetBytes(i); ms.Write(buf, 0, buf.Length); } ms.Seek(0, SeekOrigin.Begin); return ms.ToArray(); } } } }