Example #1
0
    public override string FindFileError(string fileName)
    {
        if (Path.IsPathRooted(fileName))
        {
            return(fileName);
        }

        if (Path.GetExtension(fileName) == ".lua")
        {
            fileName = fileName.Substring(0, fileName.Length - 4);
        }

        using (CString.Block())
        {
            CString sb = CString.Alloc(512);

            for (int i = 0; i < searchPaths.Count; i++)
            {
                sb.Append("\n\tno file '").Append(searchPaths[i]).Append('\'');
            }

            sb.Append("\n\tno file './Resources/").Append(fileName).Append(".lua'")
            .Append("\n\tno file '").Append(LuaConst.luaResDir).Append('/')
            .Append(fileName).Append(".lua'");
            sb = sb.Replace("?", fileName);

            return(sb.ToString());
        }
    }
Example #2
0
        //
        public virtual string FindFileError(string fileName)
        {
            if (Path.IsPathRooted(fileName))
            {
                return(fileName);
            }

            if (fileName.EndsWith(".lua"))
            {
                fileName = fileName.Substring(0, fileName.Length - 4);
            }

            using (CString.Block())
            {
                CString sb = CString.Alloc(512);

                for (int i = 0; i < searchPaths.Count; i++)
                {
                    sb.Append("\n\tno file '").Append(searchPaths[i]).Append('\'');
                }

                sb = sb.Replace("?", fileName);

                return(sb.ToString());
            }
        }
Example #3
0
        public string FindFileError(string _fileName)
        {
            if (Path.IsPathRooted(_fileName))
            {
                return(_fileName);
            }

            if (_fileName.EndsWith(".lua"))
            {
                _fileName = _fileName.Substring(0, _fileName.Length - 4);
            }

            using (CString.Block())
            {
                CString tempStr = CString.Alloc(512);
                int     tempPos = _fileName.LastIndexOf('/');
                if (tempPos > 0)
                {
                    int tempIndex = tempPos + 1;
                    tempStr.Append("\n\tno file '").Append(_fileName, tempIndex, _fileName.Length - tempIndex).Append(".lua' in ").Append("lua_");
                    tempIndex = tempStr.Length;
                    tempStr.Append(_fileName, 0, tempPos).Replace('/', '_', tempIndex, tempPos).Append(".unity3d");
                }
                else
                {
                    tempStr.Append("\n\tno file '").Append(_fileName).Append(".lua' in ").Append("lua.unity3d");
                }

                return(tempStr.ToString());
            }
        }
Example #4
0
        byte[] ReadZipFile(string fileName)
        {
            AssetBundle zipFile = null;

            byte[] buffer      = null;
            string zipName     = null;
            string newFileName = null;

            using (CString.Block())
            {
                CString sb = CString.Alloc(256);
                sb.Append("lua.unity3d");

                newFileName = string.Format("assets/luatemp/{0}", fileName.ToLower());
                //int pos = fileName.LastIndexOf('/');

                //if (pos > 0)
                //{
                //    sb.Append("_");
                //    sb.Append(fileName, 0, pos).ToLower().Replace('/', '_');
                //    fileName = fileName.Substring(pos + 1);
                //}

                //if (!fileName.EndsWith(".lua"))
                //{
                //    fileName += ".lua";
                //}


                if (!newFileName.EndsWith(".lua"))
                {
                    newFileName += ".lua";
                }
#if UNITY_5 || UNITY_5_3_OR_NEWER
                // fileName += ".bytes";
                newFileName += ".bytes";
#endif
                zipName = sb.ToString();
                zipMap.TryGetValue(zipName, out zipFile);
            }

            if (zipFile != null)
            {
#if UNITY_4_6 || UNITY_4_7
                TextAsset luaCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
#else
                //TextAsset luaCode = zipFile.LoadAsset<TextAsset>(fileName);
                TextAsset luaCode = zipFile.LoadAsset <TextAsset>(newFileName);
#endif
                if (luaCode != null)
                {
                    buffer = luaCode.bytes;
                    Resources.UnloadAsset(luaCode);
                }
            }

            return(buffer);
        }
Example #5
0
 string TestBlock(string str)
 {
     using (CString.Block())
     {
         CString sb = CString.Alloc(256);
         sb.Append("hello");
         sb.CopyToString(str);
         return(str);
     }
 }
