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 == "")
|
if (nms.txtNew.Text == "")
|
||||||
nms.txtNew.Text = "unnamed";
|
nms.txtNew.Text = "unnamed";
|
||||||
|
|
||||||
string mapfoldername = Path.Combine(nms.MapFolderPath, nms.txtNew.Text);
|
string mapZipName = Path.Combine(nms.MapFolderPath, nms.txtNew.Text + ".oramap");
|
||||||
loadedMapName = mapfoldername;
|
loadedMapName = mapZipName;
|
||||||
|
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
Directory.CreateDirectory(mapfoldername);
|
// Directory.CreateDirectory(mapZipName);
|
||||||
}
|
// }
|
||||||
catch (Exception ed)
|
// catch (Exception ed)
|
||||||
{
|
// {
|
||||||
MessageBox.Show("Directory creation failed: {0}", ed.ToString());
|
// MessageBox.Show("Directory creation failed: {0}", ed.ToString());
|
||||||
}
|
// }
|
||||||
|
|
||||||
SaveClicked(sender, e);
|
SaveClicked(sender, e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,9 @@
|
|||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using ICSharpCode.SharpZipLib;
|
||||||
|
using ICSharpCode.SharpZipLib.Zip;
|
||||||
|
using SZipFile = ICSharpCode.SharpZipLib.Zip.ZipFile;
|
||||||
|
|
||||||
namespace OpenRA.FileFormats
|
namespace OpenRA.FileFormats
|
||||||
{
|
{
|
||||||
@@ -44,5 +47,31 @@ namespace OpenRA.FileFormats
|
|||||||
s.Write(File.ReadAllBytes(file));
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -225,7 +225,7 @@ namespace OpenRA.FileFormats
|
|||||||
{
|
{
|
||||||
return string.Join("\n", y.ToLines(true).Select(x => x.TrimEnd()).ToArray());
|
return string.Join("\n", y.ToLines(true).Select(x => x.TrimEnd()).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<string> ToLines(this MiniYamlNodes y, bool lowest)
|
public static IEnumerable<string> ToLines(this MiniYamlNodes y, bool lowest)
|
||||||
{
|
{
|
||||||
foreach (var kv in y)
|
foreach (var kv in y)
|
||||||
|
|||||||
@@ -195,7 +195,6 @@ namespace OpenRA
|
|||||||
public void Save(string filepath)
|
public void Save(string filepath)
|
||||||
{
|
{
|
||||||
// Todo: save to a zip file in the support dir by default
|
// Todo: save to a zip file in the support dir by default
|
||||||
Container = new Folder(filepath, 0);
|
|
||||||
MapFormat = 3;
|
MapFormat = 3;
|
||||||
|
|
||||||
var root = new List<MiniYamlNode>();
|
var root = new List<MiniYamlNode>();
|
||||||
@@ -222,9 +221,13 @@ namespace OpenRA
|
|||||||
root.Add(new MiniYamlNode("Sequences", null, Sequences));
|
root.Add(new MiniYamlNode("Sequences", null, Sequences));
|
||||||
root.Add(new MiniYamlNode("Weapons", null, Weapons));
|
root.Add(new MiniYamlNode("Weapons", null, Weapons));
|
||||||
root.Add(new MiniYamlNode("Voices", null, Voices));
|
root.Add(new MiniYamlNode("Voices", null, Voices));
|
||||||
|
|
||||||
SaveBinaryData(Path.Combine(filepath, "map.bin"));
|
Dictionary<string, byte[]> entries = new Dictionary<string, byte[]>();
|
||||||
root.WriteToFile(Path.Combine(filepath, "map.yaml"));
|
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)
|
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))
|
using (var writer = new BinaryWriter(dataStream))
|
||||||
{
|
{
|
||||||
// File header consists of a version byte, followed by 2 ushorts for width and height
|
// 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);
|
writer.Write(MapResources[i, j].index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
File.Delete(filepath);
|
return dataStream.ToArray();
|
||||||
File.Move(filepath + ".tmp", filepath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsInMap(int2 xy)
|
public bool IsInMap(int2 xy)
|
||||||
|
|||||||
Reference in New Issue
Block a user