Files
sbox-public/game/editor/ActionGraph/Code/UserDataProperty.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

64 lines
1011 B
C#

using System;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace Editor.ActionGraphs
{
public interface IUserDataProperty
{
void Invalidate();
}
public record UserDataProperty<T> : IUserDataProperty
{
public JsonObject UserData { get; }
public string Key { get; }
public T DefaultValue { get; }
private T _value;
public UserDataProperty( JsonObject userData, string key, T defaultValue = default )
{
UserData = userData;
Key = key;
DefaultValue = defaultValue;
Invalidate();
}
public void Invalidate()
{
_value = DefaultValue;
if ( !UserData.TryGetPropertyValue( Key, out var node ) )
{
return;
}
try
{
_value = node.Deserialize<T>();
}
catch ( Exception e )
{
Log.Error( e );
}
}
public T Value
{
get => _value;
set
{
if ( (_value ?? DefaultValue).Equals( value ) )
{
return;
}
_value = value;
UserData[Key] = JsonSerializer.SerializeToNode( value );
}
}
}
}