Wire up the rest of saving. Save-as will now properly overwrite existing contents.
This commit is contained in:
@@ -41,6 +41,20 @@ namespace OpenRA.FileFormats
|
||||
return OpenPackage(filename, order++);
|
||||
}
|
||||
|
||||
public static IFolder CreatePackage(string filename, int order, Dictionary<string, byte[]> content)
|
||||
{
|
||||
if (filename.EndsWith(".mix", StringComparison.InvariantCultureIgnoreCase))
|
||||
return new MixFile(filename, order, content);
|
||||
else if (filename.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase))
|
||||
return new ZipFile(filename, order, content);
|
||||
else if (filename.EndsWith(".oramap", StringComparison.InvariantCultureIgnoreCase))
|
||||
return new ZipFile(filename, order, content);
|
||||
else if (filename.EndsWith(".Z", StringComparison.InvariantCultureIgnoreCase))
|
||||
throw new NotImplementedException("Creating .Z archives is unsupported");
|
||||
else
|
||||
return new Folder(filename, order, content);
|
||||
}
|
||||
|
||||
public static IFolder OpenPackage(string filename, int order)
|
||||
{
|
||||
if (filename.EndsWith(".mix", StringComparison.InvariantCultureIgnoreCase))
|
||||
|
||||
@@ -20,7 +20,22 @@ namespace OpenRA.FileFormats
|
||||
|
||||
int priority;
|
||||
|
||||
public Folder(string path, int priority) { this.path = path; this.priority = priority; }
|
||||
// Create a new folder package
|
||||
public Folder(string path, int priority, Dictionary<string, byte[]> contents)
|
||||
{
|
||||
this.path = path;
|
||||
this.priority = priority;
|
||||
if (Directory.Exists(path))
|
||||
Directory.Delete(path);
|
||||
|
||||
Write(contents);
|
||||
}
|
||||
|
||||
public Folder(string path, int priority)
|
||||
{
|
||||
this.path = path;
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public Stream GetContent(string filename)
|
||||
{
|
||||
|
||||
@@ -32,6 +32,17 @@ namespace OpenRA.FileFormats
|
||||
readonly Stream s;
|
||||
int priority;
|
||||
|
||||
// Create a new MixFile
|
||||
public MixFile(string filename, int priority, Dictionary<string, byte[]> contents)
|
||||
{
|
||||
this.priority = priority;
|
||||
if (File.Exists(filename))
|
||||
File.Delete(filename);
|
||||
|
||||
s = File.Create(filename);
|
||||
Write(contents);
|
||||
}
|
||||
|
||||
public MixFile(string filename, int priority)
|
||||
{
|
||||
this.priority = priority;
|
||||
@@ -162,6 +173,14 @@ namespace OpenRA.FileFormats
|
||||
|
||||
public void Write(Dictionary<string, byte[]> contents)
|
||||
{
|
||||
// Cannot modify existing mixfile - rename existing file and
|
||||
// create a new one with original content plus modifications
|
||||
FileSystem.Unmount(this);
|
||||
|
||||
// TODO: Add existing data to the contents list
|
||||
if (index.Count > 0)
|
||||
throw new NotImplementedException("Updating mix files unfinished");
|
||||
|
||||
// Construct a list of entries for the file header
|
||||
uint dataSize = 0;
|
||||
var items = new List<PackageEntry>();
|
||||
@@ -173,6 +192,8 @@ namespace OpenRA.FileFormats
|
||||
dataSize += length;
|
||||
}
|
||||
|
||||
// Write the new file
|
||||
s.Seek(0,SeekOrigin.Begin);
|
||||
using (var writer = new BinaryWriter(s))
|
||||
{
|
||||
// Write file header
|
||||
|
||||
@@ -23,8 +23,6 @@ namespace OpenRA.FileFormats
|
||||
public ZipFile(string filename, int priority)
|
||||
{
|
||||
this.priority = priority;
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
try
|
||||
{
|
||||
pkg = new SZipFile(File.OpenRead(filename));
|
||||
@@ -34,8 +32,16 @@ namespace OpenRA.FileFormats
|
||||
Log.Write("debug", "Couldn't load zip file: {0}", e.Message);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
// Create a new zip with the specified contents
|
||||
public ZipFile(string filename, int priority, Dictionary<string, byte[]> contents)
|
||||
{
|
||||
this.priority = priority;
|
||||
if (File.Exists(filename))
|
||||
File.Delete(filename);
|
||||
|
||||
pkg = SZipFile.Create(filename);
|
||||
Write(contents);
|
||||
}
|
||||
|
||||
public Stream GetContent(string filename)
|
||||
|
||||
@@ -55,24 +55,23 @@ namespace OpenRA
|
||||
|
||||
public static Map FromTileset(string tileset)
|
||||
{
|
||||
Map map = new Map();
|
||||
map.MapSize = new int2(1, 1);
|
||||
map.Tileset = tileset;
|
||||
map.MapResources = new TileReference<byte, byte>[1, 1];
|
||||
|
||||
var tile = OpenRA.Rules.TileSets[map.Tileset].Templates.First();
|
||||
map.MapTiles = new TileReference<ushort, byte>[1, 1]
|
||||
var tile = OpenRA.Rules.TileSets[tileset].Templates.First();
|
||||
Map map = new Map()
|
||||
{
|
||||
Title = "Name your map here",
|
||||
Description = "Describe your map here",
|
||||
Author = "Your name here",
|
||||
MapSize = new int2(1, 1),
|
||||
PlayerCount = 0,
|
||||
Tileset = tileset,
|
||||
MapResources = new TileReference<byte, byte>[1, 1],
|
||||
MapTiles = new TileReference<ushort, byte>[1, 1]
|
||||
{ { new TileReference<ushort, byte> {
|
||||
type = tile.Key,
|
||||
image = (byte)(tile.Value.PickAny ? 0xffu : 0),
|
||||
index = (byte)(tile.Value.PickAny ? 0xffu : 0) } } };
|
||||
|
||||
map.PlayerCount = 0;
|
||||
map.ResizeCordon(0,0,0,0);
|
||||
|
||||
map.Title = "Name your map here";
|
||||
map.Description = "Describe your map here";
|
||||
map.Author = "Your name here";
|
||||
index = (byte)(tile.Value.PickAny ? 0xffu : 0) }
|
||||
} },
|
||||
};
|
||||
|
||||
return map;
|
||||
}
|
||||
@@ -196,16 +195,6 @@ namespace OpenRA
|
||||
|
||||
public void Save(string toPath)
|
||||
{
|
||||
// Saving the map to a new location
|
||||
if (toPath != Path)
|
||||
{
|
||||
// TODO: Copy all other files (resources, rules) in the map package
|
||||
Path = toPath;
|
||||
|
||||
// TODO: This doesn't work - need a FileSystem.CreatePackage()
|
||||
Container = FileSystem.OpenPackage(Path, int.MaxValue);
|
||||
}
|
||||
|
||||
// Todo: save to a zip file in the support dir by default
|
||||
MapFormat = 3;
|
||||
|
||||
@@ -238,6 +227,18 @@ namespace OpenRA
|
||||
entries.Add("map.bin", SaveBinaryData());
|
||||
var s = root.WriteToString();
|
||||
entries.Add("map.yaml", System.Text.Encoding.UTF8.GetBytes(s));
|
||||
|
||||
// Saving the map to a new location
|
||||
if (toPath != Path)
|
||||
{
|
||||
Path = toPath;
|
||||
|
||||
// Create a new map package
|
||||
// TODO: Add other files (resources, rules) to the entries list
|
||||
Container = FileSystem.CreatePackage(Path, int.MaxValue, entries);
|
||||
}
|
||||
|
||||
// Update existing package
|
||||
Container.Write(entries);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user