Example #6
0
        byte[] ReadZipFile(string fileName)
        {
            AssetBundle zipFile = null;

            byte[] buffer  = null;
            string zipName = null;

            using (CString.Block())
            {
                CString sb = CString.Alloc(256);
                sb.Append("lua");
                int pos = fileName.LastIndexOf('/');

                if (pos > 0)
                {
                    sb.Append("_");
                    sb.Append(fileName, 0, pos).ToLower().Replace('/', '_');

                    //我这里只有一个ab包可以加载 lua
                    sb       = "lua";
                    fileName = fileName.Replace(".", "/");
                    //fileName = fileName.Substring(pos + 1);
                }

                if (!fileName.EndsWith(".lua"))
                {
                    fileName += ".lua";
                }

#if UNITY_5 || UNITY_5_3_OR_NEWER
                fileName += ".bytes";
#endif
                zipName = sb.ToString();
                zipMap.TryGetValue(zipName, out zipFile);
            }

            if (zipFile != null)
            {
#if UNITY_4_6 || UNITY_4_7
                TextAsset luaCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
#else
                //需要优化 我这里  写死了 拼接ab包的路径   LuaABTemp是ab包内的lua文件的名字
                fileName = "Assets/LuaABTemp/" + fileName;

                TextAsset luaCode = zipFile.LoadAsset <TextAsset>(fileName);
#endif
                if (luaCode != null)
                {
                    buffer = luaCode.bytes;
                    Resources.UnloadAsset(luaCode);
                }
            }

            return(buffer);
        }
Example #7
0
        byte[] ReadZipFile(string fileName)
        {
            AssetBundle zipFile = null;

            byte[] buffer  = null;
            string zipName = null;

            using (CString.Block())
            {
                CString sb = CString.Alloc(256);
                sb.Append("lua");
                int pos = fileName.IndexOf('/');

                if (pos > 0)
                {
                    sb.Append("_");
                    sb.Append(fileName.Substring(0, pos).ToLower());        //shit, unity5 assetbund'name must lower
                    sb.Replace('/', '_');
                    int luaPos = fileName.LastIndexOf('/');
                    if (luaPos > 0)
                    {
                        fileName = fileName.Substring(luaPos + 1);
                    }
                }

                if (!fileName.EndsWith(".lua"))
                {
                    fileName += ".lua";
                }

#if UNITY_5 || UNITY_2017
                fileName += ".bytes";
#endif
                zipName = sb.ToString();
                zipMap.TryGetValue(zipName, out zipFile);
            }

            if (zipFile != null)
            {
#if UNITY_5 || UNITY_2017
                TextAsset luaCode = zipFile.LoadAsset <TextAsset>(fileName);
#else
                TextAsset luaCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
#endif

                if (luaCode != null)
                {
                    buffer = luaCode.bytes;
                    Resources.UnloadAsset(luaCode);
                }
            }

            return(buffer);
        }
Example #8
0
        byte[] ReadZipFile(string fileName)
        {
            AssetBundle zipFile = null;

            byte[] buffer  = null;
            string zipName = null;

            using (CString.Block())
            {
                //注意这里的操作,如果在lua脚本里面使用require引用模块
                //一定要加上路径,这个路径相对于AppConst里面的LuaTempDir目录
                //例如require "3rd/pbc/protobuf"
                CString sb = CString.Alloc(256);
                sb.Append("lua");
                int pos = fileName.LastIndexOf('/');

                if (pos > 0)
                {
                    sb.Append("_");
                    sb.Append(fileName, 0, pos).ToLower().Replace('/', '_');
                    fileName = fileName.Substring(pos + 1);
                }

                if (!fileName.EndsWith(".lua"))
                {
                    fileName += ".lua";
                }

#if UNITY_5 || UNITY_5_3_OR_NEWER
                fileName += ".bytes";
#endif
                zipName = sb.ToString();
                zipMap.TryGetValue(zipName, out zipFile);
            }

            if (zipFile != null)
            {
#if UNITY_4_6 || UNITY_4_7
                TextAsset luaCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
#else
                TextAsset luaCode = zipFile.LoadAsset <TextAsset>(fileName);
#endif
                if (luaCode != null)
                {
                    buffer = luaCode.bytes;
                    Resources.UnloadAsset(luaCode);
                }
            }

            return(buffer);
        }
        byte[] ReadZipFile(string fileName)
        {
            AssetBundle zipFile = null;

            byte[] buffer  = null;
            string zipName = null;

            using (CString.Block())
            {
                CString sb = CString.Alloc(256);
                sb.Append("lua");
                int pos = fileName.LastIndexOf('/');

                if (pos > 0)
                {
                    sb.Append("_");
                    sb.Append(fileName, 0, pos).ToLower().Replace('/', '_');
                    fileName = fileName.Substring(pos + 1);
                }

                if (!fileName.EndsWith(".lua"))
                {
                    fileName += ".lua";
                }

#if UNITY_5 || UNITY_2017
                fileName += ".bytes";
#endif
                zipName = sb.ToString();
                zipName = "lua";  //本工程所有lua文件都打包到lua.lua.assetbundle中,与LuaFramework打包方式不同;
                zipMap.TryGetValue(zipName, out zipFile);
            }

            if (zipFile != null)
            {
#if UNITY_5 || UNITY_2017
                TextAsset luaCode = zipFile.LoadAsset <TextAsset>(fileName);
#else
                TextAsset luaCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
#endif

                if (luaCode != null)
                {
                    Debug.Log(string.Format("[LuaFileUtils]=====>Loa Lua Code:{0} success.", fileName));
                    buffer = luaCode.bytes;
                    Resources.UnloadAsset(luaCode);
                }
            }

            return(buffer);
        }
