mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-01-20 04:10:00 -05:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
64 lines
1011 B
C#
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 );
|
|
}
|
|
}
|
|
}
|
|
}
|