Exemplo n.º 1
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);
            }
        }
    }
Exemplo n.º 2
0
    private static void help_searchAndRetComs(JSVCall vc, JSComponent[] com, string typeString)
    {
        var lst = new List <JSComponent>();

        foreach (var c in com)
        {
            if (c.jsClassName == typeString ||
                JSCache.IsInheritanceRel(typeString, c.jsClassName))
            {
                lst.Add(c);
            }
        }
        for (int i = 0; i < lst.Count; i++)
        {
            int jsObjID = lst[i].GetJSObjID();
            JSApi.setObject((int)JSApi.SetType.SaveAndTempTrace, jsObjID);
            JSApi.moveSaveID2Arr(i);
        }
        JSApi.setArrayS((int)JSApi.SetType.Rval, lst.Count, true);

        //         var arrVal = new JSApi.jsval[lst.Count];
        //         for (int i = 0; i < lst.Count; i++)
        //         {
        //             JSApi.JSh_SetJsvalObject(ref arrVal[i], lst[i].jsObj);
        //         }
        //         JSMgr.datax.setArray(JSDataExchangeMgr.eSetType.SetRval, arrVal);
    }
Exemplo n.º 3
0
    /*
     * GameObject.AddComponent<T>()
     */

    public static bool GameObject_AddComponentT1(JSVCall vc, int count)
    {
        help_getGoAndType(vc);

        if (_typeInfo.IsCSMonoBehaviour)
        {
            var com = _curGo.AddComponent(_type);
            JSMgr.datax.setObject((int)JSApi.SetType.Rval, com);
        }
        else
        {
            string jsComponentName = JSCache.GetMono2JsComName(_typeString);
            var    jsComponentType = typeof(JSComponent);
            if (string.IsNullOrEmpty(jsComponentName))
            {
                Debug.LogWarning(string.Format("\"{0}\" has no JSComponent_XX. Use JSComponent instead.", _typeString));
            }
            else
            {
                jsComponentType = JSDataExchangeMgr.GetTypeByName(jsComponentName, jsComponentType);
            }

            var jsComp = (JSComponent)_curGo.AddComponent(jsComponentType);
            jsComp.jsClassName = _typeString;
            jsComp.Setup(); // 要调用 js 的 Awake

            //JSApi.JSh_SetRvalObject(vc.cx, vc.vp, jsComp.jsObj);
            JSApi.setObject((int)JSApi.SetType.Rval, jsComp.GetJSObjID());
        }
        return(true);
    }
Exemplo n.º 4
0
    /*
     * GameObject.AddComponent<T>()
     */
    public static bool GameObject_AddComponentT1(JSVCall vc, int count)
    {
        help_getGoAndType(vc);

        if (typeInfo.IsCSMonoBehaviour)
        {
            Component com = go.AddComponent(type);
            JSMgr.datax.setObject((int)JSApi.SetType.Rval, com);
        }
        else
        {
            string jsComponentName = JSCache.GetMonoBehaviourJSComponentName(typeString);
            Type   jsComponentType = typeof(JSComponent);
            if (string.IsNullOrEmpty(jsComponentName))
            {
                Debug.LogWarning(string.Format("\"{0}\" has no JSComponent_XX. Use JSComponent instead.", typeString));
            }
            else
            {
                jsComponentType = JSDataExchangeMgr.GetTypeByName(jsComponentName, jsComponentType);
            }

            JSComponent jsComp = (JSComponent)go.AddComponent(jsComponentType);
            jsComp.jsClassName = typeString;
            jsComp.jsFail      = false;
            jsComp.init(true);
            jsComp.callAwake(); // Òªµ÷Óà js µÄ Awake

            //JSApi.JSh_SetRvalObject(vc.cx, vc.vp, jsComp.jsObj);
            JSApi.setObject((int)JSApi.SetType.Rval, jsComp.GetJSObjID(false));
        }
        return(true);
    }
Exemplo n.º 5
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);
 }
Exemplo n.º 6
0
 static void help_getGoAndType(JSVCall vc)
 {
     go = goFromComponent;
     if (go == null)
     {
         go = (UnityEngine.GameObject)vc.csObj;
     }
     typeString = JSApi.getStringS((int)JSApi.GetType.Arg);
     type       = JSDataExchangeMgr.GetTypeByName(typeString);
     typeInfo   = JSCache.GetTypeInfo(type);
 }
