Skip to content

Commit 88836f5

Browse files
committed
Merge remote-tracking branch 'giawa/feature/type-variants-sep-json' into feature/type-variants-sep-json
2 parents 84ec6ed + d62cb5f commit 88836f5

File tree

6 files changed

+238
-8
lines changed

6 files changed

+238
-8
lines changed

src/CodeGenerator/CodeGenerator.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55
<TargetFramework>netcoreapp2.1</TargetFramework>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<None Remove="variants.json" />
10+
</ItemGroup>
11+
812
<ItemGroup>
913
<Content Include="structs_and_enums.json" CopyToOutputDirectory="PreserveNewest" />
1014
<Content Include="definitions.json" CopyToOutputDirectory="PreserveNewest" />
15+
<Content Include="variants.json">
16+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
17+
</Content>
1118
</ItemGroup>
1219

1320
<ItemGroup>

src/CodeGenerator/Program.cs

Lines changed: 148 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,27 @@ static void Main(string[] args)
130130
functionsJson = JObject.Load(jr);
131131
}
132132

133+
JObject variantsJson = null;
134+
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "variants.json")))
135+
{
136+
using (StreamReader fs = File.OpenText(Path.Combine(AppContext.BaseDirectory, "variants.json")))
137+
using (JsonTextReader jr = new JsonTextReader(fs))
138+
{
139+
variantsJson = JObject.Load(jr);
140+
}
141+
}
142+
143+
Dictionary<string, MethodVariant> variants = new Dictionary<string, MethodVariant>();
144+
foreach (var jt in variantsJson.Children())
145+
{
146+
JProperty jp = (JProperty)jt;
147+
ParameterVariant[] methodVariants = jp.Values().Select(jv =>
148+
{
149+
return new ParameterVariant(jv["name"].ToString(), jv["type"].ToString(), jv["variants"].Select(s => s.ToString()).ToArray());
150+
}).ToArray();
151+
variants.Add(jp.Name, new MethodVariant(jp.Name, methodVariants));
152+
}
153+
133154
EnumDefinition[] enums = typesJson["enums"].Select(jt =>
134155
{
135156
JProperty jp = (JProperty)jt;
@@ -195,11 +216,20 @@ static void Main(string[] args)
195216

196217
List<TypeReference> parameters = new List<TypeReference>();
197218

219+
// find any variants that can be applied to the parameters of this method based on the method name
220+
MethodVariant methodVariants = null;
221+
variants.TryGetValue(jp.Name, out methodVariants);
222+
198223
foreach (JToken p in val["argsT"])
199224
{
200225
string pType = p["type"].ToString();
201226
string pName = p["name"].ToString();
202-
parameters.Add(new TypeReference(pName, pType, enums));
227+
228+
// if there are possible variants for this method then try to match them based on the parameter name and expected type
229+
ParameterVariant matchingVariant = methodVariants?.Parameters.Where(pv => pv.Name == pName && pv.OriginalType == pType).FirstOrDefault() ?? null;
230+
if (matchingVariant != null) matchingVariant.Used = true;
231+
232+
parameters.Add(new TypeReference(pName, pType, enums, matchingVariant?.VariantTypes));
203233
}
204234

205235
Dictionary<string, string> defaultValues = new Dictionary<string, string>();
@@ -231,7 +261,7 @@ static void Main(string[] args)
231261
isDestructor);
232262
}).Where(od => od != null).ToArray();
233263

234-
return new FunctionDefinition(name, overloads);
264+
return new FunctionDefinition(name, overloads, enums);
235265
}).OrderBy(fd => fd.Name).ToArray();
236266

237267
foreach (EnumDefinition ed in enums)
@@ -545,6 +575,14 @@ static void Main(string[] args)
545575
writer.PopBlock();
546576
writer.PopBlock();
547577
}
578+
579+
foreach (var method in variants)
580+
{
581+
foreach (var variant in method.Value.Parameters)
582+
{
583+
if (!variant.Used) Console.WriteLine($"Error: Variants targetting parameter {variant.Name} with type {variant.OriginalType} could not be applied to method {method.Key}.");
584+
}
585+
}
548586
}
549587

