71 lines
4.6 KiB
C#
71 lines
4.6 KiB
C#
using System.Reflection;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using PlotLine.Models;
|
|
|
|
namespace PlotLine.Services;
|
|
|
|
internal static partial class ProjectBackupStoredFieldContract
|
|
{
|
|
internal static string[] Columns(string collection)=>StoredColumns.TryGetValue(collection,out var value)?value.Columns.Split(','):[];
|
|
private sealed record Field(string Name,bool LegacyProjectAudit);
|
|
private static readonly JsonDocumentOptions ReadOptions=new(){AllowTrailingCommas=true,CommentHandling=JsonCommentHandling.Skip};
|
|
private static readonly HashSet<string> MixedReferenceCollections=new(StringComparer.OrdinalIgnoreCase)
|
|
{nameof(ProjectBackupData.AssetStates),nameof(ProjectBackupData.CharacterAttributeTypes),nameof(ProjectBackupData.SceneMetricTypes)};
|
|
private static readonly HashSet<string> LegacyProjectAudit=new(StringComparer.OrdinalIgnoreCase)
|
|
{nameof(Project.CreatedByUserID),nameof(Project.ModifiedByUserID),nameof(Project.CreatedDateUTC),nameof(Project.ModifiedDateUTC)};
|
|
private static readonly Lazy<IReadOnlyDictionary<string,Field[]>> Required=new(Build);
|
|
private static IReadOnlyDictionary<string,Field[]> Build()
|
|
{
|
|
var result=new Dictionary<string,Field[]>(StringComparer.OrdinalIgnoreCase);
|
|
foreach(var contract in StoredColumns)
|
|
{
|
|
var type=contract.Key=="Project" ? typeof(Project) : typeof(ProjectBackupData).GetProperty(contract.Key)!.PropertyType.GetGenericArguments().Single();
|
|
var nullable=contract.Value.Nullable.Split(',',StringSplitOptions.RemoveEmptyEntries).ToHashSet(StringComparer.OrdinalIgnoreCase);
|
|
var fields=new List<Field>();
|
|
foreach(var column in contract.Value.Columns.Split(','))
|
|
{
|
|
var member=type.GetProperty(column,BindingFlags.Instance|BindingFlags.Public|BindingFlags.IgnoreCase)
|
|
?? throw new InvalidOperationException("Stored backup field has no model property: "+contract.Key+"."+column);
|
|
var ignore=member.GetCustomAttribute<JsonIgnoreAttribute>();
|
|
if(ignore?.Condition==JsonIgnoreCondition.Always || ignore?.Condition==JsonIgnoreCondition.WhenWritingDefault
|
|
|| ignore?.Condition==JsonIgnoreCondition.WhenWritingNull && nullable.Contains(column))continue;
|
|
fields.Add(new(member.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name ?? member.Name,contract.Key=="Project" && LegacyProjectAudit.Contains(column)));
|
|
}
|
|
result[contract.Key]=fields.ToArray();
|
|
}
|
|
return result;
|
|
}
|
|
private static JsonElement Property(JsonElement root,string name)
|
|
{
|
|
var result=default(JsonElement);
|
|
if(root.ValueKind==JsonValueKind.Object)
|
|
foreach(var property in root.EnumerateObject())if(property.Name.Equals(name,StringComparison.OrdinalIgnoreCase))result=property.Value;
|
|
return result;
|
|
}
|
|
internal static IReadOnlyList<string> Missing(string json,bool includeShared=false)
|
|
{
|
|
using var document=JsonDocument.Parse(json,ReadOptions);
|
|
var root=document.RootElement;var data=Property(root,"Data");
|
|
var version=Property(root,"FormatVersion");var legacyAudit=version.ValueKind==JsonValueKind.String && version.GetString()=="1.2";
|
|
var missing=new List<string>();
|
|
foreach(var contract in Required.Value)
|
|
{
|
|
var value=contract.Key=="Project" ? Property(root,"Project") : Property(data,contract.Key);
|
|
// Absent categories are handled using the complete safety snapshot; an explicit empty is valid.
|
|
if(contract.Key!="Project" && value.ValueKind!=JsonValueKind.Array)continue;
|
|
var rows=contract.Key=="Project" ? new[]{value} : value.EnumerateArray().ToArray();
|
|
foreach(var row in rows)
|
|
{
|
|
if(row.ValueKind!=JsonValueKind.Object){missing.Add(contract.Key+" record");continue;}
|
|
// Explicitly global reference rows are retained in place, rather than restored from author JSON.
|
|
if(!includeShared && MixedReferenceCollections.Contains(contract.Key) && Property(row,"ProjectID").ValueKind==JsonValueKind.Null)continue;
|
|
var names=row.EnumerateObject().Select(property=>property.Name).ToHashSet(StringComparer.OrdinalIgnoreCase);
|
|
foreach(var field in contract.Value)
|
|
if((!field.LegacyProjectAudit || legacyAudit) && !names.Contains(field.Name))missing.Add(contract.Key+"."+field.Name);
|
|
}
|
|
}
|
|
return missing.Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
|
|
}
|
|
}
|