Example #10
0
        byte[] ReadZipFile(string fileName)
        {
            byte[] buffer  = null;
            string zipName = null;

            AssetBundle zipFile;

            using (CString.Block())
            {
                CString sb = CString.Alloc(256);
                sb.Append("lua");
                int pos = fileName.LastIndexOf('/');

                if (pos > 0)
                {
                    sb.Append("_");
                    sb.Append(fileName, 0, pos).ToLower().Replace('/', '_');
                    fileName = fileName.Substring(pos + 1);
                }

                if (!fileName.EndsWith(".lua", System.StringComparison.Ordinal))
                {
                    fileName += ".lua";
                }

#if UNITY_5 || UNITY_5_3_OR_NEWER
                fileName += ".bytes";
#endif
                zipName = sb.ToString();
                zipMap.TryGetValue(zipName, out zipFile);
            }

            if (zipFile != null)
            {
#if UNITY_4_6 || UNITY_4_7
                TextAsset luaCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
#else
                TextAsset luaCode = zipFile.LoadAsset <TextAsset>(fileName);
#endif
                if (luaCode != null)
                {
                    buffer = luaCode.bytes;
                    Resources.UnloadAsset(luaCode);
                }
            }

            return(buffer);
        }
Example #11
0
        byte[] ReadZipFile(string fileName)
        {
            byte[] buffer  = null;
            string zipName = null;

            using (CString.Block())
            {
                CString sb = CString.Alloc(256);
                sb.Append("lua");
                int pos = fileName.LastIndexOf('/');

                if (pos > 0)
                {
                    sb.Append("_");
                    sb.Append(fileName, 0, pos).ToLower().Replace('/', '_');
                    fileName = fileName.Substring(pos + 1);
                }
                sb.Append("_");
                if (!fileName.EndsWith(".lua"))
                {
                    fileName += ".lua";
                }

#if UNITY_5 || UNITY_5_3_OR_NEWER
                //fileName += ".bytes";
#endif
                zipName = sb.ToString();
                Debug.LogWarning(string.Format("lua zip name:{0}  file:{1}", zipName, fileName.ToLower()));
            }

            foreach (var zipFile in zipMap.Values)
            {
                //string key = zipName + fileName.ToLower();
                //Debug.Log("key:" + key);
                //string luaName = luaNameMap[key];
                //Debug.Log("luaName:" + luaName);
                TextAsset luaCode = zipFile.LoadAsset <TextAsset>(luaNameMap[zipName + fileName.ToLower()]);
                if (luaCode != null)
                {
                    //Debug.Log(luaCode.text);
                    buffer = luaCode.bytes;
                    Resources.UnloadAsset(luaCode);
                    break;
                }
            }
            return(buffer);
        }
