using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SFramework
{
    public static class SLuaEnv
    {
        private static XLua.LuaEnv _luaState = null;

        public static XLua.LuaEnv Instance
        {
            get
            {
                if (null == _luaState)
                {
                    _luaState = new XLua.LuaEnv();

                    _luaState.AddLoader(SLuaEnv.CustomLoader);
                }

                return _luaState;
            }
            private set
            {
                Debug.Assert(false, "Can't set the Instance");
            }
        }

        private static byte[] CustomLoader(ref string luaScriptName)
        {
            //Debug.Log("CustomLoader Work Once. ---> " + luaScriptName);
            string fullLuaScriptPath = "LuaScripts/" + luaScriptName + ".txt";
            //Debug.Log("FullLuaScriptPath -> " + fullLuaScriptPath);

            TextAsset luaTextAssets = SFramework.SResourceManager.LoadResource(fullLuaScriptPath) as TextAsset;
            //Debug.Log("luaTextAssets -> " + luaTextAssets.text);
            Debug.Log(string.Format(
                "SFramework.SLuaEnv.CustomLoader -> luaScriptName: {0} \nluaTextAssets: {2}",
                luaScriptName, fullLuaScriptPath, luaTextAssets.text
                ));

            return luaTextAssets.bytes;
        }

    }
}
