Пример #1
0
    /// <summary>
    /// 压缩和打包Lua脚本/配置
    /// </summary>
    public static void EncryptLua()
    {
        CLZF2.Decrypt(null, 260769);
        CLZF2.Decrypt(new byte[1], 3);

        string CodeRoot  = Path.Combine(Application.dataPath, "LuaCodes");
        string scriptDir = Path.Combine(CodeRoot, "Script");
        string configDir = Path.Combine(CodeRoot, "Config");

        if (!Directory.Exists(scriptDir))
        {
            SystemTools.NeedDirectory(scriptDir);
            AssetDatabase.Refresh();
            var ai = AssetImporter.GetAtPath("Assets/LuaCodes/Script");
            ai.assetBundleName = AssetsMgr.LUA_SCRIPT;
        }
        if (!Directory.Exists(configDir))
        {
            SystemTools.NeedDirectory(configDir);
            AssetDatabase.Refresh();
            var ai = AssetImporter.GetAtPath("Assets/LuaCodes/Config");
            ai.assetBundleName = AssetsMgr.LUA_CONFIG;
        }

        var scripts    = new DirectoryInfo(scriptDir).GetFiles("*.bytes");
        var configs    = new DirectoryInfo(configDir).GetFiles("*.bytes");
        var listExists = new List <string>();

        foreach (var f in scripts)
        {
            listExists.Add("Script/" + f.Name);
        }
        foreach (var f in configs)
        {
            listExists.Add("Config/" + f.Name);
        }

        DirectoryInfo dirLua = new DirectoryInfo(LuaEnv.GetFilePath(""));

        FileInfo[] files      = dirLua.GetFiles("*.lua", SearchOption.AllDirectories);
        int        startIndex = dirLua.FullName.Length + 1;

        foreach (FileInfo f in files)
        {
            string   fullName = f.FullName.Substring(startIndex).Replace('/', '%').Replace('\\', '%');
            string   fileName = fullName.Remove(fullName.Length - 4) + ".bytes";
            string[] lines    = File.ReadAllLines(f.FullName);
            // 以"--"开头的注释以换行符代替
            List <string> liLine = new List <string>();
            foreach (var l in lines)
            {
                string ltim = l.Trim();
                if (ltim.StartsWith("--") && !ltim.StartsWith("--[[") && !ltim.StartsWith("--]]"))
                {
                    liLine.Add("\n");
                }
                else
                {
                    liLine.Add(l + "\n");
                }
            }
            string codes  = string.Concat(liLine.ToArray());
            byte[] nbytes = System.Text.Encoding.UTF8.GetBytes(codes);
            if (nbytes.Length > 0)
            {
                nbytes = CLZF2.DllCompress(nbytes);
                CLZF2.Encrypt(nbytes, nbytes.Length);
            }
            else
            {
                Debug.LogWarning("Compress Lua: " + fileName + " is empty!");
            }

            string path;
            if (fileName.StartsWith("config"))
            {
                listExists.Remove("Config/" + fileName);
                path = Path.Combine(configDir, fileName);
            }
            else
            {
                listExists.Remove("Script/" + fileName);
                path = Path.Combine(scriptDir, fileName);
            }
            if (File.Exists(path))
            {
                using (var file = File.OpenWrite(path)) {
                    file.Seek(0, SeekOrigin.Begin);
                    file.Write(nbytes, 0, nbytes.Length);
                    file.SetLength(nbytes.Length);
                }
            }
            else
            {
                File.WriteAllBytes(path, nbytes);
            }
        }
        foreach (var n in listExists)
        {
            var path = Path.Combine(CodeRoot, n);
            File.Delete(path);
            Log("Delete: {0}", n);
        }
        Log("Compress {0} files success.\n => {1}", files.Length, CodeRoot);
    }