예제 #1
0
파일: Utils.cs 프로젝트: Isuroku/GUIBuilder
 public static IKey FindChildByName(this IKey key, string inName, StringComparison comparisonType = StringComparison.InvariantCulture)
 {
     for (int i = 0; i < key.GetChildCount(); i++)
     {
         if (string.Equals(key.GetChild(i).GetName(), inName, comparisonType))
         {
             return(key.GetChild(i));
         }
     }
     return(null);
 }
예제 #2
0
파일: Utils.cs 프로젝트: Isuroku/GUIBuilder
 public static void CopyChilds(this IKey key, ICollection <IKey> outList)
 {
     for (int i = 0; i < key.GetChildCount(); i++)
     {
         outList.Add(key.GetChild(i));
     }
 }
예제 #3
0
파일: Utils.cs 프로젝트: Isuroku/GUIBuilder
        public static SWinKeyInfo[] GetWinKeyInfos(IKey inWinKeyParent, CWindowTypeDescrs inWindowTypeDescrs, ILogPrinter inLogger)
        {
            int count = inWinKeyParent.GetChildCount();
            List <SWinKeyInfo> res = new List <SWinKeyInfo>(count);

            for (int i = 0; i < count; i++)
            {
                IKey window_key = inWinKeyParent.GetChild(i);

                var info = new SWinKeyInfo();

                info.WinKey = window_key;

                info.Name = Utils.GetWindowNameFromKey(window_key, i.ToString(), inLogger);

                string[] a = Utils.TryGetParamsByNameFromSubKey("Type", window_key, inLogger, true, 1);
                if (a.Length > 0)
                {
                    string stype = a[0];

                    NamedId?id = inWindowTypeDescrs.GetWinType(stype);

                    if (id.HasValue)
                    {
                        info.WinType = id.Value;
                        res.Add(info);
                    }
                    else
                    {
                        inLogger.LogError(string.Format("Undefined type. Key [{0}]!", window_key.GetPath()));
                    }
                }
            }
            return(res.ToArray());
        }
예제 #4
0
파일: Utils.cs 프로젝트: Isuroku/GUIBuilder
 public static int FindChildIndex(this IKey key, IKey child, ILogPrinter inLogger)
 {
     for (int i = 0; i < key.GetChildCount(); i++)
     {
         if (key.GetChild(i) == child)
         {
             return(i);
         }
     }
     inLogger.LogError(string.Format("Can't FindChildIndex"));
     return(-1);
 }
예제 #5
0
        object DeserializeDictionary(object inInstance, IKey inKey, Type declaredType, int inInheriteDeep, int inStructDeep, ILogPrinter inLogger)
        {
            Type[] gen_args = declaredType.GetGenericArguments();
            if (gen_args.Length < 2)
            {
                LogError(inLogger, string.Format("DeserializeDictionary: Generic Arguments are None. Type {0}", declaredType.Name));
                return(inInstance);
            }

            object instance = inInstance;

            // Instantiate if necessary
            if (instance == null)
            {
                instance = _reflectionProvider.Instantiate(declaredType, inLogger);
            }

            var dictionary = instance as IDictionary;

            Type keyDeclaredType   = gen_args[0];
            Type valueDeclaredType = gen_args[1];

            IKey tree_key = inKey.GetChild("BaseDictionary");

            if (tree_key == null)
            {
                tree_key = inKey;
            }

            for (int i = 0; i < tree_key.GetChildCount(); ++i)
            {
                IKey sub_key = tree_key.GetChild(i);

                object dic_key;
                if (!ReflectionHelper.StringToAtomicValue(sub_key.GetName(), keyDeclaredType, out dic_key, _reflectionProvider, inLogger))
                {
                    LogError(inLogger, string.Format("SubKey {0} [{3}] for dictionary with key type {1} can't convert value {2}",
                                                     tree_key, keyDeclaredType.Name, sub_key.GetName(), tree_key.GetPath()));
                }
                else
                {
                    object dic_value = DeserializeInternal(null, sub_key, valueDeclaredType, 0, inStructDeep + 1, inLogger);

                    if (dictionary.Contains(dic_key))
                    {
                        dictionary.Remove(dic_key);
                    }
                    dictionary.Add(dic_key, dic_value);
                }
            }
            return(instance);
        }
예제 #6
0
        IKey GetKeyByArrayIndex(IKey inParent, int[] indicies)
        {
            IKey key = inParent;

            //indicies.Length - 1 - last array to one key
            for (int i = 0; i < indicies.Length - 1; i++)
            {
                int index = indicies[i];
                if (index >= key.GetChildCount())
                {
                    return(null);
                }
                key = key.GetChild(index);
            }
            return(key);
        }
예제 #7
0
        bool AllChildsAreArray(IKey inKey)
        {
            if (inKey.GetChildCount() == 0)
            {
                return(false);
            }

            bool all = true;

            for (int i = 0; i < inKey.GetChildCount() && all; ++i)
            {
                if (!inKey.GetChild(i).IsArrayKey())
                {
                    all = false;
                }
            }

            return(all);
        }
