mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 12:11:34 -05:00
- fill_plate and fill_area share one MCP fill helper - FillLinear uses FillHelpers.HasOverlappingParts - BestFitCache and FillResultCache entries derive from DrawingCacheEntry - NestDefaults and ClampSettings read through SettingsFile.TryReadText - MainForm view toggles share one ToggleView handler Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
3.4 KiB
C#
112 lines
3.4 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
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 through a temporary file swapped into place, so a failed write keeps
|
|
/// the previous settings, creating the parent directory and retrying 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);
|
|
var temp = path + ".tmp";
|
|
File.WriteAllText(temp, json);
|
|
File.Move(temp, path, overwrite: true);
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads the file's text. Returns false when the file is missing (<paramref name="missing"/>
|
|
/// true) or exists but cannot be read, for example locked or access-denied.
|
|
/// </summary>
|
|
public static bool TryReadText(
|
|
string path,
|
|
[NotNullWhen(true)] out string? text,
|
|
out bool missing
|
|
)
|
|
{
|
|
text = null;
|
|
missing = string.IsNullOrWhiteSpace(path) || !File.Exists(path);
|
|
if (missing)
|
|
return false;
|
|
|
|
try
|
|
{
|
|
text = File.ReadAllText(path);
|
|
return true;
|
|
}
|
|
catch (Exception ex) when (IsUnreadableFile(ex))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|