-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f65b7d4
commit 6fa0e64
Showing
36 changed files
with
1,036 additions
and
602 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
|
||
namespace Diffusion.IO | ||
{ | ||
public class Node | ||
{ | ||
public string Id { get; set; } | ||
public string Name { get; set; } | ||
public List<Input> Inputs { get; set; } | ||
|
||
public override int GetHashCode() | ||
{ | ||
var hash = Id.GetHashCode(); | ||
|
||
hash = (hash * 397) ^ Name.GetHashCode(); | ||
|
||
foreach (var input in Inputs) | ||
{ | ||
hash = (hash * 397) ^ input.Name.GetHashCode(); | ||
} | ||
|
||
return hash; | ||
} | ||
} | ||
|
||
public class Input | ||
{ | ||
public string Name { get; set; } | ||
public object Value { get; set; } | ||
|
||
public Input(string name, object value) | ||
{ | ||
Name = name; | ||
Value = value; | ||
} | ||
} | ||
|
||
public class ComfyUIParser | ||
{ | ||
public IReadOnlyCollection<Node> Parse(string workflow) | ||
{ | ||
if (workflow == null) return null; | ||
var root = System.Text.Json.JsonDocument.Parse(workflow); | ||
var rootElements = root.RootElement.EnumerateObject().ToDictionary(n => n.Name, n => n.Value); | ||
|
||
var nodes = new List<Node>(); | ||
|
||
foreach (var element in rootElements) | ||
{ | ||
var node = new Node(); | ||
node.Id = element.Key; | ||
|
||
foreach (var props in element.Value.EnumerateObject()) | ||
{ | ||
|
||
if (props.Name == "inputs") | ||
{ | ||
node.Inputs = new List<Input>(); | ||
foreach (var prop2 in props.Value.EnumerateObject()) | ||
{ | ||
switch (prop2.Value.ValueKind) | ||
{ | ||
case JsonValueKind.Undefined: | ||
break; | ||
case JsonValueKind.Object: | ||
break; | ||
case JsonValueKind.Array: | ||
break; | ||
case JsonValueKind.String: | ||
node.Inputs.Add(new Input(prop2.Name.Replace("_", "__"), prop2.Value.GetString())); | ||
break; | ||
case JsonValueKind.Number: | ||
node.Inputs.Add(new Input(prop2.Name.Replace("_", "__"), prop2.Value.GetDouble())); | ||
break; | ||
case JsonValueKind.True: | ||
node.Inputs.Add(new Input(prop2.Name.Replace("_", "__"), prop2.Value.GetBoolean())); | ||
break; | ||
case JsonValueKind.False: | ||
node.Inputs.Add(new Input(prop2.Name.Replace("_", "__"), prop2.Value.GetBoolean())); | ||
break; | ||
case JsonValueKind.Null: | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
} | ||
} | ||
else if (props.Name == "class_type") | ||
{ | ||
node.Name = props.Value.GetString().Replace("_", "__"); | ||
} | ||
} | ||
nodes.Add(node); | ||
} | ||
|
||
return nodes; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.