22using System . Linq ;
33using System . Threading . Tasks ;
44using System . Collections . Generic ;
5- using Microsoft . CodeAnalysis . CSharp . Scripting ;
65using System . IO ;
76using Newtonsoft . Json ;
87using Newtonsoft . Json . Linq ;
@@ -15,6 +14,13 @@ class ImguiDefinitions
1514 public TypeDefinition [ ] Types ;
1615 public FunctionDefinition [ ] Functions ;
1716 public Dictionary < string , MethodVariant > Variants ;
17+
18+ static int GetInt ( JToken token , string key )
19+ {
20+ var v = token [ key ] ;
21+ if ( v == null ) return 0 ;
22+ return v . ToObject < int > ( ) ;
23+ }
1824 public void LoadFrom ( string directory )
1925 {
2026
@@ -59,7 +65,7 @@ public void LoadFrom(string directory)
5965 {
6066 JProperty jp = ( JProperty ) jt ;
6167 string name = jp . Name ;
62- if ( typeLocations [ jp . Name ] . Value < string > ( ) == "internal" ) {
68+ if ( typeLocations ? [ jp . Name ] ? . Value < string > ( ) == "internal" ) {
6369 return null ;
6470 }
6571 EnumMember [ ] elements = jp . Values ( ) . Select ( v =>
@@ -73,16 +79,18 @@ public void LoadFrom(string directory)
7379 {
7480 JProperty jp = ( JProperty ) jt ;
7581 string name = jp . Name ;
76- if ( typeLocations [ jp . Name ] . Value < string > ( ) == "internal" ) {
82+ if ( typeLocations ? [ jp . Name ] ? . Value < string > ( ) == "internal" ) {
7783 return null ;
7884 }
7985 TypeReference [ ] fields = jp . Values ( ) . Select ( v =>
8086 {
8187 if ( v [ "type" ] . ToString ( ) . Contains ( "static" ) ) { return null ; }
8288
89+
8390 return new TypeReference (
8491 v [ "name" ] . ToString ( ) ,
8592 v [ "type" ] . ToString ( ) ,
93+ GetInt ( v , "size" ) ,
8694 v [ "template_type" ] ? . ToString ( ) ,
8795 Enums ) ;
8896 } ) . Where ( tr => tr != null ) . ToArray ( ) ;
@@ -147,7 +155,7 @@ public void LoadFrom(string directory)
147155 ParameterVariant matchingVariant = methodVariants ? . Parameters . Where ( pv => pv . Name == pName && pv . OriginalType == pType ) . FirstOrDefault ( ) ?? null ;
148156 if ( matchingVariant != null ) matchingVariant . Used = true ;
149157
150- parameters . Add ( new TypeReference ( pName , pType , Enums , matchingVariant ? . VariantTypes ) ) ;
158+ parameters . Add ( new TypeReference ( pName , pType , 0 , Enums , matchingVariant ? . VariantTypes ) ) ;
151159 }
152160
153161 Dictionary < string , string > defaultValues = new Dictionary < string , string > ( ) ;
@@ -305,16 +313,16 @@ class TypeReference
305313 public string [ ] TypeVariants { get ; }
306314 public bool IsEnum { get ; }
307315
308- public TypeReference ( string name , string type , EnumDefinition [ ] enums )
309- : this ( name , type , null , enums , null ) { }
316+ public TypeReference ( string name , string type , int asize , EnumDefinition [ ] enums )
317+ : this ( name , type , asize , null , enums , null ) { }
310318
311- public TypeReference ( string name , string type , EnumDefinition [ ] enums , string [ ] typeVariants )
312- : this ( name , type , null , enums , typeVariants ) { }
319+ public TypeReference ( string name , string type , int asize , EnumDefinition [ ] enums , string [ ] typeVariants )
320+ : this ( name , type , asize , null , enums , typeVariants ) { }
313321
314- public TypeReference ( string name , string type , string templateType , EnumDefinition [ ] enums )
315- : this ( name , type , templateType , enums , null ) { }
322+ public TypeReference ( string name , string type , int asize , string templateType , EnumDefinition [ ] enums )
323+ : this ( name , type , asize , templateType , enums , null ) { }
316324
317- public TypeReference ( string name , string type , string templateType , EnumDefinition [ ] enums , string [ ] typeVariants )
325+ public TypeReference ( string name , string type , int asize , string templateType , EnumDefinition [ ] enums , string [ ] typeVariants )
318326 {
319327 Name = name ;
320328 Type = type . Replace ( "const" , string . Empty ) . Trim ( ) ;
@@ -345,29 +353,35 @@ public TypeReference(string name, string type, string templateType, EnumDefiniti
345353 }
346354
347355 TemplateType = templateType ;
356+ ArraySize = asize ;
348357 int startBracket = name . IndexOf ( '[' ) ;
349- if ( startBracket != - 1 )
358+ if ( startBracket != - 1 && ArraySize == 0 )
350359 {
360+ //This is only for older cimgui binding jsons
351361 int endBracket = name . IndexOf ( ']' ) ;
352362 string sizePart = name . Substring ( startBracket + 1 , endBracket - startBracket - 1 ) ;
353363 ArraySize = ParseSizeString ( sizePart , enums ) ;
354364 Name = Name . Substring ( 0 , startBracket ) ;
355365 }
356-
357366 IsFunctionPointer = Type . IndexOf ( '(' ) != - 1 ;
358-
367+
359368 TypeVariants = typeVariants ;
360369
361370 IsEnum = enums . Any ( t => t . Name == type || t . FriendlyName == type ) ;
362371 }
363-
364- private int ParseSizeString ( string sizePart , EnumDefinition [ ] enums )
365- {
366- if ( sizePart . Contains ( "(" ) || sizePart . Contains ( "+" ) || sizePart . Contains ( "/" ) ||
367- sizePart . Contains ( "*" ) )
372+
373+ private int ParseSizeString ( string sizePart , EnumDefinition [ ] enums )
374+ {
375+ int plusStart = sizePart . IndexOf ( '+' ) ;
376+ if ( plusStart != - 1 )
368377 {
369- return Task . WaitAny ( CSharpScript . EvaluateAsync < int > ( $ "(int)({ sizePart } )") ) ;
378+ string first = sizePart . Substring ( 0 , plusStart ) ;
379+ string second = sizePart . Substring ( plusStart , sizePart . Length - plusStart ) ;
380+ int firstVal = int . Parse ( first ) ;
381+ int secondVal = int . Parse ( second ) ;
382+ return firstVal + secondVal ;
370383 }
384+
371385 if ( ! int . TryParse ( sizePart , out int ret ) )
372386 {
373387 foreach ( EnumDefinition ed in enums )
@@ -393,7 +407,7 @@ private int ParseSizeString(string sizePart, EnumDefinition[] enums)
393407 public TypeReference WithVariant ( int variantIndex , EnumDefinition [ ] enums )
394408 {
395409 if ( variantIndex == 0 ) return this ;
396- else return new TypeReference ( Name , TypeVariants [ variantIndex - 1 ] , TemplateType , enums ) ;
410+ else return new TypeReference ( Name , TypeVariants [ variantIndex - 1 ] , ArraySize , TemplateType , enums ) ;
397411 }
398412 }
399413
0 commit comments