Ejemplo n.º 1
0
    public static int getJSObj(object csObj, JSCache.TypeInfo typeInfo)
    {
        if (typeInfo.IsValueType)
        {
            return(0);
        }

        JS_CS_Rel Rel;
        var       wr     = csObj as WeakReference;
        object    newObj = (wr != null) ? wr.Target : csObj;

        if (mDictionary2.TryGetValue(csObj, out Rel))
        {
#if UNITY_EDITOR
            wr = Rel.csObj as WeakReference;
            object oldObj = (wr != null) ? wr.Target : Rel.csObj;
            if (!oldObj.Equals(newObj))
            {
                Debug.LogError("mDictionary2 and mDictionary1 saves different object");
            }
#endif
            return(Rel.jsObjID);
        }
        return(0);
    }
Ejemplo n.º 2
0
    public static void addJSCSRel(int jsObjID, object csObj, bool weakReference = false)
    {
        if (weakReference)
        {
            WeakReference wrObj = new WeakReference(csObj);
            var           Rel   = new JS_CS_Rel(jsObjID, wrObj);
            AddDictionary1(jsObjID, Rel);
            AddDictionary2(Rel);
        }
        else
        {
            JSCache.TypeInfo typeInfo = JSCache.GetTypeInfo(csObj.GetType());

            if (mDictionary1.ContainsKey(jsObjID))
            {
                if (typeInfo.IsValueType)
                {
                    mDictionary1.Remove(jsObjID);
                }
            }

            var Rel = new JS_CS_Rel(jsObjID, csObj);
            AddDictionary1(jsObjID, Rel);

            if (typeInfo.IsClass)
            {
                AddDictionary2(Rel);
            }
        }
    }
Ejemplo n.º 3
0
    public static int getJSObj(object csObj, JSCache.TypeInfo typeInfo)
    {
        if (typeInfo.IsValueType)
        {
            return(0);
        }

        JS_CS_Rel Rel;
        var       wr     = csObj as WeakReference;
        object    newObj = (wr != null) ? wr.Target : csObj;

        if (mDictionary2.TryGetValue(csObj, out Rel))
        {
#if UNITY_EDITOR
            wr = Rel.csObj as WeakReference;
            object oldObj = (wr != null) ? wr.Target : Rel.csObj;
            if (!oldObj.Equals(newObj))
            {
                Debug.LogError("mDictionary2 and mDictionary1 saves different object");
            }
            else if (oldObj.Equals(null) != newObj.Equals(null))
            {
                // Unity 神奇的比较,同一资源,释放之后同时加载
                Debug.LogErrorFormat("有趣,暂时发现Unity资源是这么神奇,可以忽略:{0} {1}", oldObj, newObj);
                Rel.csObj = csObj;
            }
#endif
            return(Rel.jsObjID);
        }
        return(0);
    }
Ejemplo n.º 4
0
 private static void help_getGoAndType(JSVCall vc)
 {
     _curGo = _goFromComponent;
     if (_curGo == null)
     {
         _curGo = (GameObject)vc.csObj;
     }
     _typeString = JSApi.getStringS((int)JSApi.GetType.Arg);
     _type       = JSDataExchangeMgr.GetTypeByName(_typeString);
     _typeInfo   = JSCache.GetTypeInfo(_type);
 }
Ejemplo n.º 5
0
    public static void addJSCSRel(int jsObjID, object csObj, bool weakReference = false)
    {
        //if (csObj == null || csObj.Equals(null))

//         if (csObj != null && csObj is UnityEngine.Object)
//         {
//             if (csObj.Equals(null))
//             {
//                 Debug.LogError("JSMgr.addJSCSRel object == null, call stack:" + new System.Diagnostics.StackTrace().ToString());
//                 //throw new Exception();
//             }
//         }

        if (weakReference)
        {
            int           hash  = csObj.GetHashCode();
            WeakReference wrObj = new WeakReference(csObj);
            var           Rel   = new JS_CS_Rel(jsObjID, wrObj, hash);
            mDictionary1.Add(jsObjID, Rel);
            mDictionary2.Add(hash, Rel);
        }
        else
        {
            int hash = csObj.GetHashCode();
            JSCache.TypeInfo typeInfo = JSCache.GetTypeInfo(csObj.GetType());

            if (mDictionary1.ContainsKey(jsObjID))
            {
                if (typeInfo.IsValueType)
                {
                    mDictionary1.Remove(jsObjID);
                }
            }

#if UNITY_EDITOR
            if (mDictionary1.ContainsKey(jsObjID))
            {
                Debug.Log(">_<");
            }
#endif

            var Rel = new JS_CS_Rel(jsObjID, csObj, hash);
            mDictionary1.Add(jsObjID, Rel);

            if (typeInfo.IsClass)
            {
                mDictionary2.Add(hash, Rel);
            }
        }
    }
