79 lines
4.6 KiB
C#
79 lines
4.6 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using PlotLine.Models;
|
|
|
|
namespace PlotLine.Services;
|
|
|
|
internal static class ProjectBackupCollectionPresence
|
|
{
|
|
private static readonly JsonDocumentOptions ReadOptions=new(){AllowTrailingCommas=true,CommentHandling=JsonCommentHandling.Skip};
|
|
private static readonly HashSet<string> DiagnosticCollections=new(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
nameof(ProjectBackupData.GenreMetricPresets),nameof(ProjectBackupData.GenreMetricPresetMetrics),
|
|
nameof(ProjectBackupData.ProjectAccess),nameof(ProjectBackupData.UserFiles)
|
|
};
|
|
private static readonly HashSet<string> MixedReferenceCollections=new(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
nameof(ProjectBackupData.AssetStates),nameof(ProjectBackupData.CharacterAttributeTypes),nameof(ProjectBackupData.SceneMetricTypes)
|
|
};
|
|
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;
|
|
}
|
|
private static HashSet<string> Fields(JsonElement data)=>data.ValueKind==JsonValueKind.Object
|
|
? data.EnumerateObject().Select(property=>property.Name).ToHashSet(StringComparer.OrdinalIgnoreCase)
|
|
: new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
internal static string? ValidateRequiredShape(string json)
|
|
{
|
|
using var document=JsonDocument.Parse(json,ReadOptions);
|
|
return ValidateRequiredShape(document.RootElement);
|
|
}
|
|
internal static string? ValidateRequiredShape(JsonElement root)
|
|
=> Property(root,"Format").ValueKind!=JsonValueKind.String || Property(root,"FormatVersion").ValueKind!=JsonValueKind.String
|
|
|| Property(root,"Data").ValueKind!=JsonValueKind.Object
|
|
? "Project backup format, version and data must be explicitly present. No project data was changed." : null;
|
|
|
|
internal static IReadOnlyList<string> AbsentPersistentCollections(string originalJson)
|
|
{
|
|
using var document=JsonDocument.Parse(originalJson,ReadOptions);
|
|
var names=Fields(Property(document.RootElement,"Data"));
|
|
return typeof(ProjectBackupData).GetProperties().Select(property=>property.Name)
|
|
.Where(name=>!DiagnosticCollections.Contains(name) && !names.Contains(name)).ToArray();
|
|
}
|
|
|
|
internal static IReadOnlyList<string> MissingPersistentCollections(string originalJson,string safetyJson,int projectId)
|
|
{
|
|
using var original=JsonDocument.Parse(originalJson,ReadOptions);
|
|
using var safety=JsonDocument.Parse(safetyJson,ReadOptions);
|
|
var sourceNames=Fields(Property(original.RootElement,"Data"));
|
|
var current=Property(safety.RootElement,"Data");
|
|
if(current.ValueKind!=JsonValueKind.Object)throw new InvalidDataException("Safety backup data could not be verified.");
|
|
var missing=new List<string>();
|
|
foreach(var collection in current.EnumerateObject())
|
|
{
|
|
if(sourceNames.Contains(collection.Name) || DiagnosticCollections.Contains(collection.Name)
|
|
|| collection.Value.ValueKind!=JsonValueKind.Array || collection.Value.GetArrayLength()==0)continue;
|
|
if(MixedReferenceCollections.Contains(collection.Name) && !collection.Value.EnumerateArray().Any(row=>
|
|
Property(row,"ProjectID") is var value && value.ValueKind==JsonValueKind.Number && value.TryGetInt32(out var id) && id==projectId))continue;
|
|
missing.Add(collection.Name);
|
|
}
|
|
return missing;
|
|
}
|
|
|
|
internal static bool HasRecovery(string json){using var d=JsonDocument.Parse(json,ReadOptions);var state=Property(d.RootElement,"Recovery");return state.ValueKind==JsonValueKind.Object&&Property(state,"NativeReviews").ValueKind==JsonValueKind.Array&&Property(state,"IntentAssessments").ValueKind==JsonValueKind.Array&&Property(state,"ExternalReferences").ValueKind==JsonValueKind.Array;}
|
|
internal static string PreserveOmissions(string originalJson,string mappedJson)
|
|
{
|
|
using var original=JsonDocument.Parse(originalJson,ReadOptions);
|
|
var sourceNames=Fields(Property(original.RootElement,"Data"));
|
|
var mapped=JsonNode.Parse(mappedJson)!.AsObject();
|
|
var data=mapped.First(property=>property.Key.Equals("Data",StringComparison.OrdinalIgnoreCase)).Value!.AsObject();
|
|
foreach(var name in data.Select(property=>property.Key).ToArray())
|
|
if(!sourceNames.Contains(name))data.Remove(name);
|
|
return mapped.ToJsonString();
|
|
}
|
|
}
|