UpdatableBuildEventHandler.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 Jiang Yin. All rights reserved.
  4. // Homepage: https://gameframework.cn/
  5. // Feedback: mailto:ellan@gameframework.cn
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using UnityEditor;
  11. using UnityEngine;
  12. using UnityGameFramework.Editor.ResourceTools;
  13. //using HybridCLR.Editor;
  14. namespace GFMain.Editor
  15. {
  16. /// <summary>
  17. /// 在线资源更新版生成资源处理
  18. /// 通过生成信息,生成版本信息VersionInfo文档,生成于full文件夹下
  19. /// </summary>
  20. public sealed class UpdatableBuildEventHandler : IBuildEventHandler
  21. {
  22. private const string UpdateURL = "http://192.168.1.23:8008/_res/Full";//服务器更新路径
  23. private static string OutPutDirectory { get; set; }
  24. public bool ContinueOnFailure { get { return false; } }
  25. private static VersionInfo m_VersionInfo = new VersionInfo();
  26. public void OnPreprocessAllPlatforms(string productName, string companyName, string gameIdentifier, string gameFrameworkVersion, string unityVersion, string applicableGameVersion, int internalResourceVersion,
  27. Platform platforms, AssetBundleCompressionType assetBundleCompression, string compressionHelperTypeName, bool additionalCompressionSelected, bool forceRebuildAssetBundleSelected, string buildEventHandlerTypeName, string outputDirectory, BuildAssetBundleOptions buildAssetBundleOptions,
  28. string workingPath, bool outputPackageSelected, string outputPackagePath, bool outputFullSelected, string outputFullPath, bool outputPackedSelected, string outputPackedPath, string buildReportPath)
  29. {
  30. //...
  31. //赋值Version需要参数
  32. m_VersionInfo.LatestGameVersion = applicableGameVersion;
  33. m_VersionInfo.InternalResourceVersion = internalResourceVersion;
  34. m_VersionInfo.InternalGameVersion = 1;
  35. OutPutDirectory = outputDirectory;
  36. }
  37. public void OnPostprocessAllPlatforms(string productName, string companyName, string gameIdentifier, string gameFrameworkVersion, string unityVersion, string applicableGameVersion, int internalResourceVersion,
  38. Platform platforms, AssetBundleCompressionType assetBundleCompression, string compressionHelperTypeName, bool additionalCompressionSelected, bool forceRebuildAssetBundleSelected, string buildEventHandlerTypeName, string outputDirectory, BuildAssetBundleOptions buildAssetBundleOptions,
  39. string workingPath, bool outputPackageSelected, string outputPackagePath, bool outputFullSelected, string outputFullPath, bool outputPackedSelected, string outputPackedPath, string buildReportPath)
  40. {
  41. }
  42. public void OnPreprocessPlatform(Platform platform, string workingPath, bool outputPackageSelected, string outputPackagePath, bool outputFullSelected, string outputFullPath, bool outputPackedSelected, string outputPackedPath)
  43. {
  44. //生成元数据补充清单
  45. //var jw = new LitJson.JsonWriter() { PrettyPrint = true };
  46. //var metadataArr = (AOTGenericReferences.PatchedAOTAssemblyList as List<string>).ToArray();
  47. //for (int i = 0; i < metadataArr.Length; i++)
  48. //{
  49. // metadataArr[i] = EditorUserBuildSettings.activeBuildTarget.ToString() + "/" + metadataArr[i];
  50. //}
  51. //LitJson.JsonMapper.ToJson(metadataArr, jw);
  52. //var metadataArrJson = jw.TextWriter.ToString();
  53. //var savePath = Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "HybridCLRGenerate", platform + "MetadataList.txt"));
  54. //File.WriteAllText(savePath, metadataArrJson);
  55. //Debug.Log("XXXMetadataList.txt save success!!!");
  56. ////拷贝补充元数据dll到对应目录
  57. //for (int i = 0; i < metadataArr.Length; i++)
  58. //{
  59. // var strOrigPath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("/")) + "/HybridCLRData/AssembliesPostIl2CppStrip/" + metadataArr[i];
  60. // var strDestPath = Application.dataPath + "/HybridCLRGenerate/" + metadataArr[i] + ".bytes";
  61. // string strPath = Path.GetDirectoryName(strDestPath);
  62. // if (!Directory.Exists(strPath)) Directory.CreateDirectory(strPath);
  63. // File.Copy(strOrigPath, strDestPath, true);
  64. //}
  65. ////拷贝热更dll到对应目录
  66. //string[] hotDllNames = new string[HybridCLRSettings.Instance.hotUpdateAssemblyDefinitions.Length];
  67. //for (int i = 0; i < HybridCLRSettings.Instance.hotUpdateAssemblyDefinitions.Length; ++i)
  68. //{
  69. // var path = AssetDatabase.GetAssetPath(HybridCLRSettings.Instance.hotUpdateAssemblyDefinitions[i]);
  70. // var dllName = hotDllNames[i] = HybridCLRSettings.Instance.hotUpdateAssemblyDefinitions[i].name;
  71. // var strOrigPath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("/")) + "/HybridCLRData/HotUpdateDlls/" + EditorUserBuildSettings.activeBuildTarget.ToString() + "/" + dllName + ".dll";
  72. // var strDestPath = Path.Combine(Application.dataPath + path.Substring(6).Replace("asmdef", "dll.bytes"));// "/APPContainer/Res/DLL/" + dllName + ".dll.bytes");// ;
  73. // Debug.Log(strDestPath);
  74. // string strPath = Path.GetDirectoryName(strDestPath);
  75. // if (!Directory.Exists(strPath)) Directory.CreateDirectory(strPath);
  76. // File.Copy(strOrigPath, strDestPath, true);
  77. //}
  78. }
  79. public void OnBuildAssetBundlesComplete(Platform platform, string workingPath, bool outputPackageSelected, string outputPackagePath, bool outputFullSelected, string outputFullPath, bool outputPackedSelected, string outputPackedPath, AssetBundleManifest assetBundleManifest)
  80. {
  81. }
  82. public void OnOutputUpdatableVersionListData(Platform platform, string versionListPath, int versionListLength, int versionListHashCode, int versionListCompressedLength, int versionListCompressedHashCode)
  83. {
  84. //赋值Version版本相关参数
  85. m_VersionInfo.ForceUpdateGame = false;
  86. m_VersionInfo.VersionListLength = versionListLength;
  87. m_VersionInfo.VersionListHashCode = versionListHashCode;
  88. m_VersionInfo.VersionListCompressedLength = versionListCompressedLength;
  89. m_VersionInfo.VersionListCompressedHashCode = versionListCompressedHashCode;
  90. }
  91. public void OnPostprocessPlatform(Platform platform, string workingPath, bool outputPackageSelected, string outputPackagePath, bool outputFullSelected, string outputFullPath, bool outputPackedSelected, string outputPackedPath, bool isSuccess)
  92. {
  93. if (outputFullSelected)
  94. {
  95. //保存XXXVersion.txt到Full目录
  96. var versionName = m_VersionInfo.LatestGameVersion.Replace(".", "_") + "_" + m_VersionInfo.InternalResourceVersion;
  97. m_VersionInfo.UpdatePrefixUri = Utility.Path.GetRegularPath(Path.Combine(UpdateURL, versionName, platform.ToString()));
  98. var jw = new LitJson.JsonWriter() { PrettyPrint = true };
  99. LitJson.JsonMapper.ToJson(m_VersionInfo, jw);
  100. var versionJson = jw.TextWriter.ToString();
  101. var savePath = Utility.Path.GetRegularPath(Path.Combine(OutPutDirectory, "Full", platform + "Version.txt"));
  102. File.WriteAllText(savePath, versionJson);
  103. Debug.Log("Version.txt save success!!!");
  104. }
  105. if (outputPackageSelected)
  106. {//单机,移动打包文件到StreamingAssets文件夹
  107. if (platform == Platform.Windows || platform == Platform.Windows64)
  108. {
  109. string streamingAssetsPath = Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "StreamingAssets"));
  110. string[] fileNames = Directory.GetFiles(outputPackagePath, "*", SearchOption.AllDirectories);
  111. foreach (string fileName in fileNames)
  112. {
  113. string destFileName = Utility.Path.GetRegularPath(Path.Combine(streamingAssetsPath, fileName.Substring(outputPackagePath.Length)));
  114. FileInfo destFileInfo = new FileInfo(destFileName);
  115. if (!destFileInfo.Directory.Exists)
  116. {
  117. destFileInfo.Directory.Create();
  118. }
  119. File.Copy(fileName, destFileName, true);
  120. }
  121. }
  122. }
  123. if (outputPackedSelected)
  124. {//拷贝Packed文件到StreamingAssets
  125. string streamingAssetsPath = Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "StreamingAssets"));
  126. string[] fileNames = Directory.GetFiles(outputPackedPath, "*", SearchOption.AllDirectories);
  127. foreach (string fileName in fileNames)
  128. {
  129. string destFileName = Utility.Path.GetRegularPath(Path.Combine(streamingAssetsPath, fileName.Substring(outputPackedPath.Length)));
  130. FileInfo destFileInfo = new FileInfo(destFileName);
  131. if (!destFileInfo.Directory.Exists)
  132. {
  133. destFileInfo.Directory.Create();
  134. }
  135. File.Copy(fileName, destFileName, true);
  136. }
  137. }
  138. }
  139. }
  140. }