Exemplo n.º 7
0
    public static Type GetTypeByName(string typeName, Type defaultType = null)
    {
        if (string.IsNullOrEmpty(typeName))
        {
            return(null);
        }

        Type t = null;

        if (!typeCache.TryGetValue(typeName, out t))
        {
            t = Type.GetType(typeName);
            if (t == null)
            {
                foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
                {
                    t = a.GetType(typeName);
                    if (t != null)
                    {
                        break;
                    }
                }
            }

            //忽略带JsType属性的类型
            if (t != null)
            {
                string fullName = t.FullName;
                if (JSCache.IsJsComTypeName(fullName))
                {
                    t = typeof(JSComponent);
                }
                else if (t.IsDefined(typeof(JsTypeAttribute), false) || JSCache.IsJsTypeName(t.FullName))
                {
                    t = null;
                }
            }

            typeCache[typeName] = t; // perhaps null
            //if (t == null)
            //{
            //    Debug.LogError("GetType of \"" + typeName + "\" is null. Did you export that class to JavaScript?");
            //}
        }
        if (t == null)
        {
            return(defaultType); // typeof(CSRepresentedObject);
        }
        return(t);
    }
Exemplo n.º 8
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);
            }
        }
    }
Exemplo n.º 9
0
    public static bool InitJSEngine(OnInitJSEngine onInitJSEngine)
    {
        if (InitJSEngine_ing)
        {
            Debug.LogError("FATAL ERROR: Trying to InitJSEngine twice");
            return(false);
        }

        InitJSEngine_ing = true;
        shutDown         = false;

        int initResult = JSApi.InitJSEngine(
            new JSApi.JSErrorReporter(errorReporter),
            new JSApi.CSEntry(JSMgr.CSEntry),
            new JSApi.JSNative(require),
            new JSApi.OnObjCollected(onObjCollected),
            new JSApi.JSNative(print));

        startValueMapID = JSApi.getValueMapStartIndex();
        Debug.Log("startValueMapID " + startValueMapID);

        if (initResult != 0)
        {
            Debug.LogError("InitJSEngine fail. error = " + initResult);
            onInitJSEngine(false);
            InitJSEngine_ing = false;
            return(false);
        }

        CSGenerateRegister.RegisterAll();
        JSCache.InitJsTypeConfig();

        onInitJSEngine(true);

        //if (!RefCallStaticMethod("CSGenerateRegister", "RegisterAll"))
        //{
        //    Debug.LogError("Call CSGenerateRegister.RegisterAll() failed. Did you forget to click menu [Assets | JSB | Generate JS and CS Bindings]?");
        //    onInitJSEngine(false);
        //    ret = false;
        //}
        //else
        //{
        //    onInitJSEngine(true);
        //    ret = true;
        //}

        InitJSEngine_ing = false;
        return(true);
    }
Exemplo n.º 10
0
    private static void help_searchAndRetCom(JSVCall vc, JSComponent[] jsComs, string typeString)
    {
        int id = 0;

        foreach (var jsCom in jsComs)
        {
            if (jsCom.jsClassName == typeString ||
                JSCache.IsInheritanceRel(typeString, jsCom.jsClassName))
            {
                id = jsCom.GetJSObjID();
                break;
            }
        }
        JSApi.setObject((int)JSApi.SetType.Rval, id);
    }
Exemplo n.º 11
0
    public static bool InitJSEngine(JSFileLoader jsLoader, OnInitJSEngine onInitJSEngine)
    {
        if (InitJSEngine_ing)
        {
            Debug.LogError("FATAL ERROR: Trying to InitJSEngine twice");
        }

        InitJSEngine_ing = true;
        shutDown         = false;

        int initResult = JSApi.InitJSEngine(
            new JSApi.JSErrorReporter(errorReporter),
            new JSApi.CSEntry(JSMgr.CSEntry),
            new JSApi.JSNative(require),
            new JSApi.OnObjCollected(onObjCollected),
            new JSApi.JSNative(print));

        startValueMapID = JSApi.getValueMapStartIndex();
        Debug.Log("startValueMapID " + startValueMapID);

        if (initResult != 0)
        {
            Debug.LogError("InitJSEngine fail. error = " + initResult);
            onInitJSEngine(false);
            InitJSEngine_ing = false;
            return(false);
        }

        JSMgr.jsLoader = jsLoader;

        bool ret = false;

        if (!RefCallStaticMethod("CSharpGenerated", "RegisterAll"))
        {
            Debug.LogError("Call CSharpGenerated.RegisterAll() failed. Did you forget to click menu [Assets | JSB | Generate JS and CS Bindings]?");
            onInitJSEngine(false);
            ret = false;
        }
        else
        {
            onInitJSEngine(true);
            ret = true;
        }

        JSCache.InitMonoBehaviourJSComponentName();
        InitJSEngine_ing = false;
        return(ret);
    }
Exemplo n.º 12
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);
    }