Rework IReadWritePackage interface.
This commit is contained in:
@@ -34,17 +34,6 @@ namespace OpenRA.FileSystem
|
||||
|
||||
Cache<string, List<IReadOnlyPackage>> fileIndex = new Cache<string, List<IReadOnlyPackage>>(_ => new List<IReadOnlyPackage>());
|
||||
|
||||
public IReadWritePackage CreatePackage(string filename)
|
||||
{
|
||||
var content = new Dictionary<string, byte[]>();
|
||||
if (filename.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase))
|
||||
return new ZipFile(this, filename, content);
|
||||
if (filename.EndsWith(".oramap", StringComparison.InvariantCultureIgnoreCase))
|
||||
return new ZipFile(this, filename, content);
|
||||
|
||||
return new Folder(filename, content);
|
||||
}
|
||||
|
||||
public IReadOnlyPackage OpenPackage(string filename)
|
||||
{
|
||||
if (filename.EndsWith(".mix", StringComparison.InvariantCultureIgnoreCase))
|
||||
|
||||
@@ -18,16 +18,6 @@ namespace OpenRA.FileSystem
|
||||
{
|
||||
readonly string path;
|
||||
|
||||
// Create a new folder package
|
||||
public Folder(string path, Dictionary<string, byte[]> contents)
|
||||
{
|
||||
this.path = path;
|
||||
if (Directory.Exists(path))
|
||||
Directory.Delete(path, true);
|
||||
|
||||
Write(contents);
|
||||
}
|
||||
|
||||
public Folder(string path)
|
||||
{
|
||||
this.path = path;
|
||||
@@ -57,15 +47,22 @@ namespace OpenRA.FileSystem
|
||||
return File.Exists(Path.Combine(path, filename));
|
||||
}
|
||||
|
||||
public void Write(Dictionary<string, byte[]> contents)
|
||||
public void Update(string filename, byte[] contents)
|
||||
{
|
||||
if (!Directory.Exists(path))
|
||||
Directory.CreateDirectory(path);
|
||||
|
||||
foreach (var file in contents)
|
||||
using (var dataStream = File.Create(Path.Combine(path, file.Key)))
|
||||
using (var writer = new BinaryWriter(dataStream))
|
||||
writer.Write(file.Value);
|
||||
using (var s = File.Create(Path.Combine(path, filename)))
|
||||
s.Write(contents, 0, contents.Length);
|
||||
}
|
||||
|
||||
public void Delete(string filename)
|
||||
{
|
||||
var filePath = Path.Combine(path, filename);
|
||||
if (Directory.Exists(filePath))
|
||||
Directory.Delete(filePath, true);
|
||||
else if (File.Exists(filePath))
|
||||
File.Delete(filePath);
|
||||
}
|
||||
|
||||
public void Dispose() { }
|
||||
|
||||
@@ -25,6 +25,7 @@ namespace OpenRA.FileSystem
|
||||
|
||||
public interface IReadWritePackage : IReadOnlyPackage
|
||||
{
|
||||
void Write(Dictionary<string, byte[]> contents);
|
||||
void Update(string filename, byte[] contents);
|
||||
void Delete(string filename);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,31 +27,14 @@ namespace OpenRA.FileSystem
|
||||
ZipConstants.DefaultCodePage = Encoding.UTF8.CodePage;
|
||||
}
|
||||
|
||||
public ZipFile(FileSystem context, string filename)
|
||||
public ZipFile(IReadOnlyFileSystem context, string filename, bool createOrClearContents = false)
|
||||
{
|
||||
Name = filename;
|
||||
|
||||
try
|
||||
{
|
||||
// Pull the file into memory, don't keep it open.
|
||||
pkg = new SZipFile(new MemoryStream(File.ReadAllBytes(filename)));
|
||||
}
|
||||
catch (ZipException e)
|
||||
{
|
||||
Log.Write("debug", "Couldn't load zip file: {0}", e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
// Create a new zip with the specified contents.
|
||||
public ZipFile(FileSystem context, string filename, Dictionary<string, byte[]> contents)
|
||||
{
|
||||
Name = filename;
|
||||
|
||||
if (File.Exists(filename))
|
||||
File.Delete(filename);
|
||||
|
||||
pkg = SZipFile.Create(filename);
|
||||
Write(contents);
|
||||
if (createOrClearContents)
|
||||
pkg = SZipFile.Create(filename);
|
||||
else
|
||||
pkg = new SZipFile(filename);
|
||||
}
|
||||
|
||||
public Stream GetStream(string filename)
|
||||
@@ -83,19 +66,18 @@ namespace OpenRA.FileSystem
|
||||
return pkg.GetEntry(filename) != null;
|
||||
}
|
||||
|
||||
public void Write(Dictionary<string, byte[]> contents)
|
||||
public void Update(string filename, byte[] contents)
|
||||
{
|
||||
// TODO: Clear existing content?
|
||||
pkg.Close();
|
||||
pkg = SZipFile.Create(Name);
|
||||
pkg.BeginUpdate();
|
||||
|
||||
foreach (var kvp in contents)
|
||||
pkg.Add(new StaticMemoryDataSource(kvp.Value), kvp.Key);
|
||||
|
||||
pkg.Add(new StaticMemoryDataSource(contents), filename);
|
||||
pkg.CommitUpdate();
|
||||
}
|
||||
|
||||
public void Delete(string filename)
|
||||
{
|
||||
pkg.BeginUpdate();
|
||||
pkg.Delete(filename);
|
||||
pkg.CommitUpdate();
|
||||
pkg.Close();
|
||||
pkg = new SZipFile(new MemoryStream(File.ReadAllBytes(Name)));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
Reference in New Issue
Block a user