mirror of
https://github.com/SamueleLorefice/ComfySharp
synced 2026-01-15 03:35:48 +00:00
Started deprecating the old parser in favor of the new object generator.
Added a conversion settings file.
This commit is contained in:
@@ -10,6 +10,7 @@ namespace ComfySharp;
|
|||||||
public class ComfyClient {
|
public class ComfyClient {
|
||||||
private HttpClient client;
|
private HttpClient client;
|
||||||
private List<ExpandoObject> nodes;
|
private List<ExpandoObject> nodes;
|
||||||
|
private NodeDBGenerator dbGenerator;
|
||||||
|
|
||||||
public string BaseUrl { get; set; }
|
public string BaseUrl { get; set; }
|
||||||
|
|
||||||
@@ -20,6 +21,19 @@ public class ComfyClient {
|
|||||||
DefaultRequestHeaders = { { "User-Agent", "ComfySharp" } }
|
DefaultRequestHeaders = { { "User-Agent", "ComfySharp" } }
|
||||||
};
|
};
|
||||||
nodes= new();
|
nodes= new();
|
||||||
|
|
||||||
|
try {
|
||||||
|
dbGenerator = new(ConversionSettings.FromFile("conv_config.json"));
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Console.WriteLine(e);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
ConversionSettings settings = new();
|
||||||
|
settings.Save("conv_config.json");
|
||||||
|
dbGenerator = new(settings);
|
||||||
|
Console.WriteLine("created empty settings file");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string[]?> GetEmbeddings() {
|
public async Task<string[]?> GetEmbeddings() {
|
||||||
@@ -42,14 +56,20 @@ public class ComfyClient {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//public async Task<>GetHistory
|
|
||||||
|
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||||
|
public async Task<List<HistoryEntry>?> GetHistory() {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||||
|
|
||||||
public async Task<List<Node>?> GetObjectInfo() {
|
public async Task<List<Node>?> GetObjectInfo() {
|
||||||
var req = await client.GetAsync("/object_info");
|
var req = await client.GetAsync("/object_info");
|
||||||
|
|
||||||
if (req is { IsSuccessStatusCode: true, Content: not null }) {
|
if (req is { IsSuccessStatusCode: true, Content: not null }) {
|
||||||
var doc = await req.Content.ReadFromJsonAsync<JsonDocument>();
|
var doc = await req.Content.ReadFromJsonAsync<JsonDocument>();
|
||||||
ObjectInfoParser.Parse(doc, out nodes);
|
|
||||||
|
//ObjectInfoParser.Parse(doc, out nodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -6,4 +6,10 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="conv_config.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
29
ComfySharp/ConversionSettings.cs
Normal file
29
ComfySharp/ConversionSettings.cs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace ComfySharp;
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class ConversionSettings {
|
||||||
|
public List<string> EnumConvertAsString = new();
|
||||||
|
public List<string> EnumConvertAsBool = new();
|
||||||
|
|
||||||
|
public static ConversionSettings FromFile(string path) {
|
||||||
|
if (!File.Exists(path)) throw new FileNotFoundException("Could not find settings file", path);
|
||||||
|
string json = File.ReadAllText(path);
|
||||||
|
ConversionSettings? settings = FromJson(json);
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConversionSettings FromJson(string json) {
|
||||||
|
ConversionSettings? settings = JsonSerializer.Deserialize<ConversionSettings>(json);
|
||||||
|
if(settings is null) throw new NullReferenceException("Could not deserialize settings file");
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ToJson(ConversionSettings settings) => JsonSerializer.Serialize(settings);
|
||||||
|
|
||||||
|
public void Save(string path) => File.WriteAllText(path, ToJson());
|
||||||
|
|
||||||
|
public string ToJson() => JsonSerializer.Serialize(this);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,11 +3,11 @@ using System.Text.Json;
|
|||||||
using ComfySharp.Types;
|
using ComfySharp.Types;
|
||||||
|
|
||||||
namespace ComfySharp;
|
namespace ComfySharp;
|
||||||
|
[Obsolete("This class is obsolete, use NodeDBGenerator instead",false)]
|
||||||
public static class ObjectInfoParser {
|
public static class ObjectInfoParser {
|
||||||
|
|
||||||
public static void Parse(JsonDocument document, out List<ExpandoObject> nodes) {
|
public static void Parse(JsonDocument document, out List<ExpandoObject> nodes) {
|
||||||
NodeDBGenerator dbGenerator = new();
|
NodeDBGenerator dbGenerator = new(new ConversionSettings());
|
||||||
dbGenerator.GenerateNodes(document);
|
dbGenerator.GenerateNodes(document);
|
||||||
nodes = dbGenerator.GetNodes();
|
nodes = dbGenerator.GetNodes();
|
||||||
dbGenerator.GenerateClasses(document);
|
dbGenerator.GenerateClasses(document);
|
||||||
|
|||||||
4
ComfySharp/Types/HistoryEntry.cs
Normal file
4
ComfySharp/Types/HistoryEntry.cs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
namespace ComfySharp.Types;
|
||||||
|
|
||||||
|
public class HistoryEntry {
|
||||||
|
}
|
||||||
@@ -3,12 +3,14 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Dynamic;
|
using System.Dynamic;
|
||||||
|
using System.Net;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace ComfySharp.Types;
|
namespace ComfySharp.Types;
|
||||||
|
|
||||||
public class NodeDBGenerator {
|
public class NodeDBGenerator {
|
||||||
private List<ExpandoObject> nodes;
|
private List<ExpandoObject> nodes;
|
||||||
|
private ConversionSettings settings;
|
||||||
|
|
||||||
private List<string> knownTypes = new();
|
private List<string> knownTypes = new();
|
||||||
public List<string> GetKnownTypes() => knownTypes;
|
public List<string> GetKnownTypes() => knownTypes;
|
||||||
@@ -20,8 +22,9 @@ public class NodeDBGenerator {
|
|||||||
private int typeFields = 0;
|
private int typeFields = 0;
|
||||||
private int enumFields = 0;
|
private int enumFields = 0;
|
||||||
|
|
||||||
public NodeDBGenerator() {
|
public NodeDBGenerator(ConversionSettings settings) {
|
||||||
nodes = new();
|
nodes = new();
|
||||||
|
this.settings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ResetDb() => nodes.Clear();
|
public void ResetDb() => nodes.Clear();
|
||||||
@@ -34,8 +37,6 @@ public class NodeDBGenerator {
|
|||||||
if (!knownTypes.Contains(type)) {
|
if (!knownTypes.Contains(type)) {
|
||||||
knownTypes.Add(type);
|
knownTypes.Add(type);
|
||||||
Console.WriteLine("Added new known type: {0}", type);
|
Console.WriteLine("Added new known type: {0}", type);
|
||||||
} else {
|
|
||||||
Console.WriteLine("Type {0} already known, skipped.", type);
|
|
||||||
}
|
}
|
||||||
typeFields++;
|
typeFields++;
|
||||||
}
|
}
|
||||||
@@ -54,7 +55,7 @@ public class NodeDBGenerator {
|
|||||||
values.ToList().ForEach(value => {
|
values.ToList().ForEach(value => {
|
||||||
if (!knownEnums[type].Contains(value)) {
|
if (!knownEnums[type].Contains(value)) {
|
||||||
knownEnums[type].Add(value);
|
knownEnums[type].Add(value);
|
||||||
Console.WriteLine("Added new value to known enum: {0}", value);
|
Console.WriteLine("Added new value to already known enum: {0}", value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -112,12 +113,20 @@ public class NodeDBGenerator {
|
|||||||
// if element 0 is a string, this is a type
|
// if element 0 is a string, this is a type
|
||||||
if (inputProperty.Value[0].ValueKind == JsonValueKind.String)
|
if (inputProperty.Value[0].ValueKind == JsonValueKind.String)
|
||||||
AddKnownType(inputProperty.Value[0].ToString());
|
AddKnownType(inputProperty.Value[0].ToString());
|
||||||
// else, if element 0 is an array, this is an enum
|
// else, if element 0 is an array, this is an enum or a list.
|
||||||
else if (inputProperty.Value[0].ValueKind == JsonValueKind.Array) {
|
else if (inputProperty.Value[0].ValueKind == JsonValueKind.Array) {
|
||||||
|
//if the elements inside the array are not strings, this is a list of objects and might require special handling
|
||||||
|
if (inputProperty.Value[0].EnumerateArray().Current.ValueKind != JsonValueKind.String) {
|
||||||
|
Console.WriteLine("Encountered a special case: {0}", inputProperty.Name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// these are all lists of strings and not enums
|
||||||
|
if (inputProperty.Name == "image" || inputProperty.Name == "ckpt_name" || inputProperty.Name == "lora_name" || inputProperty.Name == "control_net_name"){
|
||||||
|
|
||||||
|
}
|
||||||
List<string> enumValues = new();
|
List<string> enumValues = new();
|
||||||
inputProperty.Value[0].EnumerateArray().ToList().ForEach(value => enumValues.Add(value.ToString()));
|
inputProperty.Value[0].EnumerateArray().ToList().ForEach(value => enumValues.Add(value.ToString()));
|
||||||
AddKnownEnum(inputProperty.Name, enumValues);
|
AddKnownEnum(inputProperty.Name, enumValues);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
0
ComfySharp/conv_config.json
Normal file
0
ComfySharp/conv_config.json
Normal file
Reference in New Issue
Block a user