//
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;
}