示例#1
0
    private static ClassTypeToJsonFunction CreateTypeSerializeFunctionForDictionary(Type targetType)
    {
        // 1.check type
        if (!targetType.IsGenericType || targetType.GetGenericTypeDefinition() != typeof(Dictionary <,>))
        {
            VeerDebug.LogWarning(" CreateTypeSerializeFunctionForDictionary but target type is not dictionary : " + targetType);
            return(null);
        }

        // 2.check dictionary key type is string
        Type keyType = targetType.GetGenericArguments()[0];

        if (keyType != typeof(string))
        {
            VeerDebug.LogWarning(" ToJsonObject 不支持 KeyType 不是 string 的 Dictionary : " + targetType);
            return(null);
        }

        // 3.check register value type
        Type dicValueType = targetType.GetGenericArguments()[1];

        CheckRegister(dicValueType);

        // 4.create function
        ClassTypeToJsonFunction function = (object dictionaryObj) =>
        {
            JSONObject jsonDictionary = JSONObject.obj;

            if (dictionaryObj == null)
            {
                return(jsonDictionary);
            }

            IDictionary           iDictionary = (IDictionary)dictionaryObj;
            IDictionaryEnumerator enumerator  = iDictionary.GetEnumerator();

            if (_BasicTypeToJsonFunctionMap.ContainsKey(dicValueType))
            {
                while (enumerator.MoveNext())
                {
                    _BasicTypeToJsonFunctionMap[dicValueType](jsonDictionary, enumerator.Key.ToString(), enumerator.Value);
                }
            }
            else if (_ClassTypeToJsonFunctionMap.ContainsKey(dicValueType))
            {
                ClassTypeToJsonFunction toJsonFunc = _ClassTypeToJsonFunctionMap[dicValueType];
                if (toJsonFunc != null)
                {
                    while (enumerator.MoveNext())
                    {
                        jsonDictionary.AddField(enumerator.Key.ToString(), toJsonFunc(enumerator.Value));
                    }
                }
            }

            return(jsonDictionary);
        };

        return(function);
    }
示例#2
0
    public static void RegisterClassTypeSerializeFunction(Type classType, ClassDefaultResetFunc classDefaultResetFunc, bool bOverride = false)
    {
        if (classType == null || !classType.IsClass)
        {
#if UNITY_EDITOR
            VeerDebug.LogWarning(" RegisterTypeSerializeFunction Type 为 null 或不是 Class ...");
#endif
            return;
        }

        if (bOverride)
        {
            _ClassDefaultResetFuncMap.SetAddValue(classType, classDefaultResetFunc);
        }
        else
        {
            if (_ClassDefaultResetFuncMap.ContainsKey(classType))
            {
                return;
            }
            else
            {
                _ClassDefaultResetFuncMap.Add(classType, classDefaultResetFunc);
            }
        }
    }
示例#3
0
    private static ClassTypeToJsonFunction CreateTypeSerializeFunction(Type targetType)
    {
        IsCreatingFuncTypeList.Add(targetType);

        ClassTypeToJsonFunction toJsonObjectFunction = null;

        if (targetType.IsEnum)
        {
#if UNITY_EDITOR
            VeerDebug.LogWarning(" 目前不支持 Enum 类型 ToJsonObject ...");
#endif
            toJsonObjectFunction = null;
        }
        else if (targetType.IsArray || targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(List <>))
        {
            toJsonObjectFunction = CreateTypeSerializeFunctionForArrayList(targetType);
        }
        else if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Dictionary <,>))
        {
            toJsonObjectFunction = CreateTypeSerializeFunctionForDictionary(targetType);
        }
        else if (targetType.IsClass)
        {
            toJsonObjectFunction = CreateTypeSerializeFunctionForClass(targetType);
        }

        // 注册方法
        RegisterClassTypeSerializeFunction(targetType, toJsonObjectFunction);

        IsCreatingFuncTypeList.Remove(targetType);

        return(toJsonObjectFunction);
    }
