From 997c79130ac89b72d00b71b305f297ce0a62b62d Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 5 Oct 2014 15:49:34 +1300 Subject: [PATCH] Move TmpRA sprite loading into Mods.Common. --- OpenRA.Game/FileFormats/TmpRAReader.cs | 51 --------- OpenRA.Game/Graphics/SpriteSource.cs | 20 +--- OpenRA.Game/OpenRA.Game.csproj | 1 - OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 1 + .../SpriteLoaders/TmpRALoader.cs | 102 ++++++++++++++++++ mods/cnc/mod.yaml | 2 +- mods/d2k/mod.yaml | 2 +- mods/ra/mod.yaml | 2 +- 8 files changed, 107 insertions(+), 74 deletions(-) delete mode 100644 OpenRA.Game/FileFormats/TmpRAReader.cs create mode 100644 OpenRA.Mods.Common/SpriteLoaders/TmpRALoader.cs diff --git a/OpenRA.Game/FileFormats/TmpRAReader.cs b/OpenRA.Game/FileFormats/TmpRAReader.cs deleted file mode 100644 index cfd761e84a..0000000000 --- a/OpenRA.Game/FileFormats/TmpRAReader.cs +++ /dev/null @@ -1,51 +0,0 @@ -#region Copyright & License Information -/* - * Copyright 2007-2014 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.Drawing; -using System.IO; -using OpenRA.Graphics; - -namespace OpenRA.FileFormats -{ - public class TmpRAReader : ISpriteSource - { - public IReadOnlyList Frames { get; private set; } - - public TmpRAReader(Stream s) - { - var width = s.ReadUInt16(); - var height = s.ReadUInt16(); - var size = new Size(width, height); - - s.Position += 12; - var imgStart = s.ReadUInt32(); - s.Position += 8; - var indexEnd = s.ReadInt32(); - s.Position += 4; - var indexStart = s.ReadInt32(); - - s.Position = indexStart; - var count = indexEnd - indexStart; - var tiles = new TmpTile[count]; - Frames = tiles.AsReadOnly(); - var tilesIndex = 0; - foreach (var b in s.ReadBytes(count)) - { - if (b != 255) - { - s.Position = imgStart + b * width * height; - tiles[tilesIndex++] = new TmpTile(s.ReadBytes(width * height), size); - } - else - tiles[tilesIndex++] = new TmpTile(null, size); - } - } - } -} diff --git a/OpenRA.Game/Graphics/SpriteSource.cs b/OpenRA.Game/Graphics/SpriteSource.cs index bf81824d60..8ca32dd6ce 100644 --- a/OpenRA.Game/Graphics/SpriteSource.cs +++ b/OpenRA.Game/Graphics/SpriteSource.cs @@ -15,22 +15,9 @@ using OpenRA.FileFormats; namespace OpenRA.Graphics { // TODO: Most of this should be moved into the format parsers themselves. - public enum SpriteType { Unknown, ShpD2, TmpTD, TmpRA, TmpTS } + public enum SpriteType { Unknown, ShpD2, TmpTD, TmpTS } public static class SpriteSource { - static bool IsTmpRA(Stream s) - { - var start = s.Position; - - s.Position += 20; - var a = s.ReadUInt32(); - s.Position += 2; - var b = s.ReadUInt16(); - - s.Position = start; - return a == 0 && b == 0x2c73; - } - static bool IsTmpTD(Stream s) { var start = s.Position; @@ -106,9 +93,6 @@ namespace OpenRA.Graphics public static SpriteType DetectSpriteType(Stream s) { - if (IsTmpRA(s)) - return SpriteType.TmpRA; - if (IsTmpTD(s)) return SpriteType.TmpTD; @@ -126,8 +110,6 @@ namespace OpenRA.Graphics var type = DetectSpriteType(s); switch (type) { - case SpriteType.TmpRA: - return new TmpRAReader(s); case SpriteType.TmpTD: return new TmpTDReader(s); case SpriteType.TmpTS: diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index 11f17a7361..9b63f7b96a 100644 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -278,7 +278,6 @@ - diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index 98520994d1..d27764c409 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -115,6 +115,7 @@ + diff --git a/OpenRA.Mods.Common/SpriteLoaders/TmpRALoader.cs b/OpenRA.Mods.Common/SpriteLoaders/TmpRALoader.cs new file mode 100644 index 0000000000..900c3fdd0c --- /dev/null +++ b/OpenRA.Mods.Common/SpriteLoaders/TmpRALoader.cs @@ -0,0 +1,102 @@ +#region Copyright & License Information +/* + * Copyright 2007-2014 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.Generic; +using System.Drawing; +using System.IO; +using System.Linq; +using OpenRA.FileFormats; +using OpenRA.Graphics; + +namespace OpenRA.Mods.Common.SpriteLoaders +{ + public class TmpRALoader : ISpriteLoader + { + class TmpRAFrame : ISpriteFrame + { + public Size Size { get; private set; } + public Size FrameSize { get; private set; } + public float2 Offset { get { return float2.Zero; } } + public byte[] Data { get; set; } + public bool DisableExportPadding { get { return false; } } + + public TmpRAFrame(byte[] data, Size size) + { + FrameSize = size; + Data = data; + + if (data == null) + Data = new byte[0]; + else + Size = size; + } + } + + bool IsTmpRA(Stream s) + { + var start = s.Position; + + s.Position += 20; + var a = s.ReadUInt32(); + s.Position += 2; + var b = s.ReadUInt16(); + + s.Position = start; + return a == 0 && b == 0x2c73; + } + + TmpRAFrame[] ParseFrames(Stream s) + { + var start = s.Position; + var width = s.ReadUInt16(); + var height = s.ReadUInt16(); + var size = new Size(width, height); + + s.Position += 12; + var imgStart = s.ReadUInt32(); + s.Position += 8; + var indexEnd = s.ReadInt32(); + s.Position += 4; + var indexStart = s.ReadInt32(); + + s.Position = indexStart; + var count = indexEnd - indexStart; + var tiles = new TmpRAFrame[count]; + + var tilesIndex = 0; + foreach (var b in s.ReadBytes(count)) + { + if (b != 255) + { + s.Position = imgStart + b * width * height; + tiles[tilesIndex++] = new TmpRAFrame(s.ReadBytes(width * height), size); + } + else + tiles[tilesIndex++] = new TmpRAFrame(null, size); + } + + s.Position = start; + return tiles; + } + + public bool TryParseSprite(Stream s, out ISpriteFrame[] frames) + { + if (!IsTmpRA(s)) + { + frames = null; + return false; + } + + frames = ParseFrames(s); + return true; + } + } +} diff --git a/mods/cnc/mod.yaml b/mods/cnc/mod.yaml index 2bffd79982..676a01bd99 100644 --- a/mods/cnc/mod.yaml +++ b/mods/cnc/mod.yaml @@ -211,4 +211,4 @@ Missions: SupportsMapsFrom: cnc -SpriteFormats: ShpTD, ShpTS \ No newline at end of file +SpriteFormats: ShpTD, ShpTS, TmpRA \ No newline at end of file diff --git a/mods/d2k/mod.yaml b/mods/d2k/mod.yaml index 7cb31d3585..5f27b81cfd 100644 --- a/mods/d2k/mod.yaml +++ b/mods/d2k/mod.yaml @@ -189,4 +189,4 @@ LuaScripts: SupportsMapsFrom: d2k -SpriteFormats: R8, ShpTD \ No newline at end of file +SpriteFormats: R8, ShpTD, TmpRA \ No newline at end of file diff --git a/mods/ra/mod.yaml b/mods/ra/mod.yaml index 24882a5e1c..51875f95e3 100644 --- a/mods/ra/mod.yaml +++ b/mods/ra/mod.yaml @@ -208,4 +208,4 @@ Missions: SupportsMapsFrom: ra -SpriteFormats: ShpTD, ShpTS \ No newline at end of file +SpriteFormats: ShpTD, TmpRA, ShpTS \ No newline at end of file