First review

This commit is contained in:
Peer Hogeterp committed 2026-09-24 18:06:25 +02:00
1 parent 0e63f93696
commit ad6b700978
10 files changed
+108 -99

No files matched your search

@@ -6,10 +6,12 @@ namespace AIStudio.Tools.PluginSystem;
public static class ConfigurationImportFields
{
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(ConfigurationImportFields).Namespace, nameof(ConfigurationImportFields));
public static void ValidateExportId(LuaTable table)
{
if (!Guid.TryParse(String(table, "Id"), out _))
throw new FormatException("The exported item has an invalid ID.");
throw new FormatException(TB("The exported item has an invalid ID."));
}
public static string String(LuaTable table, string name, bool required = true)
@@ -18,18 +20,18 @@ public static class ConfigurationImportFields
{
if (value.Type is LuaValueType.String && value.TryRead<string>(out var text))
return text;
throw new FormatException($"The '{name}' field must be a string.");
throw new FormatException(string.Format(TB("The '{0}' field must be a string."), name));
}
if (!required)
return string.Empty;
throw new FormatException($"The '{name}' field must be a string.");
throw new FormatException(string.Format(TB("The '{0}' field must be a string."), name));
}
public static LuaTable Table(LuaTable table, string name)
{
if (table.TryGetValue(name, out var value) && value.Type is LuaValueType.Table && value.TryRead<LuaTable>(out var nested))
return nested;
throw new FormatException($"The '{name}' field must be a table.");
throw new FormatException(string.Format(TB("The '{0}' field must be a table."), name));
}
public static T Enum<T>(LuaTable table, string name) where T : struct, Enum
@@ -37,7 +39,7 @@ public static class ConfigurationImportFields
var text = String(table, name);
if (System.Enum.TryParse<T>(text, true, out var result) && System.Enum.IsDefined(result))
return result;
throw new FormatException($"The '{name}' field has an unknown value.");
throw new FormatException(string.Format(TB("The '{0}' field has an unknown value."), name));
}
public static bool Bool(LuaTable table, string name, bool fallback = false)
@@ -46,7 +48,7 @@ public static class ConfigurationImportFields
return fallback;
if (value.Type is LuaValueType.Boolean && value.TryRead<bool>(out var result))
return result;
throw new FormatException($"The '{name}' field must be true or false.");
throw new FormatException(string.Format(TB("The '{0}' field must be true or false."), name));
}
public static int Int(LuaTable table, string name, int fallback = 0)
@@ -55,7 +57,7 @@ public static class ConfigurationImportFields
return fallback;
if (value.Type is LuaValueType.Number && value.TryRead<double>(out var number) && number >= int.MinValue && number <= int.MaxValue && number == Math.Truncate(number))
return (int)number;
throw new FormatException($"The '{name}' field must be a whole number.");
throw new FormatException(string.Format(TB("The '{0}' field must be a whole number."), name));
}
public static List<string> Strings(LuaTable table, string name)
@@ -65,7 +67,7 @@ public static class ConfigurationImportFields
for (var i = 1; i <= nested.ArrayLength; i++)
{
if (nested[i].Type is not LuaValueType.String || !nested[i].TryRead<string>(out var text) || string.IsNullOrWhiteSpace(text))
throw new FormatException($"The '{name}' field contains an invalid entry.");
throw new FormatException(string.Format(TB("The '{0}' field contains an invalid entry."), name));
result.Add(text);
}
return result;
@@ -73,6 +75,19 @@ public static class ConfigurationImportFields
public static bool IsExistingLocalFile(string path) => Path.IsPathFullyQualified(path) && File.Exists(path);
/// <summary>Lists the given references as a warning, or returns an empty text when nothing is missing.</summary>
public static string UnavailableReferencesIssue(IReadOnlyCollection<string> missing) => missing.Count == 0
? string.Empty
: string.Format(TB("Unavailable references: {0}. Review the selections before saving."), string.Join(", ", missing));
public static string MissingProviderReference(string id) => string.Format(TB("provider {0}"), id);
public static string MissingProfileReference(string id) => string.Format(TB("profile {0}"), id);
public static string MissingToolReference(string id) => string.Format(TB("tool {0}"), id);
public static string MissingDataSourceReference(string id) => string.Format(TB("data source {0}"), id);
public static string Credential(LuaTable table, string name, out string issue, EnterpriseEncryption? decryptionService = null)
{
issue = string.Empty;
@@ -80,11 +95,11 @@ public static class ConfigurationImportFields
if (string.IsNullOrEmpty(encrypted))
return string.Empty;
if (!EnterpriseEncryption.IsEncrypted(encrypted))
throw new FormatException($"The '{name}' field must contain an ENC:v1 credential.");
throw new FormatException(string.Format(TB("The '{0}' field must contain an ENC:v1 credential."), name));
var encryption = decryptionService ?? PluginFactory.EnterpriseEncryption;
if (encryption?.IsAvailable == true && encryption.TryDecrypt(encrypted, out var decrypted))
return decrypted;
issue = "The embedded credential could not be decrypted on this device. Enter your own credential before saving.";
issue = TB("The embedded credential could not be decrypted on this device. Enter your own credential before saving.");
return string.Empty;
}
}
}