Move ShpTD sprite loading into Mods.Common.
This commit is contained in:
@@ -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, ShpTD, ShpTS, ShpD2, TmpTD, TmpRA, TmpTS, R8 }
|
public enum SpriteType { Unknown, ShpTS, ShpD2, TmpTD, TmpRA, TmpTS, R8 }
|
||||||
public static class SpriteSource
|
public static class SpriteSource
|
||||||
{
|
{
|
||||||
static bool IsTmpRA(Stream s)
|
static bool IsTmpRA(Stream s)
|
||||||
@@ -103,42 +103,6 @@ namespace OpenRA.Graphics
|
|||||||
return type < 4;
|
return type < 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool IsShpTD(Stream s)
|
|
||||||
{
|
|
||||||
var start = s.Position;
|
|
||||||
|
|
||||||
// First word is the image count
|
|
||||||
var imageCount = s.ReadUInt16();
|
|
||||||
if (imageCount == 0)
|
|
||||||
{
|
|
||||||
s.Position = start;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Last offset should point to the end of file
|
|
||||||
var finalOffset = start + 14 + 8 * imageCount;
|
|
||||||
if (finalOffset > s.Length)
|
|
||||||
{
|
|
||||||
s.Position = start;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
s.Position = finalOffset;
|
|
||||||
var eof = s.ReadUInt32();
|
|
||||||
if (eof != s.Length)
|
|
||||||
{
|
|
||||||
s.Position = start;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check the format flag on the first frame
|
|
||||||
s.Position = start + 17;
|
|
||||||
var b = s.ReadUInt8();
|
|
||||||
|
|
||||||
s.Position = start;
|
|
||||||
return b == 0x20 || b == 0x40 || b == 0x80;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool IsShpD2(Stream s)
|
static bool IsShpD2(Stream s)
|
||||||
{
|
{
|
||||||
var start = s.Position;
|
var start = s.Position;
|
||||||
@@ -198,9 +162,6 @@ namespace OpenRA.Graphics
|
|||||||
|
|
||||||
public static SpriteType DetectSpriteType(Stream s)
|
public static SpriteType DetectSpriteType(Stream s)
|
||||||
{
|
{
|
||||||
if (IsShpTD(s))
|
|
||||||
return SpriteType.ShpTD;
|
|
||||||
|
|
||||||
if (IsShpTS(s))
|
if (IsShpTS(s))
|
||||||
return SpriteType.ShpTS;
|
return SpriteType.ShpTS;
|
||||||
|
|
||||||
@@ -227,8 +188,6 @@ namespace OpenRA.Graphics
|
|||||||
var type = DetectSpriteType(s);
|
var type = DetectSpriteType(s);
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case SpriteType.ShpTD:
|
|
||||||
return new ShpReader(s);
|
|
||||||
case SpriteType.ShpTS:
|
case SpriteType.ShpTS:
|
||||||
return new ShpTSReader(s);
|
return new ShpTSReader(s);
|
||||||
case SpriteType.R8:
|
case SpriteType.R8:
|
||||||
|
|||||||
@@ -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\ShpReader.cs" />
|
|
||||||
<Compile Include="FileFormats\ShpTSReader.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" />
|
||||||
|
|||||||
@@ -113,6 +113,7 @@
|
|||||||
<Compile Include="World\ResourceClaim.cs" />
|
<Compile Include="World\ResourceClaim.cs" />
|
||||||
<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" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
|||||||
@@ -13,11 +13,63 @@ using System.Collections.Generic;
|
|||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using OpenRA.FileFormats;
|
||||||
using OpenRA.Graphics;
|
using OpenRA.Graphics;
|
||||||
|
|
||||||
namespace OpenRA.FileFormats
|
namespace OpenRA.Mods.Common.SpriteLoaders
|
||||||
{
|
{
|
||||||
public class ShpReader : ISpriteSource
|
public class ShpTDLoader : ISpriteLoader
|
||||||
|
{
|
||||||
|
static bool IsShpTD(Stream s)
|
||||||
|
{
|
||||||
|
var start = s.Position;
|
||||||
|
|
||||||
|
// First word is the image count
|
||||||
|
var imageCount = s.ReadUInt16();
|
||||||
|
if (imageCount == 0)
|
||||||
|
{
|
||||||
|
s.Position = start;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Last offset should point to the end of file
|
||||||
|
var finalOffset = start + 14 + 8 * imageCount;
|
||||||
|
if (finalOffset > s.Length)
|
||||||
|
{
|
||||||
|
s.Position = start;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Position = finalOffset;
|
||||||
|
var eof = s.ReadUInt32();
|
||||||
|
if (eof != s.Length)
|
||||||
|
{
|
||||||
|
s.Position = start;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the format flag on the first frame
|
||||||
|
s.Position = start + 17;
|
||||||
|
var b = s.ReadUInt8();
|
||||||
|
|
||||||
|
s.Position = start;
|
||||||
|
return b == 0x20 || b == 0x40 || b == 0x80;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryParseSprite(Stream s, out ISpriteFrame[] frames)
|
||||||
|
{
|
||||||
|
if (!IsShpTD(s))
|
||||||
|
{
|
||||||
|
frames = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
frames = new ShpTDSprite(s).Frames.ToArray();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ShpTDSprite
|
||||||
{
|
{
|
||||||
enum Format { Format20 = 0x20, Format40 = 0x40, Format80 = 0x80 }
|
enum Format { Format20 = 0x20, Format40 = 0x40, Format80 = 0x80 }
|
||||||
|
|
||||||
@@ -36,12 +88,12 @@ namespace OpenRA.FileFormats
|
|||||||
public Format RefFormat;
|
public Format RefFormat;
|
||||||
public ImageHeader RefImage;
|
public ImageHeader RefImage;
|
||||||
|
|
||||||
ShpReader reader;
|
ShpTDSprite reader;
|
||||||
|
|
||||||
// Used by ShpWriter
|
// Used by ShpWriter
|
||||||
public ImageHeader() { }
|
public ImageHeader() { }
|
||||||
|
|
||||||
public ImageHeader(Stream stream, ShpReader reader)
|
public ImageHeader(Stream stream, ShpTDSprite reader)
|
||||||
{
|
{
|
||||||
this.reader = reader;
|
this.reader = reader;
|
||||||
var data = stream.ReadUInt32();
|
var data = stream.ReadUInt32();
|
||||||
@@ -69,7 +121,7 @@ namespace OpenRA.FileFormats
|
|||||||
readonly long shpBytesFileOffset;
|
readonly long shpBytesFileOffset;
|
||||||
readonly byte[] shpBytes;
|
readonly byte[] shpBytes;
|
||||||
|
|
||||||
public ShpReader(Stream stream)
|
public ShpTDSprite(Stream stream)
|
||||||
{
|
{
|
||||||
imageCount = stream.ReadUInt16();
|
imageCount = stream.ReadUInt16();
|
||||||
stream.Position += 4;
|
stream.Position += 4;
|
||||||
@@ -149,12 +201,6 @@ namespace OpenRA.FileFormats
|
|||||||
return imageData;
|
return imageData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ShpReader Load(string filename)
|
|
||||||
{
|
|
||||||
using (var s = File.OpenRead(filename))
|
|
||||||
return new ShpReader(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Write(Stream s, Size size, IEnumerable<byte[]> frames)
|
public static void Write(Stream s, Size size, IEnumerable<byte[]> frames)
|
||||||
{
|
{
|
||||||
var compressedFrames = frames.Select(f => Format80.Encode(f)).ToArray();
|
var compressedFrames = frames.Select(f => Format80.Encode(f)).ToArray();
|
||||||
@@ -17,6 +17,7 @@ using System.Linq;
|
|||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using OpenRA.FileFormats;
|
using OpenRA.FileFormats;
|
||||||
|
using OpenRA.Mods.Common.SpriteLoaders;
|
||||||
|
|
||||||
namespace OpenRA.Mods.Common.UtilityCommands
|
namespace OpenRA.Mods.Common.UtilityCommands
|
||||||
{
|
{
|
||||||
@@ -36,7 +37,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
|
|||||||
throw new InvalidOperationException("All frames must be the same size");
|
throw new InvalidOperationException("All frames must be the same size");
|
||||||
|
|
||||||
using (var destStream = File.Create(dest))
|
using (var destStream = File.Create(dest))
|
||||||
ShpReader.Write(destStream, size, frames.Select(f => ToBytes(f)));
|
ShpTDSprite.Write(destStream, size, frames.Select(f => ToBytes(f)));
|
||||||
|
|
||||||
Console.WriteLine(dest + " saved.");
|
Console.WriteLine(dest + " saved.");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ using OpenRA.Traits;
|
|||||||
using OpenRA.Graphics;
|
using OpenRA.Graphics;
|
||||||
using OpenRA.FileFormats;
|
using OpenRA.FileFormats;
|
||||||
using OpenRA.FileSystem;
|
using OpenRA.FileSystem;
|
||||||
|
using OpenRA.Mods.Common.SpriteLoaders;
|
||||||
|
|
||||||
namespace OpenRA.Mods.Common.UtilityCommands
|
namespace OpenRA.Mods.Common.UtilityCommands
|
||||||
{
|
{
|
||||||
@@ -65,12 +66,14 @@ namespace OpenRA.Mods.Common.UtilityCommands
|
|||||||
.Where(a => !remap.ContainsValue(a))
|
.Where(a => !remap.ContainsValue(a))
|
||||||
.MinBy(a => ColorDistance(destPalette[a], srcPalette[i]));
|
.MinBy(a => ColorDistance(destPalette[a], srcPalette[i]));
|
||||||
|
|
||||||
var srcImage = ShpReader.Load(args[3]);
|
using (var s = File.OpenRead(args[3]))
|
||||||
|
|
||||||
using (var destStream = File.Create(args[4]))
|
using (var destStream = File.Create(args[4]))
|
||||||
ShpReader.Write(destStream, srcImage.Size,
|
{
|
||||||
|
var srcImage = new ShpTDSprite(s);
|
||||||
|
ShpTDSprite.Write(destStream, srcImage.Size,
|
||||||
srcImage.Frames.Select(im => im.Data.Select(px => (byte)remap[px]).ToArray()));
|
srcImage.Frames.Select(im => im.Data.Select(px => (byte)remap[px]).ToArray()));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int ColorDistance(uint a, uint b)
|
static int ColorDistance(uint a, uint b)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -210,3 +210,5 @@ Missions:
|
|||||||
mods/cnc/missions.yaml
|
mods/cnc/missions.yaml
|
||||||
|
|
||||||
SupportsMapsFrom: cnc
|
SupportsMapsFrom: cnc
|
||||||
|
|
||||||
|
SpriteFormats: ShpTD
|
||||||
@@ -188,3 +188,5 @@ LuaScripts:
|
|||||||
mods/common/lua/facing.lua
|
mods/common/lua/facing.lua
|
||||||
|
|
||||||
SupportsMapsFrom: d2k
|
SupportsMapsFrom: d2k
|
||||||
|
|
||||||
|
SpriteFormats: ShpTD
|
||||||
@@ -50,3 +50,5 @@ Fonts:
|
|||||||
Size:10
|
Size:10
|
||||||
|
|
||||||
LobbyDefaults:
|
LobbyDefaults:
|
||||||
|
|
||||||
|
SpriteFormats: ShpTD
|
||||||
@@ -207,3 +207,5 @@ Missions:
|
|||||||
mods/ra/missions.yaml
|
mods/ra/missions.yaml
|
||||||
|
|
||||||
SupportsMapsFrom: ra
|
SupportsMapsFrom: ra
|
||||||
|
|
||||||
|
SpriteFormats: ShpTD
|
||||||
@@ -229,3 +229,5 @@ LuaScripts:
|
|||||||
mods/common/lua/facing.lua
|
mods/common/lua/facing.lua
|
||||||
|
|
||||||
SupportsMapsFrom: ts
|
SupportsMapsFrom: ts
|
||||||
|
|
||||||
|
SpriteFormats: ShpTD
|
||||||
Reference in New Issue
Block a user