PlotDirector/PlotLine/Services/ProjectBackupMapValidation.cs

55 lines
4.5 KiB
C#

using PlotLine.Models;
namespace PlotLine.Services;
// Validate the exported graph before allocating a new project or remapping any identifiers.
internal static class ProjectBackupMapValidation
{
public static string? Validate(ProjectBackupExport backup)
{
if (backup.Data is null || typeof(ProjectBackupData).GetProperties().Any(property => property.GetValue(backup.Data) is null))
return "Project backup data contains a null entity collection.";
var data=backup.Data;
if (typeof(ProjectBackupData).GetProperties().Any(property => ((System.Collections.IEnumerable)property.GetValue(data)!).Cast<object?>().Any(item=>item is null)))
return "Project backup contains a null entity record.";
if(data.MapFeatures.Any(x=>x.Type is null || x.Layer is null || x.GeometryJson is null)) return "Project backup contains invalid map feature fields.";
if (data.StoryMaps.Any(x=>x is null) || data.MapFeatures.Any(x=>x is null) || data.MapRelationships.Any(x=>x is null)
|| data.MapJourneyNotes.Any(x=>x is null) || data.MapReferenceImages.Any(x=>x is null))
return "Project backup contains a null map record.";
var mapIds=data.StoryMaps.Select(x=>x.MapID).ToHashSet();
if(mapIds.Count!=data.StoryMaps.Count || mapIds.Any(id=>id<=0)) return "Project backup map identifiers must be positive and unique.";
var imageIds=data.MapReferenceImages.Select(x=>x.ImageID).ToHashSet();
if(imageIds.Count!=data.MapReferenceImages.Count || imageIds.Contains(Guid.Empty)) return "Project backup reference image identifiers must be unique.";
if(data.MapReferenceImages.Any(image=>image.ProjectID!=backup.Project?.ProjectID || !MapReferenceImageService.ValidBackupImage(image)))
return "Project backup contains invalid or foreign reference images.";
var maps=data.StoryMaps.ToDictionary(x=>x.MapID);
foreach(var map in data.StoryMaps)
{
if(map.ProjectID!=backup.Project?.ProjectID) return "Project backup contains a map belonging to another project.";
var error=MapValidation.Validate(new() { Map=map, Features=data.MapFeatures.Where(x=>x.MapID==map.MapID).ToList(), Relationships=data.MapRelationships.Where(x=>x.MapID==map.MapID).ToList() });
if(error is not null) return "Project backup map is invalid: "+error;
var placement=MapAppearanceJson.Read<MapReferencePlacement>(map.ReferenceJson);
if(placement.ImageID.HasValue && !imageIds.Contains(placement.ImageID.Value)) return "Project backup map references a missing image.";
if(map.ParentMapID.HasValue && !mapIds.Contains(map.ParentMapID.Value)) return "Project backup map references a missing parent map.";
if(map.ParentFeatureID.HasValue && (!map.ParentMapID.HasValue || !data.MapFeatures.Any(x=>x.MapID==map.ParentMapID && x.FeatureID==map.ParentFeatureID)))
return "Project backup map references a missing parent feature.";
var visited=new HashSet<int>();var current=map;
while(current.ParentMapID.HasValue)
{
if(!visited.Add(current.MapID)) return "Project backup map hierarchy contains a cycle.";
if(!maps.TryGetValue(current.ParentMapID.Value,out var parent)) return "Project backup map references a missing parent map.";
current=parent;
}
}
var locations=data.Locations.Select(x=>x.LocationID).ToHashSet();var floors=data.FloorPlans.Select(x=>x.FloorPlanID).ToHashSet();
if(data.MapFeatures.Any(x=>!mapIds.Contains(x.MapID) || x.LocationID.HasValue&&!locations.Contains(x.LocationID.Value) || x.FloorPlanID.HasValue&&!floors.Contains(x.FloorPlanID.Value)))
return "Project backup map feature references a missing map, location or floor plan.";
if(data.MapRelationships.Any(x=>!mapIds.Contains(x.MapID))) return "Project backup relationship references a missing map.";
var scenes=data.Scenes.Select(x=>x.SceneID).ToHashSet();var characters=data.Characters.Select(x=>x.CharacterID).ToHashSet();
if(data.MapJourneyNotes.Any(x=>!mapIds.Contains(x.MapID) || !characters.Contains(x.CharacterID) || !scenes.Contains(x.FromSceneID) || !scenes.Contains(x.ToSceneID)
|| x.RouteFeatureID.HasValue&&!data.MapFeatures.Any(f=>f.MapID==x.MapID && f.FeatureID==x.RouteFeatureID && f.GeometryClass=="Path")))
return "Project backup journey references a missing map, character, scene or route.";
return null;
}
}