Example #12
0
        public virtual string FindFileError(string fileName)
        {
            Debug.LogError("不应该走到这里了");
            if (Path.IsPathRooted(fileName))
            {
                return(fileName);
            }

            if (fileName.EndsWith(".lua"))
            {
                fileName = fileName.Substring(0, fileName.Length - 4);
            }

            using (CString.Block())
            {
                CString sb = CString.Alloc(512);

                for (int i = 0; i < searchPaths.Count; i++)
                {
                    sb.Append("\n\tno Other file '").Append(searchPaths[i]).Append('\'');
                }

                sb = sb.Replace("?", fileName);

                if (beZip)
                {
                    int pos = fileName.LastIndexOf('/');

                    if (pos > 0)
                    {
                        int tmp = pos + 1;
                        sb.Append("\n\tno file '").Append(fileName, tmp, fileName.Length - tmp).Append(".lua' in ").Append("lua_");
                        tmp = sb.Length;
                        sb.Append(fileName, 0, pos).Replace('/', '_', tmp, pos).Append(".unity3d");
                    }
                    else
                    {
                        sb.Append("\n\tno file '").Append(fileName).Append(".lua' in ").Append("lua.unity3d");
                    }
                }

                return(sb.ToString());
            }
        }
Example #13
0
        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public string FindFileError(string fileName)
        {
            if (Path.IsPathRooted(fileName))
            {
                return(fileName);
            }

            if (fileName.EndsWith(".lua"))
            {
                fileName = fileName.Substring(0, fileName.Length - 4);
            }

            using (CString.Block())
            {
                CString sb = CString.Alloc(512);

                for (int i = 0; i < luaSearchPath.Count; i++)
                {
                    sb.Append("\n\tMoon no file '").Append(luaSearchPath[i]).Append('\'');
                }

                sb = sb.Replace("?", fileName);

                if (AppConst.LuaBundleMode)
                {
                    int pos = fileName.LastIndexOf('/');

                    if (pos > 0)
                    {
                        int tmp = pos + 1;
                        sb.Append("\n\tMoon no file '").Append(fileName, tmp, fileName.Length - tmp).Append(".lua' in ").Append("lua_");
                        tmp = sb.Length;
                        sb.Append(fileName, 0, pos).Replace('/', '_', tmp, pos).Append(".unity3d");
                    }
                    else
                    {
                        sb.Append("\n\tMoon no file '").Append(fileName).Append(" in ").Append("lua.unity3d");
                    }
                }

                return(sb.ToString());
            }
        }
Example #14
0
    private byte[] ReadBundleFile(string fileName)
    {
        AssetBundle zipFile = null;

        byte[] buffer  = null;
        string zipName = "lua";

        using (CString.Block())
        {
            fileName = fileName.Replace('/', '_');
            fileName = fileName.Replace('\\', '_');
            if (!fileName.EndsWith(".lua"))
            {
                fileName += ".lua";
            }

#if UNITY_5 || UNITY_2017
            fileName += ".bytes";
#endif
            zipMap.TryGetValue(zipName, out zipFile);
        }

        if (zipFile != null)
        {
#if UNITY_5 || UNITY_2017 || UNITY_2018
            TextAsset luaCode = zipFile.LoadAsset <TextAsset>(fileName);
#else
            TextAsset luaCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
#endif

            if (luaCode != null)
            {
                buffer = luaCode.bytes;
                Resources.UnloadAsset(luaCode);
            }
        }

        return(buffer);
    }
Example #15
0
        public static string GetArrayRank(Type t)
        {
            int count = t.GetArrayRank();

            if (count == 1)
            {
                return("[]");
            }

            using (CString.Block())
            {
                CString sb = CString.Alloc(64);
                sb.Append('[');

                for (int i = 1; i < count; i++)
                {
                    sb.Append(',');
                }

                sb.Append(']');
                return(sb.ToString());
            }
        }
Example #16
0
    public string where ()
    {
        int n = LuaDLL.lua_gettop(L);

        using (CString.Block())
        {
            CString sb       = CString.Alloc(256);
            int     line     = LuaDLL.tolua_where(L, 1);
            string  filename = LuaDLL.lua_tostring(L, -1);
            LuaDLL.lua_settop(L, n);
            int offset = filename[0] == '@' ? 1 : 0;

            if (!filename.Contains("."))
            {
                sb.Append('[').Append(filename, offset, filename.Length - offset).Append(".lua:").Append(line).Append("]:");
            }
            else
            {
                sb.Append('[').Append(filename, offset, filename.Length - offset).Append(':').Append(line).Append("]:");
            }
            return(sb.ToString());
        }
    }
