资源管理(5)——资源加密

╄→гoц情女王★ 提交于 2020-01-09 00:52:11

 

 #region AssetBundleEncrypt 资源包加密
    /// <summary>
    /// 资源包加密
    /// </summary>
    /// <param name="path"></param>
    private void AssetBundleEncrypt(bool isDelete = false)
    {
        //循环设置文件夹包括子文件里边的项
        for (int i = 0; i < m_List.Count; i++)
        {
            AssetBundleEntity entity = m_List[i];//取到一个节点

            if (entity.IsEncrypt)//是否需要加密
            {
                string[] folderArr = new string[entity.PathList.Count];
                for (int j = 0; j < entity.PathList.Count; j++)
                {
                    string path = Application.dataPath + "/../AssetBundles/" + dal.GetVersion() + "/" + arrBuildTarget[buildTargetIndex] + "/" + entity.PathList[j];

                    if (entity.IsFolder == false)//是否文件夹
                    {
                        //不是遍历文件夹打包 说明这个路径只有一个文件
                        path = path + ".assetbundle";
                        //加密单个文件
                        AssetBundleEncryptFile(path, isDelete);
                    }
                    else
                    {
                        //加密文件夹下所有文件
                        AssetBundleEncryptFolder(path, isDelete);
                    }
                }
            }
        }
        Debug.Log("资源包加密完毕");
    }

还是循环m_list    AssetBundleConfig.xml

    /// <summary>
    /// 构造函数
    /// </summary>
    void OnEnable()
    {
        string xmlPath = Application.dataPath + @"\YouYouFramework\Editor\AssetBundle\AssetBundleConfig.xml";
        dal = new AssetBundleDAL(xmlPath);
        m_List = dal.GetList();

        m_Dic = new Dictionary<string, bool>();

        for (int i = 0; i < m_List.Count; i++)
        {
            m_Dic[m_List[i].Key] = true;
        }
    }

 

配置表   中  IsFolder  指的是是否遍历文件夹,

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <AssetBundle ResourceVersion="1.0.7">
    <Item Name="声音" Tag="Audio" IsFolder="False" IsFirstData="True" IsEncrypt="False">
      <Path Value="Download/Audio" />
    </Item>
    <Item Name="CusShaders" Tag="Shader" IsFolder="False" IsFirstData="True" IsEncrypt="True">
      <Path Value="Download/CusShaders" />
    </Item>
    <Item Name="DataTable" Tag="DataTable" IsFolder="False" IsFirstData="True" IsEncrypt="True">
      <Path Value="Download/DataTable" />
    </Item>
    <Item Name="xLuaLogic" Tag="Lua" IsFolder="False" IsFirstData="True" IsEncrypt="True">
      <Path Value="Download/xLuaLogic" />
    </Item>
    <Item Name="Role" Tag="Role" IsFolder="True" IsFirstData="False" IsEncrypt="False">
      <Path Value="Download/Role" />
    </Item>
    <Item Name="Effect" Tag="Effect" IsFolder="True" IsFirstData="False" IsEncrypt="False">
      <Path Value="Download/Effect" />
    </Item>
    <Item Name="初始场景" Tag="Scene" IsFolder="False" IsFirstData="True" IsEncrypt="False">
      <Path Value="Download/Scenes/GameScene_HuPaoCun_01.unity" />
      <Path Value="Download/Scenes/GameScene_HuPaoCun_02.unity" />
      <Path Value="Download/Scenes/GameScene_HuPaoCun_03.unity" />
    </Item>
    <Item Name="其他场景" Tag="Scene" IsFolder="False" IsFirstData="False" IsEncrypt="False">
      <Path Value="Download/Scenes/GameScene_DaLi.unity" />
      <Path Value="Download/Scenes/GameScene_ErHai.unity" />
      <Path Value="Download/Scenes/GameScene_JingHu.unity" />
      <Path Value="Download/Scenes/GameScene_LuoYang.unity" />
      <Path Value="Download/Scenes/GameScene_ShiLin.unity" />
      <Path Value="Download/Scenes/GameScene_WuYi.unity" />
      <Path Value="Download/Scenes/GameScene_YuXi.unity" />
    </Item>
    <Item Name="UI预设" Tag="UI" IsFolder="False" IsFirstData="True" IsEncrypt="False">
      <Path Value="Download/UI/UIPrefab" />
    </Item>
    <Item Name="UI字体" Tag="UIFont" IsFolder="False" IsFirstData="True" IsEncrypt="False">
      <Path Value="Download/UI/UIFont" />
    </Item>
    <Item Name="UI图集" Tag="UI" IsFolder="False" IsFirstData="True" IsEncrypt="False">
      <Path Value="Download/UI/UIRes/Common" />
      <Path Value="Download/UI/UIRes/Frame" />
      <Path Value="Download/UI/UIRes/TitleBar" />
    </Item>
    <Item Name="UILoading" Tag="UI" IsFolder="True" IsFirstData="True" IsEncrypt="False">
      <Path Value="Download/UI/UIRes/Loading" />
    </Item>
    <Item Name="Test" Tag="Test" IsFolder="True" IsFirstData="True" IsEncrypt="False">
      <Path Value="Download/Test" />
    </Item>
  </AssetBundle>
