示例#1
0
        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);
                    }
                }
            });
        }
示例#2
0
 private static void MergeEcfAttribute(EcfAttribute dest, EcfAttribute source)
 {
     dest.Value = source.Value;
     if (dest.AddOns == null && source.AddOns != null)
     {
         dest.AddOns = new Dictionary <string, object>(source.AddOns);
     }
     else
     {
         source.AddOns?.ToList().ForEach(P =>
         {
             if (dest.AddOns.ContainsKey(P.Key))
             {
                 dest.AddOns[P.Key] = P.Value;
             }
             else
             {
                 dest.AddOns.Add(P.Key, P.Value);
             }
         });
     }
 }
示例#3
0
        public static EcfAttribute ReadAttribute(string lineData)
        {
            var line         = lineData.Trim();
            var delimiterPos = line.IndexOfAny(new[] { ':', ' ' });

            if (delimiterPos == -1)
            {
                return(null);
            }

            EcfAttribute result = null;

            do
            {
                delimiterPos = line.IndexOfAny(new[] { ':', ' ' });
                var name = delimiterPos == -1 ? line : line.Substring(0, delimiterPos);
                line = delimiterPos == -1 ? string.Empty : line.Substring(delimiterPos + 1).Trim();

                var nextPayload = line.IndexOfAny(new[] { ',', '"' });

                if (nextPayload == -1)
                {
                    if (result == null)
                    {
                        result = new EcfAttribute()
                        {
                            Name  = name,
                            Value = ParseValue(line.Trim()),
                        }
                    }
                    ;
                    else
                    {
                        if (result.AddOns == null)
                        {
                            result.AddOns = new Dictionary <string, object>();
                        }
                        result.AddOns.Add(name, ParseValue(line.Trim()));
                    }
                    line = null;
                }
                else if (line[nextPayload] == '"')
                {
                    var payloadEnd = line.IndexOf('"', nextPayload + 1);
                    if (result == null)
                    {
                        result = new EcfAttribute()
                        {
                            Name  = name,
                            Value = ParseValue(line.Substring(nextPayload + 1, payloadEnd - nextPayload - 1).Trim()),
                        }
                    }
                    ;
                    else
                    {
                        if (result.AddOns == null)
                        {
                            result.AddOns = new Dictionary <string, object>();
                        }
                        result.AddOns.Add(name, ParseValue(line.Substring(nextPayload + 1, payloadEnd - nextPayload - 1).Trim()));
                    }

                    if (payloadEnd + 1 >= line.Length)
                    {
                        line = null;
                    }
                    else
                    {
                        payloadEnd = line.IndexOf(',', payloadEnd + 1);
                        line       = line.Substring(payloadEnd + 1).Trim();
                    }
                }
                else if (nextPayload > 0)
                {
                    if (result == null)
                    {
                        result = new EcfAttribute()
                        {
                            Name  = name,
                            Value = ParseValue(line.Substring(0, nextPayload).Trim()),
                        }
                    }
                    ;
                    else
                    {
                        if (result.AddOns == null)
                        {
                            result.AddOns = new Dictionary <string, object>();
                        }
                        result.AddOns.Add(name, ParseValue(line.Substring(0, nextPayload).Trim()));
                    }
                    line = line.Substring(nextPayload + 1).Trim();
                }
            } while (!string.IsNullOrEmpty(line));

            return(result);
        }
示例#4
0
        public static void MergeWith(this EcfBlock destination, EcfBlock source)
        {
            if (source == null)
            {
                return;
            }

            destination.Name = source.Name;

            source.Attr?.ForEach(A =>
            {
                if (destination.Attr == null)
                {
                    destination.Attr = new List <EcfAttribute>();
                }
                if (destination.Values == null)
                {
                    destination.Values = new Dictionary <string, object>();
                }
                if (destination.EcfValues == null)
                {
                    destination.EcfValues = new Dictionary <string, EcfAttribute>();
                }

                var foundAttr = destination.Attr.FirstOrDefault(a => a.Name == A.Name);
                if (foundAttr == null)
                {
                    destination.Attr.Add(foundAttr = new EcfAttribute()
                    {
                        Name   = A.Name,
                        Value  = A.Value,
                        AddOns = A.AddOns == null ? null : new Dictionary <string, object>(A.AddOns)
                    });

                    if (A.Name != null && !destination.EcfValues.ContainsKey(A.Name))
                    {
                        destination.EcfValues.Add(A.Name, foundAttr);
                    }
                    if (A.Name != null && !destination.Values.ContainsKey(A.Name))
                    {
                        destination.Values.Add(A.Name, foundAttr.Value);
                    }
                }
                else
                {
                    MergeEcfAttribute(foundAttr, A);

                    if (A.Name != null && !destination.EcfValues.ContainsKey(A.Name))
                    {
                        destination.EcfValues.Add(A.Name, foundAttr);
                    }
                    if (A.Name != null && !destination.Values.ContainsKey(A.Name))
                    {
                        destination.Values.Add(A.Name, foundAttr.Value);
                    }
                }
            });

            if (destination.Childs == null && source.Childs != null)
            {
                destination.Childs = source.Childs
                                     .ToDictionary(B => B.Key, B => { var block = new EcfBlock(); block.MergeWith(B.Value); return(block); });
            }
            else
            {
                source.Childs?
                .ToList()
                .ForEach(B =>
                {
                    if (destination.Childs.TryGetValue(B.Key, out var block))
                    {
                        block.MergeWith(B.Value);
                    }
                    else
                    {
                        var newBlock = new EcfBlock(); newBlock.MergeWith(B.Value);
                        destination.Childs.Add(B.Key, newBlock);
                    }
                });
            }

            destination.Childs?.Values
            .ToList()
            .ForEach(B => {
                B.EcfValues?.Where(A => !destination.EcfValues.ContainsKey(A.Key))
                .ToList()
                .ForEach(A =>
                {
                    if (destination.EcfValues == null)
                    {
                        destination.EcfValues = new Dictionary <string, EcfAttribute>();
                    }
                    if (destination.Values == null)
                    {
                        destination.Values = new Dictionary <string, object>();
                    }

                    destination.EcfValues.Add(A.Key, A.Value);
                    destination.Values.Add(A.Key, A.Value.Value);
                });
            });

            if (source.EcfValues != null && destination.EcfValues == null)
            {
                destination.EcfValues = new Dictionary <string, EcfAttribute>(source.EcfValues);
            }
            else if (source.EcfValues != null)
            {
                foreach (var item in source.EcfValues)
                {
                    if (destination.EcfValues.TryGetValue(item.Key, out var attr))
                    {
                        MergeEcfAttribute(attr, item.Value);
                    }
                    else
                    {
                        destination.EcfValues.Add(item.Key, item.Value);
                    }
                }
            }

            if (source.Values != null && destination.Values == null)
            {
                destination.Values = new Dictionary <string, object>(source.Values);
            }
            else if (source.Values != null)
            {
                foreach (var item in source.Values)
                {
                    if (destination.Values.ContainsKey(item.Key))
                    {
                        destination.Values[item.Key] = item.Value;
                    }
                    else
                    {
                        destination.Values.Add(item.Key, item.Value);
                    }
                }
            }
        }