ASCU_ALL/ApiServer/Parametrs/Protect.cs

95 lines
2.4 KiB
C#
Raw Permalink Normal View History

2024-09-22 04:27:05 +05:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ApiServer.Parametrs;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ApiServer.Parametrs
{
public class ProtectState
{
public byte Index { get; set; }
public string Name { get; set; }
public string StateStruct { get; set; }
}
}
namespace ApiServer
{
public static partial class Configuration
{
public static ProtectState[] GetProtectState(string name_protect)
{
var result = new List<ProtectState>();
var list_files = Directory.GetFiles(
Path.Combine(
Directory.GetCurrentDirectory(),
"Config",
"Protect"
)
);
foreach (var file in list_files) {
var json_conf =
JObject.Parse(
File.ReadAllText(
file
)
);
if (!json_conf.ContainsKey(name_protect))
continue;
var protect = json_conf.Value<JObject>(name_protect);
if (protect.ContainsKey("struct"))
{
var struct_arr = protect.Value<JArray>("struct");
foreach (JObject struct_el in struct_arr)
{
var protect_state = GetProtect(struct_el);
if (protect_state != null)
if (result.Find(x => x.Index == protect_state.Index) == null)
result.Add(protect_state);
}
}
if (protect.ContainsKey("parent"))
{
var parent_list = protect.Value<JArray>("parent");
for(var i = parent_list.Count - 1; i >= 0; i--)
{
var parent_states = GetProtectState(parent_list[i].Value<string>());
foreach (var state in parent_states)
{
if (state != null)
if (result.Find(x => x.Index == state.Index) == null)
result.Add(state);
}
}
}
break;
}
return result.ToArray();
}
private static ProtectState GetProtect(JObject data)
{
if (
data == null ||
!data.ContainsKey("index") ||
!data.ContainsKey("name") ||
!data.ContainsKey("state_struct")
)
return null;
return new ProtectState()
{
Index = data.Value<byte>("index"),
Name = data.Value<string>("name"),
StateStruct = data.Value<string>("state_struct")
};
}
}
}