Skip to content

Commit 81e2a38

Browse files
committed
Refactor Color Metadata
* Remove RgbaAttribute * Add IColorBase interface * Add ChannelType static property * Add ChannelsAreFullyUtilized static property
1 parent d496a13 commit 81e2a38

17 files changed

Lines changed: 151 additions & 182 deletions

File tree

AssetRipper.TextureDecoder.ColorGenerator/Program.cs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,20 @@ private static void WriteGenericColor((string, bool, bool, bool, bool) details)
6969
private static void WriteCustomColor(IndentedTextWriter writer, string name, Type type, bool hasRed, bool hasGreen, bool hasBlue, bool hasAlpha, bool fullyUtilized)
7070
{
7171
string typeName = CSharpPrimitives.Dictionary[type].LangName;
72-
writer.WriteLine("//This code is source generated. Do not edit manually.");
73-
writer.WriteLine();
74-
writer.WriteLine($"using {AttributeNamespace};");
72+
writer.WriteGeneratedCodeWarning();
7573
writer.WriteLine();
76-
writer.WriteLine($"namespace {OutputNamespace}");
77-
using (new CurlyBrackets(writer))
74+
using (new Namespace(writer, OutputNamespace))
7875
{
79-
WriteRgbaAttribute(writer, hasRed, hasGreen, hasBlue, hasAlpha, fullyUtilized);
8076
writer.WriteLine($"public partial struct {name} : IColor<{typeName}>");
8177
using (new CurlyBrackets(writer))
8278
{
83-
WriteHasChannelStaticProperties(writer, hasRed, hasGreen, hasBlue, hasAlpha, typeName);
79+
WriteColorBaseStaticProperties(writer, hasRed, hasGreen, hasBlue, hasAlpha, fullyUtilized, typeName);
8480
writer.WriteLineNoTabs();
8581
WriteToString(writer, hasRed, hasGreen, hasBlue, hasAlpha);
8682
}
8783
}
8884
}
8985

90-
private static void WriteRgbaAttribute(IndentedTextWriter writer, bool hasRed, bool hasGreen, bool hasBlue, bool hasAlpha, bool fullyUtilized)
91-
{
92-
writer.WriteLine($"[RgbaAttribute(RedChannel = {hasRed.ToLowerString()}, GreenChannel = {hasGreen.ToLowerString()}, BlueChannel = {hasBlue.ToLowerString()}, AlphaChannel = {hasAlpha.ToLowerString()}, FullyUtilizedChannels = {fullyUtilized.ToLowerString()})]");
93-
}
94-
9586
private static void WriteToString(IndentedTextWriter writer, bool hasRed, bool hasGreen, bool hasBlue, bool hasAlpha)
9687
{
9788
writer.WriteLine("public override string ToString()");
@@ -136,12 +127,14 @@ private static void WriteToString(IndentedTextWriter writer, bool hasRed, bool h
136127
}
137128
}
138129

139-
private static void WriteHasChannelStaticProperties(IndentedTextWriter writer, bool hasRed, bool hasGreen, bool hasBlue, bool hasAlpha, string typeName)
130+
private static void WriteColorBaseStaticProperties(IndentedTextWriter writer, bool hasRed, bool hasGreen, bool hasBlue, bool hasAlpha, bool fullyUtilized, string typeName)
140131
{
141-
writer.WriteLine($"static bool IColor<{typeName}>.HasRedChannel => {hasRed.ToLowerString()};");
142-
writer.WriteLine($"static bool IColor<{typeName}>.HasGreenChannel => {hasGreen.ToLowerString()};");
143-
writer.WriteLine($"static bool IColor<{typeName}>.HasBlueChannel => {hasBlue.ToLowerString()};");
144-
writer.WriteLine($"static bool IColor<{typeName}>.HasAlphaChannel => {hasAlpha.ToLowerString()};");
132+
writer.WriteLine($"static bool IColorBase.HasRedChannel => {hasRed.ToLowerString()};");
133+
writer.WriteLine($"static bool IColorBase.HasGreenChannel => {hasGreen.ToLowerString()};");
134+
writer.WriteLine($"static bool IColorBase.HasBlueChannel => {hasBlue.ToLowerString()};");
135+
writer.WriteLine($"static bool IColorBase.HasAlphaChannel => {hasAlpha.ToLowerString()};");
136+
writer.WriteLine($"static bool IColorBase.ChannelsAreFullyUtilized => {fullyUtilized.ToLowerString()};");
137+
writer.WriteLine($"static Type IColorBase.ChannelType => typeof({typeName});");
145138
}
146139

