Esempio n. 1
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());
            }
        }
Esempio n. 2
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());
            }
        }
Esempio n. 3
0
    //public static CString ToCString(this System.UInt32 number, string format, IFormatProvider provider)
    //{
    //    CString str = CString.Alloc(32);
    //    str.NumberToString(format, number, provider);
    //    return str;
    //}

    public static CString ToCString(this System.Int64 number)
    {
        CString str = CString.Alloc(32);

        str.Append(number);
        return(str);
    }
Esempio n. 4
0
    //public static CString ToCString(this System.Int16 number, string format, IFormatProvider provider)
    //{
    //    CString str = CString.Alloc(16);
    //    str.NumberToString(format, (int)number, provider);
    //    return str;
    //}

    public static CString ToCString(this System.UInt16 number)
    {
        CString str = CString.Alloc(8);

        str.Append(number);
        return(str);
    }
Esempio n. 5
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());
        }
    }
Esempio n. 6
0
    public static CString ToCString(this System.UInt16 number, string format)
    {
        CString str = CString.Alloc(16);

        str.AppendFormat(format, number);
        return(str);
    }
Esempio n. 7
0
    //public static CString ToCString(this System.Single number, string format, IFormatProvider provider)
    //{
    //    CString str = CString.Alloc(64);
    //    str.NumberToString(format, number, provider);
    //    return str;
    //}

    public static CString ToCString(this System.Double number)
    {
        CString str = CString.Alloc(64);

        str.Append(number);
        return(str);
    }
Esempio n. 8
0
    public static unsafe CString SubStringEx(this string str, int startIndex)
    {
        if (startIndex < 0)
        {
            throw new ArgumentOutOfRangeException("startIndex", "Cannot be negative.");
        }

        if (startIndex > str.Length)
        {
            throw new ArgumentOutOfRangeException("startIndex", "Cannot exceed length of string.");
        }

        int len = str.Length - startIndex;

        if (len == 0)
        {
            return(CString.Alloc(0));
        }

        if (startIndex == 0 && len == str.Length)
        {
            return(str);
        }

        CString cstr = CString.Alloc(len);

        cstr.Append(str, startIndex, len);
        return(cstr);
    }
Esempio n. 9
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);
        }
Esempio n. 10
0
 string TestBlock(string str)
 {
     using (CString.Block())
     {
         CString sb = CString.Alloc(256);
         sb.Append("hello");
         sb.CopyToString(str);
         return(str);
     }
 }
Esempio n. 11
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);
        }
Esempio n. 12
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);
        }
Esempio n. 13
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);
        }
Esempio n. 14
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_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);
        }
Esempio n. 15
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);
        }
Esempio n. 16
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);
        }
Esempio n. 17
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());
            }
        }
Esempio n. 18
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());
            }
        }
Esempio n. 19
0
        /// <summary>
        /// 加载lua的ab包
        /// </summary>
        /// <param name="abName"></param>
        public void AddLuaBundle(string abName)
        {
            CString sb = CString.Alloc(256);

            sb = "lua";
            sb = sb.Append(AppConst.ExtName);
            sb = sb.ToLower();

            string path = GetResPath(sb.ToString());

            if (!File.Exists(path))
            {
                Debug.LogError("找不到luaAb");
                return;
            }
            Debug.Log("lua ab包加载完成");
            byte[] stream = null;

            stream = File.ReadAllBytes(path);
            // LoadFromFile 的话是加载一个路径
            luaBundle = AssetBundle.LoadFromMemory(stream);
        }
Esempio n. 20
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());
            }
        }
Esempio n. 21
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());
        }
    }
Esempio n. 22
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);
        }
Esempio n. 23
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);
        }
Esempio n. 24
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);
        }
Esempio n. 25
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);
        }
Esempio n. 26
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));
            }
        }
Esempio n. 27
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);
            }
        }
Esempio n. 28
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);
        }