Create a mix file with a list of files
This commit is contained in:
@@ -132,6 +132,57 @@ namespace OpenRA.FileFormats
|
|||||||
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;
|
||||||
|
|||||||
@@ -30,6 +30,14 @@ 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();
|
||||||
@@ -37,6 +45,13 @@ namespace OpenRA.FileFormats
|
|||||||
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()
|
||||||
{
|
{
|
||||||
string filename;
|
string filename;
|
||||||
|
|||||||
Reference in New Issue
Block a user