147140
private static void WriteGenericColor(IndentedTextWriter writer, string name, bool hasRed, bool hasGreen, bool hasBlue, bool hasAlpha)
@@ -153,13 +146,9 @@ private static void WriteGenericColor(IndentedTextWriter writer, string name, bo
153146
writer.WriteGeneratedCodeWarning();
154147
writer.WriteLineNoTabs();
155148

156-
writer.WriteLine($"using {AttributeNamespace};");
157-
writer.WriteLineNoTabs();
158-
159149
writer.WriteFileScopedNamespace(OutputNamespace);
160150
writer.WriteLineNoTabs();
161151

162-
WriteRgbaAttribute(writer, hasRed, hasGreen, hasBlue, hasAlpha, true);
163152
string constraints = hasRed && hasGreen && hasBlue && hasAlpha
164153
? "unmanaged"
165154
: "unmanaged, INumberBase<T>, IMinMaxValue<T>";
@@ -179,7 +168,7 @@ private static void WriteGenericColor(IndentedTextWriter writer, string name, bo
179168
writer.WriteLineNoTabs();
180169
WriteSetChannels(writer, typeName, hasRed, hasGreen, hasBlue, hasAlpha);
181170
writer.WriteLineNoTabs();
182-
WriteHasChannelStaticProperties(writer, hasRed, hasGreen, hasBlue, hasAlpha, typeName);
171+
WriteColorBaseStaticProperties(writer, hasRed, hasGreen, hasBlue, hasAlpha, true, typeName);
183172
writer.WriteLineNoTabs();
184173
WriteToString(writer, hasRed, hasGreen, hasBlue, hasAlpha);
185174
}

AssetRipper.TextureDecoder.TestGenerator/GenerationData.cs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
1-
using AssetRipper.TextureDecoder.Attributes;
2-
using AssetRipper.TextureDecoder.Rgb;
1+
using AssetRipper.TextureDecoder.Rgb;
32
using AssetRipper.TextureDecoder.SourceGeneration.Common;
4-
using System.Reflection;
53

64
namespace AssetRipper.TextureDecoder.TestGenerator
75
{
86
internal readonly struct GenerationData
97
{
10-
public Type ColorType { get; }
11-
public Type ChannelType { get; }
12-
public int ColorSize { get; }
8+
public Type ColorType { get; private init; }
9+
public Type ChannelType { get; private init; }
10+
public int ColorSize { get; private init; }
1311

14-
public bool RedChannel { get; }
15-
public bool GreenChannel { get; }
16-
public bool BlueChannel { get; }
17-
public bool AlphaChannel { get; }
18-
public bool FullyUtilizedChannels { get; }
12+
public bool RedChannel { get; private init; }
13+
public bool GreenChannel { get; private init; }
14+
public bool BlueChannel { get; private init; }
15+
public bool AlphaChannel { get; private init; }
16+
public bool FullyUtilizedChannels { get; private init; }
1917

2018
public bool IsFloatingPoint => CSharpPrimitives.IsFloatingPoint(ChannelType);
2119

2220
public int BitSizePerChannel => CSharpPrimitives.Dictionary[ChannelType].Size * 8;
2321

2422
public string ChannelTypeName => CSharpPrimitives.Dictionary[ChannelType].LangName;
2523

26-
public GenerationData(Type colorType, int colorSize)
24+
public static GenerationData Create<T>(int colorSize) where T : IColorBase
2725
{
28-
ColorType = colorType;
29-
ChannelType = ColorType.GetInterfaces().Single(t => t.Name == $"{nameof(IColor<byte>)}`1").GenericTypeArguments[0];
30-
ColorSize = colorSize;
31-
32-
RgbaAttribute attribute = colorType.GetCustomAttribute<RgbaAttribute>() ?? throw new NullReferenceException(colorType.Name);
33-
RedChannel = attribute.RedChannel;
34-
GreenChannel = attribute.GreenChannel;
35-
BlueChannel = attribute.BlueChannel;
36-
AlphaChannel = attribute.AlphaChannel;
37-
FullyUtilizedChannels = attribute.FullyUtilizedChannels;
26+
return new GenerationData()
27+
{
28+
ColorType = typeof(T),
29+
ChannelType = T.ChannelType,
30+
ColorSize = colorSize,
31+
RedChannel = T.HasRedChannel,
32+
GreenChannel = T.HasGreenChannel,
33+
BlueChannel = T.HasBlueChannel,
34+
AlphaChannel = T.HasAlphaChannel,
35+
FullyUtilizedChannels = T.ChannelsAreFullyUtilized,
36+
};
3837
}
3938

4039
public bool Contains(GenerationData other)
@@ -72,4 +71,4 @@ public bool Contains(GenerationData other)
7271
return BitSizePerChannel >= other.BitSizePerChannel;
7372
}
7473
}
75-
}
74+
}