550588
private static bool IsStringFieldName(string name)
@@ -963,6 +1001,38 @@ private static string CorrectIdentifier(string identifier)
9631001
}
9641002
}
9651003

1004+
class MethodVariant
1005+
{
1006+
public string Name { get; }
1007+
1008+
public ParameterVariant[] Parameters { get; }
1009+
1010+
public MethodVariant(string name, ParameterVariant[] parameters)
1011+
{
1012+
Name = name;
1013+
Parameters = parameters;
1014+
}
1015+
}
1016+
1017+
class ParameterVariant
1018+
{
1019+
public string Name { get; }
1020+
1021+
public string OriginalType { get; }
1022+
1023+
public string[] VariantTypes { get; }
1024+
1025+
public bool Used { get; set; }
1026+
1027+
public ParameterVariant(string name, string originalType, string[] variantTypes)
1028+
{
1029+
Name = name;
1030+
OriginalType = originalType;
1031+
VariantTypes = variantTypes;
1032+
Used = false;
1033+
}
1034+
}
1035+
9661036
class EnumDefinition
9671037
{
9681038
private readonly Dictionary<string, string> _sanitizedNames;
@@ -1049,11 +1119,18 @@ class TypeReference
10491119
public string TemplateType { get; }
10501120
public int ArraySize { get; }
10511121
public bool IsFunctionPointer { get; }
1122+
public string[] TypeVariants { get; }
10521123

10531124
public TypeReference(string name, string type, EnumDefinition[] enums)
1054-
: this(name, type, null, enums) { }
1125+
: this(name, type, null, enums, null) { }
1126+
1127+
public TypeReference(string name, string type, EnumDefinition[] enums, string[] typeVariants)
1128+
: this(name, type, null, enums, typeVariants) { }
10551129

10561130
public TypeReference(string name, string type, string templateType, EnumDefinition[] enums)
1131+
: this(name, type, templateType, enums, null) { }
1132+
1133+
public TypeReference(string name, string type, string templateType, EnumDefinition[] enums, string[] typeVariants)
10571134
{
10581135
Name = name;
10591136
Type = type.Replace("const", string.Empty).Trim();
@@ -1082,6 +1159,8 @@ public TypeReference(string name, string type, string templateType, EnumDefiniti
10821159
}
10831160

10841161
IsFunctionPointer = Type.IndexOf('(') != -1;
1162+
1163+
TypeVariants = typeVariants;
10851164
}
10861165

10871166
private int ParseSizeString(string sizePart, EnumDefinition[] enums)
@@ -1117,17 +1196,76 @@ private int ParseSizeString(string sizePart, EnumDefinition[] enums)
11171196

11181197
return ret;
11191198
}
1199+
1200+
public TypeReference WithVariant(int variantIndex, EnumDefinition[] enums)
1201+
{
1202+
if (variantIndex == 0) return this;
1203+
else return new TypeReference(Name, TypeVariants[variantIndex - 1], TemplateType, enums);
1204+
}
11201205
}
11211206

