diff --git a/OpenRA.Editor/Form1.cs b/OpenRA.Editor/Form1.cs index a1064f377e..0a1dbff02c 100755 --- a/OpenRA.Editor/Form1.cs +++ b/OpenRA.Editor/Form1.cs @@ -267,17 +267,17 @@ namespace OpenRA.Editor if (nms.txtNew.Text == "") nms.txtNew.Text = "unnamed"; - string mapfoldername = Path.Combine(nms.MapFolderPath, nms.txtNew.Text); - loadedMapName = mapfoldername; + string mapZipName = Path.Combine(nms.MapFolderPath, nms.txtNew.Text + ".oramap"); + loadedMapName = mapZipName; - try - { - Directory.CreateDirectory(mapfoldername); - } - catch (Exception ed) - { - MessageBox.Show("Directory creation failed: {0}", ed.ToString()); - } +// try +// { +// Directory.CreateDirectory(mapZipName); +// } +// catch (Exception ed) +// { +// MessageBox.Show("Directory creation failed: {0}", ed.ToString()); +// } SaveClicked(sender, e); } diff --git a/OpenRA.FileFormats/Filesystem/PackageWriter.cs b/OpenRA.FileFormats/Filesystem/PackageWriter.cs index 569453a454..32aab0c3f1 100644 --- a/OpenRA.FileFormats/Filesystem/PackageWriter.cs +++ b/OpenRA.FileFormats/Filesystem/PackageWriter.cs @@ -10,6 +10,9 @@ using System.Collections.Generic; using System.IO; +using ICSharpCode.SharpZipLib; +using ICSharpCode.SharpZipLib.Zip; +using SZipFile = ICSharpCode.SharpZipLib.Zip.ZipFile; namespace OpenRA.FileFormats { @@ -44,5 +47,31 @@ namespace OpenRA.FileFormats 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/MiniYaml.cs b/OpenRA.FileFormats/MiniYaml.cs index 68b58f7b4f..3533108dc4 100755 --- a/OpenRA.FileFormats/MiniYaml.cs +++ b/OpenRA.FileFormats/MiniYaml.cs @@ -225,7 +225,7 @@ namespace OpenRA.FileFormats { return string.Join("\n", y.ToLines(true).Select(x => x.TrimEnd()).ToArray()); } - + public static IEnumerable ToLines(this MiniYamlNodes y, bool lowest) { foreach (var kv in y) diff --git a/OpenRA.Game/Map.cs b/OpenRA.Game/Map.cs index 8ea8330004..650a7c6911 100644 --- a/OpenRA.Game/Map.cs +++ b/OpenRA.Game/Map.cs @@ -195,7 +195,6 @@ namespace OpenRA public void Save(string filepath) { // Todo: save to a zip file in the support dir by default - Container = new Folder(filepath, 0); MapFormat = 3; var root = new List(); @@ -222,9 +221,13 @@ namespace OpenRA root.Add(new MiniYamlNode("Sequences", null, Sequences)); root.Add(new MiniYamlNode("Weapons", null, Weapons)); root.Add(new MiniYamlNode("Voices", null, Voices)); - - SaveBinaryData(Path.Combine(filepath, "map.bin")); - root.WriteToFile(Path.Combine(filepath, "map.yaml")); + + Dictionary entries = new Dictionary(); + 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); } static byte ReadByte(Stream s) @@ -277,9 +280,9 @@ namespace OpenRA } } - public void SaveBinaryData(string filepath) + public byte[] SaveBinaryData() { - using (var dataStream = File.Create(filepath + ".tmp")) + MemoryStream dataStream = new MemoryStream(); using (var writer = new BinaryWriter(dataStream)) { // File header consists of a version byte, followed by 2 ushorts for width and height @@ -303,8 +306,7 @@ namespace OpenRA writer.Write(MapResources[i, j].index); } } - File.Delete(filepath); - File.Move(filepath + ".tmp", filepath); + return dataStream.ToArray(); } public bool IsInMap(int2 xy)