Skip to content

Commit b2b9a52

Browse files
committed
Merge branch 'ImGuiMod_consolidate'
2 parents 520e7fa + 5231e9c commit b2b9a52

File tree

5 files changed

+69
-25
lines changed

5 files changed

+69
-25
lines changed

src/CodeGenerator/ImguiDefinitions.cs

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using Newtonsoft.Json;
77
using Newtonsoft.Json.Linq;
8+
using System.Xml.Linq;
89

910
namespace CodeGenerator
1011
{
@@ -228,21 +229,35 @@ class EnumDefinition
228229
{
229230
private readonly Dictionary<string, string> _sanitizedNames;
230231

231-
public string Name { get; }
232-
public string FriendlyName { get; }
232+
public string[] Names { get; }
233+
public string[] FriendlyNames { get; }
233234
public EnumMember[] Members { get; }
234235

235236
public EnumDefinition(string name, EnumMember[] elements)
236237
{
237-
Name = name;
238-
if (Name.EndsWith('_'))
238+
if (TypeInfo.AlternateEnumPrefixes.TryGetValue(name, out string altName))
239239
{
240-
FriendlyName = Name.Substring(0, Name.Length - 1);
240+
Names = new[] { name, altName };
241241
}
242242
else
243243
{
244-
FriendlyName = Name;
244+
Names = new[] { name };
245+
}
246+
FriendlyNames = new string[Names.Length];
247+
for (int i = 0; i < Names.Length; i++)
248+
{
249+
string n = Names[i];
250+
if (n.EndsWith('_'))
251+
{
252+
FriendlyNames[i] = n.Substring(0, n.Length - 1);
253+
}
254+
else
255+
{
256+
FriendlyNames[i] = n;
257+
}
258+
245259
}
260+
246261
Members = elements;
247262

248263
_sanitizedNames = new Dictionary<string, string>();
@@ -265,12 +280,31 @@ public string SanitizeNames(string text)
265280
private string SanitizeMemberName(string memberName)
266281
{
267282
string ret = memberName;
268-
if (memberName.StartsWith(Name))
283+
bool altSubstitution = false;
284+
285+
// Try alternate substitution first
286+
foreach (KeyValuePair<string, string> substitutionPair in TypeInfo.AlternateEnumPrefixSubstitutions)
269287
{
270-
ret = memberName.Substring(Name.Length);
271-
if (ret.StartsWith("_"))
288+
if (memberName.StartsWith(substitutionPair.Key))
272289
{
273-
ret = ret.Substring(1);
290+
ret = ret.Replace(substitutionPair.Key, substitutionPair.Value);
291+
altSubstitution = true;
292+
break;
293+
}
294+
}
295+
296+
if (!altSubstitution)
297+
{
298+
foreach (string name in Names)
299+
{
300+
if (memberName.StartsWith(name))
301+
{
302+
ret = memberName.Substring(name.Length);
303+
if (ret.StartsWith("_"))
304+
{
305+
ret = ret.Substring(1);
306+
}
307+
}
274308
}
275309
}
276310

@@ -279,7 +313,7 @@ private string SanitizeMemberName(string memberName)
279313
ret = ret.Substring(0, ret.Length - 1);
280314
}
281315

282-
if (Char.IsDigit(ret.First()))
316+
if (char.IsDigit(ret.First()))
283317
ret = "_" + ret;
284318

285319
return ret;
@@ -375,7 +409,7 @@ public TypeReference(string name, string type, int asize, string templateType, E
375409

376410
TypeVariants = typeVariants;
377411

378-
IsEnum = enums.Any(t => t.Name == type || t.FriendlyName == type || TypeInfo.WellKnownEnums.Contains(type));
412+
IsEnum = enums.Any(t => t.Names.Contains(type) || t.FriendlyNames.Contains(type) || TypeInfo.WellKnownEnums.Contains(type));
379413
}
380414

381415
private int ParseSizeString(string sizePart, EnumDefinition[] enums)
@@ -394,7 +428,7 @@ private int ParseSizeString(string sizePart, EnumDefinition[] enums)
394428
{
395429
foreach (EnumDefinition ed in enums)
396430
{
397-
if (sizePart.StartsWith(ed.Name))
431+
if (ed.Names.Any(n => sizePart.StartsWith(n)))
398432
{
399433
foreach (EnumMember member in ed.Members)
400434
{

src/CodeGenerator/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ static void Main(string[] args)
8484

8585
foreach (EnumDefinition ed in defs.Enums)
8686
{
87-
using (CSharpCodeWriter writer = new CSharpCodeWriter(Path.Combine(outputPath, ed.FriendlyName + ".gen.cs")))
87+
using (CSharpCodeWriter writer = new CSharpCodeWriter(Path.Combine(outputPath, ed.FriendlyNames[0] + ".gen.cs")))
8888
{
8989
writer.PushBlock($"namespace {projectNamespace}");
90-
if (ed.FriendlyName.Contains("Flags"))
90+
if (ed.FriendlyNames[0].Contains("Flags"))
9191
{
9292
writer.WriteLine("[System.Flags]");
9393
}
94-
writer.PushBlock($"public enum {ed.FriendlyName}");
94+
writer.PushBlock($"public enum {ed.FriendlyNames[0]}");
9595
foreach (EnumMember member in ed.Members)
9696
{
9797
string sanitizedName = ed.SanitizeNames(member.Name);

src/CodeGenerator/TypeInfo.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,17 @@ public class TypeInfo
6262
{
6363
"ImGuiMouseButton"
6464
};
65-
65+
66+
public static readonly Dictionary<string, string> AlternateEnumPrefixes = new Dictionary<string, string>()
67+
{
68+
{ "ImGuiKey", "ImGuiMod" },
69+
};
70+
71+
public static readonly Dictionary<string, string> AlternateEnumPrefixSubstitutions = new Dictionary<string, string>()
72+
{
73+
{ "ImGuiMod_", "Mod" },
74+
};
75+
6676
public static readonly Dictionary<string, string> WellKnownFieldReplacements = new Dictionary<string, string>()
6777
{
6878
{ "bool", "bool" }, // Force bool to remain as bool in type-safe wrappers.

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ public enum ImGuiKey
144144
ReservedForModAlt = 650,
145145
ReservedForModSuper = 651,
146146
COUNT = 652,
147-
ImGuiMod_None = 0,
148-
ImGuiMod_Ctrl = 4096,
149-
ImGuiMod_Shift = 8192,
150-
ImGuiMod_Alt = 16384,
151-
ImGuiMod_Super = 32768,
152-
ImGuiMod_Mask = 61440,
153-
ImGuiMod_Shortcut = 4096,
147+
ModNone = 0,
148+
ModCtrl = 4096,
149+
ModShift = 8192,
150+
ModAlt = 16384,
151+
ModSuper = 32768,
152+
ModMask = 61440,
153+
ModShortcut = 4096,
154154
NamedKey_BEGIN = 512,
155155
NamedKey_END = 652,
156156
NamedKey_COUNT = 140,

src/ImGui.NET/ImGui.NET.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>A .NET wrapper for the Dear ImGui library.</Description>
4-
<AssemblyVersion>1.89.1</AssemblyVersion>
4+
<AssemblyVersion>1.89.2</AssemblyVersion>
55
<Authors>Eric Mellino</Authors>
66
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

0 commit comments

Comments
 (0)