mirror of
https://github.com/SamueleLorefice/ComfySharp
synced 2026-01-15 04:53:42 +00:00
Moved all classes and enums to their respective files under the Types namespace. Added ObjectInfoPArser class to delegate the processing of the massice node info json to a more clean place.
Let the pain begin.
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using ComfySharp.Types;
|
||||
|
||||
namespace ComfySharp;
|
||||
|
||||
@@ -49,15 +48,11 @@ public class ComfyClient {
|
||||
|
||||
if (req is { IsSuccessStatusCode: true, Content: not null }) {
|
||||
var doc = await req.Content.ReadFromJsonAsync<JsonDocument>();
|
||||
ObjectInfoParser.Parse(doc, out nodes);
|
||||
|
||||
foreach (var node in doc.RootElement.EnumerateObject()) {
|
||||
Node n;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<byte[]?> GetImage(string filename) {
|
||||
@@ -68,102 +63,3 @@ public class ComfyClient {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
[DataContract, JsonSerializable(typeof(Node))]
|
||||
public class Node {
|
||||
[DataMember(Name = "name")]
|
||||
public string Name { get; set; }
|
||||
[DataMember(Name = "input")]
|
||||
public Input Input { get; set; }
|
||||
[DataMember(Name = "output")]
|
||||
public List<PrimitiveType> Outputs { get; set; }
|
||||
[DataMember(Name = "output_is_list")]
|
||||
public List<bool> OutputIsList { get; set; }
|
||||
[DataMember(Name = "output_name")]
|
||||
public List<string> OutputNames { get; set; }
|
||||
[DataMember(Name = "display_name")]
|
||||
public string DisplayName { get; set; }
|
||||
[DataMember(Name = "description")]
|
||||
public string Description { get; set; }
|
||||
[DataMember(Name = "category")]
|
||||
public string Category { get; set; }
|
||||
[DataMember(Name = "output_node")]
|
||||
public bool IsOutputNode { get; set; }
|
||||
|
||||
public Node() {
|
||||
Name = "";
|
||||
Input = new();
|
||||
Outputs = new();
|
||||
OutputIsList = new();
|
||||
OutputNames = new();
|
||||
DisplayName = "";
|
||||
Description = "";
|
||||
Category = "";
|
||||
IsOutputNode = false;
|
||||
}
|
||||
}
|
||||
|
||||
[DataContract]
|
||||
public struct Input {
|
||||
[DataMember]
|
||||
public List<InputField> Required { get; set; }
|
||||
[DataMember]
|
||||
public List<InputField> Optional { get; set; }
|
||||
[DataMember]
|
||||
public List<InputField> Hidden { get; set; }
|
||||
|
||||
public Input() {
|
||||
Required = new();
|
||||
Optional = new();
|
||||
Hidden = new();
|
||||
}
|
||||
}
|
||||
|
||||
public struct InputField {
|
||||
[DataMember(Name = "name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
public PrimitiveType Type { get; set; }
|
||||
}
|
||||
|
||||
public enum PrimitiveType {
|
||||
ANY,
|
||||
CLIP,
|
||||
CLIP_VISION,
|
||||
CLIP_VISION_OUTPUT,
|
||||
CONDITIONING,
|
||||
CONTROL_NET,
|
||||
EXTRA_PNGINFO,
|
||||
FLOAT,
|
||||
GLIGEN,
|
||||
IMAGE,
|
||||
INT,
|
||||
LATENT,
|
||||
MASK,
|
||||
MODEL,
|
||||
PROMPT,
|
||||
SAMPLER,
|
||||
SIGMAS,
|
||||
STRING,
|
||||
STYLE_MODEL,
|
||||
UNIQUE_ID,
|
||||
UPSCALE_MODEL,
|
||||
VAE,
|
||||
}
|
||||
|
||||
[DataContract, JsonSerializable(typeof(ImageInfo))]
|
||||
public class ImageInfo {
|
||||
[DataMember(Name = "name")]
|
||||
public string Name { get; set; }
|
||||
[DataMember(Name = "subfolder")]
|
||||
public string Subfolder { get; set; }
|
||||
[DataMember(Name = "type")]
|
||||
public DirType Type { get; set; }
|
||||
}
|
||||
|
||||
[DataContract]
|
||||
public enum DirType {
|
||||
[DataMember(Name = "input")] Input,
|
||||
[DataMember(Name = "temp")] Temp,
|
||||
[DataMember(Name = "output")] Output
|
||||
}
|
||||
83
ComfySharp/ObjectInfoParser.cs
Normal file
83
ComfySharp/ObjectInfoParser.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System.Text.Json;
|
||||
using ComfySharp.Types;
|
||||
|
||||
namespace ComfySharp;
|
||||
|
||||
public static class ObjectInfoParser {
|
||||
public static void Parse(JsonDocument document, out List<Node> nodes) {
|
||||
nodes = new List<Node>();
|
||||
foreach (var node in document.RootElement.EnumerateObject()) {
|
||||
Node n = new();
|
||||
n.Name = node.Name;
|
||||
|
||||
foreach (var prop in node.Value.EnumerateObject()) {
|
||||
switch (prop.Name) {
|
||||
case "input":
|
||||
n.Input = new();
|
||||
|
||||
foreach (var input in prop.Value.EnumerateObject()) {
|
||||
switch (input.Name) {
|
||||
case "required":
|
||||
foreach (var field in input.Value.EnumerateObject()) {
|
||||
InputField f = new();
|
||||
f.Name = field.Name;
|
||||
f.Type = Enum.Parse<PrimitiveType>(field.Value.GetString() ?? "");
|
||||
n.Input.Required.Add(f);
|
||||
}
|
||||
break;
|
||||
case "optional":
|
||||
foreach (var field in input.Value.EnumerateObject()) {
|
||||
InputField f = new();
|
||||
f.Name = field.Name;
|
||||
f.Type = Enum.Parse<PrimitiveType>(field.Value.GetString() ?? "");
|
||||
n.Input.Optional.Add(f);
|
||||
}
|
||||
break;
|
||||
case "hidden":
|
||||
foreach (var field in input.Value.EnumerateObject()) {
|
||||
InputField f = new();
|
||||
f.Name = field.Name;
|
||||
f.Type = Enum.Parse<PrimitiveType>(field.Value.GetString() ?? "");
|
||||
n.Input.Hidden.Add(f);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "output":
|
||||
foreach (var output in prop.Value.EnumerateObject()) {
|
||||
n.Outputs.Add(Enum.Parse<PrimitiveType>(output.Value.GetString() ?? ""));
|
||||
n.OutputIsList.Add(output.Value.GetBoolean());
|
||||
n.OutputNames.Add(output.Name);
|
||||
}
|
||||
break;
|
||||
case "display_name":
|
||||
n.DisplayName = prop.Value.GetString() ?? "";
|
||||
break;
|
||||
case "description":
|
||||
n.Description = prop.Value.GetString() ?? "";
|
||||
break;
|
||||
case "category":
|
||||
n.Category = prop.Value.GetString() ?? "";
|
||||
break;
|
||||
case "output_node":
|
||||
n.IsOutputNode = prop.Value.GetBoolean();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void ParseNode(JsonElement node, out Node n) {
|
||||
n = new();
|
||||
n.Name = node.GetProperty("name").GetString() ?? "";
|
||||
n.Input = ParseInput(node.GetProperty("input"));
|
||||
n.Outputs = ParseOutputs(node.GetProperty("output"));
|
||||
n.OutputIsList = ParseOutputIsList(node.GetProperty("output"));
|
||||
n.OutputNames = ParseOutputNames(node.GetProperty("output"));
|
||||
n.DisplayName = node.GetProperty("display_name").GetString() ?? "";
|
||||
n.Description = node.GetProperty("description").GetString() ?? "";
|
||||
n.Category = node.GetProperty("category").GetString() ?? "";
|
||||
n.IsOutputNode = node.GetProperty("output_node").GetBoolean();
|
||||
}
|
||||
}
|
||||
10
ComfySharp/Types/DirType.cs
Normal file
10
ComfySharp/Types/DirType.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace ComfySharp.Types;
|
||||
|
||||
[DataContract]
|
||||
public enum DirType {
|
||||
[DataMember(Name = "input")] Input,
|
||||
[DataMember(Name = "temp")] Temp,
|
||||
[DataMember(Name = "output")] Output
|
||||
}
|
||||
14
ComfySharp/Types/ImageInfo.cs
Normal file
14
ComfySharp/Types/ImageInfo.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ComfySharp.Types;
|
||||
|
||||
[DataContract, JsonSerializable(typeof(ImageInfo))]
|
||||
public class ImageInfo {
|
||||
[DataMember(Name = "name")]
|
||||
public string Name { get; set; }
|
||||
[DataMember(Name = "subfolder")]
|
||||
public string Subfolder { get; set; }
|
||||
[DataMember(Name = "type")]
|
||||
public DirType Type { get; set; }
|
||||
}
|
||||
19
ComfySharp/Types/Input.cs
Normal file
19
ComfySharp/Types/Input.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace ComfySharp.Types;
|
||||
|
||||
[DataContract]
|
||||
public struct Input {
|
||||
[DataMember]
|
||||
public List<InputField> Required { get; set; }
|
||||
[DataMember]
|
||||
public List<InputField> Optional { get; set; }
|
||||
[DataMember]
|
||||
public List<InputField> Hidden { get; set; }
|
||||
|
||||
public Input() {
|
||||
Required = new();
|
||||
Optional = new();
|
||||
Hidden = new();
|
||||
}
|
||||
}
|
||||
10
ComfySharp/Types/InputField.cs
Normal file
10
ComfySharp/Types/InputField.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace ComfySharp.Types;
|
||||
|
||||
public struct InputField {
|
||||
[DataMember(Name = "name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
public PrimitiveType Type { get; set; }
|
||||
}
|
||||
38
ComfySharp/Types/Node.cs
Normal file
38
ComfySharp/Types/Node.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ComfySharp.Types;
|
||||
|
||||
[DataContract, JsonSerializable(typeof(Node))]
|
||||
public class Node {
|
||||
[DataMember(Name = "name")]
|
||||
public string Name { get; set; }
|
||||
[DataMember(Name = "input")]
|
||||
public Input Input { get; set; }
|
||||
[DataMember(Name = "output")]
|
||||
public List<PrimitiveType> Outputs { get; set; }
|
||||
[DataMember(Name = "output_is_list")]
|
||||
public List<bool> OutputIsList { get; set; }
|
||||
[DataMember(Name = "output_name")]
|
||||
public List<string> OutputNames { get; set; }
|
||||
[DataMember(Name = "display_name")]
|
||||
public string DisplayName { get; set; }
|
||||
[DataMember(Name = "description")]
|
||||
public string Description { get; set; }
|
||||
[DataMember(Name = "category")]
|
||||
public string Category { get; set; }
|
||||
[DataMember(Name = "output_node")]
|
||||
public bool IsOutputNode { get; set; }
|
||||
|
||||
public Node() {
|
||||
Name = "";
|
||||
Input = new();
|
||||
Outputs = new();
|
||||
OutputIsList = new();
|
||||
OutputNames = new();
|
||||
DisplayName = "";
|
||||
Description = "";
|
||||
Category = "";
|
||||
IsOutputNode = false;
|
||||
}
|
||||
}
|
||||
26
ComfySharp/Types/PrimitiveType.cs
Normal file
26
ComfySharp/Types/PrimitiveType.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace ComfySharp.Types;
|
||||
|
||||
public enum PrimitiveType {
|
||||
ANY,
|
||||
CLIP,
|
||||
CLIP_VISION,
|
||||
CLIP_VISION_OUTPUT,
|
||||
CONDITIONING,
|
||||
CONTROL_NET,
|
||||
EXTRA_PNGINFO,
|
||||
FLOAT,
|
||||
GLIGEN,
|
||||
IMAGE,
|
||||
INT,
|
||||
LATENT,
|
||||
MASK,
|
||||
MODEL,
|
||||
PROMPT,
|
||||
SAMPLER,
|
||||
SIGMAS,
|
||||
STRING,
|
||||
STYLE_MODEL,
|
||||
UNIQUE_ID,
|
||||
UPSCALE_MODEL,
|
||||
VAE,
|
||||
}
|
||||
Reference in New Issue
Block a user