From c3ff679f3a8d2d4d9bcfe793b3033e29032dc927 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Wed, 29 Dec 2010 11:39:26 +1300 Subject: [PATCH] Make map saving independent of Container type. Saving zip/oramap/mix untested as the editor cannot load non-folder maps. --- OpenRA.FileFormats/Filesystem/Folder.cs | 9 +++ .../Filesystem/InstallShieldPackage.cs | 5 ++ OpenRA.FileFormats/Filesystem/MixFile.cs | 30 ++++++++ .../Filesystem/PackageWriter.cs | 77 ------------------- OpenRA.FileFormats/Filesystem/ZipFile.cs | 26 +++++++ OpenRA.FileFormats/OpenRA.FileFormats.csproj | 1 - OpenRA.Game/Map.cs | 3 +- OpenRA.TilesetBuilder/Form1.cs | 3 +- 8 files changed, 73 insertions(+), 81 deletions(-) delete mode 100644 OpenRA.FileFormats/Filesystem/PackageWriter.cs diff --git a/OpenRA.FileFormats/Filesystem/Folder.cs b/OpenRA.FileFormats/Filesystem/Folder.cs index 09a83da257..68fc3b41a0 100644 --- a/OpenRA.FileFormats/Filesystem/Folder.cs +++ b/OpenRA.FileFormats/Filesystem/Folder.cs @@ -10,6 +10,7 @@ using System.Collections.Generic; using System.IO; +using System; namespace OpenRA.FileFormats { @@ -43,5 +44,13 @@ namespace OpenRA.FileFormats { get { return priority; } } + + public void Write(Dictionary contents) + { + 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); + } } } diff --git a/OpenRA.FileFormats/Filesystem/InstallShieldPackage.cs b/OpenRA.FileFormats/Filesystem/InstallShieldPackage.cs index b260c9386d..44472481fc 100644 --- a/OpenRA.FileFormats/Filesystem/InstallShieldPackage.cs +++ b/OpenRA.FileFormats/Filesystem/InstallShieldPackage.cs @@ -117,5 +117,10 @@ namespace OpenRA.FileFormats { get { return 2000 + priority; } } + + public void Write(Dictionary contents) + { + throw new NotImplementedException("Cannot save InstallShieldPackages."); + } } } diff --git a/OpenRA.FileFormats/Filesystem/MixFile.cs b/OpenRA.FileFormats/Filesystem/MixFile.cs index 06a35da392..de1f5e49d1 100644 --- a/OpenRA.FileFormats/Filesystem/MixFile.cs +++ b/OpenRA.FileFormats/Filesystem/MixFile.cs @@ -20,6 +20,7 @@ namespace OpenRA.FileFormats Stream GetContent(string filename); bool Exists(string filename); IEnumerable AllFileHashes(); + void Write(Dictionary contents); int Priority { get; } } @@ -158,6 +159,35 @@ namespace OpenRA.FileFormats { get { return 1000 + priority; } } + + public void Write(Dictionary contents) + { + // Construct a list of entries for the file header + uint dataSize = 0; + var items = new List(); + foreach (var kv in contents) + { + uint length = (uint)kv.Value.Length; + uint hash = PackageEntry.HashFilename(Path.GetFileName(kv.Key)); + items.Add(new PackageEntry(hash, dataSize, length)); + dataSize += length; + } + + using (var writer = new BinaryWriter(s)) + { + // Write file header + writer.Write((ushort)items.Count); + writer.Write(dataSize); + foreach (var item in items) + item.Write(writer); + + writer.Flush(); + + // Copy file data + foreach (var file in contents) + s.Write(file.Value); + } + } } [Flags] diff --git a/OpenRA.FileFormats/Filesystem/PackageWriter.cs b/OpenRA.FileFormats/Filesystem/PackageWriter.cs deleted file mode 100644 index 32aab0c3f1..0000000000 --- a/OpenRA.FileFormats/Filesystem/PackageWriter.cs +++ /dev/null @@ -1,77 +0,0 @@ -#region Copyright & License Information -/* - * Copyright 2007-2010 The OpenRA Developers (see AUTHORS) - * This file is part of OpenRA, which is free software. It is made - * available to you under the terms of the GNU General Public License - * as published by the Free Software Foundation. For more information, - * see LICENSE. - */ -#endregion - -using System.Collections.Generic; -using System.IO; -using ICSharpCode.SharpZipLib; -using ICSharpCode.SharpZipLib.Zip; -using SZipFile = ICSharpCode.SharpZipLib.Zip.ZipFile; - -namespace OpenRA.FileFormats -{ - public static class PackageWriter - { - public static void CreateMix(string filename, List contents) - { - // Construct a list of entries for the file header - uint dataSize = 0; - var items = new List(); - 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; - } - - using (var s = File.Create(filename)) - using (var writer = new BinaryWriter(s)) - { - // Write file header - writer.Write((ushort)items.Count); - writer.Write(dataSize); - foreach (var item in items) - item.Write(writer); - - writer.Flush(); - - // Copy file data - foreach (var file in contents) - s.Write(File.ReadAllBytes(file)); - } - } - - class StaticMemoryDataSource : IStaticDataSource - { - byte[] data; - public StaticMemoryDataSource (byte[] data) - { - this.data = data; - } - - public Stream GetSource() - { - return new MemoryStream(data); - } - } - - public static void CreateZip(string zipFile, Dictionary contents) - { - var z = SZipFile.Create(zipFile); - z.BeginUpdate(); - foreach (var kvp in contents) - { - z.Add(new StaticMemoryDataSource(kvp.Value), kvp.Key); - } - z.CommitUpdate(); - z.Close(); - } - } -} diff --git a/OpenRA.FileFormats/Filesystem/ZipFile.cs b/OpenRA.FileFormats/Filesystem/ZipFile.cs index 48699015e7..f4d68a9a08 100644 --- a/OpenRA.FileFormats/Filesystem/ZipFile.cs +++ b/OpenRA.FileFormats/Filesystem/ZipFile.cs @@ -48,5 +48,31 @@ namespace OpenRA.FileFormats { get { return 500 + priority; } } + + public void Write(Dictionary contents) + { + pkg.BeginUpdate(); + // TODO: Clear existing content? + + foreach (var kvp in contents) + { + pkg.Add(new StaticMemoryDataSource(kvp.Value), kvp.Key); + } + pkg.CommitUpdate(); + } + } + + class StaticMemoryDataSource : IStaticDataSource + { + byte[] data; + public StaticMemoryDataSource (byte[] data) + { + this.data = data; + } + + public Stream GetSource() + { + return new MemoryStream(data); + } } } diff --git a/OpenRA.FileFormats/OpenRA.FileFormats.csproj b/OpenRA.FileFormats/OpenRA.FileFormats.csproj index 1f0ffe1a2f..e66101bab5 100644 --- a/OpenRA.FileFormats/OpenRA.FileFormats.csproj +++ b/OpenRA.FileFormats/OpenRA.FileFormats.csproj @@ -105,7 +105,6 @@ - diff --git a/OpenRA.Game/Map.cs b/OpenRA.Game/Map.cs index 650a7c6911..cd22daecd4 100644 --- a/OpenRA.Game/Map.cs +++ b/OpenRA.Game/Map.cs @@ -226,8 +226,7 @@ namespace OpenRA entries.Add("map.bin", SaveBinaryData()); var s = root.WriteToString(); entries.Add("map.yaml", System.Text.Encoding.UTF8.GetBytes(s)); - PackageWriter.CreateZip(filepath, entries); - Container = new ZipFile(filepath, 0); + Container.Write(entries); } static byte ReadByte(Stream s) diff --git a/OpenRA.TilesetBuilder/Form1.cs b/OpenRA.TilesetBuilder/Form1.cs index 051b1797c8..90f4b48caf 100644 --- a/OpenRA.TilesetBuilder/Form1.cs +++ b/OpenRA.TilesetBuilder/Form1.cs @@ -175,7 +175,8 @@ namespace OpenRA.TilesetBuilder } tileset.Save(Path.Combine(dir, tilesetFile)); - PackageWriter.CreateMix(Path.Combine(dir, mixFile),fileList); + throw new NotImplementedException("NotI"); + //PackageWriter.CreateMix(Path.Combine(dir, mixFile),fileList); // Cleanup foreach (var file in fileList)