Editor/Pillar/ArchPillarCatalog.cs

Editor catalog for pillar types. It exposes shelf and naming helpers, ensures default pillar types are written to disk, loads authored pillar type files from arch/pillars, and saves individual pillar types to disk.

File Access
using System.Collections.Generic;
using System.Linq;

namespace Sunless.Architecture;

// The only catalog that writes what it ships out as real files, because a pillar type is edited far more often
// than a preset is: an author opens the folder and tunes one rather than typing a new one from nothing.
public sealed class ArchPillarCatalog : IArchCatalog
{
	public const string Directory = "arch/pillars";

	public ArchShelf Shelf => ArchShelf.Pillars;

	public string Named( object entry ) => entry is ArchPillarType type ? type.Name : null;

	public IEnumerable<object> Shipped() => ArchPillarTypes.Defaults();

	public IEnumerable<object> Authored()
	{
		foreach ( var built in ArchPillarTypes.Defaults() )
		{
			var path = PathFor( built.Name );

			if ( !ArchStorage.DocumentExists( path ) )
			{
				ArchStorage.WriteDocument( path, built );
			}
		}

		var loaded = new List<ArchPillarType>();

		foreach ( var file in ArchStorage.DocumentFiles( Directory, ".pillartype.json" ) )
		{
			if ( ArchStorage.ReadDocument<ArchPillarType>( $"{Directory}/{file}" ) is { Name.Length: > 0, Column: not null } type )
			{
				loaded.Add( type );
			}
		}

		return loaded.OrderBy( type => type.Title ).ToList();
	}

	public bool Save( object entry ) => entry is ArchPillarType type && ArchStorage.WriteDocument( PathFor( type.Name ), type );

	static string PathFor( string name ) => $"{Directory}/{name}.pillartype.json";
}