TaskEncryption.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace YooAsset.Editor
  7. {
  8. [TaskAttribute("资源包加密")]
  9. public class TaskEncryption : IBuildTask
  10. {
  11. public class EncryptionContext : IContextObject
  12. {
  13. public List<string> EncryptList;
  14. /// <summary>
  15. /// 检测是否为加密文件
  16. /// </summary>
  17. public bool IsEncryptFile(string bundleName)
  18. {
  19. return EncryptList.Contains(bundleName);
  20. }
  21. }
  22. void IBuildTask.Run(BuildContext context)
  23. {
  24. var buildParameters = context.GetContextObject<BuildParametersContext>();
  25. var buildMapContext = context.GetContextObject<BuildMapContext>();
  26. var buildMode = buildParameters.Parameters.BuildMode;
  27. if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild)
  28. {
  29. EncryptionContext encryptionContext = new EncryptionContext();
  30. encryptionContext.EncryptList = EncryptFiles(buildParameters, buildMapContext);
  31. context.SetContextObject(encryptionContext);
  32. }
  33. else
  34. {
  35. EncryptionContext encryptionContext = new EncryptionContext();
  36. encryptionContext.EncryptList = new List<string>();
  37. context.SetContextObject(encryptionContext);
  38. }
  39. }
  40. /// <summary>
  41. /// 加密文件
  42. /// </summary>
  43. private List<string> EncryptFiles(BuildParametersContext buildParameters, BuildMapContext buildMapContext)
  44. {
  45. var encryptionServices = buildParameters.Parameters.EncryptionServices;
  46. // 加密资源列表
  47. List<string> encryptList = new List<string>();
  48. // 如果没有设置加密类
  49. if (encryptionServices == null)
  50. return encryptList;
  51. int progressValue = 0;
  52. foreach (var bundleInfo in buildMapContext.BundleInfos)
  53. {
  54. if (encryptionServices.Check(bundleInfo.BundleName))
  55. {
  56. if (bundleInfo.IsRawFile)
  57. {
  58. UnityEngine.Debug.LogWarning($"Encryption not support raw file : {bundleInfo.BundleName}");
  59. continue;
  60. }
  61. encryptList.Add(bundleInfo.BundleName);
  62. // 注意:通过判断文件合法性,规避重复加密一个文件
  63. string filePath = $"{buildParameters.PipelineOutputDirectory}/{bundleInfo.BundleName}";
  64. byte[] fileData = File.ReadAllBytes(filePath);
  65. if (EditorTools.CheckBundleFileValid(fileData))
  66. {
  67. byte[] bytes = encryptionServices.Encrypt(fileData);
  68. File.WriteAllBytes(filePath, bytes);
  69. BuildRunner.Log($"文件加密完成:{filePath}");
  70. }
  71. }
  72. // 进度条
  73. EditorTools.DisplayProgressBar("加密资源包", ++progressValue, buildMapContext.BundleInfos.Count);
  74. }
  75. EditorTools.ClearProgressBar();
  76. if(encryptList.Count == 0)
  77. UnityEngine.Debug.LogWarning($"没有发现需要加密的文件!");
  78. return encryptList;
  79. }
  80. }
  81. }