From c6285c4f56c97022326bbd1fc351e2dc6cec8482 Mon Sep 17 00:00:00 2001 From: Samuele Lorefice Date: Mon, 18 Dec 2023 19:19:14 +0100 Subject: [PATCH] Started deprecating the old parser in favor of the new object generator. Added a conversion settings file. --- ComfySharp/ComfyClient.cs | 26 +++++++++++++++++++++++--- ComfySharp/ComfySharp.csproj | 6 ++++++ ComfySharp/ConversionSettings.cs | 29 +++++++++++++++++++++++++++++ ComfySharp/ObjectInfoParser.cs | 4 ++-- ComfySharp/Types/HistoryEntry.cs | 4 ++++ ComfySharp/Types/NodeDBGenerator.cs | 21 +++++++++++++++------ ComfySharp/conv_config.json | 0 7 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 ComfySharp/ConversionSettings.cs create mode 100644 ComfySharp/Types/HistoryEntry.cs create mode 100644 ComfySharp/conv_config.json diff --git a/ComfySharp/ComfyClient.cs b/ComfySharp/ComfyClient.cs index b85dc69..e4d1fbb 100644 --- a/ComfySharp/ComfyClient.cs +++ b/ComfySharp/ComfyClient.cs @@ -10,6 +10,7 @@ namespace ComfySharp; public class ComfyClient { private HttpClient client; private List nodes; + private NodeDBGenerator dbGenerator; public string BaseUrl { get; set; } @@ -20,6 +21,19 @@ public class ComfyClient { DefaultRequestHeaders = { { "User-Agent", "ComfySharp" } } }; 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 GetEmbeddings() { @@ -42,14 +56,20 @@ public class ComfyClient { return null; } - //public async Task<>GetHistory - + +#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously + public async Task?> GetHistory() { + throw new NotImplementedException(); + } +#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously + public async Task?> GetObjectInfo() { var req = await client.GetAsync("/object_info"); if (req is { IsSuccessStatusCode: true, Content: not null }) { var doc = await req.Content.ReadFromJsonAsync(); - ObjectInfoParser.Parse(doc, out nodes); + + //ObjectInfoParser.Parse(doc, out nodes); } return null; diff --git a/ComfySharp/ComfySharp.csproj b/ComfySharp/ComfySharp.csproj index 6836c68..f874c67 100644 --- a/ComfySharp/ComfySharp.csproj +++ b/ComfySharp/ComfySharp.csproj @@ -6,4 +6,10 @@ enable + + + PreserveNewest + + + diff --git a/ComfySharp/ConversionSettings.cs b/ComfySharp/ConversionSettings.cs new file mode 100644 index 0000000..71dc9a2 --- /dev/null +++ b/ComfySharp/ConversionSettings.cs @@ -0,0 +1,29 @@ +using System.Text.Json; + +namespace ComfySharp; + +[Serializable] +public class ConversionSettings { + public List EnumConvertAsString = new(); + public List 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(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); + +} \ No newline at end of file diff --git a/ComfySharp/ObjectInfoParser.cs b/ComfySharp/ObjectInfoParser.cs index 58e9bda..2577733 100644 --- a/ComfySharp/ObjectInfoParser.cs +++ b/ComfySharp/ObjectInfoParser.cs @@ -3,11 +3,11 @@ using System.Text.Json; using ComfySharp.Types; namespace ComfySharp; - +[Obsolete("This class is obsolete, use NodeDBGenerator instead",false)] public static class ObjectInfoParser { public static void Parse(JsonDocument document, out List nodes) { - NodeDBGenerator dbGenerator = new(); + NodeDBGenerator dbGenerator = new(new ConversionSettings()); dbGenerator.GenerateNodes(document); nodes = dbGenerator.GetNodes(); dbGenerator.GenerateClasses(document); diff --git a/ComfySharp/Types/HistoryEntry.cs b/ComfySharp/Types/HistoryEntry.cs new file mode 100644 index 0000000..d917582 --- /dev/null +++ b/ComfySharp/Types/HistoryEntry.cs @@ -0,0 +1,4 @@ +namespace ComfySharp.Types; + +public class HistoryEntry { +} \ No newline at end of file diff --git a/ComfySharp/Types/NodeDBGenerator.cs b/ComfySharp/Types/NodeDBGenerator.cs index c0ea89b..2fd14bd 100644 --- a/ComfySharp/Types/NodeDBGenerator.cs +++ b/ComfySharp/Types/NodeDBGenerator.cs @@ -3,12 +3,14 @@ using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.Dynamic; +using System.Net; using System.Runtime.CompilerServices; namespace ComfySharp.Types; public class NodeDBGenerator { private List nodes; + private ConversionSettings settings; private List knownTypes = new(); public List GetKnownTypes() => knownTypes; @@ -20,8 +22,9 @@ public class NodeDBGenerator { private int typeFields = 0; private int enumFields = 0; - public NodeDBGenerator() { + public NodeDBGenerator(ConversionSettings settings) { nodes = new(); + this.settings = settings; } public void ResetDb() => nodes.Clear(); @@ -34,8 +37,6 @@ public class NodeDBGenerator { if (!knownTypes.Contains(type)) { knownTypes.Add(type); Console.WriteLine("Added new known type: {0}", type); - } else { - Console.WriteLine("Type {0} already known, skipped.", type); } typeFields++; } @@ -54,7 +55,7 @@ public class NodeDBGenerator { values.ToList().ForEach(value => { if (!knownEnums[type].Contains(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 (inputProperty.Value[0].ValueKind == JsonValueKind.String) 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) { + //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 enumValues = new(); inputProperty.Value[0].EnumerateArray().ToList().ForEach(value => enumValues.Add(value.ToString())); AddKnownEnum(inputProperty.Name, enumValues); - } } diff --git a/ComfySharp/conv_config.json b/ComfySharp/conv_config.json new file mode 100644 index 0000000..e69de29