Example #17
0
        byte[] ReadZipFile(string fileName)
        {
            AssetBundle zipFile = null;

            byte[] buffer  = null;
            string zipName = null;
            string temp    = fileName;

            using (CString.Block())
            {
                CString sb  = CString.Alloc(256);
                string  dir = "lua";
                if (dirIndex == 1)
                {
                    dir = "game_src";
                }
                sb.Append(dir);
                int pos = fileName.LastIndexOf('/');

                if (pos > 0)
                {
                    sb.Append("_");
                    sb.Append(fileName, 0, pos).ToLower().Replace('/', '_');
                    fileName = fileName.Substring(pos + 1);
                }

                if (!fileName.EndsWith(".lua"))
                {
                    fileName += ".lua";
                }

#if UNITY_5
                fileName += ".bytes";
#endif
                zipName = sb.ToString();
                zipMap.TryGetValue(zipName, out zipFile);
            }

            if (zipFile != null)
            {
#if UNITY_5
                TextAsset luaCode = zipFile.LoadAsset <TextAsset>(fileName);
#else
                TextAsset luaCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
#endif
                if (luaCode != null)
                {
                    buffer = luaCode.bytes;
                    Resources.UnloadAsset(luaCode);
                }
                else
                {
                    if (dirIndex != 1)
                    {
                        dirIndex = 1;
                        buffer   = ReadZipFile(temp);
                    }
                }
            }
            else
            {
                if (dirIndex != 1)
                {
                    dirIndex = 1;
                    buffer   = ReadZipFile(temp);
                }
            }
            dirIndex = 0;
            return(buffer);
        }
Example #18
0
        byte[] ReadZipFile(string fileName)
        {
            AssetBundle zipFile = null;

            byte[] buffer  = null;
            string zipName = null;

            using (CString.Block())
            {
                //CString sb = CString.Alloc(256);
                //sb.Append("lua");
                //int pos = fileName.LastIndexOf('/');

                //if (pos > 0)
                //{
                //    sb.Append("_");
                //    sb.Append(fileName, 0, pos).ToLower().Replace('/', '_');
                //    fileName = fileName.Substring(pos + 1);
                //}

                //if (!fileName.EndsWith(".lua"))
                //{
                //    fileName += ".lua";
                //}
                CString sb = CString.Alloc(256);
                sb.Append("lua_");
                if (fileName.IndexOf("message/") != -1)
                {
                    sb.Append("message");
                }
                else if (fileName.IndexOf("Template/") != -1)
                {
                    sb.Append("template");
                }
                else
                {
                    sb.Append("script");
                }
                int pos = fileName.LastIndexOf('/');
                if (pos > 0)
                {
                    fileName = fileName.Substring(pos + 1);
                }
                if (!fileName.EndsWith(".lua"))
                {
                    fileName += ".lua";
                }

#if UNITY_5 || UNITY_2017
                fileName += ".bytes";
#endif
                zipName = sb.ToString();
                zipMap.TryGetValue(zipName, out zipFile);
            }

            if (zipFile != null)
            {
#if UNITY_5 || UNITY_2017
                TextAsset luaCode = zipFile.LoadAsset <TextAsset>(fileName);
#else
                TextAsset luaCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
#endif

                if (luaCode != null)
                {
                    buffer = luaCode.bytes;
                    Resources.UnloadAsset(luaCode);
                }
            }

            return(buffer);
        }
Example #19
0
        byte[] ReadZipFile(string fileName)
        {
            AssetBundle zipFile = null;

            byte[] buffer  = null;
            string zipName = null;

            using (CString.Block())
            {
                CString sb = CString.Alloc(256);
                sb.Append("lua");
                int startPos = fileName.IndexOf('/');
                int pos      = fileName.LastIndexOf('/');

                if (startPos > 0) //封装出包名
                {
                    sb.Append("_");
                    sb.Append(LuaMgr.Inst.cpuTypeStr);
                    sb.Append(fileName, 0, startPos).ToLower().Replace('/', '_');
                }
                if (pos > 0) //文件名
                {
                    fileName = fileName.Substring(pos + 1);
                }

                if (!fileName.EndsWith(".lua"))
                {
                    fileName += ".lua";
                }

#if UNITY_5 || UNITY_5_3_OR_NEWER
                fileName += ".bytes";
#endif
                zipName = sb.ToString();
                zipMap.TryGetValue(zipName, out zipFile);

                //[0] 添加补丁 如果常规下找不到,就在默认的ab包中找lua文件
                if (zipFile == null)
                {
                    zipFile = zipMap["lua_" + LuaMgr.Inst.cpuTypeStr + "tolua"];
                }
                //[0]
            }

            if (zipFile != null)
            {
#if UNITY_4_6 || UNITY_4_7
                TextAsset luaCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
#else
                TextAsset luaCode = zipFile.LoadAsset <TextAsset>(fileName);
#endif

                if (luaCode != null)
                {
                    buffer = luaCode.bytes;
                    Resources.UnloadAsset(luaCode);
                }
            }

            return(buffer);
        }
