using System;
using System.IO;
using Godot;
namespace TBL.GodotSharp.Content;
///
/// 内容模块扩展功能类
///
public static class Content
{
///
/// 考虑依赖项打开文件流
///
/// 要打开的内容包
/// 要打开的路径
/// 打开的文件流
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;
}
///
/// 判断 能否满足 的版本要求
/// 用于内容包之间的依赖可用性检测
///
/// 当前版本
/// 要求的版本
/// 兼容级别
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;
}
}