Example #1
0
    /// <summary>
    /// Inits the S lua.
    /// </summary>
    /// <returns><c>true</c>, if S lua was inited, <c>false</c> otherwise.</returns>
    /// <param name="progress">Progress.</param>
    public static bool              InitSLua(System.Action <int> progress)
    {
        XBytecodeFilePicker.InitPicker();

        luaServer = new LuaSvr();

        platformPath = GetPlatformBytecodeFloder();
        if (!string.IsNullOrEmpty(platformPath))
        {
            LuaSvr.mainState.loaderDelegate = LoadScript;
        }

        Debug.LogFormat("BytecodeFloder : {0}", platformPath);

        luaServer.init(progress, () => {
                        #if !RELEASE
            LuaSvr.mainState["_DEBUG"] = true;
                        #endif

                        #if UNITY_EDITOR
            LuaSvr.mainState["_EDITOR"] = true;
                        #endif

                        #if UNITY_ANDROID
            LuaSvr.mainState["_ANDROID"] = true;
                        #endif

                        #if UNITY_IPHONE
            luaServer.luaState["_IPHONE"] = true;
                        #endif

                        #if _LANGUAGE_CN
            LuaSvr.mainState["_LANGUAGE_CN"] = true;
                        #endif

                        #if _LANGUAGE_EN
            LuaSvr.mainState["_LANGUAGE_EN"] = true;
                        #endif

                        #if _LOCAL_SERVER
            LuaSvr.mainState["_LOCAL_SERVER"] = true;
                        #endif

            var success = luaServer.start("main");
            if (success == null || (bool)success != true)
            {
                Debug.LogError("Lua main intialize failed.");
            }
        }, LuaSvrFlag.LSF_BASIC | LuaSvrFlag.LSF_EXTLIB
                        #if !RELEASE && LUA_DEBUG
                       | LuaSvrFlag.LSF_DEBUG
                        #endif
                       );

        return(true);
    }
Example #2
0
    /// <summary>
    /// Loads the script.
    /// </summary>
    /// <returns>The script.</returns>
    /// <param name="fn">Fn.</param>
    public static byte[]    LoadScript(string fn, ref string str)
    {
        fn = fn.Replace(".", "/");

#if UNITY_STANDALONE || UNITY_EDITOR
        XBufferAsset asset = ScriptableObject.CreateInstance <XBufferAsset>();

        // find config source code
        string filePath = System.IO.Path.Combine(Application.dataPath, string.Format("Config/{0}.lua", fn));
        if (!System.IO.File.Exists(filePath))
        {
            // find logic script
            filePath = System.IO.Path.Combine(Application.dataPath, string.Format("Code/Script/{0}.lua", fn));
            if (!System.IO.File.Exists(filePath))
            {
                string bytefn = fn.Replace("/", "@").ToLower();
                // find lua byte code
                filePath = System.IO.Path.Combine(Application.dataPath,
                                                  string.Format("Bytecode/{0}/{1}.bytes", platformPath, bytefn));
            }
        }

#if !RELEASE
        GLog.Log(string.Format("{0}", filePath));
#endif

        if (System.IO.File.Exists(filePath))
        {
            System.IO.FileStream fs = System.IO.File.OpenRead(filePath);
            asset.init((int)fs.Length);
            fs.Read(asset.bytes, 0, (int)fs.Length);
            fs.Close();

            return(asset.bytes);
        }
        else
        {
            Debug.LogError("can't find file : " + filePath);
        }
#else
        string bytefn = fn.Replace("/", "@").ToLower();
        string hashfn = XUtility.Md5Sum(string.Format("{0}/{1}",
                                                      platformPath, bytefn));

        XBufferAsset asset = XBytecodeFilePicker.LoadBytecodeAsset(string.Format("bytecode/{0}", hashfn));
        if (asset != null)
        {
            return(asset.bytes);
        }
        else
        {
            Debug.LogError("Can't load bytecode " + platformPath + " " + bytefn);
        }
#endif
        return(null);
    }