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 public class ZipFile : IFolder
{ {
readonly SZipFile pkg; string filename;
SZipFile pkg;
int priority; int priority;
public ZipFile(string filename, int priority) public ZipFile(string filename, int priority)
{ {
this.filename = filename;
this.priority = priority; this.priority = priority;
try 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) catch (ZipException e)
{ {
@@ -46,15 +49,18 @@ namespace OpenRA.FileFormats
public Stream GetContent(string filename) public Stream GetContent(string filename)
{ {
var ms = new MemoryStream();
var z = pkg.GetInputStream(pkg.GetEntry(filename)); using (var z = pkg.GetInputStream(pkg.GetEntry(filename)))
int bufSize = 2048; {
byte[] buf = new byte[bufSize]; var ms = new MemoryStream();
while ((bufSize = z.Read(buf, 0, buf.Length)) > 0) int bufSize = 2048;
ms.Write(buf, 0, bufSize); byte[] buf = new byte[bufSize];
while ((bufSize = z.Read(buf, 0, buf.Length)) > 0)
ms.Seek(0, SeekOrigin.Begin); ms.Write(buf, 0, bufSize);
return ms;
ms.Seek(0, SeekOrigin.Begin);
return ms;
}
} }
public IEnumerable<uint> AllFileHashes() public IEnumerable<uint> AllFileHashes()
@@ -75,14 +81,21 @@ namespace OpenRA.FileFormats
public void Write(Dictionary<string, byte[]> contents) public void Write(Dictionary<string, byte[]> contents)
{ {
pkg.Close();
pkg = SZipFile.Create(filename);
pkg.BeginUpdate(); pkg.BeginUpdate();
// TODO: Clear existing content? // TODO: Clear existing content?
foreach (var kvp in contents) foreach (var kvp in contents)
{
pkg.Add(new StaticMemoryDataSource(kvp.Value), kvp.Key); pkg.Add(new StaticMemoryDataSource(kvp.Value), kvp.Key);
}
pkg.CommitUpdate(); 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.Reflection;
using System.Security.Cryptography; using System.Security.Cryptography;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using System.Text;
namespace OpenRA namespace OpenRA
{ {
@@ -202,7 +203,7 @@ namespace OpenRA
var root = new List<MiniYamlNode>(); var root = new List<MiniYamlNode>();
foreach (var field in new string[] {"Selectable", "MapFormat", "Title", "Description", "Author", "PlayerCount", "Tileset", "MapSize", "TopLeft", "BottomRight", "UseAsShellmap", "Type"}) 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; if (f.GetValue(this) == null) continue;
root.Add( new MiniYamlNode( field, FieldSaver.FormatValue( this, f ) ) ); root.Add( new MiniYamlNode( field, FieldSaver.FormatValue( this, f ) ) );
} }
@@ -227,7 +228,7 @@ namespace OpenRA
Dictionary<string, byte[]> entries = new Dictionary<string, byte[]>(); Dictionary<string, byte[]> entries = new Dictionary<string, byte[]>();
entries.Add("map.bin", SaveBinaryData()); entries.Add("map.bin", SaveBinaryData());
var s = root.WriteToString(); 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 // Saving the map to a new location
if (toPath != Path) if (toPath != Path)

Binary file not shown.