Editor/Designers/ArchStageKey.cs

Utility that builds a cache key string for an editor preview stage. It serializes an authored object to JSON, computes an FNV-1a 32-bit hex revision from that serialization, and returns a key of the form "{kind}.{name}.{revision}".

File Access
using System.Text.Json;

namespace Sunless.Architecture;

// A preview cache key is the serialised thing itself: a key typed out field by field forgets one and then
// serves yesterday's picture for today's preset.
public static class ArchStageKey
{
	public static string Of( string kind, string name, object authored )
	{
		return $"{kind}.{name}.{Revision( JsonSerializer.Serialize( authored, authored.GetType() ) )}";
	}

	// FNV-1a: string.GetHashCode is randomised per process, and a key has to survive being written by one
	// browser and read by another in the same session.
	static string Revision( string serialized )
	{
		var hash = 2166136261u;

		foreach ( var letter in serialized )
		{
			hash = (hash ^ letter) * 16777619u;
		}

		return hash.ToString( "x8" );
	}
}