示例#4
0
    private static ClassTypeToJsonFunction CreateTypeSerializeFunctionForArrayList(Type targetType)
    {
        // 1.check type
        Type arrayListElementType = null;

        if (targetType.IsArray)
        {
            arrayListElementType = targetType.GetElementType();
        }
        else if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(List <>))
        {
            arrayListElementType = targetType.GetGenericArguments()[0];
        }
        if (arrayListElementType == null)
        {
            VeerDebug.LogWarning(" CreateTypeSerializeFunctionForArrayList but target type is not arrar or list : " + targetType);
            return(null);
        }

        // 2.check register element type
        CheckRegister(arrayListElementType);

        // 3.create function
        ClassTypeToJsonFunction function = (object arrayListObj) =>
        {
            JSONObject jsonArray = JSONObject.arr;

            if (arrayListObj == null)
            {
                return(jsonArray);
            }

            IEnumerable elementArray = arrayListObj as IEnumerable;

            if (_BasicTypeToJsonFunctionMap.ContainsKey(arrayListElementType))
            {
                foreach (var element in elementArray)
                {
                    _BasicTypeToJsonFunctionMap[arrayListElementType](jsonArray, null, element);
                }
            }
            else if (_ClassTypeToJsonFunctionMap.ContainsKey(arrayListElementType))
            {
                ClassTypeToJsonFunction toJsonFunc = _ClassTypeToJsonFunctionMap[arrayListElementType];
                if (toJsonFunc != null)
                {
                    foreach (var element in elementArray)
                    {
                        jsonArray.Add(toJsonFunc(element));
                    }
                }
            }

            return(jsonArray);
        };

        return(function);
    }
示例#5
0
    // 核心方法
    private static void DefaultReset_Internal(object obj)
    {
        if (!_Inited)
        {
            Init();
        }

        // 检查空值
        if (obj == null)
        {
            return;
        }

        Type targetType = null;

        // 检查是否为 Type 类型
        if (obj is Type)
        {
            targetType = obj as Type;

            // How to tell if a Type is a static class?
            // https://stackoverflow.com/questions/4145072/how-to-tell-if-a-type-is-a-static-class
            if (!targetType.IsClass || !targetType.IsAbstract || !targetType.IsSealed)
            {
                VeerDebug.LogWarning(" 不能对 非静态类 进行 DefaultReset_Internal(Type): " + targetType.Name);
                return;
            }
        }
        else
        {
            // 检查是否为 类的实例
            targetType = obj.GetType();
            if (!targetType.IsClass)
            {
                VeerDebug.LogWarning(" 不支持对非类的实例进行 Default Reset 操作 : " + targetType.Name);
                return;
            }
        }

        CheckRegister(targetType);

        // 检查是否已有 DefaultReset Func
        if (_ClassDefaultResetFuncMap.ContainsKey(targetType))
        {
            ClassDefaultResetFunc existingFunction = _ClassDefaultResetFuncMap[targetType];
            if (existingFunction == null)
            {
                VeerDebug.LogWarning(" 对无法支持的类型进行 DefaultReset 操作 : " + targetType.Name);
                return;
            }

            // 使用已有方法进行 DefaultReset
            existingFunction(obj);
        }
    }
示例#6
0
    private static void CheckRegister(Type targetType)
    {
        if (_ClassDefaultResetFuncMap.ContainsKey(targetType))
        {
            return;
        }

        CreateTypeDefaultResetFunc(targetType);

#if UNITY_EDITOR
        if (!_ClassDefaultResetFuncMap.ContainsKey(targetType))
        {
            VeerDebug.LogWarning(" ClassDefaultResetFunc 不支持该类型的 DefaultReset 方法 : " + targetType);
        }
#endif
    }
示例#7
0
    public static Task AsyncCreate(string jsonText, JSONObjectAsyncCreateCallback callback)
    {
#if UNITY_EDITOR
        if (callback == null)
        {
            VeerDebug.LogError(" JSONObject AsyncCreate callback is null...");
            return(null);
        }
#endif

        Task task = Task <JSONObject> .Run(() =>
        {
            JSONObject json = null;
            try
            {
                json = Create(jsonText);
            }
            catch
            {
                VeerDebug.LogWarning(" create json failed ...");
                return(null);
            }
            return(json);
        }).ContinueInMainThreadWith((obj) =>
        {
            if (obj == null)
            {
                VeerDebug.LogWarning(" async json object create timeout, maybe lost thread ...");
                callback(false, null);
                return;
            }
            else if (obj.IsFaulted || obj.IsAborted || !obj.IsCompleted)
            {
                VeerDebug.LogWarning(" async json object create timeout, maybe lost thread ...");
                callback(false, null);
                return;
            }
            else
            {
                callback(true, obj.Result);
                return;
            }
        }, 8f);

        return(task);
    }
示例#8
0
    private static void CheckRegister(Type targetType)
    {
        if (_BasicTypeToJsonFunctionMap.ContainsKey(targetType) ||
            _ClassTypeToJsonFunctionMap.ContainsKey(targetType))
        {
            return;
        }

        CreateTypeSerializeFunction(targetType);

#if UNITY_EDITOR
        if (!_ClassTypeToJsonFunctionMap.ContainsKey(targetType))
        {
            VeerDebug.LogWarning(" CreateTypeSerializeFunction 不支持该类型的 ToJsonObject 方法 : " + targetType);
        }
#endif
    }