Example #20
0
        byte[] ReadZipFile(string fileName)
        {
            //AssetBundle zipFile = null;
            byte[] buffer = null;
            //string zipName = null;

            //            using (CString.Block())
            //            {
            //                CString sb = CString.Alloc(256);
            //                sb.Append("lua");
            //                int pos = fileName.LastIndexOf('/');

            //                if (pos > 0)
            //                {
            //                    sb.Append("_");
            //                    sb.Append(fileName, 0, pos).ToLower().Replace('/', '_');
            //                    fileName = fileName.Substring(pos + 1);
            //                }

            //                if (!fileName.EndsWith(".lua"))
            //                {
            //                    fileName += ".lua";
            //                }

            //#if UNITY_5 || UNITY_5_3_OR_NEWER
            //                fileName += ".bytes";
            //#endif
            //                zipName = sb.ToString();
            //                zipMap.TryGetValue(zipName, out zipFile);
            //            }

            //            if (zipFile != null)
            //            {
            //#if UNITY_4_6 || UNITY_4_7
            //                TextAsset luaCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
            //#else
            //                TextAsset luaCode = zipFile.LoadAsset<TextAsset>(fileName);
            //#endif
            //                if (luaCode != null)
            //                {
            //                    buffer = luaCode.bytes;
            //                    Resources.UnloadAsset(luaCode);
            //                }
            //            }

            using (CString.Block())
            {
                fileName = fileName.Replace("/", ".");
                if (!fileName.EndsWith(".lua"))
                {
                    fileName += ".lua";
                }

#if UNITY_5 || UNITY_5_3_OR_NEWER
                fileName += ".bytes";
#endif
            }

            foreach (var bundleName in AppConst.LuaBundles)
            {
                AssetBundle bundle = null;
                if (zipMap.TryGetValue(bundleName, out bundle))
                {
#if UNITY_4_6 || UNITY_4_7
                    TextAsset luaCode = bundle.Load(fileName, typeof(TextAsset)) as TextAsset;
#else
                    TextAsset luaCode = bundle.LoadAsset <TextAsset>(fileName);
#endif
                    if (null == luaCode)
                    {
                        continue;
                    }
                    if (luaCode != null)
                    {
                        buffer = luaCode.bytes;
                        Resources.UnloadAsset(luaCode);
                        break;
                    }
                }
            }
            if (null == buffer)
            {
                Debug.LogError("Load lua file from bundle error! luafile name is:" + fileName);
            }
            return(buffer);
        }
Example #21
0
    //加载lua文件
    public static byte[] GetLuaScript(string fileName)
    {
#if UNITY_EDITOR && !ASSETBUNDLE_ENABLE
        string path = LuaFileUtils.Instance.FindFile(fileName);
        byte[] str  = null;

        if (!string.IsNullOrEmpty(path) && File.Exists(path))
        {
            str = File.ReadAllBytes(path);
        }
        if (str != null)
        {
            return(str);
        }
#endif
        AssetBundle assetBundle = null;
        byte[]      buffer      = null;

        using (CString.Block())
        {
            string strBundleName = "";
            if (fileName.IndexOf("message/") != -1)
            {
                strBundleName = "lua/lua_message.unity3d";
            }
            else if (fileName.IndexOf("Template/") != -1)
            {
                strBundleName = "lua/lua_template.unity3d";
            }
            else
            {
                strBundleName = "lua/lua_script.unity3d";
            }
            int pos = fileName.LastIndexOf('/');
            if (pos > 0)
            {
                fileName = fileName.Substring(pos + 1);
            }
            if (!fileName.EndsWith(".lua"))
            {
                fileName += ".lua";
            }

#if UNITY_5 || UNITY_2017
            fileName += ".bytes";
#endif
            AssetBundleData assetBundleData = null;
            if (m_dicAssetBundle.TryGetValue(strBundleName, out assetBundleData))
            {
                string keyName = "assets/" + fileName.ToLower();
                if (assetBundleData != null)
                {
                    if (assetBundleData.assetBundle == null)
                    {
                        assetBundleData.assetBundle = AssetBundle.LoadFromFile(assetBundleData.strAssetPath);
                    }
                    assetBundle = assetBundleData.assetBundle;
                }
            }
        }

        if (assetBundle != null)
        {
#if UNITY_5 || UNITY_2017
            TextAsset luaCode = assetBundle.LoadAsset <TextAsset>(fileName);
#else
            TextAsset luaCode = assetBundle.Load(fileName, typeof(TextAsset)) as TextAsset;
#endif
            if (luaCode != null)
            {
                buffer = luaCode.bytes;
                Resources.UnloadAsset(luaCode);
            }
        }
        return(buffer);
    }
