Editor/Data/ArchArchetypeCatalog.cs

Catalog for architecture archetypes in the editor. It exposes shelf identification, naming, lists built-in shipped archetypes and loads authored archetype JSON files from disk, and saves an archetype to the archetype path.

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

namespace Sunless.Architecture;

// The second shelf core stands itself, and for the same reason it stands the building subtool: a building type is
// the plan document's own vocabulary. It names the opening presets a wall cuts, the pillar kinds a beam wears and
// the option groups the sidebar shows, none of which belong to the module that later builds them.
//
// Nothing is written out on first use, unlike the pillar catalog. A shipped type stands whether or not the project
// has a folder, an authored file of the same name tops it, and a file that says Hidden takes it off the shelf - so
// removing a type core ships is a thing the next load respects rather than undoes.
public sealed class ArchArchetypeCatalog : IArchCatalog
{
	const string Suffix = ".archetype.json";

	public ArchShelf Shelf => ArchShelf.Types;

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

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

	public IEnumerable<object> Authored()
	{
		var loaded = new List<ArchArchetype>();

		foreach ( var file in ArchStorage.DocumentFiles( ArchStorage.ArchetypeDirectory, Suffix ) )
		{
			// A type with no steps is rules-only; requiring steps made the plain house vanish.
			if ( ArchStorage.ReadDocument<ArchArchetype>( $"{ArchStorage.ArchetypeDirectory}/{file}" ) is { Name.Length: > 0 } type )
			{
				loaded.Add( type );
			}
		}

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

	public bool Save( object entry )
	{
		return entry is ArchArchetype type && ArchStorage.WriteDocument( ArchStorage.ArchetypePathFor( type.Name ), type );
	}
}