Write maps to zip file from editor

This commit is contained in:
Matthew Bowra-Dean
2010-12-28 22:00:39 +13:00
committed by Matthew
parent 44e668e804
commit 13f6a13ad9
4 changed files with 50 additions and 19 deletions

View File

@@ -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);
}

View File

@@ -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<string, byte[]> 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();
}
}
}

View File

@@ -225,7 +225,7 @@ namespace OpenRA.FileFormats
{
return string.Join("\n", y.ToLines(true).Select(x => x.TrimEnd()).ToArray());
}
public static IEnumerable<string> ToLines(this MiniYamlNodes y, bool lowest)
{
foreach (var kv in y)

View File

@@ -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<MiniYamlNode>();
@@ -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<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));
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)