Пример #1
0
        public LuaManager(IVFS vfsMgr)
        {
            mVFSMgr = vfsMgr;
            //先获取配置,检查是否启用Lua
            mLuaConfig         = Config.GetTinaXConfig <LuaConfig>(Conf.ConfigPath.lua);
            mTinaXFileRootPath = Setup.Framework_Path + "/Lua/";
            if (EnableLua)
            {
                //获取lua文件的后缀名
                switch (mLuaConfig.FileExten)
                {
                default:
                    mFileExt = ".txt";
                    break;

                case LuaFileExten.lua:
                    mFileExt = ".lua";
                    break;

                case LuaFileExten.lua_txt:
                    mFileExt = ".lua.txt";
                    break;

                case LuaFileExten.txt:
                    mFileExt = ".txt";
                    break;
                }
                //初始化lua运行环境
                m_LuaVM = new LuaEnv();
                //lua 文件加载方法定义
                m_loader = LoadLuaCodeFile;
                m_LuaVM.AddLoader(m_loader);

                TimeMachine.I.AddUpdate(XUpdate);

                //添加特殊load路径处理规则
                mLoadFileHander.Add(new LoadFileHandler()
                {
                    IsTrigger = (path) =>
                    {
                        return(path.StartsWith(mTinaXFileRootPath));
                    },
                    Handler = (ref string fileName, out byte[] reData, out bool handled_filename) =>
                    {
                        fileName        += mTinaXFileExt;
                        reData           = default;
                        handled_filename = true;
                        return(false); //如果不想中断的话,一定要为false
                    }
                });
Пример #2
0
        public async Task <bool> Start()
        {
            if (mInited)
            {
                return(true);
            }
            mConfig = XConfig.GetConfig <LuaConfig>(LuaConst.ConfigPath_Resources);
            if (mConfig == null)
            {
                mStartException = new XException("[TinaX.ILRuntime] Connot found config file.");;
                return(false);
            }
            if (!mConfig.EnableLua)
            {
                return(true);
            }

            mInternal_Lua_Folder_Load_Path = mConfig.FrameworkInternalLuaFolderLoadPath;
            if (!mInternal_Lua_Folder_Load_Path.IsNullOrEmpty())
            {
                if (mInternal_Lua_Folder_Load_Path.EndsWith("/"))
                {
                    mInternal_Lua_Folder_Load_Path = mInternal_Lua_Folder_Load_Path.Substring(0, mInternal_Lua_Folder_Load_Path.Length - 1);
                }
                mInternal_Lua_Folder_Load_Path_withSlash = mInternal_Lua_Folder_Load_Path + "/";
            }
            mLuaExtension = mConfig.LuaFileExtensionName;
            if (!mLuaExtension.StartsWith("."))
            {
                mLuaExtension = "." + mLuaExtension;
            }

            if (!XCore.MainInstance.TryGetBuiltinService(out Assets))
            {
                mStartException = new XException("[TinaX.ILRuntime]" + (IsChinese? "没有任何服务实现了Framework中的内置的资产加载接口": "No service implements the built-in asset loading interface in Framework"));
                return(false);
            }

            mLoader = LoadLuaFiles;
            mLuaVM.AddLoader(mLoader);

            try
            {
                await InitInternalEntry();
            }
            catch (XException e)
            {
                mStartException = e;
                return(false);
            }

            //准备好入口文件
            if (!mConfig.EntryLuaFileLoadPath.IsNullOrEmpty())
            {
                try
                {
                    TextAsset entry_ta = await Assets.LoadAsync <TextAsset>(mConfig.EntryLuaFileLoadPath);

                    mEntryFunc = mLuaVM.LoadString <LuaFunction>(entry_ta.bytes, mConfig.EntryLuaFileLoadPath);
                    Assets.Release(entry_ta);
                }
                catch (XException e)
                {
                    mStartException = e;
                    return(false);
                }
            }

            if (mUpdateTicket != null)
            {
                mUpdateTicket.Unregister();
            }
            mUpdateTicket = TimeMachine.RegisterUpdate(OnUpdate);

            await Task.Yield();

            mInited = true;
            return(true);
        }