Пример #1
0
    static void CopyLuaBytesFiles(string sourceDir, string destDir, string searchPattern = "*.lua", SearchOption option = SearchOption.AllDirectories)
    {
        if (!Directory.Exists(sourceDir))
        {
            return;
        }

        string[] files = Directory.GetFiles(sourceDir, searchPattern, option);
        int      len   = sourceDir.Length;

        if (sourceDir[len - 1] == '/' || sourceDir[len - 1] == '\\')
        {
            --len;
        }

        Random ran = new Random(DateTime.Now.Millisecond);
        int    val = ran.Next(11111111, 99999999);

        File.WriteAllText(Application.dataPath + "/Resources/lb_pwd.txt", val.ToString());

        for (int i = 0; i < files.Length; i++)
        {
            string str = files[i].Replace("\\", "/").Replace(Application.dataPath + "/" + Paths.GameResRoot + "/", "");
            str = str.Replace("/", "@").Replace(".lua", ".bytes");
            string dest = destDir + "/" + str;
            string dir  = Path.GetDirectoryName(dest);
            Directory.CreateDirectory(dir);
            byte[] bytes = File.ReadAllBytes(files[i]);
            bytes = AES_EnorDecrypt.Encrypt(bytes, Def.AppKey + val);
            File.WriteAllBytes(dest, bytes);
        }
    }
Пример #2
0
    public override byte[] ReadFile(string fileName)
    {
        string path = string.Empty;

        if (!beZip)
        {
            path = FindFile(fileName);
            byte[] str = null;
            if (!string.IsNullOrEmpty(path) && File.Exists(path))
            {
                str = File.ReadAllBytes(path);
            }
            return(str);
        }

        byte[] buffer = null;
        //判断传递进来的fileName有没有“.lua”的后缀 没有就加上
        if (!fileName.EndsWith(".lua"))
        {
            fileName += ".lua";
        }

        path = "Lua/" + fileName;
        TextAsset text = Resources.Load(path, typeof(TextAsset)) as TextAsset;

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

        if (buffer == null)//加载AB包中的lua文件
        {
            var type = GetModulesType(fileName);
            if (type != Def.ModulesType.None)
            {
                string assetName = fileName.Replace("/", "@");
                assetName = assetName.Replace(".lua", "");
                TextAsset luaCode = ResManager.Inst.LoadRes <TextAsset>(type, assetName);
                if (luaCode != null)
                {
                    TextAsset pwd   = Resources.Load <TextAsset>("lb_pwd");
                    byte[]    bytes = luaCode.bytes;
                    bytes  = AES_EnorDecrypt.Decrypt(bytes, Def.AppKey + pwd.text);
                    buffer = bytes;
                    Resources.UnloadAsset(pwd);
                }
            }
        }
        return(buffer);
    }