Exemplo n.º 1
0
    public static bool evaluate(string jsScriptName)
    {
        if (evaluatedScript.ContainsKey(jsScriptName))
        {
            return(true);
        }
        // add even failed
        evaluatedScript.Add(jsScriptName, true);

        var bytes = JSFileLoader.LoadJSSync(jsScriptName);

        if (bytes == null)
        {
            Debug.LogError(jsScriptName + "file bytes is null");
            return(false);
        }
        else if (bytes.Length == 0)
        {
            Debug.LogError(jsScriptName + "file bytes length = 0");
            return(false);
        }

        bool ret;

        if (JSEngine.inst.UseJSC)
        {
            ret = 1 == JSApi.evaluate_jsc(bytes, (uint)bytes.Length, jsScriptName);
        }
        else
        {
            ret = 1 == JSApi.evaluate(bytes, (uint)bytes.Length, jsScriptName);
        }

        return(ret);
    }
Exemplo n.º 2
0
    public static bool InitJSEngine(JSFileLoader jsLoader, OnInitJSEngine onInitJSEngine)
    {
        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));

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

        JSMgr.jsLoader = jsLoader;

        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);
            return false;
        }
        else
        {
            onInitJSEngine(true);
            return true;
        }
    }
    // 这个函数可能从 JSComponent 调用过来
    public static void FirstInit(JSEngine jse = null)
    {
        if (!initSuccess && !initFail)
        {
            if (jse == null)
            {
                var goEngine = new GameObject("_JSEngine");
                goEngine.AddComponent <JSEngine>();
                return;
            }

            if (jse != null)
            {
                DontDestroyOnLoad(jse.gameObject);
                inst = jse;

                JSFileLoader jsLoader = jse.gameObject.GetComponent <JSFileLoader>();
                if (jsLoader == null)
                {
                    jsLoader = jse.gameObject.AddComponent <JSFileLoader>();
                }
                JSMgr.InitJSEngine(jsLoader, jse.OnInitJSEngine);
            }
        }
    }
Exemplo n.º 4
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.º 5
0
    private static void InitMono2JsComConfig()
    {
        _mono2JsComDic.Clear();

        var jsonBytes = JSFileLoader.LoadJSSync(JSPathSettings.Mono2JsComConfig);
        var table     = JsonMapper.ToObject <Dictionary <string, string> >(Encoding.UTF8.GetString(jsonBytes));

        if (table != null)
        {
            _mono2JsComDic = table;
        }
        else
        {
            Debug.LogError("Read Mono2JsCom Config Error");
        }

        Debug.Log("JSCache.Mono2JsCom OK, total: " + _mono2JsComDic.Count);
    }
Exemplo n.º 6
0
    private static void InitJsTypeInfoConfig()
    {
        _jsTypeNameSet.Clear();

        var jsonBytes = JSFileLoader.LoadJSSync(JSPathSettings.JsTypeInfoConfig);
        var list      = JsonMapper.ToObject <List <string> >(Encoding.UTF8.GetString(jsonBytes));

        if (list != null)
        {
            foreach (string item in list)
            {
                _jsTypeNameSet.Add(item);
            }
        }
        else
        {
            Debug.LogError("Read JsTypeInfo Config Error");
        }

        Debug.Log("JSCache.JsTypeInfo OK, total: " + _jsTypeNameSet.Count);
    }
Exemplo n.º 7
0
    // FirstInit may be called from JSComponent!
    public static void FirstInit(JSEngine jse = null)
    {
        if (!initSuccess && !initFail)
        {
            if (jse == null)
            {
                GameObject jseGO = GameObject.Find("JSEngine");
                if (jseGO == null)
                {
                    initFail = true;
                    Debug.LogError("JSEngine gameObject not found.");
                }
                else
                {
                    jse = jseGO.GetComponent <JSEngine>();
                }
            }

            if (jse != null)
            {
                /*
                 * Don't destroy this GameObject on load
                 */
                DontDestroyOnLoad(jse.gameObject);
                inst = jse;
#if UNITY_EDITOR
                //编辑器模式下默认输出JSEngine统计数据
                inst.showStatistics = false;
#endif

                Stopwatch stopwatch = Stopwatch.StartNew();
                JSFileLoader.StartLoading();
                JSMgr.InitJSEngine(jse.OnInitJSEngine);
                JSFileLoader.EndLoading();
                stopwatch.Stop();
                Debug.Log("==============InitJSEngine: " + stopwatch.ElapsedMilliseconds + " ms");
            }
        }
    }
Exemplo n.º 8
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;
        }

        InitMonoBehaviourJSComponentName();
        InitJSEngine_ing = false;
        return ret;
    }