diff --git a/ComfySharp/ComfyClient.cs b/ComfySharp/ComfyClient.cs index e4d1fbb..d694d49 100644 --- a/ComfySharp/ComfyClient.cs +++ b/ComfySharp/ComfyClient.cs @@ -23,14 +23,14 @@ public class ComfyClient { nodes= new(); try { - dbGenerator = new(ConversionSettings.FromFile("conv_config.json")); + dbGenerator = new(ConversionSettings.FromFile(Path.Combine(Environment.CurrentDirectory, "conv_config.json"))); } catch (Exception e) { Console.WriteLine(e); } finally { ConversionSettings settings = new(); - settings.Save("conv_config.json"); + settings.Save( "conv_config.json"); dbGenerator = new(settings); Console.WriteLine("created empty settings file"); } @@ -68,7 +68,7 @@ public class ComfyClient { if (req is { IsSuccessStatusCode: true, Content: not null }) { var doc = await req.Content.ReadFromJsonAsync(); - + dbGenerator.GenerateClasses(doc); //ObjectInfoParser.Parse(doc, out nodes); } diff --git a/ComfySharp/ComfySharp.csproj b/ComfySharp/ComfySharp.csproj index f874c67..32a1a4a 100644 --- a/ComfySharp/ComfySharp.csproj +++ b/ComfySharp/ComfySharp.csproj @@ -8,7 +8,7 @@ - PreserveNewest + Always diff --git a/ComfySharp/ConversionSettings.cs b/ComfySharp/ConversionSettings.cs index 71dc9a2..a511311 100644 --- a/ComfySharp/ConversionSettings.cs +++ b/ComfySharp/ConversionSettings.cs @@ -1,29 +1,31 @@ -using System.Text.Json; +using System.Runtime.Serialization; +using System.Text.Json; +using System.Text.Json.Serialization; namespace ComfySharp; -[Serializable] +[DataContract, JsonSerializable(typeof(ConversionSettings))] public class ConversionSettings { - public List EnumConvertAsString = new(); - public List EnumConvertAsBool = new(); - + [DataMember(IsRequired = true)] public List EnumConvertAsString = new(); + [DataMember(IsRequired = true)] 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"); + 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); - + + public string ToJson() => JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true, IncludeFields = true }); + } \ No newline at end of file diff --git a/ComfySharp/Types/NodeDBGenerator.cs b/ComfySharp/Types/NodeDBGenerator.cs index 2fd14bd..912eb82 100644 --- a/ComfySharp/Types/NodeDBGenerator.cs +++ b/ComfySharp/Types/NodeDBGenerator.cs @@ -18,6 +18,9 @@ public class NodeDBGenerator { private Dictionary> knownEnums = new(); public Dictionary> GetKnownEnums() => knownEnums; + private Dictionary> knownStringLists = new(); + public Dictionary> GetKnownStringLists() => knownStringLists; + public int Count => nodes.Count; private int typeFields = 0; private int enumFields = 0; @@ -62,6 +65,26 @@ public class NodeDBGenerator { enumFields++; } + /// + /// Adds or updates a known string list, avoids duplicates. + /// + /// stringList typename + /// Collection of string values for this StringList + private void AddKnownStringList(string type, ICollection values) { + if (!knownStringLists.ContainsKey(type)) { + knownStringLists.Add(type, values.ToList()); + Console.WriteLine("Added new known stringList: {0}", type); + } + else { + values.ToList().ForEach(value => { + if (!knownStringLists[type].Contains(value)) { + knownStringLists[type].Add(value); + Console.WriteLine("Added new value to already known stringList: {0}", value); + } + }); + } + } + public void GenerateClasses(JsonDocument document) { foreach (var node in document.RootElement.EnumerateObject()) ScanNode(node); @@ -120,13 +143,16 @@ public class NodeDBGenerator { 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(); + + List enumValues = new(); //holds all the values of the enum, valid for both following cases inputProperty.Value[0].EnumerateArray().ToList().ForEach(value => enumValues.Add(value.ToString())); - AddKnownEnum(inputProperty.Name, enumValues); + + // these are all lists of strings and not enums + if (settings.EnumConvertAsString.Contains(inputProperty.Name)) { + AddKnownStringList(inputProperty.Name, enumValues); + } else { + AddKnownEnum(inputProperty.Name, enumValues); + } } } diff --git a/ComfySharp/conv_config.json b/ComfySharp/conv_config.json index e69de29..b3b4ebb 100644 --- a/ComfySharp/conv_config.json +++ b/ComfySharp/conv_config.json @@ -0,0 +1,33 @@ +{ + "EnumConvertAsString": [ + "ckpt_name", + "image", + "lora_name", + "control_net_name", + "config_name", + "clip_name", + "clip_name1", + "clip_name2", + "unet_name", + "model_path", + "hypernetwork_name", + "model_name", + "Select to add LoRA", + "Select to add Wildcard", + "preset", + "preset_repo_id", + "checkpoint", + "video", + "model", + "bbox_detector", + "pose_estimator", + "preprocessor", + "cascade_xml", + "style", + "style1", + "style2", + "style3", + "style4" + ], + "EnumConvertAsBool": [] +} \ No newline at end of file