mirror of
https://github.com/Facepunch/sbox-public.git
synced 2025-12-23 22:48:07 -05:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
49 lines
1012 B
C#
49 lines
1012 B
C#
|
|
|
|
namespace Sandbox.Bind;
|
|
|
|
/// <summary>
|
|
/// A proxy where set and get are done via functions
|
|
/// </summary>
|
|
internal class MethodProxy<T> : Proxy
|
|
{
|
|
public Func<T> Read { get; set; }
|
|
public Action<T> Write { get; set; }
|
|
|
|
public MethodProxy( Func<T> read, Action<T> write )
|
|
{
|
|
Name = "[MethodBind]";
|
|
Read = read;
|
|
Write = write;
|
|
}
|
|
|
|
public MethodProxy( object target, Func<T> read, Action<T> write )
|
|
{
|
|
Target = new WeakReference<object>( target );
|
|
Name = "[MethodBind]";
|
|
Read = read;
|
|
Write = write;
|
|
}
|
|
|
|
public override object Value
|
|
{
|
|
get => Read.Invoke();
|
|
|
|
set
|
|
{
|
|
if ( !CanWrite ) throw new System.NotSupportedException( "Binding is read only" );
|
|
|
|
if ( !Translation.TryConvert( value, typeof( T ), out var targetValue ) )
|
|
return;
|
|
|
|
Write( (T)targetValue );
|
|
}
|
|
}
|
|
|
|
public override bool CanRead => Read != null;
|
|
public override bool CanWrite => Write != null;
|
|
public override bool IsValid => CanRead || CanWrite;
|
|
|
|
public override string ToString() => $"[MethodBind]";
|
|
}
|