Move Sequence parsing into mod code.
This commit is contained in:
@@ -12,12 +12,36 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace OpenRA.Graphics
|
||||
{
|
||||
using Sequences = IReadOnlyDictionary<string, Lazy<IReadOnlyDictionary<string, ISpriteSequence>>>;
|
||||
using UnitSequences = Lazy<IReadOnlyDictionary<string, ISpriteSequence>>;
|
||||
|
||||
public interface ISpriteSequence
|
||||
{
|
||||
string Name { get; }
|
||||
int Start { get; }
|
||||
int Length { get; }
|
||||
int Stride { get; }
|
||||
int Facings { get; }
|
||||
int Tick { get; }
|
||||
int ZOffset { get; }
|
||||
int ShadowStart { get; }
|
||||
int ShadowZOffset { get; }
|
||||
int[] Frames { get; }
|
||||
|
||||
Sprite GetSprite(int frame);
|
||||
Sprite GetSprite(int frame, int facing);
|
||||
Sprite GetShadow(int frame, int facing);
|
||||
}
|
||||
|
||||
public interface ISpriteSequenceLoader
|
||||
{
|
||||
IReadOnlyDictionary<string, ISpriteSequence> ParseUnitSequences(ModData modData, TileSet tileSet, SpriteCache cache, MiniYamlNode node);
|
||||
}
|
||||
|
||||
public class SequenceProvider
|
||||
{
|
||||
readonly Lazy<Sequences> sequences;
|
||||
@@ -77,6 +101,7 @@ namespace OpenRA.Graphics
|
||||
public sealed class SequenceCache : IDisposable
|
||||
{
|
||||
readonly ModData modData;
|
||||
readonly TileSet tileSet;
|
||||
readonly Lazy<SpriteCache> spriteCache;
|
||||
public SpriteCache SpriteCache { get { return spriteCache.Value; } }
|
||||
|
||||
@@ -85,7 +110,9 @@ namespace OpenRA.Graphics
|
||||
public SequenceCache(ModData modData, TileSet tileSet)
|
||||
{
|
||||
this.modData = modData;
|
||||
this.tileSet = tileSet;
|
||||
|
||||
// Every time we load a tile set, we create a sequence cache for it
|
||||
spriteCache = Exts.Lazy(() => new SpriteCache(modData.SpriteLoaders, tileSet.Extensions, new SheetBuilder(SheetType.Indexed)));
|
||||
}
|
||||
|
||||
@@ -116,7 +143,7 @@ namespace OpenRA.Graphics
|
||||
items.Add(node.Key, t);
|
||||
else
|
||||
{
|
||||
t = Exts.Lazy(() => CreateUnitSequences(node));
|
||||
t = Exts.Lazy(() => modData.SpriteSequenceLoader.ParseUnitSequences(modData, tileSet, SpriteCache, node));
|
||||
sequenceCache.Add(key, t);
|
||||
items.Add(node.Key, t);
|
||||
}
|
||||
@@ -125,28 +152,6 @@ namespace OpenRA.Graphics
|
||||
return new ReadOnlyDictionary<string, UnitSequences>(items);
|
||||
}
|
||||
|
||||
IReadOnlyDictionary<string, ISpriteSequence> CreateUnitSequences(MiniYamlNode node)
|
||||
{
|
||||
var unitSequences = new Dictionary<string, ISpriteSequence>();
|
||||
|
||||
foreach (var kvp in node.Value.ToDictionary())
|
||||
{
|
||||
using (new Support.PerfTimer("new Sequence(\"{0}\")".F(node.Key), 20))
|
||||
{
|
||||
try
|
||||
{
|
||||
unitSequences.Add(kvp.Key, new Sequence(spriteCache.Value, node.Key, kvp.Key, kvp.Value));
|
||||
}
|
||||
catch (FileNotFoundException ex)
|
||||
{
|
||||
Log.Write("debug", ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new ReadOnlyDictionary<string, ISpriteSequence>(unitSequences);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (spriteCache.IsValueCreated)
|
||||
|
||||
@@ -20,6 +20,17 @@ namespace OpenRA
|
||||
public enum TileShape { Rectangle, Diamond }
|
||||
public interface IGlobalModData { }
|
||||
|
||||
public sealed class SpriteSequenceFormat : IGlobalModData
|
||||
{
|
||||
public readonly string Type;
|
||||
public readonly IReadOnlyDictionary<string, MiniYaml> Metadata;
|
||||
public SpriteSequenceFormat(MiniYaml yaml)
|
||||
{
|
||||
Type = yaml.Value;
|
||||
Metadata = new ReadOnlyDictionary<string, MiniYaml>(yaml.ToDictionary());
|
||||
}
|
||||
}
|
||||
|
||||
// Describes what is to be loaded in order to run a mod
|
||||
public class Manifest
|
||||
{
|
||||
@@ -34,6 +45,7 @@ namespace OpenRA
|
||||
public readonly IReadOnlyDictionary<string, string> MapFolders;
|
||||
public readonly MiniYaml LoadScreen;
|
||||
public readonly MiniYaml LobbyDefaults;
|
||||
|
||||
public readonly Dictionary<string, Pair<string, int>> Fonts;
|
||||
public readonly Size TileSize = new Size(24, 24);
|
||||
public readonly TileShape TileShape = TileShape.Rectangle;
|
||||
@@ -155,7 +167,7 @@ namespace OpenRA
|
||||
throw new InvalidDataException("`{0}` is not a valid mod manifest entry.".F(kv.Key));
|
||||
|
||||
IGlobalModData module;
|
||||
var ctor = t.GetConstructor(new Type[] { typeof(MiniYaml) } );
|
||||
var ctor = t.GetConstructor(new Type[] { typeof(MiniYaml) });
|
||||
if (ctor != null)
|
||||
{
|
||||
// Class has opted-in to DIY initialization
|
||||
|
||||
@@ -25,6 +25,7 @@ namespace OpenRA
|
||||
public readonly WidgetLoader WidgetLoader;
|
||||
public readonly MapCache MapCache;
|
||||
public readonly ISpriteLoader[] SpriteLoaders;
|
||||
public readonly ISpriteSequenceLoader SpriteSequenceLoader;
|
||||
public readonly RulesetCache RulesetCache;
|
||||
public ILoadScreen LoadScreen { get; private set; }
|
||||
public VoxelLoader VoxelLoader { get; private set; }
|
||||
@@ -52,17 +53,25 @@ namespace OpenRA
|
||||
RulesetCache.LoadingProgress += HandleLoadingProgress;
|
||||
MapCache = new MapCache(this);
|
||||
|
||||
var loaders = new List<ISpriteLoader>();
|
||||
var spriteLoaders = new List<ISpriteLoader>();
|
||||
foreach (var format in Manifest.SpriteFormats)
|
||||
{
|
||||
var loader = ObjectCreator.FindType(format + "Loader");
|
||||
if (loader == null || !loader.GetInterfaces().Contains(typeof(ISpriteLoader)))
|
||||
throw new InvalidOperationException("Unable to find a sprite loader for type '{0}'.".F(format));
|
||||
|
||||
loaders.Add((ISpriteLoader)ObjectCreator.CreateBasic(loader));
|
||||
spriteLoaders.Add((ISpriteLoader)ObjectCreator.CreateBasic(loader));
|
||||
}
|
||||
|
||||
SpriteLoaders = loaders.ToArray();
|
||||
SpriteLoaders = spriteLoaders.ToArray();
|
||||
|
||||
var sequenceFormat = Manifest.Get<SpriteSequenceFormat>();
|
||||
var sequenceLoader = ObjectCreator.FindType(sequenceFormat.Type + "Loader");
|
||||
var ctor = sequenceLoader != null ? sequenceLoader.GetConstructor(new[] { typeof(ModData) }) : null;
|
||||
if (sequenceLoader == null || !sequenceLoader.GetInterfaces().Contains(typeof(ISpriteSequenceLoader)) || ctor == null)
|
||||
throw new InvalidOperationException("Unable to find a sequence loader for type '{0}'.".F(sequenceFormat.Type));
|
||||
|
||||
SpriteSequenceLoader = (ISpriteSequenceLoader)ctor.Invoke(new[] { this });
|
||||
|
||||
// HACK: Mount only local folders so we have a half-working environment for the asset installer
|
||||
GlobalFileSystem.UnmountAll();
|
||||
|
||||
@@ -109,7 +109,6 @@
|
||||
<Compile Include="Graphics\MappedImage.cs" />
|
||||
<Compile Include="Graphics\Minimap.cs" />
|
||||
<Compile Include="Graphics\Renderer.cs" />
|
||||
<Compile Include="Graphics\Sequence.cs" />
|
||||
<Compile Include="Graphics\SequenceProvider.cs" />
|
||||
<Compile Include="Graphics\Sheet.cs" />
|
||||
<Compile Include="Graphics\SheetBuilder.cs" />
|
||||
|
||||
@@ -9,33 +9,53 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
|
||||
namespace OpenRA.Graphics
|
||||
namespace OpenRA.Mods.Common.Graphics
|
||||
{
|
||||
public interface ISpriteSequence
|
||||
public class DefaultSpriteSequenceLoader : ISpriteSequenceLoader
|
||||
{
|
||||
string Name { get; }
|
||||
int Start { get; }
|
||||
int Length { get; }
|
||||
int Stride { get; }
|
||||
int Facings { get; }
|
||||
int Tick { get; }
|
||||
int ZOffset { get; }
|
||||
int ShadowStart { get; }
|
||||
int ShadowZOffset { get; }
|
||||
int[] Frames { get; }
|
||||
public DefaultSpriteSequenceLoader(ModData modData) { }
|
||||
|
||||
Sprite GetSprite(int frame);
|
||||
Sprite GetSprite(int frame, int facing);
|
||||
Sprite GetShadow(int frame, int facing);
|
||||
public virtual ISpriteSequence CreateSequence(ModData modData, TileSet tileSet, SpriteCache cache, string unit, string name, MiniYaml info)
|
||||
{
|
||||
return new DefaultSpriteSequence(modData, tileSet, cache, this, unit, name, info);
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<string, ISpriteSequence> ParseUnitSequences(ModData modData, TileSet tileSet, SpriteCache cache, MiniYamlNode node)
|
||||
{
|
||||
var unitSequences = new Dictionary<string, ISpriteSequence>();
|
||||
|
||||
foreach (var kvp in node.Value.ToDictionary())
|
||||
{
|
||||
using (new Support.PerfTimer("new Sequence(\"{0}\")".F(node.Key), 20))
|
||||
{
|
||||
try
|
||||
{
|
||||
unitSequences.Add(kvp.Key, CreateSequence(modData, tileSet, cache, node.Key, kvp.Key, kvp.Value));
|
||||
}
|
||||
catch (FileNotFoundException ex)
|
||||
{
|
||||
// Eat the FileNotFound exceptions from missing sprites
|
||||
Log.Write("debug", ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new ReadOnlyDictionary<string, ISpriteSequence>(unitSequences);
|
||||
}
|
||||
}
|
||||
|
||||
public class Sequence : ISpriteSequence
|
||||
public class DefaultSpriteSequence : ISpriteSequence
|
||||
{
|
||||
readonly Sprite[] sprites;
|
||||
readonly bool reverseFacings, transpose;
|
||||
|
||||
protected readonly ISpriteSequenceLoader Loader;
|
||||
|
||||
public string Name { get; private set; }
|
||||
public int Start { get; private set; }
|
||||
public int Length { get; private set; }
|
||||
@@ -47,10 +67,16 @@ namespace OpenRA.Graphics
|
||||
public int ShadowZOffset { get; private set; }
|
||||
public int[] Frames { get; private set; }
|
||||
|
||||
public Sequence(SpriteCache cache, string unit, string name, MiniYaml info)
|
||||
protected virtual string GetSpriteSrc(ModData modData, TileSet tileSet, string unit, string name, MiniYaml info, Dictionary<string, MiniYaml> d)
|
||||
{
|
||||
return info.Value ?? unit;
|
||||
}
|
||||
|
||||
public DefaultSpriteSequence(ModData modData, TileSet tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string unit, string name, MiniYaml info)
|
||||
{
|
||||
var srcOverride = info.Value;
|
||||
Name = name;
|
||||
Loader = loader;
|
||||
|
||||
var d = info.ToDictionary();
|
||||
var offset = float2.Zero;
|
||||
var blendMode = BlendMode.Alpha;
|
||||
@@ -68,7 +94,8 @@ namespace OpenRA.Graphics
|
||||
|
||||
// Apply offset to each sprite in the sequence
|
||||
// Different sequences may apply different offsets to the same frame
|
||||
sprites = cache[srcOverride ?? unit].Select(
|
||||
var src = GetSpriteSrc(modData, tileSet, unit, name, info, d);
|
||||
sprites = cache[src].Select(
|
||||
s => new Sprite(s.Sheet, s.Bounds, s.Offset + offset, s.Channel, blendMode)).ToArray();
|
||||
|
||||
if (!d.ContainsKey("Length"))
|
||||
@@ -132,7 +159,7 @@ namespace OpenRA.Graphics
|
||||
if (Start < 0 || Start + Facings * Stride > sprites.Length || ShadowStart + Facings * Stride > sprites.Length)
|
||||
throw new InvalidOperationException(
|
||||
"{6}: Sequence {0}.{1} uses frames [{2}..{3}] of SHP `{4}`, but only 0..{5} actually exist"
|
||||
.F(unit, name, Start, Start + Facings * Stride - 1, srcOverride ?? unit, sprites.Length - 1,
|
||||
.F(unit, name, Start, Start + Facings * Stride - 1, src, sprites.Length - 1,
|
||||
info.Nodes[0].Location));
|
||||
}
|
||||
catch (FormatException f)
|
||||
@@ -156,9 +183,9 @@ namespace OpenRA.Graphics
|
||||
return ShadowStart >= 0 ? GetSprite(ShadowStart, frame, facing) : null;
|
||||
}
|
||||
|
||||
Sprite GetSprite(int start, int frame, int facing)
|
||||
protected virtual Sprite GetSprite(int start, int frame, int facing)
|
||||
{
|
||||
var f = Traits.Util.QuantizeFacing(facing, Facings);
|
||||
var f = OpenRA.Traits.Util.QuantizeFacing(facing, Facings);
|
||||
|
||||
if (reverseFacings)
|
||||
f = (Facings - f) % Facings;
|
||||
@@ -577,6 +577,7 @@
|
||||
<Compile Include="UtilityCommands\ReplayMetadataCommand.cs" />
|
||||
<Compile Include="Widgets\Logic\ReplayUtils.cs" />
|
||||
<Compile Include="InstallUtils.cs" />
|
||||
<Compile Include="Graphics\DefaultSpriteSequence.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<PropertyGroup>
|
||||
|
||||
@@ -203,3 +203,5 @@ Missions:
|
||||
SupportsMapsFrom: cnc
|
||||
|
||||
SpriteFormats: ShpTD, TmpTD, ShpTS, TmpRA
|
||||
|
||||
SpriteSequenceFormat: DefaultSpriteSequence
|
||||
|
||||
@@ -178,3 +178,5 @@ Fonts:
|
||||
SupportsMapsFrom: d2k
|
||||
|
||||
SpriteFormats: R8, ShpTD, TmpRA
|
||||
|
||||
SpriteSequenceFormat: DefaultSpriteSequence
|
||||
|
||||
@@ -52,3 +52,5 @@ Fonts:
|
||||
LobbyDefaults:
|
||||
|
||||
SpriteFormats: ShpTD
|
||||
|
||||
SpriteSequenceFormat: DefaultSpriteSequence
|
||||
|
||||
@@ -201,3 +201,5 @@ Missions:
|
||||
SupportsMapsFrom: ra
|
||||
|
||||
SpriteFormats: ShpTD, TmpRA, TmpTD, ShpTS
|
||||
|
||||
SpriteSequenceFormat: DefaultSpriteSequence
|
||||
|
||||
@@ -219,3 +219,5 @@ Fonts:
|
||||
SupportsMapsFrom: ts
|
||||
|
||||
SpriteFormats: ShpTS, TmpTS, ShpTD
|
||||
|
||||
SpriteSequenceFormat: DefaultSpriteSequence
|
||||
|
||||
Reference in New Issue
Block a user