using System.Reflection; using System.Text; using System.Text.Json; using System.Xml.Serialization; using XFEExtension.NetCore.FormatExtension; namespace XFEExtension.NetCore.AutoConfig; /// /// XFE配置文件,实现配置文件读写自动化 /// public abstract class XFEProfile { private string id = Guid.NewGuid().ToString(); /// /// 配置文件所在的默认目录 /// public static string ProfilesDefaultPath { get; set; } = $@"{AppDomain.CurrentDomain.BaseDirectory}Profiles"; /// /// 配置文件存储位置 /// internal protected string CurrentProfilePath { get; set; } = string.Empty; /// /// 配置文件扩展名 /// internal protected string CurrentProfileExtension { get; set; } = ".xpf"; /// /// 默认配置文件存储和读取的操作模式 /// internal protected ProfileOperationMode DefaultProfileOperationMode { get; set; } = ProfileOperationMode.XFEDictionary; /// /// 加载操作 /// internal protected ProfileLoadOperation LoadOperation { get; set; } = XFEDictionaryLoadProfileOperation; /// /// 保存操作 /// internal protected ProfileSaveOperation SaveOperation { get; set; } = XFEDictionarySaveProfileOperation; /// /// 配置文件 “属性名称-属性类型” 字典 /// internal protected Dictionary PropertyInfoDictionary { get; set; } = []; /// /// 配置文件 “属性名称-属性设置方法” 字典 /// internal protected Dictionary PropertySetFuncDictionary { get; set; } = []; /// /// 配置文件 “属性名称-属性获取方法” 字典 /// internal protected Dictionary PropertyGetFuncDictionary { get; set; } = []; /// /// 通过XFE字典加载配置文件方法(默认) /// /// 配置文件实例 /// 配置文件字符串 /// 配置文件 “属性名称-属性类型” 字典 /// 配置文件 “属性名称-属性设置方法” 字典 /// 配置文件实例 public static XFEProfile? XFEDictionaryLoadProfileOperation(XFEProfile profileInstance, string profileString, Dictionary propertyInfoDictionary, Dictionary propertySetFuncDictionary) { XFEDictionary propertyFileContent = profileString; foreach (var property in propertyFileContent) if (propertySetFuncDictionary.TryGetValue(property.Header, out var setValueDelegate) && propertyInfoDictionary.TryGetValue(property.Header, out var type)) setValueDelegate(JsonSerializer.Deserialize(property.Content, type)); return null; } /// /// 通过XFE字典保存配置文件方法(默认) /// /// 配置文件实例 /// 配置文件 “属性名称-属性类型” 字典 /// 配置文件 “属性名称-属性值获取方法” 字典 /// 保存内容 public static string XFEDictionarySaveProfileOperation(XFEProfile profileInstance, Dictionary propertyInfoDictionary, Dictionary propertyGetFuncDictionary) { if (profileInstance is null) return string.Empty; var saveProfileDictionary = new XFEDictionary(); foreach (var property in propertyGetFuncDictionary) saveProfileDictionary.Add(property.Key, JsonSerializer.Serialize(property.Value())); return saveProfileDictionary.ToString(); } /// /// 通过Json加载配置文件方法 /// /// 配置文件实例 /// 配置文件字符串 /// 配置文件 “属性名称-属性类型” 字典 /// 配置文件 “属性名称-属性设置方法” 字典 /// 配置文件实例 public static XFEProfile? JsonLoadProfileOperation(XFEProfile profileInstance, string profileString, Dictionary propertyInfoDictionary, Dictionary propertySetFuncDictionary) => JsonSerializer.Deserialize(profileString, profileInstance.GetType()) is XFEProfile xFEProfile ? xFEProfile : null; /// /// 通过Json保存配置文件方法 /// /// 配置文件实例 /// 配置文件 “属性名称-属性类型” 字典 /// 配置文件 “属性名称-属性值获取方法” 字典 /// 保存内容 public static string JsonSaveProfileOperation(XFEProfile profileInstance, Dictionary propertyInfoDictionary, Dictionary propertyGetFuncDictionary) => profileInstance is null ? string.Empty : JsonSerializer.Serialize(profileInstance, profileInstance.GetType()); /// /// 通过XML加载配置文件方法 /// /// 配置文件实例 /// 配置文件字符串 /// 配置文件 “属性名称-属性类型” 字典 /// 配置文件 “属性名称-属性设置方法” 字典 /// 配置文件实例 public static XFEProfile? XmlLoadProfileOperation(XFEProfile profileInstance, string profileString, Dictionary propertyInfoDictionary, Dictionary propertySetFuncDictionary) => !string.IsNullOrEmpty(profileString) && new XmlSerializer(profileInstance.GetType()).Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(profileString))) is XFEProfile xFEProfile ? xFEProfile : null; /// /// 通过XML保存配置文件方法 /// /// 配置文件实例 /// 配置文件 “属性名称-属性类型” 字典 /// 配置文件 “属性名称-属性值获取方法” 字典 /// 保存内容 public static string XmlSaveProfileOperation(XFEProfile profileInstance, Dictionary propertyInfoDictionary, Dictionary propertyGetFuncDictionary) { if (profileInstance is null) return string.Empty; using var stream = new MemoryStream(); new XmlSerializer(profileInstance.GetType()).Serialize(stream, profileInstance); stream.Position = 0; return new StreamReader(stream).ReadToEnd(); } /// /// 加载配置文件 /// /// 配置文件实例 internal protected XFEProfile InstanceLoadProfile() { if (File.Exists(CurrentProfilePath) && LoadOperation(this, File.ReadAllText(CurrentProfilePath), PropertyInfoDictionary, PropertySetFuncDictionary) is XFEProfile xFEProfile) { xFEProfile.Initialize(); return xFEProfile; } return this; } /// /// 保存配置文件 /// /// 保存内容 internal protected void InstanceSaveProfile() { var saveContent = SaveOperation(this, PropertyInfoDictionary, PropertyGetFuncDictionary); var fileSavePath = Path.GetDirectoryName(CurrentProfilePath); if (!Directory.Exists(fileSavePath) && fileSavePath is not null && fileSavePath != string.Empty) Directory.CreateDirectory(fileSavePath); File.WriteAllTextAsync(CurrentProfilePath, saveContent); return; } /// /// 删除配置文件 /// internal protected void InstanceDeleteProfile() { if (File.Exists(CurrentProfilePath)) File.Delete(CurrentProfilePath); } /// /// 导出配置文件 /// /// internal protected string InstanceExportProfile() => SaveOperation(this, PropertyInfoDictionary, PropertyGetFuncDictionary); /// /// 导入配置文件 /// /// 配置文件字符串 /// internal protected XFEProfile InstanceImportProfile(string profileString) { if (LoadOperation(this, profileString, PropertyInfoDictionary, PropertySetFuncDictionary) is XFEProfile xFEProfile) { xFEProfile.Initialize(); return xFEProfile; } return this; } /// /// 设置配置文件加载和存储操作 /// internal protected void SetProfileOperation() { switch (DefaultProfileOperationMode) { case ProfileOperationMode.XFEDictionary: LoadOperation = XFEDictionaryLoadProfileOperation; SaveOperation = XFEDictionarySaveProfileOperation; CurrentProfileExtension = ".xpf"; break; case ProfileOperationMode.Json: LoadOperation = JsonLoadProfileOperation; SaveOperation = JsonSaveProfileOperation; CurrentProfileExtension = ".json"; break; case ProfileOperationMode.Xml: LoadOperation = XmlLoadProfileOperation; SaveOperation = XmlSaveProfileOperation; CurrentProfileExtension = ".xml"; break; case ProfileOperationMode.Custom: break; default: break; } } /// /// 初始化 /// public virtual void Initialize() => SetProfileOperation(); #region 已过时 [Obsolete("SaveProfilesFunc属性现已过时,对于每个配置文件实例,请使用 XXXProfile.SaveOperation")] private static Func SaveProfilesFunc { get; set; } = (i, p) => { if (p.MemberInfo is FieldInfo fieldInfo) return JsonSerializer.Serialize(fieldInfo.GetValue(i)); else if (p.MemberInfo is PropertyInfo propertyInfo) return JsonSerializer.Serialize(propertyInfo.GetValue(i)); else return string.Empty; }; [Obsolete("SaveProfilesFunc属性现已过时,对于每个配置文件实例,请使用 XXXProfile.LoadOperation")] private static Func LoadProfilesFunc { get; set; } = (x, p) => { if (p.MemberInfo is FieldInfo fieldInfo) return JsonSerializer.Deserialize(x, fieldInfo.FieldType); else if (p.MemberInfo is PropertyInfo propertyInfo) return JsonSerializer.Deserialize(x, propertyInfo.PropertyType); else return null; }; /// /// 配置文件清单 /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static List Profiles { get; private set; } = []; /// /// 加载配置文件 /// /// 配置文件信息 /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static void LoadProfiles(params ProfileInfo[] profileInfo) { Profiles.AddRange(profileInfo); foreach (var profile in Profiles) { var instance = profile.GetProfileInstance(); if (!File.Exists(profile.Path)) continue; XFEDictionary propertyFileContent = File.ReadAllText(profile.Path); for (int i = 0; i < profile.MemberInfo.Count; i++) { for (int j = 0; j < propertyFileContent.Count; j++) { var memberInfo = profile.MemberInfo[i]; var property = propertyFileContent.ElementAt(j); if (property.Header == memberInfo.Name) { if (memberInfo.MemberInfo is FieldInfo fieldInfo) fieldInfo.SetValue(instance, LoadProfilesFunc(property.Content, memberInfo)); else if (memberInfo.MemberInfo is PropertyInfo propertyInfo) propertyInfo.SetValue(instance, LoadProfilesFunc(property.Content, memberInfo)); continue; } foreach (var propertySecFind in propertyFileContent) { if (propertySecFind.Header == memberInfo.Name) { if (profile.MemberInfo[i].MemberInfo is FieldInfo fieldInfo) fieldInfo.SetValue(instance, LoadProfilesFunc(propertySecFind.Content, memberInfo)); else if (profile.MemberInfo[i].MemberInfo is PropertyInfo propertyInfo) propertyInfo.SetValue(instance, LoadProfilesFunc(propertySecFind.Content, memberInfo)); break; } } } } } } /// /// 加载配置文件 /// /// 配置文件信息 /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static async Task LoadProfilesAsync(params ProfileInfo[] profileInfo) => await Task.Run(() => LoadProfiles(profileInfo)); /// /// 储存指定的配置文件 /// /// 配置文件 /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static void SaveProfile(ProfileInfo profileInfo) { var waitSaveProfile = Profiles.Find(x => x.Profile == profileInfo.Profile); if (waitSaveProfile is null) return; var saveProfileDictionary = new XFEDictionary(); var instance = profileInfo.GetProfileInstance(); foreach (var property in waitSaveProfile.MemberInfo) { saveProfileDictionary.Add(property.Name, SaveProfilesFunc(instance, property)); } var fileSavePath = Path.GetDirectoryName(waitSaveProfile.Path); if (!Directory.Exists(fileSavePath) && fileSavePath is not null && fileSavePath != string.Empty) Directory.CreateDirectory(fileSavePath); File.WriteAllText(waitSaveProfile.Path, saveProfileDictionary); } /// /// 储存配置文件 /// /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static void SaveProfiles() { foreach (var profile in Profiles) SaveProfile(profile); } /// /// 储存指定的配置文件 /// /// 配置文件 /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static async Task SaveProfileAsync(ProfileInfo profileInfo) { var waitSaveProfile = Profiles.Find(x => x.Profile == profileInfo.Profile); if (waitSaveProfile is null) return; var saveProfileDictionary = new XFEDictionary(); var instance = profileInfo.GetProfileInstance(); foreach (var property in waitSaveProfile.MemberInfo) saveProfileDictionary.Add(property.Name, SaveProfilesFunc(instance, property)); var fileSavePath = Path.GetDirectoryName(waitSaveProfile.Path); if (!Directory.Exists(fileSavePath) && fileSavePath is not null && fileSavePath != string.Empty) Directory.CreateDirectory(fileSavePath); await File.WriteAllTextAsync(waitSaveProfile.Path, saveProfileDictionary); } /// /// 储存配置文件 /// /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static async Task SaveProfilesAsync() { foreach (var profile in Profiles) await SaveProfileAsync(profile); } /// /// 删除指定的配置文件 /// /// 指定的配置文件 [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static void DeleteProfile(ProfileInfo profileInfo) { var waitSaveProfile = Profiles.Find(x => x.Profile == profileInfo.Profile); if (waitSaveProfile is null) return; if (File.Exists(waitSaveProfile.Path)) File.Delete(waitSaveProfile.Path); } /// /// 删除指定的配置文件 /// /// 指定的配置文件 /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static async Task DeleteProfileAsync(ProfileInfo profileInfo) { await Task.Run(() => { var waitSaveProfile = Profiles.Find(x => x.Profile == profileInfo.Profile); if (waitSaveProfile is null) return; if (File.Exists(waitSaveProfile.Path)) File.Delete(waitSaveProfile.Path); }); } /// /// 删除所有配置文件 /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static void DeleteProfiles() { foreach (var profile in Profiles) DeleteProfile(profile); } /// /// 删除所有配置文件 /// /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static async Task DeleteProfilesAsync() { foreach (var profile in Profiles) await DeleteProfileAsync(profile); } /// /// 设置储存配置文件的方法 /// /// 储存方法 [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static void SetSaveProfilesFunction(Func saveProfilesFunc) => SaveProfilesFunc = saveProfilesFunc; /// /// 设置加载配置文件的方法 /// /// 加载方法 [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static void SetLoadProfilesFunction(Func loadProfilesFunc) => LoadProfilesFunc = loadProfilesFunc; /// /// 可写在属性的set访问器后,用于自动储存 /// /// /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] protected static void AutoSave(ProfileInfo profileInfo) => SaveProfile(profileInfo); /// /// 可写在属性的set访问器后,用于自动储存 /// /// /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] protected static async Task AutoSaveAsync(ProfileInfo profileInfo) => await SaveProfileAsync(profileInfo); /// /// 导出指定的配置文件 /// /// 指定的配置文件 /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static string ExportProfile(ProfileInfo profileInfo) { var waitSaveProfile = Profiles.Find(x => x.Profile == profileInfo.Profile); if (waitSaveProfile is null) return string.Empty; var saveProfileDictionary = new XFEDictionary(); var instance = profileInfo.GetProfileInstance(); foreach (var property in waitSaveProfile.MemberInfo) saveProfileDictionary.Add(property.Name, SaveProfilesFunc(instance, property)); return saveProfileDictionary.ToString(); } /// /// 导出所有配置文件 /// /// [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static string ExportProfiles() { var exportProfiles = new XFEDictionary(); foreach (var profile in Profiles) exportProfiles.Add(profile.Profile.Name, ExportProfile(profile)); return exportProfiles.ToString(); } /// /// 导入指定的配置文件
/// 本方法仅支持导入由导出的配置文件

