Files
OpenNest/OpenNest.Data/SettingsFile.cs
T
ajandClaude Opus 5.5 b550640ef0 refactor: merge duplicated geometry, settings-file and form helpers
Roslyn Bridge flagged these as copy-paste clusters; each pair now shares
one implementation so a fix lands everywhere at once:

- Shape.OffsetInward/OffsetOutward differed only in the target winding.
- BestFitFinder kept its own copy of SpatialQuery's arc-extreme points.
- SplitDrawingForm rebuilt DrawingSplitter's clip-region grid for preview.
- GeometryOptimizer's collinear/coradial finders (one misspelled
  GetCoradialArs) become one generic FindRelated.
- Five settings files repeated the write-with-retry loop, and two also
  repeated the unit parser and unreadable-file filter (SettingsFile).
- ClampsForm and SupportGridForm repeated their row/box builders.
- Vertical/HorizontalRemnantComparer share a RemnantComparer base.

Behavior is unchanged except that Engine/NestStorage settings loads now
also tolerate NotSupported/Security exceptions, matching the other
settings files' never-throw contract.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 12:59:47 -04:00

82 lines
2.5 KiB
C#

using System.Text.Json;
namespace OpenNest.Data;
/// <summary>File handling shared by the JSON settings files under %APPDATA%\OpenNest.</summary>
internal static class SettingsFile
{
/// <summary>
/// Writes the file, creating the parent directory, and retries briefly on IO collisions.
/// The last failure propagates; the host handles a persistent write failure.
/// </summary>
public static void Write(string path, string json, int maxRetries = 3)
{
var directory = Path.GetDirectoryName(Path.GetFullPath(path));
for (var attempt = 0; attempt < maxRetries; attempt++)
{
try
{
if (!string.IsNullOrEmpty(directory))
Directory.CreateDirectory(directory);
File.WriteAllText(path, json);
return;
}
catch (IOException) when (attempt < maxRetries - 1)
{
Thread.Sleep(100);
}
}
}
/// <summary>
/// Deserializes the file, or returns null when it is missing, unreadable or corrupt.
/// </summary>
public static T? Read<T>(string path, JsonSerializerOptions options)
where T : class
{
if (string.IsNullOrWhiteSpace(path) || !File.Exists(path))
return null;
try
{
return JsonSerializer.Deserialize<T>(File.ReadAllText(path), options);
}
catch (Exception ex) when (IsUnreadableFile(ex))
{
return null;
}
}
public static bool IsUnreadableFile(Exception ex) =>
ex
is JsonException
or IOException
or UnauthorizedAccessException
or NotSupportedException
or System.Security.SecurityException;
/// <summary>
/// Accepts only a defined unit name (case-insensitive). Enum.TryParse would also accept
/// numeric strings ("7") and comma-joined names, producing undefined or unintended values.
/// </summary>
public static bool TryParseUnits(JsonElement? element, out Units units)
{
units = default;
if (element is not { ValueKind: JsonValueKind.String } value)
return false;
var text = value.GetString();
foreach (var candidate in Enum.GetValues<Units>())
{
if (string.Equals(candidate.ToString(), text, StringComparison.OrdinalIgnoreCase))
{
units = candidate;
return true;
}
}
return false;
}
}