예제 #8
0
        int[] FindArrayDimension(IKey inKey, bool IsAtomicElementType)
        {
            List <int> lst = new List <int>();
            IKey       ck  = inKey;

            while (ck.GetChildCount() > 0 && ck.GetValuesCount() == 0 && AllChildsAreArray(ck))
            {
                lst.Add(ck.GetChildCount());
                ck = ck.GetChild(0);
            }
            if (IsAtomicElementType)
            {
                lst.Add(ck.GetValuesCount());
            }
            else if (lst.Count == 0 && ck.GetChildCount() > 0)
            {
                lst.Add(1);
            }
            return(lst.ToArray());
        }
예제 #9
0
        IKey GetOrCreateKeyByArrayIndex(IKey inParent, int[] indicies, int[] lengthes, bool atomic_elems)
        {
            IKey key         = inParent;
            int  keys_length = indicies.Length - 1; //length of key chaine

            //indicies.Length - 1 - last array to one key

            for (int i = 0; i < keys_length; i++)
            {
                int index  = indicies[i];
                int length = lengthes[i];
                while (index >= key.GetChildCount())
                {
                    key.CreateArrayKey();
                }

                key = key.GetChild(index);
            }
            return(key);
        }
예제 #10
0
        void AddToTree(IKey key, TreeNodeCollection nc)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < key.GetValuesCount(); i++)
            {
                IKeyValue value = key.GetValue(i);

                string val_comments = string.Empty;
                if (!string.IsNullOrEmpty(value.Comments))
                {
                    val_comments = string.Format("[//{0}]", value.Comments);
                }

                sb.AppendFormat("{0}{1}, ", value, val_comments);
            }

            string arr_flag = string.Empty;

            if (key.IsArrayKey())
            {
                arr_flag = "[a]";
            }

            string key_comments = string.Empty;

            if (!string.IsNullOrEmpty(key.Comments))
            {
                key_comments = string.Format(" //{0}", key.Comments);
            }

            TreeNode tn = new TreeNode(string.Format("{0}{1}: {2}{3}", key.GetName(), arr_flag, sb, key_comments));

            nc.Add(tn);

            for (int i = 0; i < key.GetChildCount(); i++)
            {
                IKey el = key.GetChild(i);
                AddToTree(el, tn.Nodes);
            }
        }
