Move ShpTS sprite loading into Mods.Common.

This commit is contained in:
Paul Chote
2014-10-05 15:34:56 +13:00
parent d658643b73
commit c798b306c4
7 changed files with 82 additions and 69 deletions

View File

@@ -15,7 +15,7 @@ using OpenRA.FileFormats;
namespace OpenRA.Graphics namespace OpenRA.Graphics
{ {
// TODO: Most of this should be moved into the format parsers themselves. // TODO: Most of this should be moved into the format parsers themselves.
public enum SpriteType { Unknown, ShpTS, ShpD2, TmpTD, TmpRA, TmpTS, R8 } public enum SpriteType { Unknown, ShpD2, TmpTD, TmpRA, TmpTS, R8 }
public static class SpriteSource public static class SpriteSource
{ {
static bool IsTmpRA(Stream s) static bool IsTmpRA(Stream s)
@@ -66,43 +66,6 @@ namespace OpenRA.Graphics
return test == sx * sy / 2 + 52; return test == sx * sy / 2 + 52;
} }
static bool IsShpTS(Stream s)
{
var start = s.Position;
// First word is zero
if (s.ReadUInt16() != 0)
{
s.Position = start;
return false;
}
// Sanity Check the image count
s.Position += 4;
var imageCount = s.ReadUInt16();
if (s.Position + 24 * imageCount > s.Length)
{
s.Position = start;
return false;
}
// Check the size and format flag
// Some files define bogus frames, so loop until we find a valid one
s.Position += 4;
ushort w, h, f = 0;
byte type;
do
{
w = s.ReadUInt16();
h = s.ReadUInt16();
type = s.ReadUInt8();
}
while (w == 0 && h == 0 && f++ < imageCount);
s.Position = start;
return type < 4;
}
static bool IsShpD2(Stream s) static bool IsShpD2(Stream s)
{ {
var start = s.Position; var start = s.Position;
@@ -162,9 +125,6 @@ namespace OpenRA.Graphics
public static SpriteType DetectSpriteType(Stream s) public static SpriteType DetectSpriteType(Stream s)
{ {
if (IsShpTS(s))
return SpriteType.ShpTS;
if (IsR8(s)) if (IsR8(s))
return SpriteType.R8; return SpriteType.R8;
@@ -188,8 +148,6 @@ namespace OpenRA.Graphics
var type = DetectSpriteType(s); var type = DetectSpriteType(s);
switch (type) switch (type)
{ {
case SpriteType.ShpTS:
return new ShpTSReader(s);
case SpriteType.R8: case SpriteType.R8:
return new R8Reader(s); return new R8Reader(s);
case SpriteType.TmpRA: case SpriteType.TmpRA:

View File

@@ -279,7 +279,6 @@
<Compile Include="FileFormats\PngLoader.cs" /> <Compile Include="FileFormats\PngLoader.cs" />
<Compile Include="FileFormats\R8Reader.cs" /> <Compile Include="FileFormats\R8Reader.cs" />
<Compile Include="FileFormats\ShpD2Reader.cs" /> <Compile Include="FileFormats\ShpD2Reader.cs" />
<Compile Include="FileFormats\ShpTSReader.cs" />
<Compile Include="FileFormats\TmpRAReader.cs" /> <Compile Include="FileFormats\TmpRAReader.cs" />
<Compile Include="FileFormats\TmpTDReader.cs" /> <Compile Include="FileFormats\TmpTDReader.cs" />
<Compile Include="FileFormats\TmpTSReader.cs" /> <Compile Include="FileFormats\TmpTSReader.cs" />

View File

@@ -114,6 +114,7 @@
<Compile Include="World\ResourceClaimLayer.cs" /> <Compile Include="World\ResourceClaimLayer.cs" />
<Compile Include="World\SmudgeLayer.cs" /> <Compile Include="World\SmudgeLayer.cs" />
<Compile Include="SpriteLoaders\ShpTDLoader.cs" /> <Compile Include="SpriteLoaders\ShpTDLoader.cs" />
<Compile Include="SpriteLoaders\ShpTSLoader.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup> <PropertyGroup>

View File

@@ -8,15 +8,19 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Graphics; using OpenRA.Graphics;
namespace OpenRA.FileFormats namespace OpenRA.Mods.Common.SpriteLoaders
{ {
public class ShpTSReader : ISpriteSource public class ShpTSLoader : ISpriteLoader
{ {
class FrameHeader : ISpriteFrame class ShpTSFrame : ISpriteFrame
{ {
public Size Size { get; private set; } public Size Size { get; private set; }
public Size FrameSize { get; private set; } public Size FrameSize { get; private set; }
@@ -27,7 +31,7 @@ namespace OpenRA.FileFormats
public readonly uint FileOffset; public readonly uint FileOffset;
public readonly byte Format; public readonly byte Format;
public FrameHeader(Stream stream, Size frameSize) public ShpTSFrame(Stream stream, Size frameSize)
{ {
var x = stream.ReadUInt16(); var x = stream.ReadUInt16();
var y = stream.ReadUInt16(); var y = stream.ReadUInt16();
@@ -45,20 +49,56 @@ namespace OpenRA.FileFormats
} }
} }
public IReadOnlyList<ISpriteFrame> Frames { get; private set; } bool IsShpTS(Stream s)
public ShpTSReader(Stream stream)
{ {
stream.ReadUInt16(); var start = s.Position;
var width = stream.ReadUInt16();
var height = stream.ReadUInt16();
var size = new Size(width, height);
var frameCount = stream.ReadUInt16();
var frames = new FrameHeader[frameCount]; // First word is zero
Frames = frames.AsReadOnly(); if (s.ReadUInt16() != 0)
{
s.Position = start;
return false;
}
// Sanity Check the image count
s.Position += 4;
var imageCount = s.ReadUInt16();
if (s.Position + 24 * imageCount > s.Length)
{
s.Position = start;
return false;
}
// Check the size and format flag
// Some files define bogus frames, so loop until we find a valid one
s.Position += 4;
ushort w, h, f = 0;
byte type;
do
{
w = s.ReadUInt16();
h = s.ReadUInt16();
type = s.ReadUInt8();
}
while (w == 0 && h == 0 && f++ < imageCount);
s.Position = start;
return type < 4;
}
ShpTSFrame[] ParseFrames(Stream s)
{
var start = s.Position;
s.ReadUInt16();
var width = s.ReadUInt16();
var height = s.ReadUInt16();
var size = new Size(width, height);
var frameCount = s.ReadUInt16();
var frames = new ShpTSFrame[frameCount];
for (var i = 0; i < frames.Length; i++) for (var i = 0; i < frames.Length; i++)
frames[i] = new FrameHeader(stream, size); frames[i] = new ShpTSFrame(s, size);
for (var i = 0; i < frameCount; i++) for (var i = 0; i < frameCount; i++)
{ {
@@ -66,13 +106,13 @@ namespace OpenRA.FileFormats
if (f.FileOffset == 0) if (f.FileOffset == 0)
continue; continue;
stream.Position = f.FileOffset; s.Position = f.FileOffset;
var frameSize = f.Size.Width * f.Size.Height; var frameSize = f.Size.Width * f.Size.Height;
// Uncompressed // Uncompressed
if (f.Format == 1 || f.Format == 0) if (f.Format == 1 || f.Format == 0)
f.Data = stream.ReadBytes(frameSize); f.Data = s.ReadBytes(frameSize);
// Uncompressed scanlines // Uncompressed scanlines
else if (f.Format == 2) else if (f.Format == 2)
@@ -80,9 +120,9 @@ namespace OpenRA.FileFormats
f.Data = new byte[frameSize]; f.Data = new byte[frameSize];
for (var j = 0; j < f.Size.Height; j++) for (var j = 0; j < f.Size.Height; j++)
{ {
var length = stream.ReadUInt16() - 2; var length = s.ReadUInt16() - 2;
var offset = f.Size.Width * j; var offset = f.Size.Width * j;
stream.ReadBytes(f.Data, offset, length); s.ReadBytes(f.Data, offset, length);
} }
} }
@@ -92,12 +132,27 @@ namespace OpenRA.FileFormats
f.Data = new byte[frameSize]; f.Data = new byte[frameSize];
for (var j = 0; j < f.Size.Height; j++) for (var j = 0; j < f.Size.Height; j++)
{ {
var length = stream.ReadUInt16() - 2; var length = s.ReadUInt16() - 2;
var offset = f.Size.Width * j; var offset = f.Size.Width * j;
Format2.DecodeInto(stream.ReadBytes(length), f.Data, offset); Format2.DecodeInto(s.ReadBytes(length), f.Data, offset);
} }
} }
} }
s.Position = start;
return frames;
}
public bool TryParseSprite(Stream s, out ISpriteFrame[] frames)
{
if (!IsShpTS(s))
{
frames = null;
return false;
}
frames = ParseFrames(s);
return true;
} }
} }
} }

View File

@@ -211,4 +211,4 @@ Missions:
SupportsMapsFrom: cnc SupportsMapsFrom: cnc
SpriteFormats: ShpTD SpriteFormats: ShpTD, ShpTS

View File

@@ -208,4 +208,4 @@ Missions:
SupportsMapsFrom: ra SupportsMapsFrom: ra
SpriteFormats: ShpTD SpriteFormats: ShpTD, ShpTS

View File

@@ -230,4 +230,4 @@ LuaScripts:
SupportsMapsFrom: ts SupportsMapsFrom: ts
SpriteFormats: ShpTD SpriteFormats: ShpTD, ShpTS