From 2d73b2d13a5ead748ad48c16c3d6b78c8391d354 Mon Sep 17 00:00:00 2001 From: zhangshanxue Date: Wed, 1 Dec 2021 21:32:16 +0800 Subject: [PATCH] encrypt --- Assets/Editor/AutoPack.cs | 30 ++++++++ .../Scripts/Utility/MEncyptUtil.cs | 77 ++++++++++++------- .../GameEditor/FrameTool/BuildWindow.cs | 1 + .../GameEditor/FrameTool/HotFixWindow.cs | 74 +++++++++--------- 4 files changed, 119 insertions(+), 63 deletions(-) diff --git a/Assets/Editor/AutoPack.cs b/Assets/Editor/AutoPack.cs index b089bf8966..54fbc1f9aa 100644 --- a/Assets/Editor/AutoPack.cs +++ b/Assets/Editor/AutoPack.cs @@ -241,6 +241,7 @@ public class AutoPack : EditorWindow ExportAS(i); ReplaceFromProject(i); ReplaceConfigAndVersion(i); + EncryptAB(i); GitUpdate(i); CreateLogFile(i); StartGradleBuild(i); @@ -249,6 +250,7 @@ public class AutoPack : EditorWindow case 1: // 只替换AB ReplaceFromAB(i); ReplaceConfigAndVersion(i); + EncryptAB(i); GitUpdate(i); CreateLogFile(i); StartGradleBuild(i); @@ -416,6 +418,34 @@ public class AutoPack : EditorWindow Debug.Log(PackConfig[index].name + ":替换AB包完成"); } + // 加密AB包 + private static void EncryptAB(int index) + { + string toPath = _MomPackPath + PackConfig[index].project + "/src/main/assets/Android"; + + string json = File.ReadAllText(toPath + "/"+ VersionFileName); + VersionTxt version = JsonUtility.FromJson(json); + if (version.EncyptKey != null && version.EncyptKey != "") + { + string[] pathList = Directory.GetFiles(toPath, "*.unity3d", SearchOption.AllDirectories); + float i = 0f; + Debug.Log("文件数量;" + pathList.Length); + foreach (string path in pathList) + { + GameLogic.MEncyptUtil.EncyptAB(path, version.EncyptKey); + EditorUtility.DisplayProgressBar("正在加密:", path, i++ / pathList.Length); + } + EditorUtility.ClearProgressBar(); + Debug.Log(PackConfig[index].name + ":加密AB包完成"); + } + else { + + Debug.Log(PackConfig[index].name + ":无需加密"); + } + + } + + // 替换配置文件 private static void ReplaceConfigAndVersion(int index) { diff --git a/Assets/LuaFramework/Scripts/Utility/MEncyptUtil.cs b/Assets/LuaFramework/Scripts/Utility/MEncyptUtil.cs index 3ce5da801d..b100324b09 100644 --- a/Assets/LuaFramework/Scripts/Utility/MEncyptUtil.cs +++ b/Assets/LuaFramework/Scripts/Utility/MEncyptUtil.cs @@ -43,7 +43,7 @@ namespace GameLogic { if (key == null || key == "") { - rb = crypt(data, key); + rb = data; } else { rb = crypt(data, key); @@ -55,41 +55,62 @@ namespace GameLogic { } return r; } - - - [MenuItem("Build/TEST")] - static void load() + // 文件加密 + public static void EncyptAB(string path, string key) { - byte[] a = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; - string s = ""; - for(int i = 0;i < a.Length; i++) + // 闪屏先不加密了 + if (Path.GetFileName(path).Equals("splashpanel.unity3d")) { - s += a[i]; + return; } - XDebug.Log.l("a = " + s); - - - - byte[] b = crypt(a, "key"); - s = ""; - for (int i = 0; i < b.Length; i++) + if (key == null || key == "") { - s += b[i]; + return; } - XDebug.Log.l("b = " + s); - - - - byte[] c = crypt(b, "key"); - s = ""; - for (int i = 0; i < c.Length; i++) + if (!File.Exists(path)) { - s += c[i]; + return; } - XDebug.Log.l("c = " + s); + byte[] data = File.ReadAllBytes(path); + byte[] rb = crypt(data, key); - - //File.ReadAllBytes(""); + File.WriteAllBytes(path, rb); } + + + //[MenuItem("Build/TEST")] + //static void load() + //{ + // byte[] a = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; + // string s = ""; + // for(int i = 0;i < a.Length; i++) + // { + // s += a[i]; + // } + // XDebug.Log.l("a = " + s); + + + + // byte[] b = crypt(a, "key"); + // s = ""; + // for (int i = 0; i < b.Length; i++) + // { + // s += b[i]; + // } + // XDebug.Log.l("b = " + s); + + + + // byte[] c = crypt(b, "key"); + // s = ""; + // for (int i = 0; i < c.Length; i++) + // { + // s += c[i]; + // } + // XDebug.Log.l("c = " + s); + + + // //File.ReadAllBytes(""); + //} } } \ No newline at end of file diff --git a/Assets/Scripts/Editor/GameEditor/FrameTool/BuildWindow.cs b/Assets/Scripts/Editor/GameEditor/FrameTool/BuildWindow.cs index 9049b9e7dc..df7d27de40 100644 --- a/Assets/Scripts/Editor/GameEditor/FrameTool/BuildWindow.cs +++ b/Assets/Scripts/Editor/GameEditor/FrameTool/BuildWindow.cs @@ -248,6 +248,7 @@ namespace GameEditor.FrameTool { public string packageVersion; public string version; public string serverUrl; + public string EncyptKey; //public string noticeChannel; } diff --git a/Assets/Scripts/Editor/GameEditor/FrameTool/HotFixWindow.cs b/Assets/Scripts/Editor/GameEditor/FrameTool/HotFixWindow.cs index 7e3f430cdb..3a8d87dbdd 100644 --- a/Assets/Scripts/Editor/GameEditor/FrameTool/HotFixWindow.cs +++ b/Assets/Scripts/Editor/GameEditor/FrameTool/HotFixWindow.cs @@ -56,13 +56,10 @@ namespace GameEditor.FrameTool } //遍历内部unity3d数据 - public static List GetFilesList(string filesPath) + public static List GetFilesList(string filesPath, string encryptKey = null) { - if(!File.Exists(filesPath + UpdateConfigs.FILES)) - { - return null; - } - UnityEngine.AssetBundle bundle = UnityEngine.AssetBundle.LoadFromFile(filesPath + UpdateConfigs.FILES); + //UnityEngine.AssetBundle bundle = UnityEngine.AssetBundle.LoadFromFile(filesPath + UpdateConfigs.FILES); + UnityEngine.AssetBundle bundle = UnityEngine.AssetBundle.LoadFromMemory(GameLogic.MEncyptUtil.DecyptAB(File.ReadAllBytes(filesPath + UpdateConfigs.FILES), encryptKey, 0)); ResourceFiles files = bundle.LoadAsset("game"); bundle.Unload(true); return files.files; @@ -73,43 +70,32 @@ namespace GameEditor.FrameTool { List diffList = new List(); Dictionary exDataDic = new Dictionary(); - if (exDataList != null) + for (int i = 0; i < exDataList.Count; i++) { - for (int i = 0; i < exDataList.Count; i++) + if (!exDataDic.ContainsKey(exDataList[i].fileName)) { - if (!exDataDic.ContainsKey(exDataList[i].fileName)) - { - exDataDic.Add(exDataList[i].fileName, exDataList[i]); - } - else - { - exDataDic[exDataList[i].fileName] = exDataList[i]; - } + exDataDic.Add(exDataList[i].fileName, exDataList[i]); } - } else { - XDebug.Log.error("exDataList is null!!!"); - } - if(inDataList != null) - { - for (int i = 0; i < inDataList.Count; i++) + else { - if (!exDataDic.ContainsKey(inDataList[i].fileName)) + exDataDic[exDataList[i].fileName] = exDataList[i]; + } + } + + for (int i = 0; i < inDataList.Count; i++) + { + if (!exDataDic.ContainsKey(inDataList[i].fileName)) + { + diffList.Add(inDataList[i]); + } + else + { + if (!inDataList[i].crc.Equals(exDataDic[inDataList[i].fileName].crc)) { diffList.Add(inDataList[i]); } - else - { - if (!inDataList[i].crc.Equals(exDataDic[inDataList[i].fileName].crc)) - { - diffList.Add(inDataList[i]); - } - } } } - else - { - XDebug.Log.error("inDataList is null!!!"); - } return diffList; } @@ -262,7 +248,7 @@ namespace GameEditor.FrameTool LoadCDN(); // 计算差异文件 - local_cdn_list = HotFixTool.GetDiffList(HotFixTool.GetFilesList(local_files_path), HotFixTool.GetFilesList(cdn_files_path)); + local_cdn_list = HotFixTool.GetDiffList(HotFixTool.GetFilesList(local_files_path), HotFixTool.GetFilesList(cdn_files_path, local_version.EncyptKey)); // 计算大小 local_cdn_size = HotFixTool.GetDiffSize(local_cdn_list); @@ -343,6 +329,24 @@ namespace GameEditor.FrameTool } changeFile.Add(UpdateConfigs.FILES); log.Add(UpdateConfigs.FILES); + // 复制热更文件 + string cpPath = path_cdn_setting + "/" + AppConst.PlatformPath + "/"; + int fileIndex = 0; + foreach (string file in changeFile) + { + EditorUtility.DisplayProgressBar("正在复制热更文件:", file, fileIndex++/changeFile.Count); + + string dirName = Path.GetDirectoryName(cpPath + file); + if (!Directory.Exists(dirName)) + { + Directory.CreateDirectory(dirName); + } + File.Copy(local_files_path + file, cpPath + file, true); + if (local_version.EncyptKey != null && local_version.EncyptKey != "") + { + MEncyptUtil.EncyptAB(cpPath + file, local_version.EncyptKey); + } + } // write ChangeList EditorUtility.DisplayProgressBar("正在生成文件变化列表", changeFileName, 1f); HotFixTool.WriteLog(logPath, changeFileName, changeFile.ToArray());