Example #22
0
        /// <summary>
        /// TODO:打印的简易版本,还需要lua的文件名位置等信息
        /// </summary>
        /// <param name="L"></param>
        public static int Print(IntPtr L)
        {
            try
            {
                int n = LuaDLL.lua_gettop(L);
                // CString可以喝String互转
                using (CString.Block())
                {
                    CString sb = CString.Alloc(256);
#if UNITY_EDITOR
                    int    line     = LuaDLL.tolua_where(L, 1);
                    string filename = LuaDLL.lua_tostring(L, -1);
                    LuaDLL.lua_settop(L, n);
                    int offset = filename[0] == '@' ? 1 : 0;

                    if (!filename.Contains("."))
                    {
                        sb.Append('[').Append(filename, offset, filename.Length - offset).Append(".lua:").Append(line).Append("]:");
                    }
                    else
                    {
                        sb.Append('[').Append(filename, offset, filename.Length - offset).Append(':').Append(line).Append("]:");
                    }
#endif

                    for (int i = 1; i <= n; i++)
                    {
                        if (i > 1)
                        {
                            sb.Append("    ");
                        }

                        if (LuaDLL.lua_isstring(L, i) == 1)
                        {
                            sb.Append(LuaDLL.lua_tostring(L, i));
                        }
                        else if (LuaDLL.lua_isnil(L, i))
                        {
                            sb.Append("nil");
                        }
                        else if (LuaDLL.lua_isboolean(L, i))
                        {
                            sb.Append(LuaDLL.lua_toboolean(L, i) ? "true" : "false");
                        }
                        else
                        {
                            IntPtr p = LuaDLL.lua_topointer(L, i);

                            if (p == IntPtr.Zero)
                            {
                                sb.Append("nil");
                            }
                            else
                            {
                                sb.Append(LuaDLL.luaL_typename(L, i)).Append(":0x").Append(p.ToString("X"));
                            }
                        }
                    }

                    Debugger.Log(sb.ToString());            //203行与_line一致
                }
                return(0);
            }
            catch (Exception e)
            {
                return(LuaDLL.toluaL_exception(L, e));
            }
        }
Example #23
0
        private IEnumerator AsynReadFile(string fileName, bool isCacheBundle)
        {
            AssetBundle zipFile = null;
            string      content = string.Empty;

            byte[] buffer  = null;
            string zipName = null;

            using (CString.Block())
            {
                CString sb = CString.Alloc(256);
                sb.Append("config");
                int pos = fileName.LastIndexOf('/');

                if (pos > 0)
                {
                    sb.Append("_");
                    sb.Append(fileName, 0, pos).ToLower().Replace('/', '_');
                    fileName = fileName.Substring(pos + 1);
                }
                zipName = sb.ToString();

                configMap.TryGetValue(zipName, out zipFile);
            }
            var isHaveCache = false;

            if (zipFile == null)
            {
                isHaveCache = false;
                AssetBundleCreateRequest assetBundleRequest;
                assetBundleRequest = AssetBundle.LoadFromFileAsync(ConfigConst.configResDir + "/" + zipName + Platform.AssetBundleExt);
                yield return(assetBundleRequest);

                if (assetBundleRequest.isDone)
                {
                    zipFile = assetBundleRequest.assetBundle;
                    if (isCacheBundle)
                    {
                        configMap[zipName] = zipFile;
                    }
                }
            }
            else
            {
                isHaveCache = true;
            }

            if (zipFile != null)
            {
                TextAsset configCode = null;
#if UNITY_5 || UNITY_2017 || UNITY_2018
                AssetBundleRequest assetBundleRequest;
                assetBundleRequest = zipFile.LoadAssetAsync <TextAsset>(fileName);
                yield return(assetBundleRequest);

                if (assetBundleRequest.isDone)
                {
                    configCode = (TextAsset)assetBundleRequest.asset;
                }
#else
                configCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
#endif

                if (configCode != null)
                {
                    buffer = configCode.bytes;
                    Resources.UnloadAsset(configCode);
                    byte[] decrypt = ConfigEncrypt.Decrypt(buffer);
                    content = System.Text.Encoding.UTF8.GetString(decrypt);
                }
                if (!isCacheBundle && !isHaveCache)
                {
                    zipFile.Unload(true);
                }
                //回调
                if (CallBacks.ContainsKey(fileName))
                {
                    Action <string> action = CallBacks[fileName];
                    if (action != null)
                    {
                        Delegate[] actions = action.GetInvocationList();
                        foreach (var dele in actions)
                        {
                            var act = (Action <string>)dele;
                            try
                            {
                                act(content);
                            }
                            catch (Exception e)
                            {
                                Debuger.LogError("异步加载config文件出错 " + fileName + " " + e.Message);
                            }
                        }
                    }
                    CallBacks.Remove(fileName);
                }
                yield return(null);
            }
        }
