using System.IO; using Facepunch.ActionGraphs; namespace Sandbox.ActionGraphs; /// /// A that provides . /// public interface ISerializationOptionProvider : ISourceLocation { SerializationOptions SerializationOptions { get; } } /// /// Source location for action graphs that belong to a Hammer map. This is used for stack /// traces, and for knowing which map to save when editing a graph. /// public sealed class MapSourceLocation : ISerializationOptionProvider { private static Dictionary Cached { get; } = new( StringComparer.OrdinalIgnoreCase ); /// /// Gets a from a path name. /// /// Project-relative map path ending with ".vmap" or ".vpk". public static MapSourceLocation Get( string mapPathName ) { ArgumentNullException.ThrowIfNull( mapPathName, nameof( mapPathName ) ); mapPathName = NormalizeMapPathName( mapPathName ); if ( Path.IsPathRooted( mapPathName ) ) { throw new ArgumentException( "Expected a relative path.", nameof( mapPathName ) ); } if ( Cached.TryGetValue( mapPathName, out var cached ) ) return cached; return Cached[mapPathName] = cached = new MapSourceLocation( mapPathName ); } private static string NormalizeMapPathName( string mapPathName ) { if ( mapPathName.EndsWith( ".vmap", StringComparison.OrdinalIgnoreCase ) ) { mapPathName = Path.ChangeExtension( mapPathName, ".vpk" ); } mapPathName = mapPathName.NormalizeFilename( false, false ); return mapPathName; } public string MapPathName { get; } public SerializationOptions SerializationOptions { get; } private MapSourceLocation( string mapPathName ) { MapPathName = mapPathName; SerializationOptions = new( Cache: new ActionGraphCache(), WriteCacheReferences: false, ForceUpdateCached: true, SourceLocation: this, ImpliedTarget: null ); } public override string ToString() { return $"Map:{MapPathName}"; } } /// /// Source location for action graphs that belong to a . /// These can include scenes and prefabs, or custom resources. This is used for stack /// traces, and for knowing which asset to save when editing a graph. /// /// Resource that contains action graphs. public record GameResourceSourceLocation( GameResource Resource ) : ISourceLocation { public override string ToString() { return Resource.ToString(); } }