Write maps to zip file from editor
This commit is contained in:
committed by
Matthew
parent
44e668e804
commit
13f6a13ad9
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>();
|
||||
@@ -223,8 +222,12 @@ namespace OpenRA
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user