Skip to content

Commit aea54da

Browse files
committed
Generic Color Structs
* Resolves #42 * Removes many of the nongeneric structs * Unit tests for ColorRGB32Half are disabled (#17)
1 parent 18a5b27 commit aea54da

File tree

107 files changed

+1635
-8489
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+1635
-8489
lines changed

AssetRipper.TextureDecoder.ColorGenerator/AssetRipper.TextureDecoder.ColorGenerator.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@
1111
<PackageReference Include="AssetRipper.Text.SourceGeneration" Version="1.0.0" />
1212
</ItemGroup>
1313

14+
<ItemGroup>
15+
<ProjectReference Include="..\AssetRipper.TextureDecoder.SourceGeneration.Common\AssetRipper.TextureDecoder.SourceGeneration.Common.csproj" />
16+
</ItemGroup>
17+
1418
</Project>

AssetRipper.TextureDecoder.ColorGenerator/NumericConversionGenerator.cs

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using AssetRipper.Text.SourceGeneration;
2+
using AssetRipper.TextureDecoder.SourceGeneration.Common;
23
using System.CodeDom.Compiler;
34
using System.Diagnostics;
45
using System.Diagnostics.CodeAnalysis;
@@ -108,7 +109,7 @@ private static void WriteConvertMethod(IndentedTextWriter writer, Type from)
108109
writer.WriteLine($"private static TTo {methodName}<TTo>({fromName} value) where TTo : unmanaged");
109110
using (new CurlyBrackets(writer))
110111
{
111-
if (IsSignedInteger(from, out Type? unsignedFrom))
112+
if (CSharpPrimitives.IsSignedInteger(from, out Type? unsignedFrom))
112113
{
113114
writer.WriteLine($"{CSharpPrimitives.TypeNames[unsignedFrom]} unsigned = {ChangeSign}(value);");
114115
writer.WriteLine($"return {ConvertMethodName(unsignedFrom)}<TTo>(unsigned);");
@@ -125,22 +126,22 @@ private static void WriteConvertMethod(IndentedTextWriter writer, Type from)
125126
{
126127
writer.WriteLine($"return Unsafe.As<{toName}, TTo>(ref value);");
127128
}
128-
else if (IsSignedInteger(to, out Type? unsignedTo))
129+
else if (CSharpPrimitives.IsSignedInteger(to, out Type? unsignedTo))
129130
{
130131
string unsignedToName = CSharpPrimitives.TypeNames[unsignedTo];
131132
writer.WriteLine($"{toName} converted = {ChangeSign}({methodName}<{unsignedToName}>(value));");
132133
writer.WriteLine($"return Unsafe.As<{toName}, TTo>(ref converted);");
133134
}
134-
else if (IsFloatingPoint(from))
135+
else if (CSharpPrimitives.IsFloatingPoint(from))
135136
{
136-
if (IsFloatingPoint(to))
137+
if (CSharpPrimitives.IsFloatingPoint(to))
137138
{
138139
writer.WriteLine($"{toName} converted = ({toName})value;");
139140
writer.WriteLine($"return Unsafe.As<{toName}, TTo>(ref converted);");
140141
}
141142
else
142143
{
143-
Debug.Assert(IsUnsignedInteger(to));
144+
Debug.Assert(CSharpPrimitives.IsUnsignedInteger(to));
144145
if (from == typeof(Half))
145146
{
146147
writer.WriteComment("We use float because it has enough precision to convert from Half to any integer type.");
@@ -159,8 +160,8 @@ private static void WriteConvertMethod(IndentedTextWriter writer, Type from)
159160
}
160161
else
161162
{
162-
Debug.Assert(IsUnsignedInteger(from));
163-
if (IsFloatingPoint(to))
163+
Debug.Assert(CSharpPrimitives.IsUnsignedInteger(from));
164+
if (CSharpPrimitives.IsFloatingPoint(to))
164165
{
165166
if (to == typeof(Half))
166167
{
@@ -178,7 +179,7 @@ private static void WriteConvertMethod(IndentedTextWriter writer, Type from)
178179
}
179180
else
180181
{
181-
Debug.Assert(IsUnsignedInteger(to));
182+
Debug.Assert(CSharpPrimitives.IsUnsignedInteger(to));
182183
writer.WriteComment("See https://github.com/AssetRipper/TextureDecoder/issues/19");
183184
int fromSize = CSharpPrimitives.Sizes[from];
184185
int toSize = CSharpPrimitives.Sizes[to];
@@ -214,8 +215,8 @@ private static void WriteConvertMethod(IndentedTextWriter writer, Type from)
214215
string conversionType = fromSize <= sizeof(uint) ? "uint" : "ulong";
215216
writer.WriteLine("unchecked");
216217
using (new CurlyBrackets(writer))
217-
{
218-
int offset = (fromSize / toSize - 1) * toSize * 8;
218+
{
219+
int offset = (fromSize / toSize - 1) * toSize * 8;
219220
writer.WriteLine($"{toName} converted = ({toName})(({conversionType})value >> {offset});");
220221
writer.WriteLine($"return Unsafe.As<{toName}, TTo>(ref converted);");
221222
}
@@ -239,39 +240,4 @@ private static string ConvertMethodName(Type from)
239240
{
240241
return $"Convert{from.Name}";
241242
}
242-
243-
private static bool IsFloatingPoint(Type type)
244-
{
245-
return type == typeof(Half) || type == typeof(float) || type == typeof(double) || type == typeof(decimal);
246-
}
247-
248-
private static bool IsSignedInteger(Type type, [NotNullWhen(true)] out Type? unsignedType)
249-
{
250-
if (type == typeof(sbyte))
251-
{
252-
unsignedType = typeof(byte);
253-
}
254-
else if (type == typeof(short))
255-
{
256-
unsignedType = typeof(ushort);
257-
}
258-
else if (type == typeof(int))
259-
{
260-
unsignedType = typeof(uint);
261-
}
262-
else if (type == typeof(long))
263-
{
264-
unsignedType = typeof(ulong);
265-
}
266-
else
267-
{
268-
unsignedType = null;
269-
}
270-
return unsignedType is not null;
271-
}
272-
273-
private static bool IsUnsignedInteger(Type type)
274-
{
275-
return type == typeof(byte) || type == typeof(ushort) || type == typeof(uint) || type == typeof(ulong);
276-
}
277243
}

0 commit comments

Comments
 (0)