Example #24
0
        /// <summary>
        /// 这里重写了 </summary>
        private byte[] ReadZipFile(string fileName)
        {
            AssetBundle zipFile;

            byte[] buffer = null;
            using (CString.Block())
            {
                CString sb = CString.Alloc(256);
                sb.Append("lua");
                int pos = fileName.LastIndexOf('/');

                if (pos > 0)
                {
                    sb.Append("_");
                    sb.Append(fileName, 0, pos).ToLower().Replace('/', '_');
                    fileName = fileName.Substring(pos + 1);
                }

                if (!fileName.EndsWith(".lua"))
                {
                    fileName += ".lua";
                }

#if UNITY_5 || UNITY_5_3_OR_NEWER
                fileName += ".bytes";
#endif
                var zipName = sb.ToString();
                //重写部分,如果没查找到,则去文件夹查找
                zipMap.TryGetValue(zipName, out zipFile);
                if (!zipMap.TryGetValue(zipName, out zipFile))
                {
                    string path;
                    if (AllLuaBundlesPathColl.TryGetValue(zipName, out path))
                    {
                        var         bytes  = File.ReadAllBytes(path);
                        AssetBundle bundle = AssetBundle.LoadFromMemory(bytes);
                        if (bundle != null)
                        {
                            base.AddSearchBundle(zipName, bundle);
                            zipFile = bundle;
                        }
                    }
                }
            }

            if (zipFile != null)
            {
#if UNITY_4_6 || UNITY_4_7
                TextAsset luaCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
#else
                TextAsset luaCode = zipFile.LoadAsset <TextAsset>(fileName);
#endif
                if (luaCode != null)
                {
                    buffer = luaCode.bytes;
                    Resources.UnloadAsset(luaCode);
                }
            }

            return(buffer);
        }
Example #25
0
        string ReadZipFile(string fileName, bool isCacheBundle)
        {
            AssetBundle zipFile = null;
            string      content = string.Empty;

            byte[] buffer  = null;
            string zipName = null;

            using (CString.Block())
            {
                CString sb = CString.Alloc(256);
                sb.Append("config");
                int pos = fileName.LastIndexOf('/');

                if (pos > 0)
                {
                    sb.Append("_");
                    sb.Append(fileName, 0, pos).ToLower().Replace('/', '_');
                    fileName = fileName.Substring(pos + 1);
                }
                zipName = sb.ToString();

                configMap.TryGetValue(zipName, out zipFile);
            }
            var isHaveCache = false;

            if (zipFile == null)
            {
                isHaveCache = false;
                zipFile     = AssetBundle.LoadFromFile(ConfigConst.configResDir + "/" + zipName + Platform.AssetBundleExt);
                if (isCacheBundle)
                {
                    configMap[zipName] = zipFile;
                }
            }
            else
            {
                isHaveCache = true;
            }

            if (zipFile != null)
            {
#if UNITY_5 || UNITY_2017 || UNITY_2018
                TextAsset configCode = zipFile.LoadAsset <TextAsset>(fileName);
#else
                TextAsset configCode = zipFile.Load(fileName, typeof(TextAsset)) as TextAsset;
#endif

                if (configCode != null)
                {
                    buffer = configCode.bytes;
                    Resources.UnloadAsset(configCode);
                    byte[] decrypt = ConfigEncrypt.Decrypt(buffer);
                    content = System.Text.Encoding.UTF8.GetString(decrypt);
                }
                if (!isCacheBundle && !isHaveCache)
                {
                    zipFile.Unload(true);
                }
            }

            return(content);
        }