11221207
class FunctionDefinition
11231208
{
11241209
public string Name { get; }
11251210
public OverloadDefinition[] Overloads { get; }
11261211

1127-
public FunctionDefinition(string name, OverloadDefinition[] overloads)
1212+
public FunctionDefinition(string name, OverloadDefinition[] overloads, EnumDefinition[] enums)
11281213
{
11291214
Name = name;
1130-
Overloads = overloads;
1215+
Overloads = ExpandOverloadVariants(overloads, enums);
1216+
}
1217+
1218+
private OverloadDefinition[] ExpandOverloadVariants(OverloadDefinition[] overloads, EnumDefinition[] enums)
1219+
{
1220+
List<OverloadDefinition> newDefinitions = new List<OverloadDefinition>();
1221+
1222+
foreach (OverloadDefinition overload in overloads)
1223+
{
1224+
bool hasVariants = false;
1225+
int[] variantCounts = new int[overload.Parameters.Length];
1226+
1227+
for (int i = 0; i < overload.Parameters.Length; i++)
1228+
{
1229+
if (overload.Parameters[i].TypeVariants != null)
1230+
{
1231+
hasVariants = true;
1232+
variantCounts[i] = overload.Parameters[i].TypeVariants.Length + 1;
1233+
}
1234+
else
1235+
{
1236+
variantCounts[i] = 1;
1237+
}
1238+
}
1239+
1240+
if (hasVariants)
1241+
{
1242+
int totalVariants = variantCounts[0];
1243+
for (int i = 1; i < variantCounts.Length; i++) totalVariants *= variantCounts[i];
1244+
1245+
for (int i = 0; i < totalVariants; i++)
1246+
{
1247+
TypeReference[] parameters = new TypeReference[overload.Parameters.Length];
1248+
int div = 1;
1249+
1250+
for (int j = 0; j < parameters.Length; j++)
1251+
{
1252+
int k = (i / div) % variantCounts[j];
1253+
1254+
parameters[j] = overload.Parameters[j].WithVariant(k, enums);
1255+
1256+
if (j > 0) div *= variantCounts[j];
1257+
}
1258+
1259+
newDefinitions.Add(overload.WithParameters(parameters));
1260+
}
1261+
}
1262+
else
1263+
{
1264+
newDefinitions.Add(overload);
1265+
}
1266+
}
1267+
1268+
return newDefinitions.ToArray();
11311269
}
11321270
}
11331271

@@ -1166,6 +1304,11 @@ public OverloadDefinition(
11661304
IsConstructor = isConstructor;
11671305
IsDestructor = isDestructor;
11681306
}
1307+
1308+
public OverloadDefinition WithParameters(TypeReference[] parameters)
1309+
{
1310+
return new OverloadDefinition(ExportedName, FriendlyName, parameters, DefaultValues, ReturnType, StructName, Comment, IsConstructor, IsDestructor);
1311+
}
11691312
}
11701313

11711314
class MarshalledParameter

src/CodeGenerator/variants.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"ImFontAtlas_GetTexDataAsAlpha8": [
3+
{
4+
"name": "out_pixels",
5+
"type": "unsigned char**",
6+
"variants": [ "IntPtr*" ]
7+
}
8+
],
9+
"ImFontAtlas_GetTexDataAsRGBA32": [
10+
{
11+
"name": "out_pixels",
12+
"type": "unsigned char**",
13+
"variants": [ "IntPtr*" ]
14+
}
15+
]
16+
}

