From b0b72b5a8355e4cac27fb6d956cc780d6bf831f8 Mon Sep 17 00:00:00 2001 From: CallumDev Date: Tue, 25 Aug 2020 15:02:06 +0930 Subject: [PATCH] Generator: Optional locations json. Use json size instead of CSharpScript --- src/CodeGenerator/CodeGenerator.csproj | 2 - src/CodeGenerator/ImguiDefinitions.cs | 56 ++++++++++++++++---------- src/CodeGenerator/Program.cs | 1 - 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/CodeGenerator/CodeGenerator.csproj b/src/CodeGenerator/CodeGenerator.csproj index b86b10b..78613cf 100644 --- a/src/CodeGenerator/CodeGenerator.csproj +++ b/src/CodeGenerator/CodeGenerator.csproj @@ -12,8 +12,6 @@ - - diff --git a/src/CodeGenerator/ImguiDefinitions.cs b/src/CodeGenerator/ImguiDefinitions.cs index ac5409f..3d4ac96 100644 --- a/src/CodeGenerator/ImguiDefinitions.cs +++ b/src/CodeGenerator/ImguiDefinitions.cs @@ -2,7 +2,6 @@ using System; using System.Linq; using System.Threading.Tasks; using System.Collections.Generic; -using Microsoft.CodeAnalysis.CSharp.Scripting; using System.IO; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -15,6 +14,13 @@ namespace CodeGenerator public TypeDefinition[] Types; public FunctionDefinition[] Functions; public Dictionary Variants; + + static int GetInt(JToken token, string key) + { + var v = token[key]; + if (v == null) return 0; + return v.ToObject(); + } public void LoadFrom(string directory) { @@ -59,7 +65,7 @@ namespace CodeGenerator { JProperty jp = (JProperty)jt; string name = jp.Name; - if (typeLocations[jp.Name].Value() == "internal") { + if (typeLocations?[jp.Name]?.Value() == "internal") { return null; } EnumMember[] elements = jp.Values().Select(v => @@ -73,16 +79,18 @@ namespace CodeGenerator { JProperty jp = (JProperty)jt; string name = jp.Name; - if (typeLocations[jp.Name].Value() == "internal") { + if (typeLocations?[jp.Name]?.Value() == "internal") { return null; } TypeReference[] fields = jp.Values().Select(v => { if (v["type"].ToString().Contains("static")) { return null; } + return new TypeReference( v["name"].ToString(), v["type"].ToString(), + GetInt(v, "size"), v["template_type"]?.ToString(), Enums); }).Where(tr => tr != null).ToArray(); @@ -147,7 +155,7 @@ namespace CodeGenerator ParameterVariant matchingVariant = methodVariants?.Parameters.Where(pv => pv.Name == pName && pv.OriginalType == pType).FirstOrDefault() ?? null; if (matchingVariant != null) matchingVariant.Used = true; - parameters.Add(new TypeReference(pName, pType, Enums, matchingVariant?.VariantTypes)); + parameters.Add(new TypeReference(pName, pType, 0, Enums, matchingVariant?.VariantTypes)); } Dictionary defaultValues = new Dictionary(); @@ -305,16 +313,16 @@ namespace CodeGenerator public string[] TypeVariants { get; } public bool IsEnum { get; } - public TypeReference(string name, string type, EnumDefinition[] enums) - : this(name, type, null, enums, null) { } + public TypeReference(string name, string type, int asize, EnumDefinition[] enums) + : this(name, type, asize, null, enums, null) { } - public TypeReference(string name, string type, EnumDefinition[] enums, string[] typeVariants) - : this(name, type, null, enums, typeVariants) { } + public TypeReference(string name, string type, int asize, EnumDefinition[] enums, string[] typeVariants) + : this(name, type, asize, null, enums, typeVariants) { } - public TypeReference(string name, string type, string templateType, EnumDefinition[] enums) - : this(name, type, templateType, enums, null) { } + public TypeReference(string name, string type, int asize, string templateType, EnumDefinition[] enums) + : this(name, type, asize, templateType, enums, null) { } - public TypeReference(string name, string type, string templateType, EnumDefinition[] enums, string[] typeVariants) + public TypeReference(string name, string type, int asize, string templateType, EnumDefinition[] enums, string[] typeVariants) { Name = name; Type = type.Replace("const", string.Empty).Trim(); @@ -345,29 +353,35 @@ namespace CodeGenerator } TemplateType = templateType; + ArraySize = asize; int startBracket = name.IndexOf('['); - if (startBracket != -1) + if (startBracket != -1 && ArraySize == 0) { + //This is only for older cimgui binding jsons int endBracket = name.IndexOf(']'); string sizePart = name.Substring(startBracket + 1, endBracket - startBracket - 1); ArraySize = ParseSizeString(sizePart, enums); Name = Name.Substring(0, startBracket); } - IsFunctionPointer = Type.IndexOf('(') != -1; - + TypeVariants = typeVariants; IsEnum = enums.Any(t => t.Name == type || t.FriendlyName == type); } - - private int ParseSizeString(string sizePart, EnumDefinition[] enums) - { - if (sizePart.Contains("(") || sizePart.Contains("+") || sizePart.Contains("/") || - sizePart.Contains("*")) + + private int ParseSizeString(string sizePart, EnumDefinition[] enums) + { + int plusStart = sizePart.IndexOf('+'); + if (plusStart != -1) { - return Task.WaitAny(CSharpScript.EvaluateAsync($"(int)({sizePart})")); + string first = sizePart.Substring(0, plusStart); + string second = sizePart.Substring(plusStart, sizePart.Length - plusStart); + int firstVal = int.Parse(first); + int secondVal = int.Parse(second); + return firstVal + secondVal; } + if (!int.TryParse(sizePart, out int ret)) { foreach (EnumDefinition ed in enums) @@ -393,7 +407,7 @@ namespace CodeGenerator public TypeReference WithVariant(int variantIndex, EnumDefinition[] enums) { if (variantIndex == 0) return this; - else return new TypeReference(Name, TypeVariants[variantIndex - 1], TemplateType, enums); + else return new TypeReference(Name, TypeVariants[variantIndex - 1], ArraySize, TemplateType, enums); } } diff --git a/src/CodeGenerator/Program.cs b/src/CodeGenerator/Program.cs index 3ccd814..ba3f057 100644 --- a/src/CodeGenerator/Program.cs +++ b/src/CodeGenerator/Program.cs @@ -8,7 +8,6 @@ using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; -using Microsoft.CodeAnalysis.CSharp.Scripting; namespace CodeGenerator {