Content.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.IO;
  3. using Godot;
  4. namespace TBL.GodotSharp.Content;
  5. /// <summary>
  6. /// 内容模块扩展功能类
  7. /// </summary>
  8. public static class Content
  9. {
  10. /// <summary>
  11. /// 考虑依赖项打开文件流
  12. /// </summary>
  13. /// <param name="package">要打开的内容包</param>
  14. /// <param name="path">要打开的路径</param>
  15. /// <returns>打开的文件流</returns>
  16. public static Stream OpenFileStreamWithDependency(
  17. this Package package, string path)
  18. {
  19. var stream = package.OpenFileStream(path);
  20. // 直接打开成功的场合
  21. if (stream != null)
  22. return stream;
  23. // 在同级节点中寻找依赖项
  24. Node container;
  25. if (null != (container = package.GetParent()))
  26. {
  27. for (int i = 0, count = container.GetChildCount();
  28. i < count;
  29. i++)
  30. {
  31. if (container.GetChild(i) is not Package dependency)
  32. continue;
  33. // 遍历依赖项信息
  34. foreach (var info in package.Dependencies)
  35. {
  36. // 符合依赖项的包将会被试图追溯
  37. if (dependency.SelfInfo.IsCompatibleWith(info))
  38. {
  39. stream = dependency.OpenFileStreamWithDependency(path);
  40. // 直接打开成功的场合
  41. if (stream != null)
  42. return stream;
  43. }
  44. }
  45. }
  46. }
  47. // 打开失败的场合
  48. return null;
  49. }
  50. /// <summary>
  51. /// 判断 <paramref name="actualVersion"/> 能否满足 <paramref name="targetVersion"/> 的版本要求
  52. /// <para>用于内容包之间的依赖可用性检测</para>
  53. /// </summary>
  54. /// <param name="actualVersion">当前版本</param>
  55. /// <param name="targetVersion">要求的版本</param>
  56. /// <param name="level">兼容级别</param>
  57. public static bool IsCompatibleWith(Version actualVersion, Version targetVersion, int level)
  58. {
  59. // 主版本号
  60. if (actualVersion.Major != targetVersion.Major)
  61. return false;
  62. if (level < 1)
  63. return true;
  64. // 次版本号
  65. if (actualVersion.Minor != targetVersion.Minor)
  66. return false;
  67. if (level < 2)
  68. return true;
  69. // 构建版本号
  70. if (actualVersion.Build != targetVersion.Build)
  71. return false;
  72. return true;
  73. }
  74. }