Generator: Optional locations json. Use json size instead of CSharpScript

internals
CallumDev 4 years ago
parent 4ec0c37abc
commit b0b72b5a83
  1. 2
      src/CodeGenerator/CodeGenerator.csproj
  2. 56
      src/CodeGenerator/ImguiDefinitions.cs
  3. 1
      src/CodeGenerator/Program.cs

@ -12,8 +12,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.7.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup> </ItemGroup>

@ -2,7 +2,6 @@ using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using System.IO; using System.IO;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -15,6 +14,13 @@ namespace CodeGenerator
public TypeDefinition[] Types; public TypeDefinition[] Types;
public FunctionDefinition[] Functions; public FunctionDefinition[] Functions;
public Dictionary<string, MethodVariant> Variants; public Dictionary<string, MethodVariant> Variants;
static int GetInt(JToken token, string key)
{
var v = token[key];
if (v == null) return 0;
return v.ToObject<int>();
}
public void LoadFrom(string directory) public void LoadFrom(string directory)
{ {
@ -59,7 +65,7 @@ namespace CodeGenerator
{ {
JProperty jp = (JProperty)jt; JProperty jp = (JProperty)jt;
string name = jp.Name; string name = jp.Name;
if (typeLocations[jp.Name].Value<string>() == "internal") { if (typeLocations?[jp.Name]?.Value<string>() == "internal") {
return null; return null;
} }
EnumMember[] elements = jp.Values().Select(v => EnumMember[] elements = jp.Values().Select(v =>
@ -73,16 +79,18 @@ namespace CodeGenerator
{ {
JProperty jp = (JProperty)jt; JProperty jp = (JProperty)jt;
string name = jp.Name; string name = jp.Name;
if (typeLocations[jp.Name].Value<string>() == "internal") { if (typeLocations?[jp.Name]?.Value<string>() == "internal") {
return null; return null;
} }
TypeReference[] fields = jp.Values().Select(v => TypeReference[] fields = jp.Values().Select(v =>
{ {
if (v["type"].ToString().Contains("static")) { return null; } if (v["type"].ToString().Contains("static")) { return null; }
return new TypeReference( return new TypeReference(
v["name"].ToString(), v["name"].ToString(),
v["type"].ToString(), v["type"].ToString(),
GetInt(v, "size"),
v["template_type"]?.ToString(), v["template_type"]?.ToString(),
Enums); Enums);
}).Where(tr => tr != null).ToArray(); }).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; ParameterVariant matchingVariant = methodVariants?.Parameters.Where(pv => pv.Name == pName && pv.OriginalType == pType).FirstOrDefault() ?? null;
if (matchingVariant != null) matchingVariant.Used = true; 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<string, string> defaultValues = new Dictionary<string, string>(); Dictionary<string, string> defaultValues = new Dictionary<string, string>();
@ -305,16 +313,16 @@ namespace CodeGenerator
public string[] TypeVariants { get; } public string[] TypeVariants { get; }
public bool IsEnum { get; } public bool IsEnum { get; }
public TypeReference(string name, string type, EnumDefinition[] enums) public TypeReference(string name, string type, int asize, EnumDefinition[] enums)
: this(name, type, null, enums, null) { } : this(name, type, asize, null, enums, null) { }
public TypeReference(string name, string type, EnumDefinition[] enums, string[] typeVariants) public TypeReference(string name, string type, int asize, EnumDefinition[] enums, string[] typeVariants)
: this(name, type, null, enums, typeVariants) { } : this(name, type, asize, null, enums, typeVariants) { }
public TypeReference(string name, string type, string templateType, EnumDefinition[] enums) public TypeReference(string name, string type, int asize, string templateType, EnumDefinition[] enums)
: this(name, type, templateType, enums, null) { } : 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; Name = name;
Type = type.Replace("const", string.Empty).Trim(); Type = type.Replace("const", string.Empty).Trim();
@ -345,29 +353,35 @@ namespace CodeGenerator
} }
TemplateType = templateType; TemplateType = templateType;
ArraySize = asize;
int startBracket = name.IndexOf('['); int startBracket = name.IndexOf('[');
if (startBracket != -1) if (startBracket != -1 && ArraySize == 0)
{ {
//This is only for older cimgui binding jsons
int endBracket = name.IndexOf(']'); int endBracket = name.IndexOf(']');
string sizePart = name.Substring(startBracket + 1, endBracket - startBracket - 1); string sizePart = name.Substring(startBracket + 1, endBracket - startBracket - 1);
ArraySize = ParseSizeString(sizePart, enums); ArraySize = ParseSizeString(sizePart, enums);
Name = Name.Substring(0, startBracket); Name = Name.Substring(0, startBracket);
} }
IsFunctionPointer = Type.IndexOf('(') != -1; IsFunctionPointer = Type.IndexOf('(') != -1;
TypeVariants = typeVariants; TypeVariants = typeVariants;
IsEnum = enums.Any(t => t.Name == type || t.FriendlyName == type); IsEnum = enums.Any(t => t.Name == type || t.FriendlyName == type);
} }
private int ParseSizeString(string sizePart, EnumDefinition[] enums) private int ParseSizeString(string sizePart, EnumDefinition[] enums)
{ {
if (sizePart.Contains("(") || sizePart.Contains("+") || sizePart.Contains("/") || int plusStart = sizePart.IndexOf('+');
sizePart.Contains("*")) if (plusStart != -1)
{ {
return Task.WaitAny(CSharpScript.EvaluateAsync<int>($"(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)) if (!int.TryParse(sizePart, out int ret))
{ {
foreach (EnumDefinition ed in enums) foreach (EnumDefinition ed in enums)
@ -393,7 +407,7 @@ namespace CodeGenerator
public TypeReference WithVariant(int variantIndex, EnumDefinition[] enums) public TypeReference WithVariant(int variantIndex, EnumDefinition[] enums)
{ {
if (variantIndex == 0) return this; 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);
} }
} }

@ -8,7 +8,6 @@ using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Scripting;
namespace CodeGenerator namespace CodeGenerator
{ {

Loading…
Cancel
Save