Framework/Core/MapCatalog.cs

Static map id catalog for the Roleplay framework. Distinguishes built-in scenes from cloud map package idents via a regex, validates a map id with a user-facing message, and returns the hosting scene (built-in scene path or scenes/world.scene for cloud maps).

using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace RoleplayFramework.Core;

/// <summary>
/// Map ids. A map is either
/// <list type="bullet">
/// <item>a built-in scene of this project (e.g. "minimal" → scenes/minimal.scene), or</item>
/// <item>a cloud map package ident from sbox.game (e.g. "thieves.rpdowntown3t"), loaded at runtime into
/// <see cref="CloudWorldScene"/> through its networked MapInstance.</item>
/// </list>
/// </summary>
public static class MapCatalog
{
	public const string CloudWorldScene = "scenes/world.scene";

	public static readonly IReadOnlyDictionary<string, string> BuiltInScenes = new Dictionary<string, string>
	{
		["minimal"] = "scenes/minimal.scene",
	};

	static readonly Regex PackageIdent = new( @"^[a-z0-9_\-]{1,64}\.[a-z0-9_\-]{1,64}$" );

	public static bool IsBuiltIn( string map ) => map is not null && BuiltInScenes.ContainsKey( map );

	public static bool IsCloud( string map ) => map is not null && !IsBuiltIn( map ) && PackageIdent.IsMatch( map );

	/// <summary>Null if valid, else a user-facing reason.</summary>
	public static string Validate( string map )
	{
		if ( string.IsNullOrWhiteSpace( map ) ) return "No map selected.";
		if ( IsBuiltIn( map ) || IsCloud( map ) ) return null;
		return $"'{map}' is not a built-in map ({string.Join( ", ", BuiltInScenes.Keys )}) or a package ident like org.mapname.";
	}

	/// <summary>The scene that hosts this map.</summary>
	public static string SceneFor( string map ) => IsBuiltIn( map ) ? BuiltInScenes[map] : CloudWorldScene;
}