当前位置:   article > 正文

HybridCLR 热更新(打AB包)练习_hybirdclr补充元数据

hybirdclr补充元数据
  1. public class ABTool : EditorWindow
  2. {
  3. static string dllpath = Application.dataPath + "/../" + HybridCLRSettings.Instance.hotUpdateDllCompileOutputRootDir;
  4. public static List<string> AOTMetaAssemblyNames { get; } = new List<string>()
  5. {
  6. "mscorlib.dll",
  7. "System.dll",
  8. "System.Core.dll",
  9. };
  10. [MenuItem("ABTool/生成AB包")]
  11. public static void CreatAB()
  12. {
  13. BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
  14. //找到对应目录
  15. DirectoryInfo direction = new DirectoryInfo(dllpath + "/" + target);
  16. //获取目录下所有文件
  17. FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
  18. //把所有dll文件拷贝到s目录下
  19. foreach (var item in files)
  20. {
  21. if (item.Name.EndsWith(".dll"))
  22. {
  23. string dllPath = $"{dllpath + "/" + target}/{item.Name}";
  24. string dllBytesPath = $"{Application.streamingAssetsPath }/{item.Name}.bytes";
  25. File.Copy(dllPath, dllBytesPath, true);
  26. Debug.Log($"hotfix dll{dllPath}->{ dllBytesPath}");
  27. }
  28. }
  29. //预制体
  30. List<AssetBundleBuild> abs = new List<AssetBundleBuild>();
  31. //找到预制体目录下的所有目录
  32. AddABPath(abs, $"{Application.dataPath}/Prefabs/");
  33. AddABPath(abs, $"{Application.dataPath}/Resources/");
  34. //构建AB包放到S目录下
  35. BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, abs.ToArray(), BuildAssetBundleOptions.None, target);
  36. //加载补充元数据
  37. CopyAOTAssembliesToStreamingAssets();
  38. AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  39. }
  40. public static void CopyAOTAssembliesToStreamingAssets()
  41. {
  42. var target = EditorUserBuildSettings.activeBuildTarget;
  43. string aotAssembliesSrcDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
  44. string aotAssembliesDrcDir = Application.streamingAssetsPath;
  45. foreach (var dll in AOTMetaAssemblyNames)
  46. {
  47. string srcDllPath = $"{aotAssembliesSrcDir}/{dll}";
  48. if (!File.Exists(srcDllPath))
  49. {
  50. Debug.LogError($"ab中添加AOT补充元数据dll:{srcDllPath}时发生错误,文件不存在。裁剪后的AOT dll在BuildPlayer时才能生成,因此需要你先构建一次游戏App后再打包。");
  51. continue;
  52. }
  53. string dllBytesPath = $"{aotAssembliesDrcDir}/{dll}.bytes";
  54. File.Copy(srcDllPath, dllBytesPath, true);
  55. Debug.Log($"[CopyAOTAssembliesToStreamingAssets] copy AOT dll{srcDllPath}->{dllBytesPath}");
  56. }
  57. }
  58. public static void AddABPath(List<AssetBundleBuild> abs, string path)
  59. {
  60. DirectoryInfo directionInfo = new DirectoryInfo(path);//用于创建 移动..目录
  61. DirectoryInfo[] directions = directionInfo.GetDirectories();//返回子目录
  62. //每个目录打一个AB包
  63. for (int i = 0; i < directions.Length; i++)
  64. {
  65. List<string> prefabAssets = new List<string>();
  66. Debug.Log("打包文件夹:" + directions[i].Name);
  67. FileInfo[] fileinfos = directions[i].GetFiles("*", SearchOption.AllDirectories);
  68. Debug.Log("目录下文件夹个数:" + fileinfos.Length);
  69. foreach (var item in fileinfos)
  70. {
  71. if (item.Name.EndsWith(".meta"))
  72. {
  73. continue;
  74. }
  75. Debug.Log("打包文件:" + item.Name);
  76. prefabAssets.Add(item.FullName.Replace("\\", "/"));
  77. AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  78. }
  79. AssetBundleBuild PrefabsAb = new AssetBundleBuild
  80. {
  81. assetBundleName = directions[i].Name,
  82. assetNames = prefabAssets.Select(s => ToReleateAssetPath(s)).ToArray()
  83. };
  84. abs.Add(PrefabsAb);
  85. }
  86. }
  87. public static string ToReleateAssetPath(string s)
  88. {
  89. string path = s.Substring(s.IndexOf("Assets/"));
  90. Debug.Log(path);
  91. return path;
  92. }
  93. [MenuItem("ABTool/生成version")]//版本
  94. public static void MakeVersion()
  95. {
  96. string json = AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/Resources/Config/config.json").text;
  97. Config config = JsonConvert.DeserializeObject<Config>(json);
  98. List<VersionData> versionDatas = new List<VersionData>();
  99. //第一条是假数据,用力判断是否需要热更新
  100. VersionData version = new VersionData();
  101. //判断使用测试版还是正式版热更新
  102. config.upid++;
  103. version.abName = config.isBate ? config.hotPath_Bate : config.hotPath;
  104. version.len = config.upid;//更新号
  105. version.md5 = config.version;//版本号
  106. versionDatas.Add(version);
  107. //重新写入
  108. json = JsonConvert.SerializeObject(config);
  109. File.WriteAllText(Application.dataPath+ "/Resources/Config/config.json", json);
  110. //找到s目录下所有文件
  111. string[] files = Directory.GetFiles(Application.streamingAssetsPath, ".", SearchOption.AllDirectories);
  112. foreach (var item in files)
  113. {
  114. //取后缀名
  115. string extensionName = Path.GetExtension(item);
  116. //排除.meat和.manifest文件
  117. if (extensionName.Contains("meta") || extensionName.Contains("manifest")) continue;
  118. Debug.Log(item);
  119. string abName = item.Replace(Application.dataPath, "Assets");
  120. abName = abName.Replace("\\", "/");
  121. string assetBundleName = "";
  122. assetBundleName = abName.Replace("Assets/StreamingAssets/", string.Empty);
  123. VersionData versionData = new VersionData();
  124. versionData.abName = assetBundleName;
  125. versionData.len = File.ReadAllText(item).Length;
  126. versionData.md5 = FileMD5(item);
  127. versionDatas.Add(versionData);
  128. }
  129. //写入文件
  130. string versionInfo = JsonConvert.SerializeObject(versionDatas);
  131. File.WriteAllText(Application.streamingAssetsPath + "/version.txt", versionInfo, Encoding.UTF8);
  132. AssetDatabase.Refresh();
  133. Debug.Log("version.txt创建成功");
  134. }
  135. static string FileMD5(string filepath)
  136. {
  137. FileStream fileStream = new FileStream(filepath, FileMode.Open);
  138. MD5 mD5 = new MD5CryptoServiceProvider();
  139. byte[] bytes = mD5.ComputeHash(fileStream);
  140. StringBuilder sb = new StringBuilder();
  141. for (int i = 0; i <bytes.Length; i++)
  142. {
  143. sb.Append(bytes[i].ToString("x2"));
  144. }
  145. return sb.ToString();
  146. }
  147. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号