コード例 #1
0
        public static Ray2D CreateRay2D(Dictionary <string, object> v)
        {
            Vector2 origin = Vector2.zero, direction = Vector2.zero;

            foreach (var keyvalue in v)
            {
                var key   = keyvalue.Key;
                var value = keyvalue.Value;

                switch (key)
                {
                case "origin":
                    origin = VectorFactory.CreateVector2((Dictionary <string, object>)value);
                    continue;

                case "direction":
                    direction = VectorFactory.CreateVector2((Dictionary <string, object>)value);
                    continue;

                default:
                    continue;
                }
            }

            return(new Ray2D(origin, direction));
        }
コード例 #2
0
        public static BoundsInt CreateBoundsInt(Dictionary <string, object> v)
        {
            Vector3Int center = Vector3Int.zero, size = Vector3Int.zero;

            foreach (var keyvalue in v)
            {
                var key   = keyvalue.Key;
                var value = keyvalue.Value;

                switch (key)
                {
                case "position":
                    center = VectorFactory.CreateVector3Int((Dictionary <string, object>)value);
                    continue;

                case "size":
                    size = VectorFactory.CreateVector3Int((Dictionary <string, object>)value);
                    continue;

                default:
                    continue;
                }
            }

            return(new BoundsInt(center, size));
        }
