Beispiel #1
0
        public static string[] TryGetParamsByNameFromSubKey(string inKeyName, IKey inKey, ILogPrinter inLogger, bool inSubKeyMustBe, params int[] inParamCounts)
        {
            IKey sub_key = inKey.FindChildByName(inKeyName, StringComparison.InvariantCultureIgnoreCase);

            if (sub_key == null)
            {
                if (inSubKeyMustBe)
                {
                    inLogger.LogError(string.Format("Can't find subkey by name. Key [{0}] must have subkey {1}!", inKey.GetPath(), inKeyName));
                }
                return(null);
            }

            int value_count = sub_key.GetValuesCount();

            if (!inParamCounts.ContainsCheck(value_count))
            {
                string counts = inParamCounts.ToString(", ");
                inLogger.LogError(string.Format("Subkey [{0}] must have [{1}] counts of values, but was found {2}", sub_key.GetPath(), counts, value_count));
                return(null);
            }

            var a = new string[value_count];

            for (int i = 0; i < value_count; i++)
            {
                a[i] = sub_key.GetValueAsString(i);
            }

            return(a);
        }
Beispiel #2
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());
        }
Beispiel #3
0
        public void DeserializationFromCscd(IKey key, ILogPrinter inLogger)
        {
            if (key.GetValuesCount() != 3)
            {
                return;
            }

            x = key.GetValue(0).ToFloat();
            y = key.GetValue(1).ToFloat();
            z = key.GetValue(2).ToFloat();
        }
Beispiel #4
0
        public object ReadKey(IKey key, ILogPrinter inLogger)
        {
            int count = key.GetValuesCount();
            var arr   = new NamedId[count];

            for (int i = 0; i < count; i++)
            {
                arr[i] = NamedId.GetNamedId(key.GetValueAsString(i));
            }
            return(arr);
        }
Beispiel #5
0
        public void DeserializationFromCscd(IKey key, ILogPrinter inLogger)
        {
            if (key.GetValuesCount() == 0)
            {
                inLogger.LogError(string.Format("NamedId.CscdConverter: Key {0} hasnt value!", key.GetPath()));
                return;
            }
            string  n   = key.GetValue(0).ToString();
            NamedId nid = GetNamedId(n);

            id   = nid.id;
            name = nid.name;
        }
Beispiel #6
0
        object DeserializeAtomic(object inInstance, IKey inKey, Type type, int inStructDeep, ILogPrinter inLogger)
        {
            object instance;
            IKey   key = inKey;

            if (key.GetValuesCount() == 0)
            {
                instance = ReflectionHelper.GetDefaultValue(type, _reflectionProvider, inLogger);
            }
            else if (key.GetValuesCount() > 1)
            {
                var sb = new StringBuilder();
                sb.Append("\"");
                for (int i = 0; i < key.GetValuesCount(); ++i)
                {
                    if (i > 0)
                    {
                        sb.Append(",");
                    }
                    sb.Append(key.GetValueAsString(i));
                }
                sb.Append("\"");

                LogError(inLogger, string.Format("Need one value for type {1}. Key: {0} [{3}]; Values: {2} ", key, type.Name, sb, key.GetPath()));
                instance = ReflectionHelper.GetDefaultValue(type, _reflectionProvider, inLogger);
            }
            else
            {
                string key_value = key.GetValueAsString(0);
                if (!ReflectionHelper.StringToAtomicValue(key_value, type, out instance, _reflectionProvider, inLogger))
                {
                    LogError(inLogger, string.Format("Key {0} [{3}] with value {1} can't convert value to type {2}", key, key_value, type.Name, key.GetPath()));
                    instance = ReflectionHelper.GetDefaultValue(type, _reflectionProvider, inLogger);
                }
            }
            return(instance);
        }
Beispiel #7
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);
            }
        }
Beispiel #8
0
        void SerializeGenericCollection(object instance, Type type, IKey inKey, int inInheriteDeep, ILogPrinter inLogger)
        {
            var collection = instance as IEnumerable;

            Type[] gen_args = type.GetGenericArguments();
            if (gen_args.Length == 0)
            {
                LogError(inLogger, string.Format("SerializeGenericCollection: Generic Arguments are None. Type {0}. Instance {1}", type.Name, instance));
                return;
            }

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

            IKey tree_key = inKey;

            if (tree_key.GetValuesCount() > 0 || tree_key.GetChildCount() > 0)
            {
                tree_key = inKey.CreateChildKey("BaseCollection");
            }

            if (atomic_member)
            {
                foreach (var item in collection)
                {
                    AddValueToKey(tree_key, item);
                }
            }
            else
            {
                foreach (var item in collection)
                {
                    IKey child = tree_key.CreateArrayKey();
                    Serialize(item, declaredItemType, child, 0, inLogger);
                }
            }
        }
Beispiel #9
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);
        }