|
@@ -0,0 +1,81 @@
|
|
|
|
+using System;
|
|
|
|
+using System.IO;
|
|
|
|
+using Godot;
|
|
|
|
+
|
|
|
|
+namespace TBL.GodotSharp.Content;
|
|
|
|
+
|
|
|
|
+/// <summary>
|
|
|
|
+/// 内容模块扩展功能类
|
|
|
|
+/// </summary>
|
|
|
|
+public static class Content
|
|
|
|
+{
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 考虑依赖项打开文件流
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="package">要打开的内容包</param>
|
|
|
|
+ /// <param name="path">要打开的路径</param>
|
|
|
|
+ /// <returns>打开的文件流</returns>
|
|
|
|
+ public static Stream OpenFileStreamWithDependency(
|
|
|
|
+ this Package package, string path)
|
|
|
|
+ {
|
|
|
|
+ var stream = package.OpenFileStream(path);
|
|
|
|
+
|
|
|
|
+ // 直接打开成功的场合
|
|
|
|
+ if (stream != null)
|
|
|
|
+ return stream;
|
|
|
|
+
|
|
|
|
+ // 在同级节点中寻找依赖项
|
|
|
|
+ Node container;
|
|
|
|
+ if (null != (container = package.GetParent()))
|
|
|
|
+ {
|
|
|
|
+ for (int i = 0, count = container.GetChildCount();
|
|
|
|
+ i < count;
|
|
|
|
+ i++)
|
|
|
|
+ {
|
|
|
|
+ if (container.GetChild(i) is not Package dependency)
|
|
|
|
+ continue;
|
|
|
|
+ // 遍历依赖项信息
|
|
|
|
+ foreach (var info in package.Dependencies)
|
|
|
|
+ {
|
|
|
|
+ // 符合依赖项的包将会被试图追溯
|
|
|
|
+ if (dependency.SelfInfo.IsCompatibleWith(info))
|
|
|
|
+ {
|
|
|
|
+ stream = dependency.OpenFileStreamWithDependency(path);
|
|
|
|
+
|
|
|
|
+ // 直接打开成功的场合
|
|
|
|
+ if (stream != null)
|
|
|
|
+ return stream;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 打开失败的场合
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 判断 <paramref name="actualVersion"/> 能否满足 <paramref name="targetVersion"/> 的版本要求
|
|
|
|
+ /// <para>用于内容包之间的依赖可用性检测</para>
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="actualVersion">当前版本</param>
|
|
|
|
+ /// <param name="targetVersion">要求的版本</param>
|
|
|
|
+ /// <param name="level">兼容级别</param>
|
|
|
|
+ public static bool IsCompatibleWith(Version actualVersion, Version targetVersion, int level)
|
|
|
|
+ {
|
|
|
|
+ // 主版本号
|
|
|
|
+ if (actualVersion.Major != targetVersion.Major)
|
|
|
|
+ return false;
|
|
|
|
+ if (level < 1)
|
|
|
|
+ return true;
|
|
|
|
+ // 次版本号
|
|
|
|
+ if (actualVersion.Minor != targetVersion.Minor)
|
|
|
|
+ return false;
|
|
|
|
+ if (level < 2)
|
|
|
|
+ return true;
|
|
|
|
+ // 构建版本号
|
|
|
|
+ if (actualVersion.Build != targetVersion.Build)
|
|
|
|
+ return false;
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+}
|