/// <summary> /// 加载单元 /// </summary> /// <param name="unitKey"></param> /// <returns></returns> public UnitSettings LoadUnit(String unitKey) { if (!this.Useable) { return(null); } /*特殊处理*/ if (unitKey == "self" || unitKey == "wind" || unitKey == "daemon") { return(null); } /*特殊处理*/ LoggerModuleHelper.TryLog("Modules.UnitManageModule.LoadUnit", "开始加载单元配置文件"); //读取文件 String unitFilePath = String.Concat(this.UnitsDirectory, Path.DirectorySeparatorChar, unitKey, ".json"); if (!File.Exists(unitFilePath)) { LoggerModuleHelper.TryLog("Modules.UnitManageModule.LoadUnit[Error]", $"单元配置文件 {unitFilePath} 不存在"); return(null); } FileInfo fileInfo; try { fileInfo = new FileInfo(unitFilePath); }catch (Exception exception) { LoggerModuleHelper.TryLog( "Modules.UnitManageModule.LoadUnit[Error]", $"单元配置文件 {unitFilePath} 文件信息异常,: {exception.Message}\n异常堆栈: {exception.StackTrace}"); return(null); } //setting UnitSettings unitSettings = UnitManageModuleHelper.ParseUnitSettingsFile(fileInfo); if (unitSettings == null) { LoggerModuleHelper.TryLog("Modules.UnitManageModule.LoadUnit[Warning]", $"单元文件\"{unitFilePath}\"读取失败"); return(null); } //检查是新增或更新 if (this.UnitDictionary.ContainsKey(unitKey)) { this.UnitDictionary[unitKey].Settings = unitSettings; } else { _ = this.UnitDictionary.TryAdd(unitKey, new Unit { Key = unitKey, Settings = unitSettings }); } LoggerModuleHelper.TryLog("Modules.UnitManageModule.LoadUnit", $"单元\"{unitKey}\"读取成功,已加入单元列表"); return(unitSettings); }
/// <summary> /// 解析所有单元配置文件 /// </summary> /// <returns>解析成功单元列表</returns> public List <UnitSettings> LoadAllUnits() { if (!this.Useable) { return(null); } //读取文件目录 LoggerModuleHelper.TryLog("Modules.UnitManageModule.LoadAllUnits", "开始解析所有单元配置文件"); FileInfo[] fileInfoArray; try { DirectoryInfo directoryInfo = new DirectoryInfo(this.UnitsDirectory); fileInfoArray = directoryInfo.GetFiles("*.json", SearchOption.TopDirectoryOnly); }catch (Exception exception) { LoggerModuleHelper.TryLog( "Modules.UnitManageModule.LoadAllUnits[Error]", $"读取单元存放目录异常,{exception.Message}\n异常堆栈: {exception.StackTrace}"); return(null); } if (fileInfoArray.Length < 1) { return(null); } //解析文件 List <UnitSettings> unitSettingsList = new List <UnitSettings>(); for (Int32 i1 = 0; i1 < fileInfoArray.Length; i1++) { //key String unitKey = UnitManageModuleHelper.GetUnitKey(fileInfoArray[i1]); /*特殊处理*/ if (unitKey == "self" || unitKey == "wind" || unitKey == "daemon") { continue; } /*特殊处理*/ if (String.IsNullOrWhiteSpace(unitKey) || unitKey.Length > 32) { LoggerModuleHelper.TryLog("Modules.UnitManageModule.LoadAllUnits[Warning]", $"单元文件\"{fileInfoArray[i1].FullName}\"标识错误,已跳过"); continue; } //setting UnitSettings unitSettings = UnitManageModuleHelper.ParseUnitSettingsFile(fileInfoArray[i1]); if (unitSettings == null) { LoggerModuleHelper.TryLog("Modules.UnitManageModule.LoadAllUnits[Warning]", $"单元文件\"{fileInfoArray[i1].FullName}\"读取失败,已跳过"); continue; } //检查是新增或更新 if (this.UnitDictionary.ContainsKey(unitKey)) { this.UnitDictionary[unitKey].Settings = unitSettings; } else { _ = this.UnitDictionary.TryAdd(unitKey, new Unit { Key = unitKey, Settings = unitSettings }); } unitSettingsList.Add(unitSettings); LoggerModuleHelper.TryLog("Modules.UnitManageModule.LoadAllUnits", $"单元\"{unitKey}\"读取成功,已加入单元列表"); } //完成 LoggerModuleHelper.TryLog("Modules.UnitManageModule.LoadAllUnits", $"已解析 {this.UnitDictionary.Count} 个单元配置文件"); return(this.UnitDictionary.Count > 0?unitSettingsList:null); }