AssetRipper.TextureDecoder.TestGenerator/Program.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ internal static class Program
1010
{
1111
internal static readonly List<GenerationData> dataList = new()
1212
{
13-
new GenerationData(typeof(ColorARGB16), 2),
14-
new GenerationData(typeof(ColorARGB32), 4),
15-
new GenerationData(typeof(ColorBGRA32), 4),
16-
new GenerationData(typeof(ColorRGB16), 2),
17-
new GenerationData(typeof(ColorRGB9e5), 4),
18-
new GenerationData(typeof(ColorRGBA16), 2),
13+
GenerationData.Create<ColorARGB16>(2),
14+
GenerationData.Create<ColorARGB32>(4),
15+
GenerationData.Create<ColorBGRA32>(4),
16+
GenerationData.Create<ColorRGB16>(2),
17+
GenerationData.Create<ColorRGB9e5>(4),
18+
GenerationData.Create<ColorRGBA16>(2),
1919
};
2020

2121
static void Main()

AssetRipper.TextureDecoder/Attributes/RgbaAttribute.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

AssetRipper.TextureDecoder/Rgb/Formats/ColorA.g.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// Auto-generated code. Do not modify manually.
22

3-
using AssetRipper.TextureDecoder.Attributes;
4-
53
namespace AssetRipper.TextureDecoder.Rgb.Formats;
64

7-
[RgbaAttribute(RedChannel = false, GreenChannel = false, BlueChannel = false, AlphaChannel = true, FullyUtilizedChannels = true)]
85
public partial struct ColorA<T> : IColor<T> where T : unmanaged, INumberBase<T>, IMinMaxValue<T>
96
{
107
public readonly T R
@@ -40,10 +37,12 @@ public void SetChannels(T r, T g, T b, T a)
4037
A = a;
4138
}
4239

43-
static bool IColor<T>.HasRedChannel => false;
44-
static bool IColor<T>.HasGreenChannel => false;
45-
static bool IColor<T>.HasBlueChannel => false;
46-
static bool IColor<T>.HasAlphaChannel => true;
40+
static bool IColorBase.HasRedChannel => false;
41+
static bool IColorBase.HasGreenChannel => false;
42+
static bool IColorBase.HasBlueChannel => false;
43+
static bool IColorBase.HasAlphaChannel => true;
44+
static bool IColorBase.ChannelsAreFullyUtilized => true;
45+
static Type IColorBase.ChannelType => typeof(T);
4746

4847
public override string ToString()
4948
{

AssetRipper.TextureDecoder/Rgb/Formats/ColorARGB16.g.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
//This code is source generated. Do not edit manually.
2-
3-
using AssetRipper.TextureDecoder.Attributes;
1+
// Auto-generated code. Do not modify manually.
42

53
namespace AssetRipper.TextureDecoder.Rgb.Formats
64
{
7-
[RgbaAttribute(RedChannel = true, GreenChannel = true, BlueChannel = true, AlphaChannel = true, FullyUtilizedChannels = false)]
85
public partial struct ColorARGB16 : IColor<byte>
96
{
10-
static bool IColor<byte>.HasRedChannel => true;
11-
static bool IColor<byte>.HasGreenChannel => true;
12-
static bool IColor<byte>.HasBlueChannel => true;
13-
static bool IColor<byte>.HasAlphaChannel => true;
7+
static bool IColorBase.HasRedChannel => true;
8+
static bool IColorBase.HasGreenChannel => true;
9+
static bool IColorBase.HasBlueChannel => true;
10+
static bool IColorBase.HasAlphaChannel => true;
11+
static bool IColorBase.ChannelsAreFullyUtilized => false;
12+
static Type IColorBase.ChannelType => typeof(byte);
1413

1514
public override string ToString()
1615
{

AssetRipper.TextureDecoder/Rgb/Formats/ColorARGB32.g.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
//This code is source generated. Do not edit manually.
2-
3-
using AssetRipper.TextureDecoder.Attributes;
1+
// Auto-generated code. Do not modify manually.
42

53
namespace AssetRipper.TextureDecoder.Rgb.Formats
64
{
7-
[RgbaAttribute(RedChannel = true, GreenChannel = true, BlueChannel = true, AlphaChannel = true, FullyUtilizedChannels = true)]
85
public partial struct ColorARGB32 : IColor<byte>
96
{
10-
static bool IColor<byte>.HasRedChannel => true;
11-
static bool IColor<byte>.HasGreenChannel => true;
12-
static bool IColor<byte>.HasBlueChannel => true;
13-
static bool IColor<byte>.HasAlphaChannel => true;
7+
static bool IColorBase.HasRedChannel => true;
8+
static bool IColorBase.HasGreenChannel => true;
9+
static bool IColorBase.HasBlueChannel => true;
10+
static bool IColorBase.HasAlphaChannel => true;
11+
static bool IColorBase.ChannelsAreFullyUtilized => true;
12+
static Type IColorBase.ChannelType => typeof(byte);
1413

1514
public override string ToString()
1615
{

AssetRipper.TextureDecoder/Rgb/Formats/ColorBGRA32.g.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
//This code is source generated. Do not edit manually.
2-
3-
using AssetRipper.TextureDecoder.Attributes;
1+
// Auto-generated code. Do not modify manually.
42

53
namespace AssetRipper.TextureDecoder.Rgb.Formats
64
{
7-
[RgbaAttribute(RedChannel = true, GreenChannel = true, BlueChannel = true, AlphaChannel = true, FullyUtilizedChannels = true)]
85
public partial struct ColorBGRA32 : IColor<byte>
96
{
10-
static bool IColor<byte>.HasRedChannel => true;
11-
static bool IColor<byte>.HasGreenChannel => true;
12-
static bool IColor<byte>.HasBlueChannel => true;
13-
static bool IColor<byte>.HasAlphaChannel => true;
7+
static bool IColorBase.HasRedChannel => true;
8+
static bool IColorBase.HasGreenChannel => true;
9+
static bool IColorBase.HasBlueChannel => true;
10+
static bool IColorBase.HasAlphaChannel => true;
11+
static bool IColorBase.ChannelsAreFullyUtilized => true;
12+
static Type IColorBase.ChannelType => typeof(byte);
1413

1514
public override string ToString()
1615
{

AssetRipper.TextureDecoder/Rgb/Formats/ColorR.g.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// Auto-generated code. Do not modify manually.
22

3-
using AssetRipper.TextureDecoder.Attributes;
4-
53
namespace AssetRipper.TextureDecoder.Rgb.Formats;
64

7-
[RgbaAttribute(RedChannel = true, GreenChannel = false, BlueChannel = false, AlphaChannel = false, FullyUtilizedChannels = true)]
85
public partial struct ColorR<T> : IColor<T> where T : unmanaged, INumberBase<T>, IMinMaxValue<T>
96
{
107
public T R { get; set; }
@@ -40,10 +37,12 @@ public void SetChannels(T r, T g, T b, T a)
4037
R = r;
4138
}
4239

43-
static bool IColor<T>.HasRedChannel => true;
44-
static bool IColor<T>.HasGreenChannel => false;
45-
static bool IColor<T>.HasBlueChannel => false;
46-
static bool IColor<T>.HasAlphaChannel => false;
40+
static bool IColorBase.HasRedChannel => true;
41+
static bool IColorBase.HasGreenChannel => false;
42+
static bool IColorBase.HasBlueChannel => false;
43+
static bool IColorBase.HasAlphaChannel => false;
44+
static bool IColorBase.ChannelsAreFullyUtilized => true;
45+
static Type IColorBase.ChannelType => typeof(T);
4746

4847
public override string ToString()
4948
{

AssetRipper.TextureDecoder/Rgb/Formats/ColorRG.g.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// Auto-generated code. Do not modify manually.
22

3-
using AssetRipper.TextureDecoder.Attributes;
4-
53
namespace AssetRipper.TextureDecoder.Rgb.Formats;
64

7-
[RgbaAttribute(RedChannel = true, GreenChannel = true, BlueChannel = false, AlphaChannel = false, FullyUtilizedChannels = true)]
85
public partial struct ColorRG<T> : IColor<T> where T : unmanaged, INumberBase<T>, IMinMaxValue<T>
96
{
107
public T R { get; set; }
@@ -37,10 +34,12 @@ public void SetChannels(T r, T g, T b, T a)
3734
G = g;
3835
}
3936

40-
static bool IColor<T>.HasRedChannel => true;
41-
static bool IColor<T>.HasGreenChannel => true;
42-
static bool IColor<T>.HasBlueChannel => false;
43-
static bool IColor<T>.HasAlphaChannel => false;
37+
static bool IColorBase.HasRedChannel => true;
38+
static bool IColorBase.HasGreenChannel => true;
39+
static bool IColorBase.HasBlueChannel => false;
40+
static bool IColorBase.HasAlphaChannel => false;
41+
static bool IColorBase.ChannelsAreFullyUtilized => true;
42+
static Type IColorBase.ChannelType => typeof(T);
4443

4544
public override string ToString()
4645
{

0 commit comments

Comments
 (0)