Remove HasEmbeddedPalette from sequences.

This commit is contained in:
Paul Chote
2023-03-06 21:00:59 +00:00
committed by Gustas
parent dab3ca0025
commit 992ba1a9a2
9 changed files with 114 additions and 61 deletions

View File

@@ -19,26 +19,6 @@ using OpenRA.Primitives;
namespace OpenRA.Mods.Common.Graphics namespace OpenRA.Mods.Common.Graphics
{ {
public class EmbeddedSpritePalette
{
readonly uint[] filePalette = null;
readonly Dictionary<int, uint[]> framePalettes = null;
public EmbeddedSpritePalette(uint[] filePalette = null, Dictionary<int, uint[]> framePalettes = null)
{
this.filePalette = filePalette;
this.framePalettes = framePalettes;
}
public bool TryGetPaletteForFrame(int frame, out uint[] palette)
{
if (framePalettes == null || !framePalettes.TryGetValue(frame, out palette))
palette = filePalette;
return palette != null;
}
}
public class DefaultSpriteSequenceLoader : ISpriteSequenceLoader public class DefaultSpriteSequenceLoader : ISpriteSequenceLoader
{ {
static readonly MiniYaml NoData = new MiniYaml(null); static readonly MiniYaml NoData = new MiniYaml(null);
@@ -256,11 +236,6 @@ namespace OpenRA.Mods.Common.Graphics
[Desc("X, Y offset to apply to the depth sprite.")] [Desc("X, Y offset to apply to the depth sprite.")]
static readonly SpriteSequenceField<float2> DepthSpriteOffset = new SpriteSequenceField<float2>(nameof(DepthSpriteOffset), float2.Zero); static readonly SpriteSequenceField<float2> DepthSpriteOffset = new SpriteSequenceField<float2>(nameof(DepthSpriteOffset), float2.Zero);
[Desc("Make a custom palette embedded in the sprite available to the PaletteFromEmbeddedSpritePalette trait.")]
static readonly SpriteSequenceField<bool> HasEmbeddedPalette = new SpriteSequenceField<bool>(nameof(HasEmbeddedPalette), false);
public readonly uint[] EmbeddedPalette;
protected virtual string GetSpriteFilename(ModData modData, string tileset, string image, string sequence, MiniYaml data, MiniYaml defaults) protected virtual string GetSpriteFilename(ModData modData, string tileset, string image, string sequence, MiniYaml data, MiniYaml defaults)
{ {
return LoadField(Filename, data, defaults); return LoadField(Filename, data, defaults);
@@ -502,19 +477,6 @@ namespace OpenRA.Mods.Common.Graphics
}).ToArray(); }).ToArray();
} }
if (LoadField(HasEmbeddedPalette, data, defaults))
{
var filename = GetSpriteFilename(modData, tileSet, image, sequence, data, defaults);
if (filename == null)
throw new YamlException($"Sequence {image}.{sequence} does not define a filename.");
var metadata = cache.FrameMetadata(filename);
var i = frames != null ? frames[0] : start;
var palettes = metadata?.GetOrDefault<EmbeddedSpritePalette>();
if (palettes == null || !palettes.TryGetPaletteForFrame(i, out EmbeddedPalette))
throw new YamlException($"Cannot export palette from {filename}: frame {i} does not define an embedded palette.");
}
var boundSprites = SpriteBounds(sprites, frames, start, facings, length, stride, transpose); var boundSprites = SpriteBounds(sprites, frames, start, facings, length, stride, transpose);
if (shadowStart > 0) if (shadowStart > 0)
boundSprites = boundSprites.Concat(SpriteBounds(sprites, frames, shadowStart, facings, length, stride, transpose)); boundSprites = boundSprites.Concat(SpriteBounds(sprites, frames, shadowStart, facings, length, stride, transpose));

View File

