Make map saving independent of Container type. Saving zip/oramap/mix untested as the editor cannot load non-folder maps.

This commit is contained in:
Paul Chote
2010-12-29 11:39:26 +13:00
parent fa36c71023
commit c3ff679f3a
8 changed files with 73 additions and 81 deletions

View File

@@ -10,6 +10,7 @@
using System.Collections.Generic;
using System.IO;
using System;
namespace OpenRA.FileFormats
{
@@ -43,5 +44,13 @@ namespace OpenRA.FileFormats
{
get { return priority; }
}
public void Write(Dictionary<string, byte[]> contents)
{
foreach (var file in contents)
using (var dataStream = File.Create(Path.Combine(path, file.Key)))
using (var writer = new BinaryWriter(dataStream))
writer.Write(file.Value);
}
}
}

View File

@@ -117,5 +117,10 @@ namespace OpenRA.FileFormats
{
get { return 2000 + priority; }
}
public void Write(Dictionary<string, byte[]> contents)
{
throw new NotImplementedException("Cannot save InstallShieldPackages.");
}
}
}

View File

@@ -20,6 +20,7 @@ namespace OpenRA.FileFormats
Stream GetContent(string filename);
bool Exists(string filename);
IEnumerable<uint> AllFileHashes();
void Write(Dictionary<string, byte[]> contents);
int Priority { get; }
}
@@ -158,6 +159,35 @@ namespace OpenRA.FileFormats
{
get { return 1000 + priority; }
}
public void Write(Dictionary<string, byte[]> contents)
{
// Construct a list of entries for the file header
uint dataSize = 0;
var items = new List<PackageEntry>();
foreach (var kv in contents)
{
uint length = (uint)kv.Value.Length;
uint hash = PackageEntry.HashFilename(Path.GetFileName(kv.Key));
items.Add(new PackageEntry(hash, dataSize, length));
dataSize += length;
}
using (var writer = new BinaryWriter(s))
{
// Write file header
writer.Write((ushort)items.Count);
writer.Write(dataSize);
foreach (var item in items)
item.Write(writer);
writer.Flush();
// Copy file data
foreach (var file in contents)
s.Write(file.Value);
}
}
}
[Flags]

View File

@@ -1,77 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see LICENSE.
*/
#endregion
using System.Collections.Generic;
using System.IO;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using SZipFile = ICSharpCode.SharpZipLib.Zip.ZipFile;
namespace OpenRA.FileFormats
{
public static class PackageWriter
{
public static void CreateMix(string filename, List<string> contents)
{
// Construct a list of entries for the file header
uint dataSize = 0;
var items = new List<PackageEntry>();
foreach (var file in contents)
{
uint length = (uint)new FileInfo(file).Length;
uint hash = PackageEntry.HashFilename(Path.GetFileName(file));
items.Add(new PackageEntry(hash, dataSize, length));
dataSize += length;
}
using (var s = File.Create(filename))
using (var writer = new BinaryWriter(s))
{
// Write file header
writer.Write((ushort)items.Count);
writer.Write(dataSize);
foreach (var item in items)
item.Write(writer);
writer.Flush();
// Copy file data
foreach (var file in contents)
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

@@ -48,5 +48,31 @@ namespace OpenRA.FileFormats
{
get { return 500 + priority; }
}
public void Write(Dictionary<string, byte[]> contents)
{
pkg.BeginUpdate();
// TODO: Clear existing content?
foreach (var kvp in contents)
{
pkg.Add(new StaticMemoryDataSource(kvp.Value), kvp.Key);
}
pkg.CommitUpdate();
}
}
class StaticMemoryDataSource : IStaticDataSource
{
byte[] data;
public StaticMemoryDataSource (byte[] data)
{
this.data = data;
}
public Stream GetSource()
{
return new MemoryStream(data);
}
}
}

View File

@@ -105,7 +105,6 @@
<Compile Include="Filesystem\MixFile.cs" />
<Compile Include="Filesystem\FileSystem.cs" />
<Compile Include="Filesystem\Folder.cs" />
<Compile Include="Filesystem\PackageWriter.cs" />
<Compile Include="Filesystem\InstallShieldPackage.cs" />
<Compile Include="FileFormats\Blast.cs" />
<Compile Include="Filesystem\ZipFile.cs" />

View File

@@ -226,8 +226,7 @@ namespace OpenRA
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);
Container.Write(entries);
}
static byte ReadByte(Stream s)

View File

@@ -175,7 +175,8 @@ namespace OpenRA.TilesetBuilder
}
tileset.Save(Path.Combine(dir, tilesetFile));
PackageWriter.CreateMix(Path.Combine(dir, mixFile),fileList);
throw new NotImplementedException("NotI");
//PackageWriter.CreateMix(Path.Combine(dir, mixFile),fileList);
// Cleanup
foreach (var file in fileList)