-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathProgram.cs
954 lines (838 loc) · 43.1 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.CommandLine;
namespace CodeGenerator
{
internal static class Program
{
private const string InternalNamespace = ".Internal";
static async Task<int> Main(string[] args)
{
// internal vars for command line results used by the rest of the program.
bool runApp = false;
string outputPath = string.Empty;
string libraryName = string.Empty;
bool useInternals = false;
#region Command line handler
var optionOutputPath = new Option<DirectoryInfo>(
aliases: new[] { "--outputDir", "-o" },
description: "The directory to place generated code files.",
parseArgument: result =>
{
if (result.Tokens.Count == 0)
return new DirectoryInfo(AppContext.BaseDirectory);
string value = result.Tokens.Single().Value;
try { return Directory.CreateDirectory(value); }
catch (Exception) { result.ErrorMessage = $"Unable to create directory: {value}"; return null; }
},
isDefault: true);
var optionLibraryname = new Option<string>(
aliases: new[] { "--library", "-l" },
description: "The library to read parse.",
getDefaultValue: () => "cimgui")
.FromAmong("cimgui", "cimplot", "cimnodes", "cimguizmo");
var optionInternal = new Option<bool>(
name: "--internal",
description: "When set to true, includes the internal header file.",
parseArgument: result =>
{
// Using parse with isDefault: false, instead of the normal validation, allows us to use "--internal" without specifying true to mean true.
if (result.Tokens.Count == 0)
return true;
if (bool.TryParse(result.Tokens.Single().Value, out var value))
return value;
result.ErrorMessage = "Invalid option for --internal. Value must be true or false.";
return false; // ignored because of error message.
},
isDefault: false);
var rootCommand = new RootCommand("Generates code for the ImGui.NET libraries based on the cimgui definition files.");
rootCommand.AddOption(optionInternal);
rootCommand.AddOption(optionOutputPath);
rootCommand.AddOption(optionLibraryname);
rootCommand.SetHandler((outputPathValue, libNameValue, useInternalValue) =>
{
outputPath = outputPathValue.FullName;
libraryName = libNameValue;
useInternals = useInternalValue;
runApp = true;
}, optionOutputPath, optionLibraryname, optionInternal);
var commandResult = await rootCommand.InvokeAsync(args);
if (!runApp)
return commandResult;
#endregion
string projectNamespace = libraryName switch
{
"cimgui" => "ImGuiNET",
"cimplot" => "ImPlotNET",
"cimnodes" => "imnodesNET",
"cimguizmo" => "ImGuizmoNET",
_ => throw new NotImplementedException($"Library \"{libraryName}\" is not supported.")
};
bool referencesImGui = libraryName switch
{
"cimgui" => false,
"cimplot" => true,
"cimnodes" => true,
"cimguizmo" => true,
_ => throw new NotImplementedException($"Library \"{libraryName}\" is not supported.")
};
string classPrefix = libraryName switch
{
"cimgui" => "ImGui",
"cimplot" => "ImPlot",
"cimnodes" => "imnodes",
"cimguizmo" => "ImGuizmo",
_ => throw new NotImplementedException($"Library \"{libraryName}\" is not supported.")
};
string dllName = libraryName switch
{
"cimgui" => "cimgui",
"cimplot" => "cimplot",
"cimnodes" => "cimnodes",
"cimguizmo" => "cimguizmo",
_ => throw new NotImplementedException()
};
string definitionsPath = Path.Combine(AppContext.BaseDirectory, "definitions", libraryName);
var defs = new ImguiDefinitions();
defs.LoadFrom(definitionsPath, useInternals);
Console.WriteLine($"Outputting generated code files to {outputPath}.");
foreach (EnumDefinition ed in defs.Enums)
{
using (CSharpCodeWriter writer = new CSharpCodeWriter(Path.Combine(outputPath, ed.FriendlyName + ".gen.cs")))
{
writer.PushBlock($"namespace {projectNamespace}");
if (ed.FriendlyName.Contains("Flags"))
{
writer.WriteLine("[System.Flags]");
}
writer.PushBlock($"public enum {ed.FriendlyName}");
foreach (EnumMember member in ed.Members)
{
string sanitizedName = ed.SanitizeNames(member.Name);
string sanitizedValue = ed.SanitizeNames(member.Value);
writer.WriteLine($"{sanitizedName} = {sanitizedValue},");
}
writer.PopBlock();
writer.PopBlock();
}
}
foreach (TypeDefinition td in defs.Types)
{
if (TypeInfo.CustomDefinedTypes.Contains(td.Name)) { continue; }
using (CSharpCodeWriter writer = new CSharpCodeWriter(Path.Combine(outputPath, td.Name + ".gen.cs")))
{
writer.Using("System");
writer.Using("System.Numerics");
writer.Using("System.Runtime.CompilerServices");
writer.Using("System.Text");
if (referencesImGui)
{
writer.Using("ImGuiNET");
}
writer.WriteLine(string.Empty);
writer.PushBlock($"namespace {projectNamespace}{(td.IsInternal ? InternalNamespace : string.Empty)}");
writer.PushBlock($"public unsafe partial struct {td.Name}");
foreach (TypeReference field in td.Fields)
{
string typeStr = GetTypeString(field.Type, field.IsFunctionPointer);
if (field.ArraySize != 0)
{
if (TypeInfo.LegalFixedTypes.Contains(typeStr))
{
writer.WriteLine($"public fixed {typeStr} {field.Name}[{field.ArraySize}];");
}
else
{
for (int i = 0; i < field.ArraySize; i++)
{
writer.WriteLine($"public {typeStr} {field.Name}_{i};");
}
}
}
else
{
writer.WriteLine($"public {typeStr} {field.Name};");
}
}
writer.PopBlock();
string ptrTypeName = td.Name + "Ptr";
writer.PushBlock($"public unsafe partial struct {ptrTypeName}");
writer.WriteLine($"public {td.Name}* NativePtr {{ get; }}");
writer.WriteLine($"public {ptrTypeName}({td.Name}* nativePtr) => NativePtr = nativePtr;");
writer.WriteLine($"public {ptrTypeName}(IntPtr nativePtr) => NativePtr = ({td.Name}*)nativePtr;");
writer.WriteLine($"public static implicit operator {ptrTypeName}({td.Name}* nativePtr) => new {ptrTypeName}(nativePtr);");
writer.WriteLine($"public static implicit operator {td.Name}* ({ptrTypeName} wrappedPtr) => wrappedPtr.NativePtr;");
writer.WriteLine($"public static implicit operator {ptrTypeName}(IntPtr nativePtr) => new {ptrTypeName}(nativePtr);");
foreach (TypeReference field in td.Fields)
{
string typeStr = GetTypeString(field.Type, field.IsFunctionPointer);
string rawType = typeStr;
if (TypeInfo.WellKnownFieldReplacements.TryGetValue(field.Type, out string wellKnownFieldType))
{
typeStr = wellKnownFieldType;
}
if (field.ArraySize != 0)
{
string addrTarget = TypeInfo.LegalFixedTypes.Contains(rawType) ? $"NativePtr->{field.Name}" : $"&NativePtr->{field.Name}_0";
writer.WriteLine($"public RangeAccessor<{typeStr}> {field.Name} => new RangeAccessor<{typeStr}>({addrTarget}, {field.ArraySize});");
}
else if (typeStr.Contains("ImVector"))
{
string vectorElementType = GetTypeString(field.TemplateType, false);
if (TypeInfo.WellKnownTypes.TryGetValue(vectorElementType, out string wellKnown))
{
vectorElementType = wellKnown;
}
if (GetWrappedType(vectorElementType + "*", out string wrappedElementType))
{
writer.WriteLine($"public ImPtrVector<{wrappedElementType}> {field.Name} => new ImPtrVector<{wrappedElementType}>(NativePtr->{field.Name}, Unsafe.SizeOf<{vectorElementType}>());");
}
else
{
if (GetWrappedType(vectorElementType, out wrappedElementType))
{
vectorElementType = wrappedElementType;
}
writer.WriteLine($"public ImVector<{vectorElementType}> {field.Name} => new ImVector<{vectorElementType}>(NativePtr->{field.Name});");
}
}
else
{
if (typeStr.Contains("*") && !typeStr.Contains("ImVector"))
{
if (GetWrappedType(typeStr, out string wrappedTypeName))
{
writer.WriteLine($"public {wrappedTypeName} {field.Name} => new {wrappedTypeName}(NativePtr->{field.Name});");
}
else if (typeStr == "byte*" && IsStringFieldName(field.Name))
{
writer.WriteLine($"public NullTerminatedString {field.Name} => new NullTerminatedString(NativePtr->{field.Name});");
}
else
{
writer.WriteLine($"public IntPtr {field.Name} {{ get => (IntPtr)NativePtr->{field.Name}; set => NativePtr->{field.Name} = ({typeStr})value; }}");
}
}
else
{
writer.WriteLine($"public ref {typeStr} {field.Name} => ref Unsafe.AsRef<{typeStr}>(&NativePtr->{field.Name});");
}
}
}
foreach (FunctionDefinition fd in defs.Functions)
{
foreach (OverloadDefinition overload in fd.Overloads)
{
if (overload.StructName != td.Name)
{
continue;
}
if (overload.IsConstructor)
{
// TODO: Emit a static function on the type that invokes the native constructor.
// Also, add a "Dispose" function or similar.
continue;
}
string exportedName = overload.ExportedName;
if (exportedName.StartsWith("ig"))
{
exportedName = exportedName.Substring(2, exportedName.Length - 2);
}
if (exportedName.Contains("~")) { continue; }
if (overload.Parameters.Any(tr => tr.Type.Contains('('))) { continue; } // TODO: Parse function pointer parameters.
bool hasVaList = false;
for (int i = 0; i < overload.Parameters.Length; i++)
{
TypeReference p = overload.Parameters[i];
string paramType = GetTypeString(p.Type, p.IsFunctionPointer);
if (p.Name == "...") { continue; }
if (paramType == "va_list")
{
hasVaList = true;
break;
}
}
if (hasVaList) { continue; }
KeyValuePair<string, string>[] orderedDefaults = overload.DefaultValues.OrderByDescending(
kvp => GetIndex(overload.Parameters, kvp.Key)).ToArray();
for (int i = overload.DefaultValues.Count; i >= 0; i--)
{
Dictionary<string, string> defaults = new Dictionary<string, string>();
for (int j = 0; j < i; j++)
{
defaults.Add(orderedDefaults[j].Key, orderedDefaults[j].Value);
}
EmitOverload(writer, overload, defaults, "NativePtr", classPrefix);
}
}
}
writer.PopBlock();
writer.PopBlock();
}
}
using (CSharpCodeWriter writer = new CSharpCodeWriter(Path.Combine(outputPath, $"{classPrefix}Native.gen.cs")))
{
writer.Using("System");
writer.Using("System.Numerics");
writer.Using("System.Runtime.InteropServices");
if (referencesImGui)
{
writer.Using("ImGuiNET");
}
if (useInternals)
{
writer.Using($"{projectNamespace}{InternalNamespace}");
}
writer.WriteLine(string.Empty);
writer.PushBlock($"namespace {projectNamespace}");
writer.PushBlock($"public static unsafe partial class {classPrefix}Native");
foreach (FunctionDefinition fd in defs.Functions)
{
foreach (OverloadDefinition overload in fd.Overloads)
{
string exportedName = overload.ExportedName;
if (exportedName.Contains("~")) { continue; }
if (exportedName.Contains("ImVector_")) { continue; }
if (exportedName.Contains("ImChunkStream_")) { continue; }
if (overload.Parameters.Any(tr => tr.Type.Contains('('))) { continue; } // TODO: Parse function pointer parameters.
string ret = GetTypeString(overload.ReturnType, false);
bool hasVaList = false;
List<string> paramParts = new List<string>();
for (int i = 0; i < overload.Parameters.Length; i++)
{
TypeReference p = overload.Parameters[i];
string paramType = GetTypeString(p.Type, p.IsFunctionPointer);
if (p.ArraySize != 0)
{
paramType = paramType + "*";
}
if (p.Name == "...") { continue; }
paramParts.Add($"{paramType} {CorrectIdentifier(p.Name)}");
if (paramType == "va_list")
{
hasVaList = true;
break;
}
}
if (hasVaList) { continue; }
string parameters = string.Join(", ", paramParts);
bool isUdtVariant = exportedName.Contains("nonUDT");
string methodName = isUdtVariant
? exportedName.Substring(0, exportedName.IndexOf("_nonUDT"))
: exportedName;
if (isUdtVariant)
{
writer.WriteLine($"[DllImport(\"{dllName}\", CallingConvention = CallingConvention.Cdecl, EntryPoint = \"{exportedName}\")]");
}
else
{
writer.WriteLine($"[DllImport(\"{dllName}\", CallingConvention = CallingConvention.Cdecl)]");
}
writer.WriteLine($"public static extern {ret} {methodName}({parameters});");
}
}
writer.PopBlock();
writer.PopBlock();
}
// Root ImGui* class items - Noninternal
using (CSharpCodeWriter writer = new CSharpCodeWriter(Path.Combine(outputPath, $"{classPrefix}.gen.cs")))
{
writer.Using("System");
writer.Using("System.Numerics");
writer.Using("System.Runtime.InteropServices");
writer.Using("System.Text");
if (referencesImGui)
{
writer.Using("ImGuiNET");
}
writer.WriteLine(string.Empty);
writer.PushBlock($"namespace {projectNamespace}");
writer.PushBlock($"public static unsafe partial class {classPrefix}");
EmitFunctions(false);
writer.PopBlock();
writer.PopBlock();
if (useInternals)
{
writer.PushBlock($"namespace {projectNamespace}{InternalNamespace}");
writer.PushBlock($"public static unsafe partial class {classPrefix}");
EmitFunctions(true);
writer.PopBlock();
writer.PopBlock();
}
void EmitFunctions(bool isInternal)
{
foreach (FunctionDefinition fd in defs.Functions)
{
if (TypeInfo.SkippedFunctions.Contains(fd.Name)) { continue; }
foreach (OverloadDefinition overload in fd.Overloads.Where(o => !o.IsMemberFunction && o.IsInternal == isInternal))
{
string exportedName = overload.ExportedName;
if (exportedName.StartsWith("ig"))
{
exportedName = exportedName.Substring(2, exportedName.Length - 2);
}
if (exportedName.Contains("~")) { continue; }
if (overload.Parameters.Any(tr => tr.Type.Contains('('))) { continue; } // TODO: Parse function pointer parameters.
bool hasVaList = false;
for (int i = 0; i < overload.Parameters.Length; i++)
{
TypeReference p = overload.Parameters[i];
string paramType = GetTypeString(p.Type, p.IsFunctionPointer);
if (p.Name == "...") { continue; }
if (paramType == "va_list")
{
hasVaList = true;
break;
}
}
if (hasVaList) { continue; }
KeyValuePair<string, string>[] orderedDefaults = overload.DefaultValues.OrderByDescending(
kvp => GetIndex(overload.Parameters, kvp.Key)).ToArray();
for (int i = overload.DefaultValues.Count; i >= 0; i--)
{
Dictionary<string, string> defaults = new Dictionary<string, string>();
for (int j = 0; j < i; j++)
{
defaults.Add(orderedDefaults[j].Key, orderedDefaults[j].Value);
}
EmitOverload(writer, overload, defaults, null, classPrefix);
}
}
}
}
}
foreach (var method in defs.Variants)
{
foreach (var variant in method.Value.Parameters)
{
if (!variant.Used) Console.WriteLine($"Error: Variants targetting parameter {variant.Name} with type {variant.OriginalType} could not be applied to method {method.Key}.");
}
}
return 0;
}
private static bool IsStringFieldName(string name)
{
return Regex.IsMatch(name, ".*Filename.*")
|| Regex.IsMatch(name, ".*Name");
}
private static string GetImVectorElementType(string typeStr)
{
int start = typeStr.IndexOf('<') + 1;
int end = typeStr.IndexOf('>');
int length = end - start;
return typeStr.Substring(start, length);
}
private static int GetIndex(TypeReference[] parameters, string key)
{
for (int i = 0; i < parameters.Length; i++)
{
if (key == parameters[i].Name) { return i; }
}
throw new InvalidOperationException();
}
private static void EmitOverload(
CSharpCodeWriter writer,
OverloadDefinition overload,
Dictionary<string, string> defaultValues,
string selfName,
string classPrefix)
{
if (overload.Parameters.Where(tr => tr.Name.EndsWith("_begin") || tr.Name.EndsWith("_end"))
.Any(tr => !defaultValues.ContainsKey(tr.Name)))
{
return;
}
Debug.Assert(!overload.IsMemberFunction || selfName != null);
string nativeRet = GetTypeString(overload.ReturnType, false);
bool isWrappedType = GetWrappedType(nativeRet, out string safeRet);
if (!isWrappedType)
{
safeRet = GetSafeType(overload.ReturnType);
}
List<string> invocationArgs = new List<string>();
MarshalledParameter[] marshalledParameters = new MarshalledParameter[overload.Parameters.Length];
List<string> preCallLines = new List<string>();
List<string> postCallLines = new List<string>();
List<string> byRefParams = new List<string>();
int selfIndex = -1;
int pOutIndex = -1;
string overrideRet = null;
for (int i = 0; i < overload.Parameters.Length; i++)
{
TypeReference tr = overload.Parameters[i];
if (tr.Name == "self")
{
selfIndex = i;
continue;
}
if (tr.Name == "...") { continue; }
string correctedIdentifier = CorrectIdentifier(tr.Name);
string nativeTypeName = GetTypeString(tr.Type, tr.IsFunctionPointer);
if (correctedIdentifier == "pOut" && overload.ReturnType == "void")
{
pOutIndex = i;
overrideRet = nativeTypeName.TrimEnd('*');
preCallLines.Add($"{overrideRet} __retval;");
continue;
}
if (tr.Type == "char*")
{
string textToEncode = correctedIdentifier;
bool hasDefault = false;
if (defaultValues.TryGetValue(tr.Name, out string defaultStrVal))
{
hasDefault = true;
if (!CorrectDefaultValue(defaultStrVal, tr, out string correctedDefault))
{
correctedDefault = defaultStrVal;
}
textToEncode = correctedDefault;
}
string nativeArgName = "native_" + tr.Name;
marshalledParameters[i] = new MarshalledParameter("string", false, nativeArgName, hasDefault);
if (textToEncode == "null")
{
preCallLines.Add($"byte* {nativeArgName} = null;");
}
else
{
preCallLines.Add($"byte* {nativeArgName};");
preCallLines.Add($"int {correctedIdentifier}_byteCount = 0;");
if (!hasDefault)
{
preCallLines.Add($"if ({textToEncode} != null)");
preCallLines.Add("{");
}
preCallLines.Add($" {correctedIdentifier}_byteCount = Encoding.UTF8.GetByteCount({textToEncode});");
preCallLines.Add($" if ({correctedIdentifier}_byteCount > Util.StackAllocationSizeLimit)");
preCallLines.Add($" {{");
preCallLines.Add($" {nativeArgName} = Util.Allocate({correctedIdentifier}_byteCount + 1);");
preCallLines.Add($" }}");
preCallLines.Add($" else");
preCallLines.Add($" {{");
preCallLines.Add($" byte* {nativeArgName}_stackBytes = stackalloc byte[{correctedIdentifier}_byteCount + 1];");
preCallLines.Add($" {nativeArgName} = {nativeArgName}_stackBytes;");
preCallLines.Add($" }}");
preCallLines.Add($" int {nativeArgName}_offset = Util.GetUtf8({textToEncode}, {nativeArgName}, {correctedIdentifier}_byteCount);");
preCallLines.Add($" {nativeArgName}[{nativeArgName}_offset] = 0;");
if (!hasDefault)
{
preCallLines.Add("}");
preCallLines.Add($"else {{ {nativeArgName} = null; }}");
}
postCallLines.Add($"if ({correctedIdentifier}_byteCount > Util.StackAllocationSizeLimit)");
postCallLines.Add($"{{");
postCallLines.Add($" Util.Free({nativeArgName});");
postCallLines.Add($"}}");
}
}
else if (defaultValues.TryGetValue(tr.Name, out string defaultVal))
{
if (!CorrectDefaultValue(defaultVal, tr, out string correctedDefault))
{
correctedDefault = defaultVal;
}
marshalledParameters[i] = new MarshalledParameter(nativeTypeName, false, correctedIdentifier, true);
preCallLines.Add($"{nativeTypeName} {correctedIdentifier} = {correctedDefault};");
}
else if (tr.Type == "char* []")
{
string nativeArgName = "native_" + tr.Name;
marshalledParameters[i] = new MarshalledParameter("string[]", false, nativeArgName, false);
preCallLines.Add($"int* {correctedIdentifier}_byteCounts = stackalloc int[{correctedIdentifier}.Length];");
preCallLines.Add($"int {correctedIdentifier}_byteCount = 0;");
preCallLines.Add($"for (int i = 0; i < {correctedIdentifier}.Length; i++)");
preCallLines.Add("{");
preCallLines.Add($" string s = {correctedIdentifier}[i];");
preCallLines.Add($" {correctedIdentifier}_byteCounts[i] = Encoding.UTF8.GetByteCount(s);");
preCallLines.Add($" {correctedIdentifier}_byteCount += {correctedIdentifier}_byteCounts[i] + 1;");
preCallLines.Add("}");
preCallLines.Add($"byte* {nativeArgName}_data = stackalloc byte[{correctedIdentifier}_byteCount];");
preCallLines.Add("int offset = 0;");
preCallLines.Add($"for (int i = 0; i < {correctedIdentifier}.Length; i++)");
preCallLines.Add("{");
preCallLines.Add($" string s = {correctedIdentifier}[i];");
preCallLines.Add($" fixed (char* sPtr = s)");
preCallLines.Add(" {");
preCallLines.Add($" offset += Encoding.UTF8.GetBytes(sPtr, s.Length, {nativeArgName}_data + offset, {correctedIdentifier}_byteCounts[i]);");
preCallLines.Add($" {nativeArgName}_data[offset] = 0;");
preCallLines.Add($" offset += 1;");
preCallLines.Add(" }");
preCallLines.Add("}");
preCallLines.Add($"byte** {nativeArgName} = stackalloc byte*[{correctedIdentifier}.Length];");
preCallLines.Add("offset = 0;");
preCallLines.Add($"for (int i = 0; i < {correctedIdentifier}.Length; i++)");
preCallLines.Add("{");
preCallLines.Add($" {nativeArgName}[i] = &{nativeArgName}_data[offset];");
preCallLines.Add($" offset += {correctedIdentifier}_byteCounts[i] + 1;");
preCallLines.Add("}");
}
else if (tr.Type == "bool")
{
string nativeArgName = "native_" + tr.Name;
marshalledParameters[i] = new MarshalledParameter("bool", false, nativeArgName, false);
preCallLines.Add($"byte {nativeArgName} = {tr.Name} ? (byte)1 : (byte)0;");
}
else if (tr.Type == "bool*")
{
string nativeArgName = "native_" + tr.Name;
marshalledParameters[i] = new MarshalledParameter("ref bool", false, nativeArgName, false);
preCallLines.Add($"byte {nativeArgName}_val = {correctedIdentifier} ? (byte)1 : (byte)0;");
preCallLines.Add($"byte* {nativeArgName} = &{nativeArgName}_val;");
postCallLines.Add($"{correctedIdentifier} = {nativeArgName}_val != 0;");
}
else if (tr.Type == "void*" || tr.Type == "ImWchar*")
{
string nativePtrTypeName = tr.Type == "void*" ? "void*" : "ushort*";
string nativeArgName = "native_" + tr.Name;
marshalledParameters[i] = new MarshalledParameter("IntPtr", false, nativeArgName, false);
preCallLines.Add($"{nativePtrTypeName} {nativeArgName} = ({nativePtrTypeName}){correctedIdentifier}.ToPointer();");
}
else if (GetWrappedType(tr.Type, out string wrappedParamType)
&& !TypeInfo.WellKnownTypes.ContainsKey(tr.Type)
&& !TypeInfo.WellKnownTypes.ContainsKey(tr.Type.Substring(0, tr.Type.Length - 1)))
{
marshalledParameters[i] = new MarshalledParameter(wrappedParamType, false, "native_" + tr.Name, false);
string nativeArgName = "native_" + tr.Name;
marshalledParameters[i] = new MarshalledParameter(wrappedParamType, false, nativeArgName, false);
preCallLines.Add($"{tr.Type} {nativeArgName} = {correctedIdentifier}.NativePtr;");
}
else if ((tr.Type.EndsWith("*") || tr.Type.Contains("[") || tr.Type.EndsWith("&")) && tr.Type != "void*" && tr.Type != "ImGuiContext*" && tr.Type != "ImPlotContext*"&& tr.Type != "EditorContext*")
{
string nonPtrType;
if (tr.Type.Contains("["))
{
string wellKnown = TypeInfo.WellKnownTypes[tr.Type];
nonPtrType = GetTypeString(wellKnown.Substring(0, wellKnown.Length - 1), false);
}
else
{
nonPtrType = GetTypeString(tr.Type.Substring(0, tr.Type.Length - 1), false);
}
string nativeArgName = "native_" + tr.Name;
bool isOutParam = tr.Name.Contains("out_") || tr.Name == "out";
string direction = isOutParam ? "out" : "ref";
marshalledParameters[i] = new MarshalledParameter($"{direction} {nonPtrType}", true, nativeArgName, false);
marshalledParameters[i].PinTarget = CorrectIdentifier(tr.Name);
}
else
{
marshalledParameters[i] = new MarshalledParameter(nativeTypeName, false, correctedIdentifier, false);
}
if (!marshalledParameters[i].HasDefaultValue)
{
invocationArgs.Add($"{marshalledParameters[i].MarshalledType} {correctedIdentifier}");
}
}
string invocationList = string.Join(", ", invocationArgs);
string friendlyName = overload.FriendlyName;
string staticPortion = selfName == null ? "static " : string.Empty;
writer.PushBlock($"public {staticPortion}{overrideRet ?? safeRet} {friendlyName}({invocationList})");
foreach (string line in preCallLines)
{
writer.WriteLine(line);
}
List<string> nativeInvocationArgs = new List<string>();
for (int i = 0; i < marshalledParameters.Length; i++)
{
TypeReference tr = overload.Parameters[i];
if (selfIndex == i)
{
//Some overloads seem to be generated with IntPtr as self
//instead of the proper pointer type. TODO: investigate
string tstr = GetTypeString(tr.Type, false);
nativeInvocationArgs.Add($"({tstr})({selfName})");
continue;
}
if (pOutIndex == i)
{
nativeInvocationArgs.Add("&__retval");
continue;
}
MarshalledParameter mp = marshalledParameters[i];
if (mp == null) { continue; }
if (mp.IsPinned)
{
string nativePinType = GetTypeString(tr.Type, false);
writer.PushBlock($"fixed ({nativePinType} native_{tr.Name} = &{mp.PinTarget})");
}
nativeInvocationArgs.Add(mp.VarName);
}
string nativeInvocationStr = string.Join(", ", nativeInvocationArgs);
string ret = safeRet == "void" ? string.Empty : $"{nativeRet} ret = ";
string targetName = overload.ExportedName;
if (targetName.Contains("nonUDT"))
{
targetName = targetName.Substring(0, targetName.IndexOf("_nonUDT"));
}
writer.WriteLine($"{ret}{classPrefix}Native.{targetName}({nativeInvocationStr});");
foreach (string line in postCallLines)
{
writer.WriteLine(line);
}
if (safeRet != "void")
{
if (safeRet == "bool")
{
writer.WriteLine("return ret != 0;");
}
else if (overload.ReturnType == "char*")
{
writer.WriteLine("return Util.StringFromPtr(ret);");
}
else if (overload.ReturnType == "ImWchar*")
{
writer.WriteLine("return (IntPtr)ret;");
}
else if (overload.ReturnType == "void*")
{
writer.WriteLine("return (IntPtr)ret;");
}
else
{
string retVal = isWrappedType ? $"new {safeRet}(ret)" : "ret";
writer.WriteLine($"return {retVal};");
}
}
if (overrideRet != null)
writer.WriteLine("return __retval;");
for (int i = 0; i < marshalledParameters.Length; i++)
{
MarshalledParameter mp = marshalledParameters[i];
if (mp == null) { continue; }
if (mp.IsPinned)
{
writer.PopBlock();
}
}
writer.PopBlock();
}
private static string GetSafeType(string nativeRet)
{
if (nativeRet == "bool")
{
return "bool";
}
else if (nativeRet == "char*")
{
return "string";
}
else if (nativeRet == "ImWchar*" || nativeRet == "void*")
{
return "IntPtr";
}
return GetTypeString(nativeRet, false);
}
private static string GetSafeType(TypeReference typeRef)
{
return typeRef.Type;
}
private static bool GetWrappedType(string nativeType, out string wrappedType)
{
if (nativeType.StartsWith("Im") && nativeType.EndsWith("*") && !nativeType.StartsWith("ImVector"))
{
int pointerLevel = nativeType.Length - nativeType.IndexOf('*');
if (pointerLevel > 1)
{
wrappedType = null;
return false; // TODO
}
string nonPtrType = nativeType.Substring(0, nativeType.Length - pointerLevel);
if (TypeInfo.WellKnownTypes.ContainsKey(nonPtrType))
{
wrappedType = null;
return false;
}
wrappedType = nonPtrType + "Ptr";
return true;
}
else
{
wrappedType = null;
return false;
}
}
private static bool CorrectDefaultValue(string defaultVal, TypeReference tr, out string correctedDefault)
{
if (tr.Type == "ImGuiContext*" || tr.Type == "ImPlotContext*" || tr.Type == "EditorContext*")
{
correctedDefault = "IntPtr.Zero";
return true;
}
if (TypeInfo.WellKnownDefaultValues.TryGetValue(defaultVal, out correctedDefault)) { return true; }
if (tr.Type == "bool")
{
correctedDefault = bool.Parse(defaultVal) ? "1" : "0";
return true;
}
if (defaultVal.Contains("%")) { correctedDefault = null; return false; }
if (tr.IsEnum)
{
if (defaultVal.StartsWith("-"))
{
correctedDefault = $"({tr.Type})({defaultVal})";
}
else
{
correctedDefault = $"({tr.Type}){defaultVal}";
}
return true;
}
correctedDefault = defaultVal;
return true;
}
private static string GetTypeString(string typeName, bool isFunctionPointer)
{
int pointerLevel = 0;
if (typeName.EndsWith("**")) { pointerLevel = 2; }
else if (typeName.EndsWith("*")) { pointerLevel = 1; }
if (!TypeInfo.WellKnownTypes.TryGetValue(typeName, out string typeStr))
{
if (TypeInfo.WellKnownTypes.TryGetValue(typeName.Substring(0, typeName.Length - pointerLevel), out typeStr))
{
typeStr = typeStr + new string('*', pointerLevel);
}
else if (!TypeInfo.WellKnownTypes.TryGetValue(typeName, out typeStr))
{
typeStr = typeName;
if (isFunctionPointer) { typeStr = "IntPtr"; }
}
}
return typeStr;
}
private static string CorrectIdentifier(string identifier)
{
if (TypeInfo.IdentifierReplacements.TryGetValue(identifier, out string replacement))
{
return replacement;
}
else
{
return identifier;
}
}
}
class MarshalledParameter
{
public MarshalledParameter(string marshalledType, bool isPinned, string varName, bool hasDefaultValue)
{
MarshalledType = marshalledType;
IsPinned = isPinned;
VarName = varName;
HasDefaultValue = hasDefaultValue;
}
public string MarshalledType { get; }
public bool IsPinned { get; }
public string VarName { get; }
public bool HasDefaultValue { get; }
public string PinTarget { get; internal set; }
}
}