hack up ZipFile to fix #464; filesystem needs rework to be sane

This commit is contained in:
Chris Forbes
2011-01-01 15:28:07 +13:00
parent cf2ad68a7c
commit dffb5293d0
4 changed files with 30 additions and 16 deletions

View File

@@ -17,15 +17,18 @@ namespace OpenRA.FileFormats
{
public class ZipFile : IFolder
{
readonly SZipFile pkg;
string filename;
SZipFile pkg;
int priority;
public ZipFile(string filename, int priority)
{
this.filename = filename;
this.priority = priority;
try
{
pkg = new SZipFile(File.OpenRead(filename));
// pull the file into memory, dont keep it open.
pkg = new SZipFile(new MemoryStream(File.ReadAllBytes(filename)));
}
catch (ZipException e)
{
@@ -46,15 +49,18 @@ namespace OpenRA.FileFormats
public Stream GetContent(string filename)
{
var ms = new MemoryStream();
var z = pkg.GetInputStream(pkg.GetEntry(filename));
int bufSize = 2048;
byte[] buf = new byte[bufSize];
while ((bufSize = z.Read(buf, 0, buf.Length)) > 0)
ms.Write(buf, 0, bufSize);
ms.Seek(0, SeekOrigin.Begin);
return ms;
using (var z = pkg.GetInputStream(pkg.GetEntry(filename)))
{
var ms = new MemoryStream();
int bufSize = 2048;
byte[] buf = new byte[bufSize];
while ((bufSize = z.Read(buf, 0, buf.Length)) > 0)
ms.Write(buf, 0, bufSize);
ms.Seek(0, SeekOrigin.Begin);
return ms;
}
}
public IEnumerable<uint> AllFileHashes()
@@ -75,14 +81,21 @@ namespace OpenRA.FileFormats
public void Write(Dictionary<string, byte[]> contents)
{
pkg.Close();
pkg = SZipFile.Create(filename);
pkg.BeginUpdate();
// TODO: Clear existing content?
foreach (var kvp in contents)
{
pkg.Add(new StaticMemoryDataSource(kvp.Value), kvp.Key);
}
pkg.CommitUpdate();
pkg.Close();
pkg = new SZipFile(new MemoryStream(File.ReadAllBytes(filename)));
}
}

View File

@@ -16,6 +16,7 @@ using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using OpenRA.FileFormats;
using System.Text;
namespace OpenRA
{
@@ -202,7 +203,7 @@ namespace OpenRA
var root = new List<MiniYamlNode>();
foreach (var field in new string[] {"Selectable", "MapFormat", "Title", "Description", "Author", "PlayerCount", "Tileset", "MapSize", "TopLeft", "BottomRight", "UseAsShellmap", "Type"})
{
FieldInfo f = this.GetType().GetField(field);
var f = this.GetType().GetField(field);
if (f.GetValue(this) == null) continue;
root.Add( new MiniYamlNode( field, FieldSaver.FormatValue( this, f ) ) );
}
@@ -227,7 +228,7 @@ namespace OpenRA
Dictionary<string, byte[]> entries = new Dictionary<string, byte[]>();
entries.Add("map.bin", SaveBinaryData());
var s = root.WriteToString();
entries.Add("map.yaml", System.Text.Encoding.UTF8.GetBytes(s));
entries.Add("map.yaml", Encoding.UTF8.GetBytes(s));
// Saving the map to a new location
if (toPath != Path)

Binary file not shown.