// default UID is the SHA1 of the byte stream representing this resource public virtual UID getUID() { byte[] buffer = SaveToByteStream(); SHA1CryptoServiceProvider cryptoTransformSHA1 = new SHA1CryptoServiceProvider(); byte[] hashResult = cryptoTransformSHA1.ComputeHash(buffer); // UID is only the first 6 bytes of the SHA1 UID uid = new UID(hashResult); return uid; }
// SiDComponent public override void LoadFromByteStream(BinaryReader br, Int32 streamLength) { Iterate((Int32 x, Int32 y, ref UID uid) => { byte[] uidbytes = br.ReadBytes(6); uid = new UID(uidbytes); }); for (Int32 y = 0; y < Constants.RoomSize; y++) { for (Int32 x = 0; x < Constants.RoomSize; x++) { wallFlags[x, y] = br.ReadByte(); } } Name = ByteUtils.readNullTermString11(br); }
// SiDComponent public override byte[] SaveToByteStream() { List<byte> byteStream = new List<byte>(1064); Iterate((Int32 x, Int32 y, ref UID uid) => { // this magic number is the SHA1 of a blank, empty tile UID uidToStore = new UID(new byte[6] { 0x88, 0xBB, 0xE0, 0xFB, 0x7B, 0x40 }); if (uid != null) { uidToStore = uid; } // each tile is stored by UID byteStream.AddRange(uidToStore.UIDBytes); }); for (Int32 y = 0; y < Constants.RoomSize; y++) { for (Int32 x = 0; x < Constants.RoomSize; x++) { byteStream.Add(wallFlags[x, y]); } } // append tile name byte[] stringBytes = ASCIIEncoding.UTF8.GetBytes(Name); byteStream.AddRange(stringBytes); byteStream.Add(0); // terminate return byteStream.ToArray(); }
public bool Equals(UID rhs) { return (UIDBytes.SequenceEqual(rhs.UIDBytes)); }
// get a component by its UID; may return null public SiDComponent LookupByUID(UID compUID) { SiDComponent dupeComponent = null; mComponentsByUID.TryGetValue(compUID.ToString(), out dupeComponent); return dupeComponent; }
// public bool Load(String filename) { Console.WriteLine("ResourcePack::Load - {0}", filename); using (FileStream fs = File.Open(filename, FileMode.Open)) { byte[] unpackBuffer = null; using (BinaryReader br = new BinaryReader(fs)) { UInt32 sizeUncompressed = ByteUtils.SwapUInt32(br.ReadUInt32()); unpackBuffer = new byte[sizeUncompressed]; byte[] packedBuf = new byte[br.BaseStream.Length - 4]; br.Read(packedBuf, 0, packedBuf.Length); Console.WriteLine(" ... uncompressed size is {0}", sizeUncompressed); using (MemoryStream ms = new MemoryStream(packedBuf)) { using (ZInputStream zStream = new ZInputStream(ms)) { Int32 bOffset = 0; while (bOffset < sizeUncompressed) { Int32 decS = zStream.read(unpackBuffer, bOffset, (Int32)sizeUncompressed - bOffset); bOffset += decS; } } } } fs.Close(); using (MemoryStream ms = new MemoryStream(unpackBuffer)) { using (BinaryReader ir = new BinaryReader(ms)) { UInt32 numChunks = ByteUtils.SwapUInt32(ir.ReadUInt32()); Console.WriteLine(" ... processing {0} chunks", numChunks); for (UInt32 i = 0; i < numChunks; i++) { String componentTypeName = ByteUtils.readNullTermString11(ir); UID componentUID = new UID(ir.ReadBytes(6)); String componentName = ByteUtils.readNullTermString11(ir); UInt32 componentDataLength = ByteUtils.SwapUInt32(ir.ReadUInt32()); // go see if we know how to build a component from this data type Type typeToBuild = null; if (mComponentFactory.TryGetValue(componentTypeName, out typeToBuild)) { // yes; so instantiate the returned type SiDComponent newComponent = Activator.CreateInstance(typeToBuild) as SiDComponent; newComponent.Name = componentName; // ask it to load from the bytestream newComponent.LoadFromByteStream(ir, (Int32)componentDataLength); // record it if (!Add(newComponent)) { Console.WriteLine("Warning! duplicate item in single resource pack? {0}", componentUID); } Console.WriteLine(" + {0}:{1}: - {2}", componentUID, SiDComponent.getResourceTypeName(typeToBuild), newComponent.Name); } else { // we don't know how to construct this type; store as a 'black box' blob Blob newBlob = new Blob(componentTypeName); newBlob.Name = componentName; // blobs just hold onto the data as raw bytes, ready to parrot it back out on Save newBlob.LoadFromByteStream(ir, (Int32)componentDataLength); // record it if (!Add(newBlob)) { Console.WriteLine("Warning! duplicate (blob) item in single resource pack? {0}", componentUID); } Console.WriteLine(" + {0}:{1}:BLOB: - {2}", componentUID, componentTypeName, componentName); } } } } } return true; }