示例#9
0
    // 核心方法
    private static JSONObject ToJsonObject_Internal(object target)
    {
        if (!_Inited)
        {
            Init();
        }

        // 检查空值
        if (target == null)
        {
            return(null);
        }

        // 检查是否为 类的实例
        Type targetType = target.GetType();

        if (!targetType.IsClass)
        {
            VeerDebug.LogWarning(" 不支持对非类的实例进行 ToJsonObject 操作 : " + targetType.Name);
            return(null);
        }

        // register
        CheckRegister(targetType);

        // 检查是否已有 ToJson方法
        if (_ClassTypeToJsonFunctionMap.ContainsKey(targetType))
        {
            ClassTypeToJsonFunction existingFunction = _ClassTypeToJsonFunctionMap[targetType];
            if (existingFunction == null)
            {
                VeerDebug.LogWarning(" 对无法支持的类型进行 ToJsonObject 操作 : " + targetType.Name);
                return(null);
            }
            // 使用已有方法进行 ToJson
            return(_ClassTypeToJsonFunctionMap[targetType](target));
        }
        else
        {
            return(null);
        }
    }
示例#10
0
    public static void BindObjectRecursive(Component root)
    {
        if (root == null)
        {
            VeerDebug.LogWarning(" BindObjectRecursive root is null ...");
            return;
        }

        Type rootType = root.GetType();

        FieldInfo[] fieldInfos = rootType.GetFields(
            BindingFlags.Instance |
            BindingFlags.Public |
            BindingFlags.NonPublic);

        bool bFieldIsComponentOrGameObject = false;

        foreach (var f in fieldInfos)
        {
            if (f.FieldType.IsSubclassOf(typeof(Component)))
            {
                bFieldIsComponentOrGameObject = true;
            }
            else if (f.FieldType.Equals(typeof(GameObject)))
            {
                bFieldIsComponentOrGameObject = false;
            }
            else
            {
                continue;
            }

            foreach (var attr in f.GetCustomAttributes(true))
            {
                if (attr is ObjectBinderAttribute)
                {
                    f.SetValue(root, null);

                    ObjectBinderAttribute objectBinder = attr as ObjectBinderAttribute;

                    string attrObjectName = objectBinder.ObjectName;
                    if (string.IsNullOrEmpty(attrObjectName))
                    {
                        VeerDebug.LogWarning(" ObjectBinderAttribute attrObjectName is null or empty ... " + f.Name);
                        continue;
                    }

#if UNITY_EDITOR
                    if (!attrObjectName.StartsWith(VeerYeastConst.Auto_Bind_Tag))
                    {
                        VeerDebug.LogError(" ObjectBinderAttribute attrObjectName is not start with " + VeerYeastConst.Auto_Bind_Tag + " ... " + f.Name);
                        continue;
                    }
#endif

                    GameObject targetGameObject = root.gameObject.GetChildGoByName(attrObjectName, true);
                    if (targetGameObject == null)
                    {
                        VeerDebug.LogWarning(" ObjectBinderAttribute targetGameObject is null ... " + f.Name);
                        continue;
                    }

                    if (!bFieldIsComponentOrGameObject)
                    {
                        f.SetValue(root, targetGameObject);
                    }
                    else
                    {
                        Component targetMonoBehaviour = targetGameObject.GetComponent(f.FieldType) as Component;
                        if (targetMonoBehaviour == null)
                        {
                            VeerDebug.LogWarning(" ObjectBinderAttribute targetMonoBehaviour is null ... " + f.Name);
                            continue;
                        }

                        f.SetValue(root, targetMonoBehaviour);
                    }
                }

                if (attr is ObjectBinderRootAttribute)
                {
                    Component rootMonoBehaviour = f.GetValue(root) as Component;
                    if (rootMonoBehaviour == null)
                    {
#if UNITY_EDITOR
                        VeerDebug.LogWarning(" ObjectBinderRootAttribute root is null ... " + f.Name);
#endif
                        continue;
                    }

                    BindObjectRecursive(rootMonoBehaviour);
                }
            }
        }
    }
