SerializableUtils.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using XGame.Editor.Asset;
  2. using XGame.Framework.Asset;
  3. using XGame.Framework.Asset.Addressable;
  4. using System;
  5. using System.IO;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using XGame.Framework.Serialization;
  9. namespace XGame.Editor.Build.AssetBundles
  10. {
  11. internal static class SerializableUtils
  12. {
  13. public static bool SerializeAllConfigs()
  14. {
  15. return true;
  16. //if (!SerializeAddressableCfg())
  17. // return false;
  18. //if (!SerializeAssetbundleCfg())
  19. // return false;
  20. //if (!SerializeAssetReferenceCfg())
  21. // return false;
  22. //return SerializeSceneInfoCfg();
  23. }
  24. //public static bool SerializeAddressableCfg()
  25. //{
  26. // var buildInSo = AddressableHelper.LoadAssetManifest(AddressableClassify.BuiltIn);
  27. // var infosBinary = new AddressableInfosBinary();
  28. // buildInSo.Copy(ref infosBinary);
  29. // var productSo = AddressableHelper.LoadAssetManifest(AddressableClassify.Product);
  30. // productSo.Copy(ref infosBinary);
  31. // var path = FileDefine.AddressableCfg.StreamingPath;
  32. // return Write(infosBinary, path);
  33. //}
  34. //public static bool SerializeAssetbundleCfg()
  35. //{
  36. // var infosSo = AssetBundleUtils.LoadManifest();
  37. // if (infosSo == null || infosSo.bundleInfos == null)
  38. // {
  39. // Debug.Log($"没有找到配置文件或者内容为空。Path:{PathDefine.BundleManifestPath}");
  40. // return false;
  41. // }
  42. // var infosBinary = new AssetBundleInfosBinary()
  43. // {
  44. // bundleInfoMap = new System.Collections.Generic.Dictionary<string, AssetBundleInfo>()
  45. // };
  46. // foreach (var info in infosSo.bundleInfos)
  47. // {
  48. // infosBinary.bundleInfoMap.Add(info.FullName, info);
  49. // }
  50. // var path = FileDefine.AssetbundleCfg.StreamingPath;
  51. // return Write(infosBinary, path);
  52. //}
  53. //public static bool SerializeAssetReferenceCfg()
  54. //{
  55. // var refManifestPath = PathDefine.ReferenceManifestPath;
  56. // var infosSo = AssetDatabase.LoadAssetAtPath<AssetReferenceInfosSo>(refManifestPath);
  57. // if (infosSo == null || infosSo.references == null)
  58. // {
  59. // Debug.Log($"没有找到配置文件或者内容为空。Path:{refManifestPath}");
  60. // return false;
  61. // }
  62. // var infosBinary = new AssetReferenceInfosBinary()
  63. // {
  64. // referencesMap = new System.Collections.Generic.Dictionary<string, string[]>()
  65. // };
  66. // foreach(var reference in infosSo.references)
  67. // {
  68. // infosBinary.referencesMap.Add(reference.assetName, reference.references);
  69. // }
  70. // var path = FileDefine.AssetReferenceCfg.StreamingPath;
  71. // return Write(infosBinary, path);
  72. //}
  73. //public static bool SerializeSceneInfoCfg()
  74. //{
  75. // var infosSoPath = PathDefine.SceneAssetBundleManifestPath;
  76. // var infosSo = AssetDatabase.LoadAssetAtPath<SceneInfosSo>(infosSoPath);
  77. // if (infosSo == null || infosSo.sceneInfos == null)
  78. // {
  79. // Debug.Log($"没有找到配置文件或者内容为空。Path:{infosSoPath}");
  80. // return false;
  81. // }
  82. // var infosBinary = new SceneInfosBinary()
  83. // {
  84. // sceneInfoMap = new System.Collections.Generic.Dictionary<string, string>()
  85. // };
  86. // foreach(var info in infosSo.sceneInfos)
  87. // {
  88. // infosBinary.sceneInfoMap.Add(info.sceneName, info.assetName);
  89. // }
  90. // var path = FileDefine.SceneInfoCfg.StreamingPath;
  91. // return Write(infosBinary, path);
  92. //}
  93. public static bool Write(ISerializable serializable, string filePath)
  94. {
  95. try
  96. {
  97. var write = new Writer();
  98. serializable.Serialize(write);
  99. var bytes = write.Finish();
  100. SerializationUtils.EncryptXor(ref bytes, 1023);
  101. var directoryPath = Path.GetDirectoryName(filePath);
  102. if (!Directory.Exists(directoryPath))
  103. {
  104. Directory.CreateDirectory(directoryPath);
  105. }
  106. File.WriteAllBytes(filePath, bytes);
  107. }
  108. catch(Exception ex)
  109. {
  110. Debug.LogException(ex);
  111. return false;
  112. }
  113. Debug.Log($"序列化二进制文件成功. Path:{filePath}");
  114. return true;
  115. }
  116. }
  117. }