@@ -0,0 +1,35 @@
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
namespace OpenRA.Mods.Common.SpriteLoaders
{
public class EmbeddedSpritePalette
{
readonly uint[] filePalette;
readonly Dictionary<int, uint[]> framePalettes;
public EmbeddedSpritePalette(uint[] filePalette = null, Dictionary<int, uint[]> framePalettes = null)
{
this.filePalette = filePalette;
this.framePalettes = framePalettes;
}
public bool TryGetPaletteForFrame(int frame, out uint[] palette)
{
if (framePalettes == null || !framePalettes.TryGetValue(frame, out palette))
palette = filePalette;
return palette != null;
}
}
}

View File

@@ -16,7 +16,6 @@ using System.IO;
using System.Linq; using System.Linq;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Primitives; using OpenRA.Primitives;
namespace OpenRA.Mods.Common.SpriteLoaders namespace OpenRA.Mods.Common.SpriteLoaders

View File

@@ -10,10 +10,9 @@
#endregion #endregion
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using OpenRA.FileSystem; using OpenRA.FileSystem;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics; using OpenRA.Mods.Common.SpriteLoaders;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
@@ -27,13 +26,11 @@ namespace OpenRA.Mods.Common.Traits
public readonly string Name = null; public readonly string Name = null;
[FieldLoader.Require] [FieldLoader.Require]
[Desc("Sequence image holding the palette definition")] [Desc("Filename of sprite that contains the palette definition.")]
public readonly string Image = null; public readonly string Filename = null;
[FieldLoader.Require] [Desc("Image frame associated with the palette definition, if relevant.")]
[SequenceReference(nameof(Image))] public readonly int Frame = 0;
[Desc("Sequence holding the palette definition")]
public readonly string Sequence = null;
[Desc("Allow palette modifiers to change the palette.")] [Desc("Allow palette modifiers to change the palette.")]
public readonly bool AllowModifiers = true; public readonly bool AllowModifiers = true;
@@ -47,8 +44,12 @@ namespace OpenRA.Mods.Common.Traits
ImmutablePalette IProvidesCursorPaletteInfo.ReadPalette(IReadOnlyFileSystem fileSystem) ImmutablePalette IProvidesCursorPaletteInfo.ReadPalette(IReadOnlyFileSystem fileSystem)
{ {
var sequence = (DefaultSpriteSequence)Game.ModData.DefaultSequences.Values.First().GetSequence(Image, Sequence); FrameLoader.GetFrames(fileSystem, Filename, Game.ModData.SpriteLoaders, out var metadata);
return new ImmutablePalette(sequence.EmbeddedPalette); var palettes = metadata?.GetOrDefault<EmbeddedSpritePalette>();
if (palettes == null || !palettes.TryGetPaletteForFrame(Frame, out var embeddedPalette))
throw new YamlException($"Cannot export palette from {Filename}: frame {Frame} does not define an embedded palette");
return new ImmutablePalette(embeddedPalette);
} }
} }
@@ -59,8 +60,12 @@ namespace OpenRA.Mods.Common.Traits
public void LoadPalettes(WorldRenderer wr) public void LoadPalettes(WorldRenderer wr)
{ {
var sequence = (DefaultSpriteSequence)wr.World.Map.Rules.Sequences.GetSequence(info.Image, info.Sequence); FrameLoader.GetFrames(wr.World.Map, info.Filename, Game.ModData.SpriteLoaders, out var metadata);
wr.AddPalette(info.Name, new ImmutablePalette(sequence.EmbeddedPalette), info.AllowModifiers); var palettes = metadata?.GetOrDefault<EmbeddedSpritePalette>();
if (palettes == null || !palettes.TryGetPaletteForFrame(info.Frame, out var embeddedPalette))
throw new YamlException($"Cannot export palette from {info.Filename}: frame {info.Frame} does not define an embedded palette");
wr.AddPalette(info.Name, new ImmutablePalette(embeddedPalette), info.AllowModifiers);
} }
public IEnumerable<string> PaletteNames { get { yield return info.Name; } } public IEnumerable<string> PaletteNames { get { yield return info.Name; } }

