/// <summary> /// 从源数据反射为对应的配置数组 /// </summary> /// <returns></returns> private static Array Source2Configs(SheetSource source) { Type configType = FindType(source.className); int count = source.row - 3; Array configs = Array.CreateInstance(configType, count); for (int y = 3, i = 0; i < count; y++, i++) { object config = Activator.CreateInstance(configType); for (int x = 0; x < source.column; x++) { string valueType = source.matrix[1, x]; string valueField = source.matrix[2, x]; string valueString = source.matrix[y, x]; FieldInfo field = configType.GetField(valueField); try { object value = ConfigTools.SourceValue2Object(valueType, valueString); field.SetValue(config, value); } catch { UnityEngine.Debug.LogError(string.Format("SourceValue2Object Error!valueType={0},valueString={1},source={2},column={3},row={4}", valueType, valueString, source.originalName, x, y)); } } configs.SetValue(config, i); } return(configs); }
private static SheetSource CreateSource(object original, string originalName, string className, string[,] matrix) { SheetSource source = new SheetSource(); source.original = original; source.originalName = originalName; //文件名 source.className = className; //类名 source.matrix = matrix; return(source); }
/// <summary> /// 获取所有源 /// </summary> /// <returns></returns> private void GetSources(out List <SheetSource> sheets, out List <StructSource> structs) { //获取所有配置文件 DirectoryInfo directory = new DirectoryInfo(cache.sourceFolder); FileInfo[] files = directory.GetFiles("*.*", SearchOption.AllDirectories); //源 sheets = new List <SheetSource>(); structs = new List <StructSource>(); for (int i = 0, l = files.Length; i < l; i++) { FileInfo file = files[i]; if (IsTemporaryFile(file.Name))//临时文件 { continue; } OriginalType type; if (!TypeEnabled(file.Extension, out type)) { continue; } //可以同时读流 FileStream fileStream = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite); ExcelWorksheet excelData = null; string content = ""; //读取Excel if (type == OriginalType.Xlsx) { ExcelPackage package = new ExcelPackage(fileStream); excelData = package.Workbook.Worksheets[1]; fileStream.Close(); } //其他 else { //读Byte byte[] bytes; BinaryReader br = new BinaryReader(fileStream); bytes = br.ReadBytes((int)fileStream.Length); StreamReader renderer = new StreamReader(fileStream); content = renderer.ReadToEnd(); ConfigTools.DetectTextEncoding(bytes, out content);//转换不同的编码格式 if (string.IsNullOrEmpty(content)) { Debug.LogWarning(file.Name + "内容为空!"); continue; } } switch (type) { case OriginalType.Txt: case OriginalType.Csv: try { SheetSource source = SheetParser.Parse(content, file.Name); sheets.Add(source); } catch (Exception e) { UnityEngine.Debug.LogError(file.Name + "解析失败!请检查格式是否正确,如果格式正确请联系作者:https://github.com/RickJiangShu/ConfigManager/issues" + "\n" + e); } break; case OriginalType.Json: try { StructSource st = JsonParser.Parse(content, file.Name); structs.Add(st); } catch (Exception e) { UnityEngine.Debug.LogError(file.Name + "解析失败!请检查格式是否正确,如果格式正确请联系作者:https://github.com/RickJiangShu/ConfigManager/issues" + "\n" + e); } break; case OriginalType.Xml: try { StructSource st = XmlParser.Parse(content, file.Name); structs.Add(st); } catch (Exception e) { UnityEngine.Debug.LogError(file.Name + "解析失败!请检查格式是否正确,如果格式正确请联系作者:https://github.com/RickJiangShu/ConfigManager/issues" + "\n" + e); } break; case OriginalType.Xlsx: try { SheetSource st = SheetParser.Parse(excelData, file.Name); sheets.Add(st); } catch (Exception e) { UnityEngine.Debug.LogError(file.Name + "解析失败!请检查格式是否正确,如果格式正确请联系作者:https://github.com/RickJiangShu/ConfigManager/issues" + "\n" + e); } break; } } }