//加载所有模块全部lua文件 private void UF_LoadLuaModulesFiles(List <AssetDataBases.AssetFileInfo> list) { if (m_Luastate == null || list == null) { return; } int top = m_Luastate.LuaGetTop(); try { //packages 作为排序权重 list.Sort((a, b) => { return(UF_GetPackageFileWeight(a.absName) < UF_GetPackageFileWeight(b.absName) ? -1 : 1); }); m_Luastate.LuaGetGlobal(MODULE_NAME); //开始加载文件 foreach (var v in list) { if (string.IsNullOrEmpty(v.path)) { continue; } //只加载Lua包中文件 if (!(v.absName.IndexOf("Runtimes/Lua/", StringComparison.Ordinal) > -1)) { continue; } int s_top = m_Luastate.LuaGetTop(); string pkgName = GHelper.UF_GetDirectoryName(v.absName); m_Luastate.LuaGetField(-1, pkgName); if (m_Luastate.LuaIsNil(-1)) { Debugger.UF_Error(string.Format("Can not find Module[{0}],Load file[{1}] invalid!", pkgName, v.name)); m_Luastate.LuaSetTop(s_top); continue; //throw new LuaException(string.Format("Can not find Module[{0}],Load file[{1}] invalid!", pkgName, v.name), LuaException.GetLastError()); } else { int curTop = m_Luastate.LuaGetTop(); string r_name = Path.GetFileNameWithoutExtension(v.name); if (!UF_DoFile(m_Luastate.LuaGetL(), v.path, r_name)) { Debugger.UF_Error(string.Format("Load Lua Error:{0}", m_Luastate.LuaToString(-1))); m_Luastate.LuaSetTop(s_top); continue; //throw new LuaException(m_Luastate.LuaToString(-1), LuaException.GetLastError()); } if (m_Luastate.LuaGetTop() <= curTop) { Debugger.UF_Error(string.Format("Lua File[ {0} ] Not Return Any Value,Load Invaild", v.name)); m_Luastate.LuaSetTop(s_top); continue; //throw new LuaException(string.Format("Lua File[ {0} ] Not Return Any Value,Load Invaild", v.name), LuaException.GetLastError()); } //添加到对应到包模块中 m_Luastate.LuaSetField(-2, r_name); } m_Luastate.LuaSetTop(s_top); } } catch (Exception e) { //模块无法成功加载 Debugger.UF_Exception(e); } m_Luastate.LuaSetTop(top); }
//所有模块的包先被加载 //所有Lua中的文件夹定义为一个包 private void UF_LoadLuaModulesPackages(List <AssetDataBases.AssetFileInfo> list) { if (m_Luastate == null || list == null) { return; } HashSet <string> hashmap = new HashSet <string>(); //遍历全部的资源信息文件,获取其相对路径 foreach (var v in list) { string dirName = GHelper.UF_GetDirectoryName(v.absName); if (!hashmap.Contains(dirName)) { hashmap.Add(dirName); } } int top = m_Luastate.LuaGetTop(); try { //设置module 记录包路径模块 m_Luastate.LuaCreateTable(); m_Luastate.LuaPushValue(-1); m_Luastate.LuaSetGlobal(MODULE_NAME); //按层级创建talbe pacakge foreach (var v_path in hashmap) { int s_top = m_Luastate.LuaGetTop(); foreach (var p_name in GHelper.UF_SplitString(v_path, '/')) { if (string.IsNullOrEmpty(p_name)) { continue; } if (p_name == "Runtimes") { //Runtimes作为全局根结点 m_Luastate.LuaGetGlobal("_G"); continue; } m_Luastate.LuaGetField(-1, p_name); if (m_Luastate.LuaIsNil(-1)) { m_Luastate.LuaRemove(-1); m_Luastate.LuaCreateTable(); m_Luastate.LuaPushValue(-1); m_Luastate.LuaSetField(-3, p_name); } } //设置到Module中记录 m_Luastate.LuaGetGlobal(MODULE_NAME); m_Luastate.LuaPushValue(-2); m_Luastate.LuaSetField(-2, v_path); m_Luastate.LuaSetTop(s_top); } } catch (Exception e) { Debugger.UF_Exception(e); } m_Luastate.LuaSetTop(top); }