private void ReadIList(IList list, Type eleType, XElement[] eleEleArr, ConfigAttributeTypes configAttributeTypes) { ConfigDataType configDataType = this.GetDataType(eleType); string value, eleName = null; object obj; switch (configDataType) { case ConfigDataType.IDictionary: foreach (var eleEle in eleEleArr) { obj = Activator.CreateInstance(eleType); this.ReadIDictionary((IDictionary)obj, eleEle.Elements(eleName).ToArray(), eleType, configAttributeTypes); list.Add(obj); } break; case ConfigDataType.IList: var itemType2 = this.GetIListElementType(eleType); eleName = this.GetIListElementName(itemType2); XElement[] eleEleArr2; foreach (var eleEle in eleEleArr) { eleEleArr2 = eleEle.Elements(eleName).ToArray(); obj = this.CreateIList(eleType, eleEleArr2.Length); this.ReadIList((IList)obj, itemType2, eleEleArr2, configAttributeTypes); list.Add(obj); } break; case ConfigDataType.Object: PropertyInfo[] objPropertyInfoArr = this.GetTypePropertyInfos(eleType); int index = 0; foreach (var eleEle in eleEleArr) { obj = Activator.CreateInstance(eleType); this.ReadConfigToXml(eleEle, objPropertyInfoArr, ref obj, configAttributeTypes); if (list is Array) { list[index++] = obj; } else { list.Add(obj); } } break; case ConfigDataType.Basic: foreach (var eleEle in eleEleArr) { value = XmlEx.GetXElementAttributeValue(eleEle, _VALUE, true); list.Add(this.StringToObject(value, eleType)); } break; default: throw new NotImplementedException(configDataType.ToString()); } }
private void WriteIList(string name, XElement collectionEle, IList list, Type eleType, ConfigAttributeTypes configAttributeTypes) { if (list == null || list.Count == 0) { return; } string eleName = this.GetIListElementName(eleType), eleName2 = null; ConfigDataType configDataType = this.GetDataType(eleType); switch (configDataType) { case ConfigDataType.IDictionary: eleName2 = this.GetIDictionaryElementName(null); foreach (var item in list) { this.WriteIDictionary(collectionEle, (IDictionary)item, eleType, eleName, eleName2, null, configAttributeTypes); } break; case ConfigDataType.IList: var itemType2 = this.GetIListElementType(eleType); eleName = this.GetIListElementName(itemType2); foreach (var item in list) { XElement itemEle = new XElement(name); collectionEle.Add(itemEle); this.WriteIList(eleName, itemEle, (IList)item, itemType2, configAttributeTypes); } break; case ConfigDataType.Object: PropertyInfo[] objPropertyInfoArr = this.GetTypePropertyInfos(eleType); foreach (var item in list) { if (item == null) { continue; } XElement childEle = new XElement(name); this.WriteConfigToXml(childEle, objPropertyInfoArr, item, configAttributeTypes); collectionEle.Add(childEle); } break; case ConfigDataType.Basic: foreach (var item in list) { this.WriteItem(collectionEle, name, item, null); } break; default: throw new NotImplementedException(configDataType.ToString()); } }
//添加一个配置数据 private void AddData(ConfigDataType type, ConfigDataCommon data) { if (allConfigData.ContainsKey(type)) { Utils.LogSys.Log(type.ToString() + " is already exist"); return; } allConfigData.Add(type, data); }
internal string[] GetArchiveNames(ConfigData fileAccessor, ConfigDataType type, string itemName) { string typeName = typeof(T).Name; if (typeof(T).IsInterface) { typeName = typeName.Substring(1); } string archiveDirName = fileAccessor.BaseDirectory + type.ToString() + "/" + fileAccessor.ArchiveDirectoryName; if (!fileAccessor.VerifyDirectory(archiveDirName)) { return(new string[0]); } DirectoryInfo filesDirectory = new DirectoryInfo(archiveDirName); return(filesDirectory.EnumerateDirectories().Where(dir => dir.GetFiles(itemName + "." + typeName, SearchOption.TopDirectoryOnly).Any()) .Select(dir => dir.Name).ToArray()); }
/// <summary> /// Archives everything /// </summary> public void ArchiveFull(ConfigDataType type, string backupName = "") { string dirName = BaseDirectory; switch (type) { default: dirName += type.ToString(); break; case ConfigDataType.Player: return; //we don't do players here, ever } string currentDirName = dirName + "/" + CurrentDirectoryName; string archivedDirName = dirName + "/" + ArchiveDirectoryName; //wth, no current directory? Noithing to move then if (VerifyDirectory(currentDirName, false) && VerifyDirectory(archivedDirName)) { CullDirectoryCount(archivedDirName); DirectoryInfo currentRoot = new DirectoryInfo(currentDirName); string backupDir = archivedDirName + DatedBackupDirectory; if (!string.IsNullOrWhiteSpace(backupName)) { backupDir = string.Format("{0}{1}/", archivedDirName, backupName); } currentRoot.CopyTo(backupDir); } //something very wrong is happening, it'll get logged if (!VerifyDirectory(currentDirName)) { throw new Exception("Can not locate or verify current data directory."); } }
internal void GetArchivedTemplate(ConfigData fileAccessor, ConfigDataType type, T item) { Type templateType = typeof(T); string typeName = templateType.Name; if (templateType.IsInterface) { typeName = typeName.Substring(1); templateType = typeof(EntityPartial).Assembly.GetTypes().SingleOrDefault(x => !x.IsAbstract && x.GetInterfaces().Contains(templateType)); } DirectoryInfo archiveDir = new DirectoryInfo(fileAccessor.BaseDirectory + type.ToString() + "/" + fileAccessor.ArchiveDirectoryName + ArchivePath + "/"); FileInfo[] potentialFiles = archiveDir.GetFiles(item.UniqueKey + "." + typeName); if (potentialFiles.Any()) { DataTemplate = (T)fileAccessor.ReadEntity(potentialFiles.First(), templateType); } }
private void ReadIDictionary(IDictionary dic, XElement[] eleEleArr, Type eleType, ConfigAttributeTypes configAttributeTypes) { XElement keyEle, valueEle; string keyStr, valueStr; object key, value = null; Type[] argsTypeArr = eleType.GetGenericArguments(); ConfigDataType configDataType = this.GetDataType(argsTypeArr[1]); string name2 = null, eleName2 = null, eleName3 = null; Type eleType2 = null; PropertyInfo[] objPropertyInfoArr = null; XElement[] eleEleArr2 = null; foreach (var childEle in eleEleArr) { keyEle = childEle.Element(_KEY); keyStr = XmlEx.GetXElementAttributeValue(keyEle, _VALUE, false); key = this.StringToObject(keyStr, argsTypeArr[0]); valueEle = childEle.Element(_VALUE); switch (configDataType) { case ConfigDataType.Basic: valueStr = XmlEx.GetXElementAttributeValue(valueEle, _VALUE, true); if (valueStr == null) { value = null; } else { value = this.StringToObject(valueStr, argsTypeArr[1]); } break; case ConfigDataType.IDictionary: if (name2 == null) { name2 = this.GetIListElementName(argsTypeArr[1]); } if (eleName2 == null) { eleName2 = this.GetIDictionaryElementName(null); } value = Activator.CreateInstance(argsTypeArr[1]); this.ReadIDictionary((IDictionary)value, valueEle.XPathSelectElements($"{name2}/{eleName2}").ToArray(), argsTypeArr[1], configAttributeTypes); break; case ConfigDataType.IList: if (eleType2 == null) { eleType2 = this.GetIListElementType(argsTypeArr[1]); } if (eleName2 == null) { eleName2 = this.GetIListElementName(argsTypeArr[1]); } if (eleName3 == null) { eleName3 = this.GetIListElementName(eleType2); } eleEleArr2 = valueEle.XPathSelectElements($"{eleName2}/{eleName3}").ToArray(); value = this.CreateIList(argsTypeArr[1], eleEleArr2.Length); this.ReadIList((IList)value, eleType2, eleEleArr2, configAttributeTypes); break; case ConfigDataType.Object: if (objPropertyInfoArr == null) { objPropertyInfoArr = this.GetTypePropertyInfos(argsTypeArr[1]); } if (eleName2 == null) { eleName2 = this.GetIListElementName(argsTypeArr[1]); } value = Activator.CreateInstance(argsTypeArr[1]); this.ReadConfigToXml(valueEle.Element(eleName2), objPropertyInfoArr, ref value, configAttributeTypes); break; default: throw new NotSupportedException(configDataType.ToString()); } dic[key] = value; } }
private void WriteIDictionary(XElement ownerEle, IDictionary dic, Type dicType, string name, string eleName, string des, ConfigAttributeTypes configAttributeTypes) { if (dic == null || dic.Count == 0) { return; } XElement dicEle = new XElement(name); this.AddDes(dicEle, des); ownerEle.Add(dicEle); object value; Type[] argsTypeArr = dicType.GetGenericArguments(); ConfigDataType keyDataType = this.GetDataType(argsTypeArr[0]); ConfigDataType valueDataType = this.GetDataType(argsTypeArr[1]); PropertyInfo[] objPropertyInfoArr = null; string name2 = null, eleName2 = null, eleName3 = null; Type eleType = null; foreach (var key in dic.Keys) { XElement eleEle = new XElement(eleName); dicEle.Add(eleEle); //key XElement keyEle = new XElement(_KEY); switch (keyDataType) { case ConfigDataType.Basic: keyEle.Add(new XAttribute(_VALUE, key.ToString())); break; case ConfigDataType.IDictionary: case ConfigDataType.IList: case ConfigDataType.Object: default: throw new NotSupportedException($"字典集合的key只能为基础类型数据,类型{keyDataType.ToString()}无效"); } eleEle.Add(keyEle); value = dic[key]; XElement valueEle = new XElement(_VALUE); switch (valueDataType) { case ConfigDataType.Basic: if (value != null) { valueEle.Add(new XAttribute(_VALUE, value.ToString())); } break; case ConfigDataType.IDictionary: if (name2 == null) { name2 = this.GetIListElementName(argsTypeArr[1]); } if (eleName2 == null) { eleName2 = this.GetIDictionaryElementName(null); } this.WriteIDictionary(valueEle, (IDictionary)value, argsTypeArr[1], name2, eleName2, null, configAttributeTypes); break; case ConfigDataType.IList: if (eleType == null) { eleType = this.GetIListElementType(argsTypeArr[1]); } if (eleName2 == null) { eleName2 = this.GetIListElementName(argsTypeArr[1]); } if (eleName3 == null) { eleName3 = this.GetIListElementName(eleType); } XElement listChildEle = new XElement(eleName2); this.WriteIList(eleName3, listChildEle, (IList)value, eleType, configAttributeTypes); valueEle.Add(listChildEle); break; case ConfigDataType.Object: if (objPropertyInfoArr == null) { objPropertyInfoArr = this.GetTypePropertyInfos(argsTypeArr[1]); } if (eleName2 == null) { eleName2 = this.GetIListElementName(argsTypeArr[1]); } XElement childEle = new XElement(eleName2); this.WriteConfigToXml(childEle, objPropertyInfoArr, value, configAttributeTypes); valueEle.Add(childEle); break; default: throw new NotSupportedException(valueDataType.ToString()); } eleEle.Add(valueEle); } }