using Microsoft.Win32;
|
using OpenTap.Addin;
|
using System;
|
using System.Collections.Concurrent;
|
using System.Collections.Generic;
|
using System.Collections.ObjectModel;
|
using System.IO;
|
using UtilLib;
|
|
namespace AddInPlugin
|
{
|
public class DefaultStationGlobalsManager : IStationGlobalsManager
|
{
|
protected string GetRegister()
|
{
|
string Company = "JingCe", Program = "SequenceEditor";
|
|
string registryPath = $@"SOFTWARE\{Company}\{Program}";
|
// 尝试打开注册表键
|
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(registryPath))
|
{
|
if (key == null)
|
{
|
throw new Exception("注册表不存在,请先运行一次编辑器");
|
}
|
|
string value = key.GetValue("Location")?.ToString();
|
if (string.IsNullOrEmpty(value))
|
{
|
throw new Exception("注册表不存在,请先运行一次编辑器");
|
}
|
return value;
|
}
|
}
|
|
public virtual IEnumerable<TestVariable> ReadFromLocal()
|
{
|
string path = Path.Combine(GetRegister(), "Config", "StationGlobals.data");
|
lock (this)
|
{
|
if (!File.Exists(path))
|
{
|
return new ObservableCollection<TestVariable>();
|
}
|
return XmlHelper.DeserializeFromXml<ObservableCollection<TestVariable>>(path);
|
}
|
}
|
|
public virtual ConcurrentDictionary<string, RuntimeVariable> ToRuntime()
|
{
|
var VariablePool = new ConcurrentDictionary<string, RuntimeVariable>();
|
foreach (var data in ReadFromLocal())
|
{
|
RuntimeVariable runTime = data.ToRuntime();
|
if (runTime == null) continue;
|
VariablePool[runTime.Name] = runTime;
|
}
|
return VariablePool;
|
}
|
|
public virtual bool WriteToLocal(IEnumerable<TestVariable> datas)
|
{
|
string path = Path.Combine(GetRegister(), "Config", "StationGlobals.data");
|
lock (this)
|
{
|
XmlHelper.SerializeToXml(path, datas);
|
return true;
|
}
|
}
|
}
|
}
|