View File

@@ -0,0 +1,56 @@
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
namespace OpenRA.Mods.Common.UpdateRules.Rules
{
public class RemoveSequenceHasEmbeddedPalette : UpdateRule
{
public override string Name => "Remove sequence HasEmbeddedPalette property.";
public override string Description =>
"The PaletteFromEmbeddedSpritePalette trait no longer references a sequence.\n" +
"Image and Sequence are replaced by Filename and Frame.";
readonly HashSet<string> locations = new HashSet<string>();
public override IEnumerable<string> AfterUpdate(ModData modData)
{
if (locations.Count > 0)
yield return "The PaletteFromEmbeddedSpritePalette trait no longer references a sequence.\n" +
"You must manually define Filename (and Frame if needed) on the following actors:\n" +
UpdateUtils.FormatMessageList(locations);
locations.Clear();
}
public override IEnumerable<string> UpdateSequenceNode(ModData modData, MiniYamlNode imageNode)
{
foreach (var sequenceNode in imageNode.Value.Nodes)
sequenceNode.RemoveNodes("HasEmbeddedPalette");
yield break;
}
public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNode actorNode)
{
foreach (var traitNode in actorNode.ChildrenMatching("PaletteFromEmbeddedSpritePalette"))
{
traitNode.RemoveNodes("Image");
traitNode.RemoveNodes("Sequence");
locations.Add($"{actorNode.Key} ({actorNode.Location.Filename})");
}
yield break;
}
}
}

View File

@@ -95,6 +95,7 @@ namespace OpenRA.Mods.Common.UpdateRules
new ProductionTabsWidgetAddTabButtonCollection(), new ProductionTabsWidgetAddTabButtonCollection(),
new RemoveTSRefinery(), new RemoveTSRefinery(),
new RenameMcvCrateAction(), new RenameMcvCrateAction(),
new RemoveSequenceHasEmbeddedPalette(),
}) })
}; };

View File

@@ -12,7 +12,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics; using OpenRA.Mods.Common.SpriteLoaders;
using OpenRA.Primitives; using OpenRA.Primitives;
namespace OpenRA.Mods.D2k.SpriteLoaders namespace OpenRA.Mods.D2k.SpriteLoaders

View File

@@ -24,8 +24,8 @@
AllowModifiers: false AllowModifiers: false
PaletteFromEmbeddedSpritePalette@moveflash-base: PaletteFromEmbeddedSpritePalette@moveflash-base:
Name: moveflash-base Name: moveflash-base
Image: moveflsh Filename: DATA.R8
Sequence: idle Frame: 3874
PaletteFromScaledPalette@moveflash: PaletteFromScaledPalette@moveflash:
Name: moveflash Name: moveflash
BasePalette: moveflash-base BasePalette: moveflash-base
@@ -47,8 +47,8 @@
Offset: -64 Offset: -64
PaletteFromEmbeddedSpritePalette@shroud: PaletteFromEmbeddedSpritePalette@shroud:
Name: shroud Name: shroud
Image: shroud Filename: DATA.R8
Sequence: palette Frame: 38
D2kFogPalette@fog: D2kFogPalette@fog:
Name: fog Name: fog
BasePalette: shroud BasePalette: shroud

View File

@@ -534,7 +534,6 @@ moveflsh:
Tick: 80 Tick: 80
BlendMode: Multiply BlendMode: Multiply
ZOffset: 2047 ZOffset: 2047
HasEmbeddedPalette: True
resources: resources:
spicea: spicea:
@@ -563,10 +562,6 @@ shroud:
BlendMode: Subtractive BlendMode: Subtractive
Offset: -16,-16 Offset: -16,-16
Length: 14 Length: 14
palette:
Filename: DATA.R8
Start: 38
HasEmbeddedPalette: True
shrouda: shrouda:
Filename: DATA.R8 Filename: DATA.R8
Start: 40 Start: 40