273 lines
10 KiB
C#
273 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using ThreatSource.Missile;
|
|
using ThreatSource.Indicator;
|
|
using ThreatSource.Utils;
|
|
using ThreatSource.Simulation;
|
|
|
|
namespace ThreatSource.Data
|
|
{
|
|
/// <summary>
|
|
/// 威胁源数据管理器,负责加载和管理所有设备的配置数据
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 该类提供以下功能:
|
|
/// - 加载配置数据
|
|
/// - 获取可用设备列表
|
|
/// - 获取设备配置数据
|
|
/// 用于统一管理威胁源库中的各类设备数据
|
|
/// </remarks>
|
|
public class ThreatSourceDataManager
|
|
{
|
|
private static readonly string DATA_PATH = "../ThreatSource/data";
|
|
private static readonly JsonSerializerOptions _jsonOptions = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
WriteIndented = true,
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
Converters =
|
|
{
|
|
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)
|
|
}
|
|
};
|
|
private readonly Dictionary<string, MissileData> _missiles = new();
|
|
private readonly Dictionary<string, IndicatorData> _indicators = new();
|
|
private readonly Dictionary<string, SensorData> _sensors = new();
|
|
private readonly Dictionary<string, TargetData> _targets = new();
|
|
|
|
/// <summary>
|
|
/// 初始化威胁源数据管理器
|
|
/// </summary>
|
|
/// <param name="dataPath">可选的数据目录路径</param>
|
|
public ThreatSourceDataManager(string? dataPath = null)
|
|
{
|
|
if (dataPath != null)
|
|
LoadData(dataPath);
|
|
else
|
|
LoadData(DATA_PATH);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载数据
|
|
/// </summary>
|
|
/// <param name="path">数据目录路径</param>
|
|
private void LoadData(string path)
|
|
{
|
|
LoadMissiles(Path.Combine(path, "missiles"));
|
|
LoadIndicators(Path.Combine(path, "indicators"));
|
|
LoadSensors(Path.Combine(path, "sensors"));
|
|
LoadTargets(Path.Combine(path, "targets"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载导弹数据
|
|
/// </summary>
|
|
private void LoadMissiles(string path)
|
|
{
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Console.WriteLine($"导弹数据目录不存在:{path}");
|
|
return;
|
|
}
|
|
|
|
foreach (var file in Directory.GetFiles(path, "*.json", SearchOption.AllDirectories))
|
|
{
|
|
try
|
|
{
|
|
var jsonContent = File.ReadAllText(file);
|
|
Console.WriteLine($"读取导弹文件内容:{jsonContent}");
|
|
|
|
var data = JsonSerializer.Deserialize<MissileData>(jsonContent, _jsonOptions);
|
|
if (data != null)
|
|
{
|
|
string model = Path.GetFileNameWithoutExtension(file);
|
|
_missiles[model] = data;
|
|
Console.WriteLine($"已加载导弹数据:{model}");
|
|
Console.WriteLine($"读取到的数据:{JsonSerializer.Serialize(data, _jsonOptions)}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"加载导弹数据文件失败:{file},错误:{ex.Message}");
|
|
Console.WriteLine($"异常堆栈:{ex.StackTrace}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载指示器数据
|
|
/// </summary>
|
|
private void LoadIndicators(string path)
|
|
{
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Console.WriteLine($"指示器数据目录不存在:{path}");
|
|
return;
|
|
}
|
|
|
|
foreach (var file in Directory.GetFiles(path, "*.json", SearchOption.AllDirectories))
|
|
{
|
|
try
|
|
{
|
|
var jsonContent = File.ReadAllText(file);
|
|
Console.WriteLine($"读取指示器文件内容:{jsonContent}");
|
|
|
|
var data = JsonSerializer.Deserialize<IndicatorData>(jsonContent, _jsonOptions);
|
|
if (data != null)
|
|
{
|
|
string model = Path.GetFileNameWithoutExtension(file);
|
|
_indicators[model] = data;
|
|
Console.WriteLine($"已加载指示器数据:{model}");
|
|
Console.WriteLine($"读取到的数据:{JsonSerializer.Serialize(data, _jsonOptions)}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"加载指示器数据文件失败:{file},错误:{ex.Message}");
|
|
Console.WriteLine($"异常堆栈:{ex.StackTrace}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载传感器数据
|
|
/// </summary>
|
|
private void LoadSensors(string path)
|
|
{
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Console.WriteLine($"传感器数据目录不存在:{path}");
|
|
return;
|
|
}
|
|
|
|
foreach (var file in Directory.GetFiles(path, "*.json", SearchOption.AllDirectories))
|
|
{
|
|
try
|
|
{
|
|
var jsonContent = File.ReadAllText(file);
|
|
Console.WriteLine($"读取传感器文件内容:{jsonContent}");
|
|
|
|
var data = JsonSerializer.Deserialize<SensorData>(jsonContent, _jsonOptions);
|
|
if (data != null)
|
|
{
|
|
string model = Path.GetFileNameWithoutExtension(file);
|
|
_sensors[model] = data;
|
|
Console.WriteLine($"已加载传感器数据:{model}");
|
|
Console.WriteLine($"读取到的数据:{JsonSerializer.Serialize(data, _jsonOptions)}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"加载传感器数据文件失败:{file},错误:{ex.Message}");
|
|
Console.WriteLine($"异常堆栈:{ex.StackTrace}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载目标数据
|
|
/// </summary>
|
|
private void LoadTargets(string path)
|
|
{
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Console.WriteLine($"目标数据目录不存在:{path}");
|
|
return;
|
|
}
|
|
|
|
foreach (var file in Directory.GetFiles(path, "*.json", SearchOption.AllDirectories))
|
|
{
|
|
try
|
|
{
|
|
var jsonContent = File.ReadAllText(file);
|
|
Console.WriteLine($"读取目标文件内容:{jsonContent}");
|
|
|
|
var data = JsonSerializer.Deserialize<TargetData>(jsonContent, _jsonOptions);
|
|
if (data != null)
|
|
{
|
|
string model = Path.GetFileNameWithoutExtension(file);
|
|
_targets[model] = data;
|
|
Console.WriteLine($"正在加载目标数据:{file}");
|
|
Console.WriteLine($"读取到的数据:{JsonSerializer.Serialize(data, _jsonOptions)}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"加载目标数据文件失败:{file},错误:{ex.Message}");
|
|
Console.WriteLine($"异常堆栈:{ex.StackTrace}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取导弹配置数据
|
|
/// </summary>
|
|
/// <param name="model">导弹型号</param>
|
|
/// <returns>导弹配置数据</returns>
|
|
public MissileData GetMissile(string model)
|
|
{
|
|
if (_missiles.TryGetValue(model, out var data))
|
|
return data;
|
|
throw new KeyNotFoundException($"Missile {model} not found");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指示器配置数据
|
|
/// </summary>
|
|
/// <param name="model">指示器型号</param>
|
|
/// <returns>指示器配置数据</returns>
|
|
public IndicatorData GetIndicator(string model)
|
|
{
|
|
if (_indicators.TryGetValue(model, out var data))
|
|
return data;
|
|
throw new KeyNotFoundException($"Indicator {model} not found");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取传感器配置数据
|
|
/// </summary>
|
|
/// <param name="model">传感器型号</param>
|
|
/// <returns>传感器配置数据</returns>
|
|
public SensorData GetSensor(string model)
|
|
{
|
|
if (_sensors.TryGetValue(model, out var data))
|
|
return data;
|
|
throw new KeyNotFoundException($"Sensor {model} not found");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取目标配置数据
|
|
/// </summary>
|
|
/// <param name="model">目标型号</param>
|
|
/// <returns>目标配置数据</returns>
|
|
public TargetData GetTarget(string model)
|
|
{
|
|
if (_targets.TryGetValue(model, out var data))
|
|
return data;
|
|
throw new KeyNotFoundException($"Target {model} not found");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有可用的导弹ID列表
|
|
/// </summary>
|
|
public IEnumerable<string> GetAvailableMissiles() => _missiles.Keys;
|
|
|
|
/// <summary>
|
|
/// 获取所有可用的指示器ID列表
|
|
/// </summary>
|
|
public IEnumerable<string> GetAvailableIndicators() => _indicators.Keys;
|
|
|
|
/// <summary>
|
|
/// 获取所有可用的传感器ID列表
|
|
/// </summary>
|
|
public IEnumerable<string> GetAvailableSensors() => _sensors.Keys;
|
|
|
|
/// <summary>
|
|
/// 获取所有可用的目标ID列表
|
|
/// </summary>
|
|
public IEnumerable<string> GetAvailableTargets() => _targets.Keys;
|
|
}
|
|
} |