コード例 #3
0
        private static object ParseDictionary(Dictionary <string, object> d, Dictionary <string, object> globaltypes, Type type, object input)
        {
            //Debug.Log(type);
            if (type == typeof(NameValueCollection))
            {
                return(CreateNv(d));
            }
            if (type == typeof(StringDictionary))
            {
                return(CreateSd(d));
            }

            if (type == null)
            {
                throw new Exception("Cannot determine type");
            }

            var typename = type.FullName;
            var o        = input ?? (Reflection.Instance.FastCreateInstance(type));

            var props = Reflection.Instance.Getproperties(type, typename);//, Reflection.Instance.IsTypeRegistered(type));

            //Debug.Log(d.Count);
            foreach (var kv in d)
            {
                var n    = kv.Key;
                var v    = kv.Value;
                var name = Reflection.CleanUpName(n);
                if (name == "$map")
                {
                    ProcessMap(o, props, (Dictionary <string, object>)d[name]);
                    continue;
                }
                MyPropInfo pi;
                if (props.TryGetValue(name, out pi) == false)
                {
                    Debug.Log(name);
                    continue;
                }

                if (!pi.CanWrite)
                {
                    continue;
                }

                if (v == null)
                {
                    continue;
                }
                object oset = null;

                //Debug.Log(name);
                //Debug.Log(v.GetType());
                //Debug.Log(pi.Type);
                switch (pi.Type)
                {
                case MyPropInfoType.Int: oset = (int)(long)v; break;

                case MyPropInfoType.Float: oset = (float)TryGetDoubleValue(v); break;

                case MyPropInfoType.Char: oset = ((string)v)[0]; break;

                case MyPropInfoType.Byte: oset = (byte)(long)v; break;

                case MyPropInfoType.Decimal: oset = (decimal)TryGetDoubleValue(v); break;

                case MyPropInfoType.Double: oset = TryGetDoubleValue(v); break;

                case MyPropInfoType.Short: oset = (short)(long)v; break;

                case MyPropInfoType.Long: oset = (long)v; break;

                case MyPropInfoType.String: oset = (string)v; break;

                case MyPropInfoType.Bool: oset = (bool)v; break;

                case MyPropInfoType.DateTime: oset = CreateDateTime((string)v); break;

                case MyPropInfoType.Enum: oset = CreateEnum(pi.Pt, v); break;

                case MyPropInfoType.Guid: oset = CreateGuid((string)v); break;

                case MyPropInfoType.List: oset = CreateGenericList((List <object>)v, pi.Pt, pi.Bt, globaltypes); break;

                case MyPropInfoType.Array:
                    if (!pi.IsValueType)
                    {
                        oset = CreateArray((List <object>)v, pi.Bt, globaltypes);
                    }
                    // what about 'else'?
                    break;

                case MyPropInfoType.ByteArray: oset = Convert.FromBase64String((string)v); break;

                case MyPropInfoType.Hashtable:     // same case as Dictionary
                case MyPropInfoType.Dictionary: oset = CreateDictionary((List <object>)v, pi.Pt, pi.GenericTypes, globaltypes); break;

                case MyPropInfoType.StringKeyDictionary: oset = CreateStringKeyDictionary((Dictionary <string, object>)v, pi.Pt, pi.GenericTypes, globaltypes); break;

                case MyPropInfoType.NameValue: oset = CreateNv((Dictionary <string, object>)v); break;

                case MyPropInfoType.StringDictionary: oset = CreateSd((Dictionary <string, object>)v); break;

                    #region Unity_Build-in_Classes
                case MyPropInfoType.Vector2:
                    oset = VectorFactory.CreateVector2((Dictionary <string, object>)v);
                    break;

                case MyPropInfoType.Vector2Int:
                    oset = VectorFactory.CreateVector2Int((Dictionary <string, object>)v);
                    break;

                case MyPropInfoType.Vector3:
                    oset = VectorFactory.CreateVector3((Dictionary <string, object>)v);
                    break;

                case MyPropInfoType.Vector3Int:
                    oset = VectorFactory.CreateVector3Int((Dictionary <string, object>)v);
                    break;

                case MyPropInfoType.Vector4:
                    oset = VectorFactory.CreateVector4((Dictionary <string, object>)v);
                    break;

                case MyPropInfoType.Color:
                    oset = UnityClassesFactoryMain.CreateColor((Dictionary <string, object>)v);
                    break;

                case MyPropInfoType.Color32:
                    oset = UnityClassesFactoryMain.CreateColor32((Dictionary <string, object>)v);
                    break;

                case MyPropInfoType.Rect:
                    oset = UnityClassesFactoryMain.CreateRect((Dictionary <string, object>)v);
                    break;

                case MyPropInfoType.RectInt:
                    oset = UnityClassesFactoryMain.CreateRectInt((Dictionary <string, object>)v);
                    break;

                case MyPropInfoType.Bounds:
                    oset = UnityClassesFactoryMain.CreateBounds((Dictionary <string, object>)v);
                    break;

                case MyPropInfoType.BoundsInt:
                    oset = UnityClassesFactoryMain.CreateBoundsInt((Dictionary <string, object>)v);
                    break;

                case MyPropInfoType.Ray:
                    oset = UnityClassesFactoryMain.CreateRay((Dictionary <string, object>)v);
                    break;

                case MyPropInfoType.Ray2D:
                    oset = UnityClassesFactoryMain.CreateRay2D((Dictionary <string, object>)v);
                    break;

                case MyPropInfoType.Quaternion:
                    oset = UnityClassesFactoryMain.CreateQuaternion((Dictionary <string, object>)v);
                    break;

                    #endregion

                case MyPropInfoType.Custom: oset = Reflection.Instance.CreateCustom((string)v, pi.Pt); break;

//                    case MyPropInfoType.Unknown:
//                        Debug.LogError("Your class is unsupported: " + pi.Pt + ". Pls register with JSONObject.RegisterCustomType().");
//                        break;
                default:
                {
                    if (pi.IsGenericType && pi.IsValueType == false && v is List <object> )
                    {
                        oset = CreateGenericList((List <object>)v, pi.Pt, pi.Bt, globaltypes);
                    }

                    else if ((pi.IsClass || pi.IsStruct || pi.IsInterface) && v is Dictionary <string, object> )
                    {
                        //Debug.Log("ParseDictionary");
                        oset = ParseDictionary((Dictionary <string, object>)v, globaltypes, pi.Pt, pi.Getter(o));
                    }
                    else if (v is List <object> )
                    {
                        oset = CreateArray((List <object>)v, typeof(object), globaltypes);
                    }

                    else if (pi.IsValueType)
                    {
                        oset = ChangeType(v, pi.ChangeType);
                    }

                    else
                    {
                        oset = v;
                    }
                }
                break;
                }

                o = pi.Setter(o, oset);
            }
            return(o);
        }