예제 #11
0
        object DeserializeClass(object inInstance, IKey inKey, Type type, int inStructDeep, ILogPrinter inLogger)
        {
            IKey type_key = inKey.GetChild("RealObjectType");

            if (type_key != null)
            {
                string type_name     = type_key.GetValueAsString(0);
                string assembly_name = type_key.GetValueAsString(1);

                try
                {
                    Assembly assembly = Assembly.Load(assembly_name);
                    Type     obj_type = assembly.GetType(type_name, true);
                    if (obj_type != null)
                    {
                        type = obj_type;
                    }
                }
                catch (Exception ex)
                {
                    LogError(inLogger, string.Format("Cant take type from RealObjectType {0}. Exception: {1}", type_name, ex.Message));
                }
            }

            object instance = inInstance;

            if (instance == null)
            {
                instance = _reflectionProvider.Instantiate(type, inLogger);
            }

            if (instance != null)
            {
                //MethodInfo mi = type.GetMethod("DeserializationFromCscd", new Type[] { typeof(CascadeParser.IKey), typeof(CascadeParser.ILogPrinter) });
                MethodInfo mi = type.GetMethod("DeserializationFromCscd", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                if (mi != null)
                {
                    if (inKey != null && !inKey.IsEmpty)
                    {
                        IKey key = inKey;
                        mi.Invoke(instance, new object[] { key, inLogger });
                    }
                }
                else
                {
                    MemberInfo[] member_infos = _reflectionProvider.GetSerializableMembers(type);
                    foreach (MemberInfo memberInfo in member_infos)
                    {
                        SCustomMemberParams member_params = GetMemberParams(memberInfo);

                        Type memberType = memberInfo.GetMemberType();

                        IKey sub_key = inKey.GetChild(member_params.ChangedName);
                        if (sub_key == null)
                        {
                            sub_key = inKey.GetChild(member_params.Name);
                        }

                        if (sub_key != null)
                        {
                            object readValue;
                            if (member_params.Converter != null)
                            {
                                readValue = member_params.Converter.ReadKey(sub_key, inLogger);
                            }
                            else
                            {
                                readValue = DeserializeInternal(null, sub_key, memberType, 0, inStructDeep + 1, inLogger);
                            }

                            // This dirty check is naive and doesn't provide performance benefits
                            //if (memberType.IsClass && readValue != currentValue && (readValue == null || !readValue.Equals(currentValue)))
                            _reflectionProvider.SetValue(memberInfo, instance, readValue, inLogger);
                        }
                        else if (member_params.DefaultValue != null)
                        {
                            _reflectionProvider.SetValue(memberInfo, instance, member_params.DefaultValue, inLogger);
                        }
                        else if (memberType.IsClass || memberType.IsStruct())
                        {
                            object already_exists_member = _reflectionProvider.GetValue(memberInfo, instance);
                            if (already_exists_member != null)
                            {
                                //for set default values inside this object
                                already_exists_member = DeserializeInternal(already_exists_member, IKeyFactory.CreateKey(string.Empty), memberType, 0, inStructDeep + 1, inLogger);
                                if (already_exists_member != null)
                                {
                                    _reflectionProvider.SetValue(memberInfo, instance, already_exists_member, inLogger);
                                }
                            }
                        }
                    }
                }

                mi = type.GetMethod("OnDeserializedMethod", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                if (mi != null)
                {
                    var context = new StreamingContext(StreamingContextStates.Other);
                    mi.Invoke(instance, new object[] { context });
                }
            }

            return(instance);
        }
예제 #12
0
        object DeserializeGenericCollection(object inInstance, IKey inKey, Type type, int inInheriteDeep, int inStructDeep, ILogPrinter inLogger)
        {
            Type[] gen_args = type.GetGenericArguments();
            if (gen_args.Length == 0)
            {
                LogError(inLogger, string.Format("DeserializeGenericCollection: Generic Arguments are None. Type {0}", type.Name));
                return(inInstance);
            }

            Type declaredItemType = gen_args[0];
            bool is_atomic_elems  = declaredItemType.IsAtomic();

            object instance = inInstance;

            if (instance == null)
            {
                instance = _reflectionProvider.Instantiate(type, inLogger);
            }

            instance = instance as IEnumerable;

            SCollect collect = new SCollect(instance, type, _reflectionProvider);

            IKey tree_key = inKey.GetChild("BaseCollection");

            if (tree_key == null)
            {
                tree_key = inKey;
            }

            if (is_atomic_elems)
            {
                int element_count = tree_key.GetValuesCount();
                for (int i = 0; i < element_count; i++)
                {
                    object obj_value;
                    string str_value = tree_key.GetValueAsString(i);
                    if (!ReflectionHelper.StringToAtomicValue(str_value, declaredItemType, out obj_value, _reflectionProvider, inLogger))
                    {
                        LogError(inLogger, string.Format("Key {0} [{3}] for collection with element type {1} can't convert value {2}",
                                                         tree_key, declaredItemType.Name, str_value, tree_key.GetPath()));
                    }

                    collect.AddValue(obj_value);
                }
            }
            else if (tree_key.GetChildCount() > 0)
            {
                if (AllChildsAreArray(tree_key))
                {
                    int element_count = tree_key.GetChildCount();

                    for (int i = 0; i < element_count; i++)
                    {
                        IKey   sub_key   = tree_key.GetChild(i);
                        object obj_value = DeserializeInternal(null, sub_key, declaredItemType, 0, inStructDeep + 1, inLogger);
                        collect.AddValue(obj_value);
                    }
                }
                else
                {
                    object obj_value = DeserializeInternal(null, tree_key, declaredItemType, 0, inStructDeep + 1, inLogger);
                    collect.AddValue(obj_value);
                }
            }

            return(instance);
        }
예제 #13
0
        object DeserializeArray(IKey inKey, Type type, int inStructDeep, ILogPrinter inLogger)
        {
            if (inKey.IsEmpty)
            {
                return(null);
            }

            IKey key = inKey;

            Array multi_dim_array  = null;
            Type  declaredItemType = type.GetElementType();

            bool is_atomic_elems = declaredItemType.IsAtomic();
            bool is_array_elems  = declaredItemType.IsArray;

            int[] dims;
            if (is_array_elems)
            {
                dims = new int[] { key.GetChildCount() }
            }
            ;
            else
            {
                dims = FindArrayDimension(key, is_atomic_elems);
            }

            if (dims.Length == 0)
            {
                return(null);
            }

            object instance = Array.CreateInstance(declaredItemType, dims);

            multi_dim_array = instance as Array;

            CMultiArrayIndexer indexer = new CMultiArrayIndexer(multi_dim_array);

            while (indexer.MoveNext())
            {
                IKey dim_child = GetKeyByArrayIndex(key, indexer.Current);
                if (dim_child == null)
                {
                    LogError(inLogger, string.Format("Cant get value for multi array index {0}", indexer));
                }
                else
                {
                    object obj_value;
                    int    last_index = indexer.Current[indexer.Current.Length - 1];
                    if (is_atomic_elems)
                    {
                        string str_value;
                        if (dim_child.GetValuesCount() == 0)
                        {
                            str_value = string.Empty;
                        }
                        else
                        {
                            str_value = dim_child.GetValueAsString(last_index);
                        }
                        if (!ReflectionHelper.StringToAtomicValue(str_value, declaredItemType, out obj_value, _reflectionProvider, inLogger))
                        {
                            LogError(inLogger, string.Format("Key {0} [{3}] for collection with element type {1} can't convert value {2}",
                                                             key, declaredItemType.Name, str_value, key.GetPath()));
                        }
                    }
                    else
                    {
                        IKey child = dim_child.GetChild(last_index);
                        obj_value = DeserializeInternal(null, child, declaredItemType, 0, inStructDeep + 1, inLogger);
                    }

                    multi_dim_array.SetValue(obj_value, indexer.Current);
                }
            }

            return(instance);
        }