Ejemplo n.º 6
0
    public static int getJSObj(object csObj, JSCache.TypeInfo typeInfo)
    {
        if (typeInfo.IsValueType)
        {
            return(0);
        }

        JS_CS_Rel Rel;
        object    newObj = (csObj is WeakReference) ? ((WeakReference)csObj).Target : csObj;
        int       hash   = newObj.GetHashCode();

        if (mDictionary2.TryGetValue(hash, out Rel))
        {
#if UNITY_EDITOR
            object oldObj = (Rel.csObj is WeakReference) ? ((WeakReference)Rel.csObj).Target : Rel.csObj;
            if (!oldObj.Equals(newObj))
            {
                Debug.LogError("mDictionary2 and mDictionary1 saves different object");
            }
#endif
            return(Rel.jsObjID);
        }
        return(0);
    }
    /// <summary>
    /// Should return base type object?
    /// </summary>
    /// <param name="typeName">Name of the type.</param>
    /// <returns></returns>
//     public bool shouldReturnBaseTypeObject(string typeName)
//     {
//
//     }

    /// <summary>
    /// Sets the object.
    /// if e == UpdateRefARGV, currIndex must be set before this function
    ///
    /// operation of this function:
    /// 1) if JavaScript already exists, return that JavaScript object.
    /// 2) else, create a new JavaScript object and return it.
    /// </summary>
    /// <param name="e">The e.</param>
    /// <param name="csObj">The cs object.</param>
    /// <returns></returns>
    public int setObject(/* JSApi.SetType */ int e, object csObj)
    {
        // http://answers.unity3d.com/questions/1087158/unityengineobject-is-null-but-systemobject-is-not.html
        if (csObj != null && (csObj is UnityEngine.Object))
        {
            if (csObj.Equals(null))
            {
                csObj = null;
            }
        }

        int jsObjID = 0;

        if (csObj != null)
        {
            Type             csType   = csObj.GetType();
            JSCache.TypeInfo typeInfo = JSCache.GetTypeInfo(csType);

            if (typeInfo.IsClass)
            {
                jsObjID = JSMgr.getJSObj(csObj, typeInfo);
            }
            if (jsObjID == 0)
            {
                if (csObj is CSRepresentedObject)
                {
                    jsObjID = ((CSRepresentedObject)csObj).jsObjID;
                }
                else
                {
                    if (typeInfo.IsDelegate)
                    {
                        jsObjID = JSMgr.getFunIDByDelegate((Delegate)csObj);
                    }
                    if (jsObjID == 0)
                    {
                        string typeName = string.Empty;
                        // create a JSRepresentedObject object in JS to represent a C# delegate object
                        if (typeInfo.IsDelegate)
                        {
                            typeName = "JSRepresentedObject";
                            jsObjID  = JSApi.createJSClassObject(typeName);
                        }
                        else
                        {
                            typeName = typeInfo.JSTypeFullName;
                            jsObjID  = JSApi.createJSClassObject(typeName);
                            if (jsObjID == 0)
                            {
                                Type             baseType     = csType.BaseType;
                                JSCache.TypeInfo baseTypeInfo = JSCache.GetTypeInfo(baseType);
                                if (baseType != null)
                                {
                                    var baseTypeName = baseTypeInfo.JSTypeFullName;
                                    jsObjID = JSApi.createJSClassObject(baseTypeName);
                                    if (jsObjID != 0)
                                    {
                                        Debug.LogWarning("WARNING: Return a \"" + typeName + "\" to JS failed. Return base type\"" + baseTypeName + "\" instead.");
                                    }
                                }
                            }
                        }

                        if (jsObjID != 0)
                        {
                            JSMgr.addJSCSRel(jsObjID, csObj);
                        }
                        else
                        {
                            Debug.LogError("Return a \"" + typeName + "\" to JS failed. Did you forget to export that class?");
                        }
                    }
                }
            }
        }

        JSApi.setObject(e, jsObjID);
        return(jsObjID);
    }