using OpenTap.Addin.Entity;
|
using System.Collections.Concurrent;
|
using System.Collections.Generic;
|
using System.Dynamic;
|
|
namespace OpenTap.Addin
|
{
|
public class VariableConte : DynamicObject
|
{
|
private static readonly char SPLIT_MARK = '.';
|
|
private readonly ConcurrentDictionary<string, RuntimeVariable> _____Variables_____;
|
|
|
private readonly string _____name_____;
|
|
public VariableConte(string name, ConcurrentDictionary<string, RuntimeVariable> variables)
|
{
|
_____name_____ = name;
|
_____Variables_____ = variables;
|
}
|
|
public override bool TryGetMember(GetMemberBinder binder, out dynamic result)
|
{
|
result = null;
|
string name = binder.Name;
|
var type = binder.ReturnType;
|
|
if (_____Variables_____.TryGetValue(name, out var variable))
|
{
|
if (type == typeof(RuntimeVariable))
|
{
|
result = variable;
|
}
|
else
|
{
|
result = variable;//.data;
|
}
|
return true;
|
}
|
result = new NullVariable($"{this._____name_____}.{name}");
|
return true;
|
}
|
|
public override bool TrySetMember(SetMemberBinder binder, object value)
|
{
|
string name = binder.Name;
|
if (_____Variables_____.TryGetValue(name, out var variable))
|
{
|
if (value is RuntimeVariable rv)
|
{
|
// 引用传递
|
_____Variables_____[name] = rv;
|
}
|
else
|
{
|
variable.__data___ = NumberData.Format(value);
|
}
|
return true;
|
}
|
else
|
{
|
if (value is RuntimeVariable rv)
|
{
|
// 引用传递
|
_____Variables_____[name] = rv;
|
}
|
else
|
{
|
var v = new RuntimeVariable(null, name, NumberData.Format(value));
|
_____Variables_____[name] = v;
|
}
|
|
return true;
|
}
|
}
|
|
public void Merge(VariableConte source)
|
{
|
if (source?._____Variables_____ != null)
|
{
|
foreach (var key in source._____Variables_____.Keys)
|
{
|
_____Variables_____[key] = source._____Variables_____[key];
|
}
|
}
|
}
|
|
public void Merge(ConcurrentDictionary<string, RuntimeVariable> Variables)
|
{
|
if (Variables != null)
|
{
|
foreach (var key in Variables.Keys)
|
{
|
this._____Variables_____[key] = Variables[key];
|
}
|
}
|
}
|
|
public static VariableConte FromTestVariables(string name, IEnumerable<TestVariable> variables)
|
{
|
var runtimeVariablePool = new ConcurrentDictionary<string, RuntimeVariable>();
|
foreach (var data in variables)
|
{
|
RuntimeVariable runTime = data.ToRuntime(null);
|
if (runTime is null) continue;
|
runtimeVariablePool[runTime.__name___] = runTime;
|
}
|
return new VariableConte(name, runtimeVariablePool);
|
}
|
}
|
}
|