AssetUtils.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using XGame.Editor.Asset;
  4. namespace XGame.Editor.Build
  5. {
  6. static class AssetUtils
  7. {
  8. /// <summary>
  9. /// 海报资源标签
  10. /// </summary>
  11. public const string PlacardTag = "/placard/";
  12. /// <summary>
  13. /// 内置资源标签(必须保留在安装包内部)
  14. /// </summary>
  15. public static string[] InnerAssetsTags => new string[] { "/XGame/", "/launch/", PlacardTag };
  16. /// <summary>
  17. /// 忽略文件
  18. /// </summary>
  19. public static string[] IgnoreFiles = new string[] { ".manifest", ".pdb", ".gitkeep", "buildlogtep.json" };
  20. /// <summary>
  21. /// 判断一个资源是否为内置资源
  22. /// </summary>
  23. /// <param name="assetPath">资源路径</param>
  24. /// <param name="innerAssetsTags">内置资源标签,为空使用默认值</param>
  25. /// <returns></returns>
  26. public static bool IsInnerAsset(string assetPath, params string[] innerAssetsTags)
  27. {
  28. if (null == innerAssetsTags) innerAssetsTags = InnerAssetsTags;
  29. assetPath = assetPath.ToLower();
  30. foreach (var tag in innerAssetsTags)
  31. {
  32. if (!assetPath.Contains(tag)) continue;
  33. return true;
  34. }
  35. return false;
  36. }
  37. /// <summary>
  38. /// 判断资源文件是否为忽略文件
  39. /// </summary>
  40. /// <param name="path"></param>
  41. /// <param name="ignorFiles"></param>
  42. /// <returns></returns>
  43. public static bool IsIgnorFile(string path, string[] ignorFiles = null)
  44. {
  45. var ext = Path.GetExtension(path);
  46. if (string.IsNullOrEmpty(ext))
  47. {
  48. //文件没有后缀
  49. BuildLog.Warn($"[CmdSubPackage] [BuildHelperControl] Unknown file extensions. Path: {path}");
  50. return true;
  51. }
  52. if (null == ignorFiles) ignorFiles = IgnoreFiles;
  53. foreach (var ignor in ignorFiles)
  54. {
  55. if (path.EndsWith(ignor, System.StringComparison.OrdinalIgnoreCase))
  56. {
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62. /// <summary>
  63. /// 获取相对路径
  64. /// </summary>
  65. /// <param name="absolutePath">绝对路径</param>
  66. /// <param name="relativeDir">相对目录</param>
  67. /// <returns></returns>
  68. public static string GetRelativePath(string absolutePath, string relativeDir = null)
  69. {
  70. if (string.IsNullOrEmpty(absolutePath))
  71. return absolutePath;
  72. absolutePath = absolutePath.Replace('\\', '/');
  73. if (string.IsNullOrEmpty(relativeDir)) relativeDir = Framework.Define.PathDefine.WorkPath;
  74. relativeDir = relativeDir.Replace('\\', '/');
  75. int index = absolutePath.LastIndexOf(relativeDir);
  76. if (index < 0)
  77. return absolutePath;
  78. if (relativeDir.EndsWith("/"))
  79. return absolutePath.Substring(index + relativeDir.Length);
  80. return absolutePath.Substring(index + relativeDir.Length + 1);
  81. }
  82. ///// <summary>
  83. ///// 获取二进制文件的校验码
  84. ///// </summary>
  85. ///// <param name="filePath">文件路径</param>
  86. ///// <param name="checkCodeType">校验码类型</param>
  87. ///// <returns></returns>
  88. //public static string GetBinaryCheckCode(string filePath, ECheckCodeType checkCodeType)
  89. //{
  90. // string result;
  91. // switch (checkCodeType)
  92. // {
  93. // case ECheckCodeType.MD5:
  94. // default:
  95. // result = KailashNative.GetMD5(filePath);
  96. // break;
  97. // }
  98. // if (string.IsNullOrEmpty(result))
  99. // BuildLog.Error("[CmdSubPackage] [BuildHelperControl] GetFileCheckCode find invalid {0}, file path is {1}.", checkCodeType, filePath);
  100. // return result;
  101. //}
  102. #region collect assets
  103. /// <summary>
  104. /// 收集所有资源信息
  105. /// </summary>
  106. /// <returns></returns>
  107. public static Dictionary<string, string> CollectionAllAssets()
  108. {
  109. Dictionary<string, string> assets = new Dictionary<string, string>();
  110. var platformNames = System.Enum.GetNames(typeof(Framework.PlatformType));
  111. CollectionAssetsInternal(Framework.Define.PathDefine.WorkPath, 0, ref assets);
  112. return assets;
  113. }
  114. private static void CollectionAssetsInternal(string root, int platformTag, ref Dictionary<string, string> assets)
  115. {
  116. string[] directories = Directory.GetDirectories(root);
  117. foreach (var dir in directories)
  118. {
  119. var subPlatformTag = platformTag;
  120. var dirPath = dir.Replace("\\", "/");
  121. if (subPlatformTag != 1)
  122. subPlatformTag = FileUtil.VerifyPlatformPath(dirPath);
  123. if (subPlatformTag == -1)
  124. continue;
  125. CollectionAssetsInternal(dirPath, subPlatformTag, ref assets);
  126. }
  127. string[] files = Directory.GetFiles(root);
  128. foreach (var file in files)
  129. {
  130. var filePath = file.Replace("\\", "/");
  131. if (IsIgnorFile(filePath)) continue;
  132. string relativePath = GetRelativePath(filePath);
  133. if (platformTag == 1)
  134. {
  135. relativePath = relativePath.Replace(PlatformUtil.ActivePlatform.ToString() + '/', string.Empty);
  136. }
  137. assets.Add(filePath, relativePath);
  138. }
  139. }
  140. #endregion
  141. }
  142. }