Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions RPGModder.Core/Merge/AddedIdIndex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Newtonsoft.Json.Linq;

namespace RPGModder.Core.Merge;

/// <summary>
/// The ids a change set adds, by kind and scope (GR-R2, GR-R3). An op the builder writes for a record vanilla does
/// not have is the only kind of added id: a map event is an <c>AddRecord</c> on <c>event:n</c> of a vanilla map; a
/// map is a whole new <c>MapNNN.json</c>, its <c>MapInfos</c> record, or both. Step 1 lists these two kinds only.
/// </summary>
internal static class AddedIdIndex
{
public static IReadOnlyList<AddedId> Of(ModChangeSet changeSet)
{
var events = new List<AddedId>();
var maps = new SortedDictionary<int, (JToken? File, JToken? Info)>();
foreach (ChangeOp op in changeSet.Ops)
{
if (op.Kind != ChangeKind.AddRecord || op.Path.Length != 0 || op.Value is null)
{
continue;
}

if (op.Key.IsWholeFile && RpgDataSchema.TryGetMapId(op.Key.File, out int mapId))
{
maps[mapId] = (op.Value, maps.GetValueOrDefault(mapId).Info);
}
else if (op.Key.File == RpgDataSchema.MapInfosKey && RecordKey.TryGetIndex(op.Key.Record, out int infoId) && infoId > 0)
{
maps[infoId] = (maps.GetValueOrDefault(infoId).File, op.Value);
}
else if (RpgDataSchema.IsMapFile(op.Key.File) && RecordKey.TryGetEvent(op.Key.Record, out int eventId) && eventId > 0)
{
events.Add(new AddedId(IdKind.MapEvent, op.Key.File, eventId, op.Value));
}
}

var result = new List<AddedId>(events);
foreach ((int id, (JToken? file, JToken? info)) in maps)
{
// Both halves are the content, cloned so no op gains a parent; two mods that add one map with one tree entry are one map (GR-R3).
result.Add(new AddedId(IdKind.Map, "", id, new JArray(file?.DeepClone() ?? JValue.CreateNull(), info?.DeepClone() ?? JValue.CreateNull())));
}

return result;
}

/// <summary>True when the change set still adds <paramref name="remap"/>'s old id; otherwise the entry is inactive (GR-R12).</summary>
public static bool IsActive(IdRemap remap, IReadOnlyList<AddedId> added)
{
return added.Any(item => item.Kind == remap.Kind && item.Scope == remap.Scope && item.Id == remap.FromId);
}
}

/// <summary>One added id and the content that decides whether two mods add the same thing.</summary>
internal sealed record AddedId(IdKind Kind, string Scope, int Id, JToken Content);
10 changes: 10 additions & 0 deletions RPGModder.Core/Merge/BaseFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace RPGModder.Core.Merge;

/// <summary>
/// A vanilla file, or the live config save (GR-M10). <c>Text</c> is set when
/// <see cref="RpgDataSchema.KindOf"/> is not <see cref="MergeFileKind.Opaque"/>; <c>Sha256</c> is lower-case hex.
/// </summary>
public sealed record BaseFile(
string Path,
string? Text,
string Sha256);
37 changes: 37 additions & 0 deletions RPGModder.Core/Merge/BaseFileSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace RPGModder.Core.Merge;

/// <summary>
/// The base files of one plan, looked up by key. A path with no entry means vanilla does not have the file (GR-M9).
/// </summary>
public sealed class BaseFileSet
{
private readonly Dictionary<string, BaseFile> _byKey;

private BaseFileSet(Dictionary<string, BaseFile> byKey)
{
_byKey = byKey;
}

/// <summary>Two entries with one key are a caller bug, because the plan would depend on which one wins.</summary>
public static BaseFileSet From(IEnumerable<BaseFile> files)
{
ArgumentNullException.ThrowIfNull(files);

var byKey = new Dictionary<string, BaseFile>(StringComparer.Ordinal);
foreach (BaseFile file in files)
{
string key = MergePath.ToKey(file.Path);
if (!byKey.TryAdd(key, file))
{
throw new ArgumentException($"Two base files share the key {key}.", nameof(files));
}
}

return new BaseFileSet(byKey);
}

public BaseFile? Find(string path)
{
return _byKey.GetValueOrDefault(MergePath.ToKey(path));
}
}
12 changes: 12 additions & 0 deletions RPGModder.Core/Merge/ChangeKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace RPGModder.Core.Merge;

public enum ChangeKind
{
SetLeaf,
RemoveLeaf,
AddRecord,
RemoveRecord,
EditSequence,
EditCommands,
ReplaceFile
}
15 changes: 15 additions & 0 deletions RPGModder.Core/Merge/ChangeOp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Newtonsoft.Json.Linq;

namespace RPGModder.Core.Merge;

/// <summary>
/// One change a mod makes against vanilla (GR-M1). <c>Value</c> is <c>null</c> for the remove kinds; a leaf
/// set to JSON <c>null</c> holds a <see cref="JValue"/> of type null instead.
/// </summary>
public sealed record ChangeOp(
string ModId,
RecordKey Key,
string Path,
ChangeKind Kind,
JToken? Value,
string? SourceFile);
Loading
Loading