public static EcfFile Deserialize(params string[] lines) { var result = new EcfFile(); var i = -1; do { string currentLine = ReadNextLine(); if (!string.IsNullOrEmpty(currentLine)) { if (currentLine.StartsWith("{")) { var block = ReadBlock(false, currentLine, ReadNextLine); if (result.Blocks == null) { result.Blocks = new List <EcfBlock>(); } result.Blocks.Add(block); } } } while (i < lines.Length - 1); return(result); string ReadNextLine() { var line = lines[++i].Trim(); var commentPos = line.IndexOf('#'); line = (commentPos >= 0 ? line.Substring(0, commentPos) : line).Trim(); commentPos = line.IndexOf("/*"); if (commentPos >= 0) { var commentEnd = line.IndexOf("*/"); if (commentEnd >= 0) { return ((line.Substring(0, commentPos) + (line.Length == commentEnd + 2 ? string.Empty : line.Substring(commentEnd + 2))).Trim()); } while (line.IndexOf("*/") == -1) { line = lines[++i].Trim(); } return(string.Empty); } return(line); } }
public static void ReplaceWithMappedIds(EcfFile result, IDictionary <string, int> blockIdMapping) { result.Blocks.ForEach(B => { if (B.Name != "Block" && B.Name != "Item") { return; } var blockIdAttr = B.Attr?.FirstOrDefault(a => a.Name == "Id"); var blockId = blockIdAttr?.Value; var blockNameAttr = B.Attr?.FirstOrDefault(a => a.Name == "Name"); var blockName = blockNameAttr?.Value; if (string.IsNullOrEmpty(blockName?.ToString()) && blockIdAttr != null) { blockIdAttr.AddOns?.TryGetValue("Name", out blockName); } if (!string.IsNullOrEmpty(blockName?.ToString()) && blockIdMapping.TryGetValue(blockName.ToString(), out var id)) { if (blockIdAttr != null) { blockIdAttr.Value = id; } else if (blockName != null && blockId == null) { var idAttr = new EcfAttribute { Name = "Id", Value = id, AddOns = blockNameAttr.AddOns ?? new Dictionary <string, object>() }; idAttr.AddOns.Add("Name", blockName.ToString()); B.Attr.Remove(blockNameAttr); B.Attr.Insert(0, idAttr); B.EcfValues.Add("Id", idAttr); } if (B.Values.ContainsKey("Id")) { B.Values["Id"] = id; } else { B.Values.Add("Id", id); } } }); }
public static EcfFile Ecf(params EcfFile[] files) { EcfFile result = null; foreach (var ecf in files) { if (result == null) { result = ecf; } else { result.MergeWith(ecf); } } return(result); }
public static void MergeWith(this EcfFile ecf, EcfFile add) { if (ecf.Blocks == null) { ecf.Blocks = new List <EcfBlock>(); } add?.Blocks?.ForEach(B => { var found = ecf.Blocks .Where(b => b.Name == B.Name) .Where(b => Equals(b.Values?.FirstOrDefault(a => a.Key == "Id").Value, B.Values?.FirstOrDefault(a => a.Key == "Id").Value)) .Where(b => Equals(b.Values?.FirstOrDefault(a => a.Key == "Name").Value, B.Values?.FirstOrDefault(a => a.Key == "Name").Value)) .FirstOrDefault(); if (found == null) { ecf.Blocks.Add(B); } else { found.MergeWith(B); } }); }