示例#11
0
    private static ClassTypeToJsonFunction CreateTypeSerializeFunctionForClass(Type targetType)
    {
        // 1.check type
        if (!targetType.IsClass)
        {
            VeerDebug.LogWarning(" CreateTypeSerializeFunctionForClass but target type is not Class : " + targetType);
            return(null);
        }

        // 2.找到全部 targetType 需要 ToJson 的字段
        List <FieldInfo> needToJsonFieldInfos = new List <FieldInfo>();
        BindingFlags     flag = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

        foreach (FieldInfo field in targetType.GetFields(flag))
        {
            Type fieldType = field.GetType();
            if (_BasicTypeToJsonFunctionMap.ContainsKey(fieldType))
            {
                needToJsonFieldInfos.Add(field);
            }
            else if (fieldType.IsClass)
            {
                needToJsonFieldInfos.Add(field);
            }
        }

        // 3.register all types
        foreach (FieldInfo field in needToJsonFieldInfos)
        {
            Type fieldType = field.FieldType;
            if (!IsCreatingFuncTypeList.Contains(fieldType))
            {
                CheckRegister(fieldType);
            }
        }

        // 4.create function
        ClassTypeToJsonFunction function = (object classInstance) =>
        {
            JSONObject json = JSONObject.obj;

            if (classInstance == null)
            {
                return(JSONObject.nullJO);
            }

            foreach (var field in needToJsonFieldInfos)
            {
                object fieldValue = field.GetValue(classInstance);
                Type   fieldType  = field.FieldType;

                if (_BasicTypeToJsonFunctionMap.ContainsKey(fieldType))
                {
                    _BasicTypeToJsonFunctionMap[fieldType](json, field.Name, fieldValue);
                }
                else if (_ClassTypeToJsonFunctionMap.ContainsKey(fieldType))
                {
                    ClassTypeToJsonFunction toJsonFunc = _ClassTypeToJsonFunctionMap[fieldType];
                    if (toJsonFunc != null)
                    {
                        json.AddField(field.Name, toJsonFunc(fieldValue));
                    }
                }
            }

            return(json);
        };

        return(function);
    }
示例#12
0
        public static void CallbackConvert(SimpleHttpReq simpleReq, HTTPResponse res, SimpleHttpCallback callback)
        {
            // 0.如果 callbacck 为 null 则直接 return
            if (callback == null)
            {
                return;
            }

            // 1.请求成功则直接 callback
            if (res != null)
            {
                if (res.StatusCode == 200 || res.StatusCode == 304)
                {
                    callback(SimpleHttpResult.Success, res, simpleReq.ReqId);
                    return;
                }
                else if (res.StatusCode == 400)
                {
                    // code 400 说明服务器正常 但是 数据有错误 所以 不需要再 retry 了
                    VeerDebug.LogWarning(" req failed with code 400 : " + simpleReq.Req.Uri + " error msg : " + res.Message);
                    callback(SimpleHttpResult.FailedFor400, res, simpleReq.ReqId);
                    return;
                }
                else if (res.StatusCode == 404)
                {
                    // code 404 地址不存在 不需要再 retry 了
                    //VeerDebug.LogWarning(" req failed with code 404 : " + simpleReq.Req.Uri + " error msg : " + res.Message);
                    callback(SimpleHttpResult.FailedForNetwork, res, simpleReq.ReqId);
                    return;
                }
            }

            // 2.Abort 则 直接 callback
            if (simpleReq.Req.State == HTTPRequestStates.Aborted)
            {
                callback(SimpleHttpResult.Abort, res, simpleReq.ReqId);
                return;
            }

            // 3.其它情况需要尝试 retry
            if (simpleReq._CurrentRetryCount < simpleReq._RetryCount)
            {
                simpleReq._CurrentRetryCount++;
                VeerDebug.Log(" retry http req time : " + simpleReq._CurrentRetryCount + " api : " + simpleReq.Req.Uri + " code : " + res.StatusCode);
                simpleReq.Send(true);
                return;
            }

            // 4.retry 结束后返回 callback
            if (simpleReq.Req.State == HTTPRequestStates.Error)
            {
                VeerDebug.Log(" request error : " + simpleReq.Req.Exception);

                callback(SimpleHttpResult.FailedForNetwork, res, simpleReq.ReqId);
                return;
            }

            if (simpleReq.Req.State == HTTPRequestStates.ConnectionTimedOut || simpleReq.Req.State == HTTPRequestStates.TimedOut)
            {
                VeerDebug.Log(" request time out ...");

                callback(SimpleHttpResult.FailedForNetwork, res, simpleReq.ReqId);
                return;
            }

            callback(SimpleHttpResult.FailedForNetwork, res, simpleReq.ReqId);
        }