TaskEncryption.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. void IBuildTask.Run(BuildContext context)
  12. {
  13. var buildParameters = context.GetContextObject<BuildParametersContext>();
  14. var buildMapContext = context.GetContextObject<BuildMapContext>();
  15. var buildMode = buildParameters.Parameters.BuildMode;
  16. if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild)
  17. {
  18. EncryptingBundleFiles(buildParameters, buildMapContext);
  19. }
  20. }
  21. /// <summary>
  22. /// 加密文件
  23. /// </summary>
  24. private void EncryptingBundleFiles(BuildParametersContext buildParametersContext, BuildMapContext buildMapContext)
  25. {
  26. var encryptionServices = buildParametersContext.Parameters.EncryptionServices;
  27. if (encryptionServices == null)
  28. return;
  29. if (encryptionServices.GetType() == typeof(EncryptionNone))
  30. return;
  31. int progressValue = 0;
  32. string pipelineOutputDirectory = buildParametersContext.GetPipelineOutputDirectory();
  33. foreach (var bundleInfo in buildMapContext.Collection)
  34. {
  35. EncryptFileInfo fileInfo = new EncryptFileInfo();
  36. fileInfo.BundleName = bundleInfo.BundleName;
  37. fileInfo.FilePath = $"{pipelineOutputDirectory}/{bundleInfo.BundleName}";
  38. var encryptResult = encryptionServices.Encrypt(fileInfo);
  39. if (encryptResult.LoadMethod != EBundleLoadMethod.Normal)
  40. {
  41. // 注意:原生文件不支持加密
  42. if (bundleInfo.IsRawFile)
  43. {
  44. BuildLogger.Warning($"Encryption not support raw file : {bundleInfo.BundleName}");
  45. continue;
  46. }
  47. string filePath = $"{pipelineOutputDirectory}/{bundleInfo.BundleName}.encrypt";
  48. FileUtility.WriteAllBytes(filePath, encryptResult.EncryptedData);
  49. bundleInfo.EncryptedFilePath = filePath;
  50. bundleInfo.LoadMethod = encryptResult.LoadMethod;
  51. BuildLogger.Log($"Bundle文件加密完成:{filePath}");
  52. }
  53. // 进度条
  54. EditorTools.DisplayProgressBar("加密资源包", ++progressValue, buildMapContext.Collection.Count);
  55. }
  56. EditorTools.ClearProgressBar();
  57. }
  58. }
  59. }