src/ImGui.NET.SampleProgram/ImGuiController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ private byte[] GetEmbeddedResourceBytes(string resourceName)
244244
/// <summary>
245245
/// Recreates the device texture used to render text.
246246
/// </summary>
247-
public unsafe void RecreateFontDeviceTexture(GraphicsDevice gd)
247+
public void RecreateFontDeviceTexture(GraphicsDevice gd)
248248
{
249249
ImGuiIOPtr io = ImGui.GetIO();
250250
// Build
251-
byte* pixels;
251+
IntPtr pixels;
252252
int width, height, bytesPerPixel;
253253
io.Fonts.GetTexDataAsRGBA32(out pixels, out width, out height, out bytesPerPixel);
254254
// Store our identifier
@@ -264,7 +264,7 @@ public unsafe void RecreateFontDeviceTexture(GraphicsDevice gd)
264264
_fontTexture.Name = "ImGui.NET Font Texture";
265265
gd.UpdateTexture(
266266
_fontTexture,
267-
(IntPtr)pixels,
267+
pixels,
268268
(uint)(bytesPerPixel * width * height),
269269
0,
270270
0,

src/ImGui.NET/Generated/ImFontAtlas.gen.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,36 @@ public void GetTexDataAsAlpha8(out byte* out_pixels, out int out_width, out int
434434
}
435435
}
436436
}
437+
public void GetTexDataAsAlpha8(out IntPtr out_pixels, out int out_width, out int out_height)
438+
{
439+
int* out_bytes_per_pixel = null;
440+
fixed (IntPtr* native_out_pixels = &out_pixels)
441+
{
442+
fixed (int* native_out_width = &out_width)
443+
{
444+
fixed (int* native_out_height = &out_height)
445+
{
446+
ImGuiNative.ImFontAtlas_GetTexDataAsAlpha8(NativePtr, native_out_pixels, native_out_width, native_out_height, out_bytes_per_pixel);
447+
}
448+
}
449+
}
450+
}
451+
public void GetTexDataAsAlpha8(out IntPtr out_pixels, out int out_width, out int out_height, out int out_bytes_per_pixel)
452+
{
453+
fixed (IntPtr* native_out_pixels = &out_pixels)
454+
{
455+
fixed (int* native_out_width = &out_width)
456+
{
457+
fixed (int* native_out_height = &out_height)
458+
{
459+
fixed (int* native_out_bytes_per_pixel = &out_bytes_per_pixel)
460+
{
461+
ImGuiNative.ImFontAtlas_GetTexDataAsAlpha8(NativePtr, native_out_pixels, native_out_width, native_out_height, native_out_bytes_per_pixel);
462+
}
463+
}
464+
}
465+
}
466+
}
437467
public void GetTexDataAsRGBA32(out byte* out_pixels, out int out_width, out int out_height)
438468
{
439469
int* out_bytes_per_pixel = null;
@@ -464,6 +494,36 @@ public void GetTexDataAsRGBA32(out byte* out_pixels, out int out_width, out int
464494
}
465495
}
466496
}
497+
public void GetTexDataAsRGBA32(out IntPtr out_pixels, out int out_width, out int out_height)
498+
{
499+
int* out_bytes_per_pixel = null;
500+
fixed (IntPtr* native_out_pixels = &out_pixels)
501+
{
502+
fixed (int* native_out_width = &out_width)
503+
{
504+
fixed (int* native_out_height = &out_height)
505+
{
506+
ImGuiNative.ImFontAtlas_GetTexDataAsRGBA32(NativePtr, native_out_pixels, native_out_width, native_out_height, out_bytes_per_pixel);
507+
}
508+
}
509+
}
510+
}
511+
public void GetTexDataAsRGBA32(out IntPtr out_pixels, out int out_width, out int out_height, out int out_bytes_per_pixel)
512+
{
513+
fixed (IntPtr* native_out_pixels = &out_pixels)
514+
{
515+
fixed (int* native_out_width = &out_width)
516+
{
517+
fixed (int* native_out_height = &out_height)
518+
{
519+
fixed (int* native_out_bytes_per_pixel = &out_bytes_per_pixel)
520+
{
521+
ImGuiNative.ImFontAtlas_GetTexDataAsRGBA32(NativePtr, native_out_pixels, native_out_width, native_out_height, native_out_bytes_per_pixel);
522+
}
523+
}
524+
}
525+
}
526+
}
467527
public bool IsBuilt()
468528
{
469529
byte ret = ImGuiNative.ImFontAtlas_IsBuilt(NativePtr);

src/ImGui.NET/Generated/ImGuiNative.gen.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,8 +909,12 @@ public static unsafe partial class ImGuiNative
909909
[DllImport("cimgui", CallingConvention = CallingConvention.Cdecl)]
910910
public static extern void ImFontAtlas_GetTexDataAsAlpha8(ImFontAtlas* self, byte** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel);
911911
[DllImport("cimgui", CallingConvention = CallingConvention.Cdecl)]
912+
public static extern void ImFontAtlas_GetTexDataAsAlpha8(ImFontAtlas* self, IntPtr* out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel);
913+
[DllImport("cimgui", CallingConvention = CallingConvention.Cdecl)]
912914
public static extern void ImFontAtlas_GetTexDataAsRGBA32(ImFontAtlas* self, byte** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel);
913915
[DllImport("cimgui", CallingConvention = CallingConvention.Cdecl)]
916+
public static extern void ImFontAtlas_GetTexDataAsRGBA32(ImFontAtlas* self, IntPtr* out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel);
917+
[DllImport("cimgui", CallingConvention = CallingConvention.Cdecl)]
914918
public static extern ImFontAtlas* ImFontAtlas_ImFontAtlas();
915919
[DllImport("cimgui", CallingConvention = CallingConvention.Cdecl)]
916920
public static extern byte ImFontAtlas_IsBuilt(ImFontAtlas* self);

0 commit comments

Comments
 (0)