public static StructSource Parse(string content, string fileName) { StructSource source = new StructSource(); source.original = content; source.originalName = fileName.Substring(0, fileName.LastIndexOf('.'));; //文件名 source.className = source.originalName + "Json"; //类名 source.obj = ParseRoot(content); return(source); }
public static StructSource Parse(string content, string fileName) { StructSource source = new StructSource(); source.original = content; source.originalName = fileName.Substring(0, fileName.LastIndexOf('.'));; //文件名 source.className = source.originalName + "Xml"; //类名 XmlDocument document = new XmlDocument(); document.LoadXml(content); source.obj = (Dictionary <string, object>)ParseNode(document); 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; } } }