Added special cases handling and a setting class for it to be defined externally

This commit is contained in:
Samuele Lorefice
2023-12-19 01:40:58 +01:00
parent c6285c4f56
commit 4fe15a4215
5 changed files with 83 additions and 22 deletions

View File

@@ -23,14 +23,14 @@ public class ComfyClient {
nodes= new(); nodes= new();
try { try {
dbGenerator = new(ConversionSettings.FromFile("conv_config.json")); dbGenerator = new(ConversionSettings.FromFile(Path.Combine(Environment.CurrentDirectory, "conv_config.json")));
} }
catch (Exception e) { catch (Exception e) {
Console.WriteLine(e); Console.WriteLine(e);
} }
finally { finally {
ConversionSettings settings = new(); ConversionSettings settings = new();
settings.Save("conv_config.json"); settings.Save( "conv_config.json");
dbGenerator = new(settings); dbGenerator = new(settings);
Console.WriteLine("created empty settings file"); Console.WriteLine("created empty settings file");
} }
@@ -68,7 +68,7 @@ public class ComfyClient {
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>();
dbGenerator.GenerateClasses(doc);
//ObjectInfoParser.Parse(doc, out nodes); //ObjectInfoParser.Parse(doc, out nodes);
} }

View File

@@ -8,7 +8,7 @@
<ItemGroup> <ItemGroup>
<None Update="conv_config.json"> <None Update="conv_config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>

View File

@@ -1,11 +1,13 @@
using System.Text.Json; using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ComfySharp; namespace ComfySharp;
[Serializable] [DataContract, JsonSerializable(typeof(ConversionSettings))]
public class ConversionSettings { public class ConversionSettings {
public List<string> EnumConvertAsString = new(); [DataMember(IsRequired = true)] public List<string> EnumConvertAsString = new();
public List<string> EnumConvertAsBool = new(); [DataMember(IsRequired = true)] public List<string> EnumConvertAsBool = new();
public static ConversionSettings FromFile(string path) { public static ConversionSettings FromFile(string path) {
if (!File.Exists(path)) throw new FileNotFoundException("Could not find settings file", path); if (!File.Exists(path)) throw new FileNotFoundException("Could not find settings file", path);
@@ -16,7 +18,7 @@ public class ConversionSettings {
public static ConversionSettings FromJson(string json) { public static ConversionSettings FromJson(string json) {
ConversionSettings? settings = JsonSerializer.Deserialize<ConversionSettings>(json); ConversionSettings? settings = JsonSerializer.Deserialize<ConversionSettings>(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; return settings;
} }
@@ -24,6 +26,6 @@ public class ConversionSettings {
public void Save(string path) => File.WriteAllText(path, ToJson()); 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 });
} }

View File

@@ -18,6 +18,9 @@ public class NodeDBGenerator {
private Dictionary<string, List<string>> knownEnums = new(); private Dictionary<string, List<string>> knownEnums = new();
public Dictionary<string, List<string>> GetKnownEnums() => knownEnums; public Dictionary<string, List<string>> GetKnownEnums() => knownEnums;
private Dictionary<string, List<string>> knownStringLists = new();
public Dictionary<string, List<string>> GetKnownStringLists() => knownStringLists;
public int Count => nodes.Count; public int Count => nodes.Count;
private int typeFields = 0; private int typeFields = 0;
private int enumFields = 0; private int enumFields = 0;
@@ -62,6 +65,26 @@ public class NodeDBGenerator {
enumFields++; enumFields++;
} }
/// <summary>
/// Adds or updates a known string list, avoids duplicates.
/// </summary>
/// <param name="type">stringList typename</param>
/// <param name="values">Collection of string values for this StringList</param>
private void AddKnownStringList(string type, ICollection<string> 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) { public void GenerateClasses(JsonDocument document) {
foreach (var node in document.RootElement.EnumerateObject()) ScanNode(node); 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); Console.WriteLine("Encountered a special case: {0}", inputProperty.Name);
return; 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(); //holds all the values of the enum, valid for both following cases
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);
// these are all lists of strings and not enums
if (settings.EnumConvertAsString.Contains(inputProperty.Name)) {
AddKnownStringList(inputProperty.Name, enumValues);
} else {
AddKnownEnum(inputProperty.Name, enumValues);
}
} }
} }

View File

@@ -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": []
}