diff --git a/OpenRA.FileFormats/Filesystem/D2kSoundResources.cs b/OpenRA.FileFormats/Filesystem/D2kSoundResources.cs new file mode 100644 index 0000000000..977cecc66f --- /dev/null +++ b/OpenRA.FileFormats/Filesystem/D2kSoundResources.cs @@ -0,0 +1,109 @@ +#region Copyright & License Information +/* + * Copyright 2007-2013 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 COPYING. + */ +#endregion + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; + +namespace OpenRA.FileFormats +{ + public class D2kSoundResources : IFolder + { + readonly Stream s; + + readonly string filename; + readonly List filenames; + readonly int priority; + + readonly Dictionary index = new Dictionary(); + + public D2kSoundResources(string filename, int priority) + { + this.filename = filename; + this.priority = priority; + + s = FileSystem.Open(filename); + s.Seek(0, SeekOrigin.Begin); + + filenames = new List(); + + var headerLength = s.ReadUInt32(); + while (s.Position < headerLength + 4) + { + var name = ""; + var letter = s.ReadASCII(1); + while (letter != "\0") + { + name += letter; + letter = s.ReadASCII(1); + } + + var offset = s.ReadUInt32(); + var length = s.ReadUInt32(); + + var hash = PackageEntry.HashFilename(name, PackageHashType.Classic); + if (!index.ContainsKey(hash)) + index.Add(hash, new PackageEntry(hash, offset, length)); + + filenames.Add(name); + } + } + + public Stream GetContent(uint hash) + { + PackageEntry e; + if (!index.TryGetValue(hash, out e)) + return null; + + s.Seek(e.Offset, SeekOrigin.Begin); + var data = new byte[e.Length]; + s.Read(data, 0, (int)e.Length); + + return new MemoryStream(data); + } + + public Stream GetContent(string filename) + { + return GetContent(PackageEntry.HashFilename(filename, PackageHashType.Classic)); + } + + public bool Exists(string filename) + { + return index.ContainsKey(PackageEntry.HashFilename(filename, PackageHashType.Classic)); + } + + public IEnumerable AllFileNames() + { + return filenames; + } + + public string Name { get { return filename; } } + + public int Priority { get { return 1000 + priority; }} + + public IEnumerable ClassicHashes() + { + return index.Keys; + } + + public IEnumerable CrcHashes() + { + yield break; + } + + public void Write(Dictionary contents) + { + throw new NotImplementedException("Cannot save Dune 2000 Sound Resources."); + } + } +} diff --git a/OpenRA.FileFormats/Filesystem/FileSystem.cs b/OpenRA.FileFormats/Filesystem/FileSystem.cs index 26e6af6f92..886f308046 100644 --- a/OpenRA.FileFormats/Filesystem/FileSystem.cs +++ b/OpenRA.FileFormats/Filesystem/FileSystem.cs @@ -53,6 +53,8 @@ namespace OpenRA.FileFormats return new ZipFile(filename, order, content); else if (filename.EndsWith(".oramap", StringComparison.InvariantCultureIgnoreCase)) return new ZipFile(filename, order, content); + else if (filename.EndsWith(".RS", StringComparison.InvariantCultureIgnoreCase)) + throw new NotImplementedException("Creating .RS archives is unsupported"); else if (filename.EndsWith(".Z", StringComparison.InvariantCultureIgnoreCase)) throw new NotImplementedException("Creating .Z archives is unsupported"); else @@ -71,6 +73,8 @@ namespace OpenRA.FileFormats return new ZipFile(filename, order); else if (filename.EndsWith(".oramap", StringComparison.InvariantCultureIgnoreCase)) return new ZipFile(filename, order); + else if (filename.EndsWith(".RS", StringComparison.InvariantCultureIgnoreCase)) + return new D2kSoundResources(filename, order); else if (filename.EndsWith(".Z", StringComparison.InvariantCultureIgnoreCase)) return new InstallShieldPackage(filename, order); else diff --git a/OpenRA.FileFormats/Filesystem/InstallShieldPackage.cs b/OpenRA.FileFormats/Filesystem/InstallShieldPackage.cs index bff057b838..da156fbbfa 100644 --- a/OpenRA.FileFormats/Filesystem/InstallShieldPackage.cs +++ b/OpenRA.FileFormats/Filesystem/InstallShieldPackage.cs @@ -87,7 +87,7 @@ namespace OpenRA.FileFormats var FileName = new String(reader.ReadChars(NameLength)); var hash = PackageEntry.HashFilename(FileName, PackageHashType.Classic); - if(!index.ContainsKey(hash)) + if (!index.ContainsKey(hash)) index.Add(hash, new PackageEntry(hash,AccumulatedData, CompressedSize)); filenames.Add(FileName); AccumulatedData += CompressedSize; @@ -102,9 +102,9 @@ namespace OpenRA.FileFormats if (!index.TryGetValue(hash, out e)) return null; - s.Seek( dataStart + e.Offset, SeekOrigin.Begin ); - byte[] data = new byte[ e.Length ]; - s.Read( data, 0, (int)e.Length ); + s.Seek(dataStart + e.Offset, SeekOrigin.Begin); + var data = new byte[e.Length]; + s.Read(data, 0, (int)e.Length); return new MemoryStream(Blast.Decompress(data)); } diff --git a/OpenRA.FileFormats/OpenRA.FileFormats.csproj b/OpenRA.FileFormats/OpenRA.FileFormats.csproj index 89b9dcb1ff..3c846f7fe6 100644 --- a/OpenRA.FileFormats/OpenRA.FileFormats.csproj +++ b/OpenRA.FileFormats/OpenRA.FileFormats.csproj @@ -145,6 +145,7 @@ +