@@ -382,140 +382,181 @@ int GetIntTag(string key)
382382 private static FileParameters ReadComfyUIParameters ( string file , string description )
383383 {
384384 var fp = new FileParameters ( ) ;
385- var json = description . Substring ( "prompt: " . Length ) ;
386385
387- var root = JsonDocument . Parse ( json ) ;
388- var nodes = root . RootElement . EnumerateObject ( ) . ToDictionary ( o => o . Name , o => o . Value ) ;
386+ try
387+ {
388+ var json = description . Substring ( "prompt: " . Length ) ;
389389
390- var isSDXL = false ;
391- var isEfficient = false ;
390+ // fix for errant nodes
391+ json = json . Replace ( "NaN" , "null" ) ;
392392
393- var ksampler = nodes . Values . SingleOrDefault ( o =>
394- {
395- if ( o . TryGetProperty ( "class_type" , out var element ) )
396- {
397- return element . GetString ( ) == "KSampler" ;
398- }
393+ var root = JsonDocument . Parse ( json ) ;
394+ var nodes = root . RootElement . EnumerateObject ( ) . ToDictionary ( o => o . Name , o => o . Value ) ;
399395
400- return false ;
401- } ) ;
396+ var isSDXL = false ;
397+ var isEfficient = false ;
398+ var isEfficientSDXL = false ;
402399
403- if ( ksampler . ValueKind == JsonValueKind . Undefined )
404- {
405- ksampler = nodes . Values . FirstOrDefault ( o =>
400+ var ksampler = nodes . Values . SingleOrDefault ( o =>
406401 {
407402 if ( o . TryGetProperty ( "class_type" , out var element ) )
408403 {
409- return element . GetString ( ) == "KSampler (Efficient) " ;
404+ return element . GetString ( ) == "KSampler" ;
410405 }
411406
412407 return false ;
413408 } ) ;
414409
415- if ( ksampler . ValueKind != JsonValueKind . Undefined )
416- {
417- isEfficient = true ;
418- }
419- }
420-
421- if ( ksampler . ValueKind == JsonValueKind . Undefined )
422- {
423- ksampler = nodes . Values . FirstOrDefault ( o =>
410+ if ( ksampler . ValueKind == JsonValueKind . Undefined )
424411 {
425- if ( o . TryGetProperty ( "class_type" , out var element ) )
412+ ksampler = nodes . Values . FirstOrDefault ( o =>
426413 {
427- return element . GetString ( ) == "KSamplerAdvanced" ;
428- }
414+ if ( o . TryGetProperty ( "class_type" , out var element ) )
415+ {
416+ return element . GetString ( ) == "KSampler (Efficient)" ;
417+ }
429418
430- return false ;
431- } ) ;
419+ return false ;
420+ } ) ;
432421
433- if ( ksampler . ValueKind != JsonValueKind . Undefined )
434- {
435- isSDXL = true ;
422+ if ( ksampler . ValueKind != JsonValueKind . Undefined )
423+ {
424+ isEfficient = true ;
425+ }
436426 }
437- }
438427
428+ if ( ksampler . ValueKind == JsonValueKind . Undefined )
429+ {
430+ ksampler = nodes . Values . FirstOrDefault ( o =>
431+ {
432+ if ( o . TryGetProperty ( "class_type" , out var element ) )
433+ {
434+ return element . GetString ( ) == "KSamplerAdvanced" ;
435+ }
439436
440- if ( ksampler . ValueKind != JsonValueKind . Undefined )
441- {
442-
443- var image = ksampler . GetProperty ( "inputs" ) ;
437+ return false ;
438+ } ) ;
444439
445- if ( image . TryGetProperty ( "positive" , out var positive ) )
446- {
447- var promptIndex = positive . EnumerateArray ( ) . First ( ) . GetString ( ) ;
448- var promptObject = nodes [ promptIndex ] . GetProperty ( "inputs" ) ;
449- if ( isSDXL )
440+ if ( ksampler . ValueKind != JsonValueKind . Undefined )
450441 {
451- fp . Prompt = promptObject . GetProperty ( "text_g" ) . GetString ( ) ;
442+ isSDXL = true ;
452443 }
453- else if ( isEfficient )
444+ }
445+
446+ if ( ksampler . ValueKind == JsonValueKind . Undefined )
447+ {
448+ ksampler = nodes . Values . FirstOrDefault ( o =>
454449 {
455- fp . Prompt = promptObject . GetProperty ( "positive" ) . GetString ( ) ;
456- }
457- else
450+ if ( o . TryGetProperty ( "class_type" , out var element ) )
451+ {
452+ return element . GetString ( ) == "Eff. Loader SDXL" ;
453+ }
454+
455+ return false ;
456+ } ) ;
457+
458+ if ( ksampler . ValueKind != JsonValueKind . Undefined )
458459 {
459- fp . Prompt = promptObject . GetProperty ( "text" ) . GetString ( ) ;
460+ isEfficientSDXL = true ;
460461 }
461462 }
462463
463- if ( image . TryGetProperty ( "negative" , out var negative ) )
464+
465+ if ( ksampler . ValueKind != JsonValueKind . Undefined )
464466 {
465- var promptIndex = negative . EnumerateArray ( ) . First ( ) . GetString ( ) ;
466- var promptObject = nodes [ promptIndex ] . GetProperty ( "inputs" ) ;
467- if ( isSDXL )
467+
468+ var image = ksampler . GetProperty ( "inputs" ) ;
469+
470+ if ( image . TryGetProperty ( "positive" , out var positive ) )
468471 {
469- fp . NegativePrompt = promptObject . GetProperty ( "text_g" ) . GetString ( ) ;
472+ var promptIndex = positive . EnumerateArray ( ) . First ( ) . GetString ( ) ;
473+ var promptObject = nodes [ promptIndex ] . GetProperty ( "inputs" ) ;
474+ if ( isSDXL )
475+ {
476+ fp . Prompt = promptObject . GetProperty ( "text_g" ) . GetString ( ) ;
477+ }
478+ else if ( isEfficient )
479+ {
480+ fp . Prompt = promptObject . GetProperty ( "positive" ) . GetString ( ) ;
481+ }
482+ else if ( isEfficientSDXL )
483+ {
484+ fp . Prompt = promptObject . GetProperty ( "text" ) . GetString ( ) ;
485+ }
486+ else
487+ {
488+ fp . Prompt = promptObject . GetProperty ( "text" ) . GetString ( ) ;
489+ }
470490 }
471- else if ( isEfficient )
491+
492+ if ( image . TryGetProperty ( "negative" , out var negative ) )
472493 {
473- fp . NegativePrompt = promptObject . GetProperty ( "negative" ) . GetString ( ) ;
494+ if ( negative . ValueKind == JsonValueKind . Array )
495+ {
496+ var promptIndex = negative . EnumerateArray ( ) . First ( ) . GetString ( ) ;
497+ var promptObject = nodes [ promptIndex ] . GetProperty ( "inputs" ) ;
498+ if ( isSDXL )
499+ {
500+ fp . NegativePrompt = promptObject . GetProperty ( "text_g" ) . GetString ( ) ;
501+ }
502+ else if ( isEfficient )
503+ {
504+ fp . NegativePrompt = promptObject . GetProperty ( "negative" ) . GetString ( ) ;
505+ }
506+ else
507+ {
508+ fp . NegativePrompt = promptObject . GetProperty ( "text" ) . GetString ( ) ;
509+ }
510+ }
511+ else if ( negative . ValueKind == JsonValueKind . String )
512+ {
513+ fp . NegativePrompt = negative . GetString ( ) ;
514+ }
515+
474516 }
475- else
517+
518+ if ( image . TryGetProperty ( "latent_image" , out var latent_image ) )
476519 {
477- fp . NegativePrompt = promptObject . GetProperty ( "text" ) . GetString ( ) ;
520+ var index = latent_image . EnumerateArray ( ) . First ( ) . GetString ( ) ;
521+ var promptObject = nodes [ index ] . GetProperty ( "inputs" ) ;
522+ var hasWidth = promptObject . TryGetProperty ( "width" , out var widthObject ) ;
523+ var hasHeight = promptObject . TryGetProperty ( "height" , out var heightObject ) ;
524+
525+ if ( hasWidth && hasHeight )
526+ {
527+ fp . Width = widthObject . GetInt32 ( ) ;
528+ fp . Height = heightObject . GetInt32 ( ) ;
529+ }
478530 }
479- }
480531
481- if ( image . TryGetProperty ( "latent_image" , out var latent_image ) )
482- {
483- var index = latent_image . EnumerateArray ( ) . First ( ) . GetString ( ) ;
484- var promptObject = nodes [ index ] . GetProperty ( "inputs" ) ;
485- var hasWidth = promptObject . TryGetProperty ( "width" , out var widthObject ) ;
486- var hasHeight = promptObject . TryGetProperty ( "height" , out var heightObject ) ;
532+ fp . Steps = image . GetProperty ( "steps" ) . GetInt32 ( ) ;
533+ fp . CFGScale = image . GetProperty ( "cfg" ) . GetDecimal ( ) ;
487534
488- if ( hasWidth && hasHeight )
535+ if ( isSDXL )
489536 {
490- fp . Width = widthObject . GetInt32 ( ) ;
491- fp . Height = heightObject . GetInt32 ( ) ;
537+ fp . Seed = image . GetProperty ( "noise_seed" ) . GetInt64 ( ) ;
492538 }
493- }
539+ else
540+ {
541+ var seed = image . GetProperty ( "seed" ) ;
494542
495- fp . Steps = image . GetProperty ( "steps" ) . GetInt32 ( ) ;
496- fp . CFGScale = image . GetProperty ( "cfg" ) . GetDecimal ( ) ;
543+ if ( seed . ValueKind == JsonValueKind . Number )
544+ {
545+ fp . Seed = seed . GetInt64 ( ) ;
546+ }
547+ }
497548
498- if ( isSDXL )
499- {
500- fp . Seed = image . GetProperty ( "noise_seed" ) . GetInt64 ( ) ;
501- }
502- else
503- {
504- var seed = image . GetProperty ( "seed" ) ;
549+ fp . Sampler = image . GetProperty ( "sampler_name" ) . GetString ( ) ;
505550
506- if ( seed . ValueKind == JsonValueKind . Number )
507- {
508- fp . Seed = seed . GetInt64 ( ) ;
509- }
551+ fp . OtherParameters = $ "Steps: { fp . Steps } Sampler: { fp . Sampler } CFG Scale: { fp . CFGScale } Seed: { fp . Seed } Size: { fp . Width } x{ fp . Height } ";
510552 }
511553
512- fp . Sampler = image . GetProperty ( "sampler_name" ) . GetString ( ) ;
513-
514- fp . OtherParameters = $ "Steps: { fp . Steps } Sampler: { fp . Sampler } CFG Scale: { fp . CFGScale } Seed: { fp . Seed } Size: { fp . Width } x{ fp . Height } ";
554+ return fp ;
555+ }
556+ catch
557+ {
558+ return fp ;
515559 }
516-
517-
518- return fp ;
519560 }
520561
521562 private static FileParameters ReadInvokeAIParametersNew ( string file , string description )
0 commit comments