/// 如需导入由导出的配置文件,请使用 ///
/// 指定的配置文件 /// 配置文件字符串 /// 导入后是否自动储存 [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static void ImportProfile(ProfileInfo profileInfo, string profileString, bool autoSave = true) { var instance = profileInfo.GetProfileInstance(); var waitSaveProfile = Profiles.Find(x => x.Profile == profileInfo.Profile); if (waitSaveProfile is null) return; var importProfileDictionary = new XFEDictionary(profileString); foreach (var property in waitSaveProfile.MemberInfo) { if (importProfileDictionary[property.Name] is not null) { if (property.MemberInfo is FieldInfo fieldInfo) fieldInfo.SetValue(instance, LoadProfilesFunc(importProfileDictionary[property.Name]!, property)); else if (property.MemberInfo is PropertyInfo propertyInfo) propertyInfo.SetValue(instance, LoadProfilesFunc(importProfileDictionary[property.Name]!, property)); } } if (autoSave) SaveProfile(profileInfo); } /// /// 导入所有配置文件
/// 本方法仅支持导入由导出的配置文件

/// 如需导入由导出的配置文件,请使用 ///
/// 配置文件字符串 /// 导入后是否自动储存 [Obsolete("XFEProfile现在不再对配置文件实行统一的管理,请对每个配置文件单独操作")] public static void ImportProfiles(string profileString, bool autoSave = true) { var importProfiles = new XFEDictionary(profileString); foreach (var profile in Profiles) { if (importProfiles[profile.Profile.Name] is not null) ImportProfile(profile, importProfiles[profile.Profile.Name]!, autoSave); } } #endregion } /// /// 配置文件保存方法 /// /// 配置文件实例 /// 配置文件 “属性名称-属性类型” 字典 /// 配置文件 “属性名称-属性值获取方法” 字典 /// 保存内容 public delegate string ProfileSaveOperation(XFEProfile profileInstance, Dictionary propertyInfoDictionary, Dictionary propertyGetFuncDictionary); /// /// 配置文件加载方法 /// /// 配置文件实例 /// 配置文件字符串 /// 配置文件 “属性名称-属性类型” 字典 /// 配置文件 “属性名称-属性设置方法” 字典 public delegate XFEProfile? ProfileLoadOperation(XFEProfile profileInstance, string profileString, Dictionary propertyInfoDictionary, Dictionary propertySetFuncDictionary); /// /// 设置配置文件属性值委托 /// /// 要设置的值 public delegate void SetValueDelegate(object? value); /// /// 获取配置文件属性值委托 /// /// 获取的属性值 public delegate object? GetValueDelegate();