83 lines
3.8 KiB
C#
83 lines
3.8 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using PlotLine.Models;
|
|
|
|
namespace PlotLine.Services;
|
|
|
|
internal static class ProjectBackupStoryMemory
|
|
{
|
|
internal static string? EntityTable(string? type) => type?.ToLowerInvariant() switch
|
|
{
|
|
"character" => "StoryMemoryCharacters",
|
|
"asset" => "StoryMemoryAssets",
|
|
"location" => "StoryMemoryLocations",
|
|
_ => null
|
|
};
|
|
|
|
private static string? Key(JsonObject node,string name)
|
|
{
|
|
var matches=node.Select(p=>p.Key).Where(key=>key.Equals(name,StringComparison.OrdinalIgnoreCase)).ToArray();
|
|
if(matches.Length>1)throw new JsonException("Profile evidence has ambiguous property names.");
|
|
return matches.FirstOrDefault();
|
|
}
|
|
|
|
private static IEnumerable<JsonObject> EvidenceItems(JsonObject root)
|
|
{
|
|
foreach(var name in new[]{"EvidenceTimeline","Contradictions"})
|
|
{
|
|
var key=Key(root,name);
|
|
if(key is null || root[key] is null)continue;
|
|
if(root[key] is not JsonArray items)throw new JsonException("Profile evidence must be an array.");
|
|
foreach(var item in items)
|
|
{
|
|
if(item is not JsonObject evidence)throw new JsonException("Profile evidence records must be objects.");
|
|
yield return evidence;
|
|
}
|
|
}
|
|
}
|
|
|
|
internal static string? Validate(ProjectBackupExport backup)
|
|
{
|
|
var memory=new Dictionary<string,HashSet<int>>(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
["Character"]=backup.Data.StoryMemoryCharacters.Select(row=>row.StoryMemoryCharacterID).ToHashSet(),
|
|
["Asset"]=backup.Data.StoryMemoryAssets.Select(row=>row.StoryMemoryAssetID).ToHashSet(),
|
|
["Location"]=backup.Data.StoryMemoryLocations.Select(row=>row.StoryMemoryLocationID).ToHashSet()
|
|
};
|
|
foreach(var row in backup.Data.StoryMemoryRelationships)
|
|
if(string.IsNullOrEmpty(row.SourceEntityType) || string.IsNullOrEmpty(row.TargetEntityType)
|
|
|| !memory.TryGetValue(row.SourceEntityType,out var source) || !source.Contains(row.SourceEntityID)
|
|
|| !memory.TryGetValue(row.TargetEntityType,out var target) || !target.Contains(row.TargetEntityID))
|
|
return "Project backup story-memory relationship references a missing or unsupported memory entity.";
|
|
var results=backup.Data.StoryIntelligenceSceneResults.Select(row=>row.SceneResultID).ToHashSet();
|
|
try
|
|
{
|
|
foreach(var profile in backup.Data.CharacterIntelligenceProfiles)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(profile.ProfileJson))return "Project backup character profile JSON is missing.";
|
|
if(JsonNode.Parse(profile.ProfileJson) is not JsonObject root)return "Project backup character profile must contain a JSON object.";
|
|
foreach(var item in EvidenceItems(root))
|
|
{
|
|
var key=Key(item,"SceneResultID");
|
|
if(key is null || item[key] is null)continue;
|
|
if(item[key] is not JsonValue value || !value.TryGetValue<int>(out var id) || !results.Contains(id))
|
|
return "Project backup character profile evidence references a missing or foreign scene result.";
|
|
}
|
|
}
|
|
}
|
|
catch(JsonException) { return "Project backup character profile evidence JSON is invalid."; }
|
|
return null;
|
|
}
|
|
|
|
internal static string RemapProfile(string json,Func<int,int> remap)
|
|
{
|
|
var root=JsonNode.Parse(json)!.AsObject();
|
|
foreach(var item in EvidenceItems(root))
|
|
{
|
|
var key=Key(item,"SceneResultID");
|
|
if(key is not null && item[key] is JsonValue value && value.TryGetValue<int>(out var id))item[key]=remap(id);
|
|
}
|
|
return root.ToJsonString();
|
|
}
|
|
}
|