Editor/ProjectTools.cs

Editor MCP toolset named "project". Reads the project's physics collision rules (per-tag defaults, explicit tag pairs, effective result for two tags) and writes a rule via SetCollisionRule, normalizing tags to lowercase and saving ProjectSettings/Collision.config.

using System;
using System.Collections.Generic;
using System.Linq;
using Editor;
using Editor.Mcp;
using Sandbox;
using Sandbox.Physics;

namespace LevelTools;

[McpToolset( "project", "Project settings an agent cannot reach cleanly elsewhere - the physics collision rule matrix" )]
public static class ProjectTools
{
    /// <summary>
    /// Read the project's collision rules: each tag's default result and every tag pair's explicit result.
    /// A pair's rule wins over the defaults; with no pair, the less colliding of the two defaults applies,
    /// and Unset means Collide. Give two tags to also get the Effective result between them.
    /// </summary>
    /// <param name="tagA">First tag, for the Effective result. Optional.</param>
    /// <param name="tagB">Second tag, for the Effective result. Optional.</param>
    [McpTool.ReadOnly( "project_get_collision" )]
    public static object GetCollision( string tagA = "", string tagB = "" )
    {
        var rules = ProjectSettings.Collision;

        var defaults = rules.Defaults.OrderBy( x => x.Key, StringComparer.OrdinalIgnoreCase )
            .Select( x => new { Tag = x.Key, Result = x.Value.ToString() } ).ToList();
        var pairs = rules.Pairs.Where( x => x.Value != CollisionRules.Result.Unset )
            .OrderBy( x => x.Key.Left, StringComparer.OrdinalIgnoreCase ).ThenBy( x => x.Key.Right, StringComparer.OrdinalIgnoreCase )
            .Select( x => new { A = x.Key.Left, B = x.Key.Right, Result = x.Value.ToString() } ).ToList();
        var tags = rules.Tags.OrderBy( x => x, StringComparer.OrdinalIgnoreCase ).ToList();

        if ( string.IsNullOrWhiteSpace( tagA ) || string.IsNullOrWhiteSpace( tagB ) )
            return new { Defaults = defaults, Pairs = pairs, Tags = tags };

        var effective = rules.GetCollisionRule( Tag( tagA ), Tag( tagB ) ).ToString();
        return new { Defaults = defaults, Pairs = pairs, Tags = tags, Effective = effective };
    }

    /// <summary>
    /// Set a collision rule and save it to ProjectSettings/Collision.config immediately. With tagB, sets the
    /// rule for that pair of tags (order doesn't matter); without it, sets tagA's default. Unset removes the
    /// entry. Tags are lowercased and trimmed. The user cannot undo this - to undo it, set the returned
    /// Before value back.
    /// </summary>
    /// <param name="tagA">A tag, e.g. 'player'.</param>
    /// <param name="result">Collide, Trigger (no collision, but touch callbacks), Ignore, or Unset to remove the entry.</param>
    /// <param name="tagB">Second tag for a pair rule. Empty sets tagA's default.</param>
    [McpTool( "project_set_collision_rule" )]
    public static object SetCollisionRule( string tagA, CollisionRules.Result result, string tagB = "" )
    {
        var rules = ProjectSettings.Collision;
        var a = Tag( tagA );
        var isPair = !string.IsNullOrWhiteSpace( tagB );
        var b = isPair ? Tag( tagB ) : null;

        CollisionRules.Result before;

        if ( isPair )
        {
            CollisionRules.Pair key = (a, b);
            before = rules.Pairs.GetValueOrDefault( key );

            if ( result == CollisionRules.Result.Unset ) rules.Pairs.Remove( key );
            else rules.Pairs[key] = result;
        }
        else
        {
            // Defaults is case-sensitive: fold any differently-cased entry into the lowercase one
            var existing = rules.Defaults.Keys.Where( k => string.Equals( k, a, StringComparison.OrdinalIgnoreCase ) ).ToList();
            before = existing.Count > 0 ? rules.Defaults[existing[0]] : CollisionRules.Result.Unset;

            foreach ( var k in existing ) rules.Defaults.Remove( k );
            if ( result != CollisionRules.Result.Unset ) rules.Defaults[a] = result;
        }

        EditorUtility.SaveProjectSettings( rules, "Collision.config" );

        if ( !isPair )
            return new { Key = a, Before = before.ToString(), After = result.ToString() };

        return new { Key = $"{a}, {b}", Before = before.ToString(), After = result.ToString(), Effective = rules.GetCollisionRule( a, b ).ToString() };
    }

    // Tags are lowercase in the engine, but the rule dictionaries compare as given
    static string Tag( string tag )
    {
        var t = (tag ?? "").Trim().ToLowerInvariant();

        if ( t.Length == 0 )
            throw new Exception( "Give a tag, e.g. 'player'" );
        if ( t.Any( char.IsWhiteSpace ) )
            throw new Exception( $"'{tag}' has whitespace inside it - a tag is one word, e.g. 'player_clip'" );

        return t;
    }
}