Create a mix file with a list of files

This commit is contained in:
Paul Chote
2010-07-17 18:06:27 +12:00
parent a6ba5ec453
commit 04758bacde
2 changed files with 66 additions and 0 deletions

View File

@@ -131,7 +131,58 @@ namespace OpenRA.FileFormats
dataStart = s.Position; dataStart = s.Position;
return items; return items;
} }
public static void CreateMix(string filename, List<string> contents)
{
// Construct a list of entries for the file header
ushort numFiles = 0;
uint dataSize = 0;
List<PackageEntry> items = new List<PackageEntry>();
foreach (var file in contents)
{
uint length = (uint) new FileInfo(file).Length;
uint hash = PackageEntry.HashFilename(Path.GetFileName(file));
items.Add(new PackageEntry(hash, dataSize, length));
dataSize += length;
numFiles++;
}
Stream s = new FileStream(filename, FileMode.Create);
var writer = new BinaryWriter(s);
// Write file header
writer.Write(numFiles);
writer.Write(dataSize);
foreach(var item in items)
item.Write(writer);
writer.Flush();
// Copy file data
foreach (var file in contents)
{
var f = File.Open(file,FileMode.Open);
CopyStream(f,s);
f.Close();
}
writer.Close();
s.Close();
}
static void CopyStream (Stream readStream, Stream writeStream)
{
var Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer,0,Length);
while( bytesRead > 0 )
{
writeStream.Write(buffer,0,bytesRead);
bytesRead = readStream.Read(buffer,0,Length);
}
}
public Stream GetContent(uint hash) public Stream GetContent(uint hash)
{ {
PackageEntry e; PackageEntry e;

View File

@@ -30,12 +30,27 @@ namespace OpenRA.FileFormats
public readonly uint Offset; public readonly uint Offset;
public readonly uint Length; public readonly uint Length;
public PackageEntry(uint hash, uint offset, uint length)
{
Hash = hash;
Offset = offset;
Length = length;
}
public PackageEntry(BinaryReader r) public PackageEntry(BinaryReader r)
{ {
Hash = r.ReadUInt32(); Hash = r.ReadUInt32();
Offset = r.ReadUInt32(); Offset = r.ReadUInt32();
Length = r.ReadUInt32(); Length = r.ReadUInt32();
} }
public void Write(BinaryWriter w)
{
w.Write(Hash);
w.Write(Offset);
w.Write(Length);
}
public override string ToString() public override string ToString()
{ {