Player/Ghost/GhostRecording.cs

A simple data container representing a single-lap ghost recording. It stores the car resource path and a serialized MovieClip as JSON, exposes the clip via a property that serializes/deserializes to ClipJson, and holds an optional player name.

File Access
using Sandbox.MovieMaker.Compiled;
using System.Text.Json.Serialization;

namespace Machines.Ghost;

/// <summary>
/// A ghost recording for one lap. Contains the car type and a serialized movie clip.
/// </summary>
public class GhostRecording : IValid
{
	/// <summary>
	/// The car resource path used during this recording.
	/// </summary>
	public string CarResourcePath { get; set; }

	/// <summary>
	/// Player name shown on the ghost nameplate.
	/// </summary>
	[JsonIgnore]
	public string PlayerName { get; set; }

	[JsonIgnore]
	public MovieClip Clip
	{
		get => Json.Deserialize<MovieClip>( ClipJson );
		set => ClipJson = Json.Serialize( value );
	}

	/// <summary>
	/// The movie clip serialized as a JSON string.
	/// </summary>
	public string ClipJson { get; set; }

	/// <summary>
	/// True when the recording has valid clip data.
	/// </summary>
	[JsonIgnore]
	public bool IsValid => Clip is not null;

	/// <summary>
	/// Creates a <see cref="GhostRecording"/> from a MovieClip.
	/// </summary>
	public static GhostRecording Create( string carResourcePath, MovieClip clip, string playerName = null )
	{
		return new GhostRecording
		{
			CarResourcePath = carResourcePath,
			PlayerName = playerName,
			Clip = clip
		};
	}
}