</Root>

 

加密文件夹下所有文件

    /// <summary>
    /// 加密文件夹下所有文件
    /// </summary>
    /// <param name="folderPath"></param>
    private void AssetBundleEncryptFolder(string folderPath, bool isDelete)
    {
        DirectoryInfo directory = new DirectoryInfo(folderPath);

        //拿到文件夹下所有文件
        FileInfo[] arrFiles = directory.GetFiles("*", SearchOption.AllDirectories);

        foreach (FileInfo file in arrFiles)
        {
            AssetBundleEncryptFile(file.FullName, isDelete);
        }
    }

 

加密单个文件

    /// <summary>
    /// 加密单个文件
    /// </summary>
    /// <param name="filePath"></param>
    private void AssetBundleEncryptFile(string filePath, bool isDelete)
    {
        if (isDelete)
        {
            //如果当前存在同名文件,则删除文件
            if (File.Exists(filePath)) File.Delete(filePath);
            if (File.Exists(filePath + ".manifest")) File.Delete(filePath + ".manifest");

            return;
        }

        FileInfo fileInfo = new FileInfo(filePath);

        if (!fileInfo.Extension.Equals(".assetbundle", StringComparison.CurrentCultureIgnoreCase) &&
            !fileInfo.Extension.Equals(".unity3d", StringComparison.CurrentCultureIgnoreCase)
            )
        {
            return;
        }

        byte[] buffer = null;

        using (FileStream fs = new FileStream(filePath, FileMode.Open))
        {
            buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
        }

        //异或加密
        buffer = SecurityUtil.Xor(buffer);

        using (FileStream fs = new FileStream(filePath, FileMode.Create))
        {
            fs.Write(buffer, 0, buffer.Length);
            fs.Flush();
        }
    }
    #endregion

打包完毕之后调用加密方法

    #region OnAssetBundleCallBack AssetBundle打包 按钮回调
    /// <summary>
    /// AssetBundle打包按钮回调
    /// </summary>
    private void OnAssetBundleCallBack()
    {
        AssetBundleEncrypt(true);
        //AssetBundle打包
        string toPath = Application.dataPath + "/../AssetBundles/" + dal.GetVersion() + "/" + arrBuildTarget[buildTargetIndex];
        if (!Directory.Exists(toPath)) Directory.CreateDirectory(toPath);//如果路径不存在 创建路径

        BuildPipeline.BuildAssetBundles(toPath, BuildAssetBundleOptions.ChunkBasedCompression, target);
        Debug.Log("资源打包完毕 打包路径==" + toPath);

        //AssetBundle加密
        AssetBundleEncrypt();

        //生成依赖文件
        OnCreateDependenciesFile();

        //生成版本文件
        OnCreateVersionFileCallBack();
        Debug.Log("AssetBundle 打包并加密完毕!");
    }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!