|
@@ -0,0 +1,76 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using Godot;
|
|
|
+using Zio;
|
|
|
+
|
|
|
+namespace TBL.GodotSharp.Content.Package
|
|
|
+{
|
|
|
+ /// <summary>
|
|
|
+ /// 基于文件系统的抽象内容包
|
|
|
+ /// <para>会在内部自动检索配置文件来获取描述符和依赖项信息</para>
|
|
|
+ /// </summary>
|
|
|
+ public class FileSystemPackage : PackageNode
|
|
|
+ {
|
|
|
+ public override IFileSystem SelfFileSystem { get; }
|
|
|
+
|
|
|
+ public sealed override Info SelfInfo { get; }
|
|
|
+
|
|
|
+ public sealed override IEnumerable<DependencyInfo> Dependencies { get; }
|
|
|
+
|
|
|
+ public FileSystemPackage(IFileSystem fileSystem)
|
|
|
+ {
|
|
|
+ Dependencies = null;
|
|
|
+ var iniFound = false;
|
|
|
+ foreach (var iniPath in fileSystem.EnumeratePaths(UPath.Root, "*.ini", SearchOption.TopDirectoryOnly, SearchTarget.File))
|
|
|
+ {
|
|
|
+ var ini = new ConfigFile();
|
|
|
+ var err = ini.Parse(fileSystem.ReadAllText(iniPath));
|
|
|
+ if (err != Error.Ok)
|
|
|
+ throw new GodotException(err);
|
|
|
+
|
|
|
+ iniFound = true;
|
|
|
+
|
|
|
+ if (SelfInfo.Name.Empty() && ini.HasSection(nameof(SelfInfo)))
|
|
|
+ {
|
|
|
+ SelfInfo = new Info(
|
|
|
+ ini.GetValue(nameof(SelfInfo), nameof(SelfInfo.Name)) as string,
|
|
|
+ ini.GetValue(nameof(SelfInfo), nameof(SelfInfo.AuthorInfo)) as string,
|
|
|
+ ini.GetValue(nameof(SelfInfo), nameof(SelfInfo.VersionInfo)) as string
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Dependencies == null && ini.HasSection(nameof(Dependencies)))
|
|
|
+ {
|
|
|
+ var dependencies = new List<DependencyInfo>();
|
|
|
+ foreach (var dependencyName in ini.GetSectionKeys(nameof(Dependencies)))
|
|
|
+ {
|
|
|
+ var info = ini.GetValue(nameof(Dependencies), dependencyName) as Godot.Collections.Array;
|
|
|
+ if (info == null || info.Count < 3)
|
|
|
+ continue;
|
|
|
+ var flags = DependencyInfo.FlagsEnum.None;
|
|
|
+ if (info.Count > 3 && info[3] is string flagsInfo)
|
|
|
+ {
|
|
|
+ foreach (var flagInfo in flagsInfo.Split(';'))
|
|
|
+ if (Enum.TryParse<DependencyInfo.FlagsEnum>(flagInfo.Trim(), out var flag))
|
|
|
+ flags |= flag;
|
|
|
+ }
|
|
|
+ var dependency = new DependencyInfo(new Info(info[0] as string, info[1] as string, info[2] as string), flags);
|
|
|
+ dependencies.Add(dependency);
|
|
|
+ }
|
|
|
+ Dependencies = dependencies;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!SelfInfo.Name.Empty() &&
|
|
|
+ Dependencies != null)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!iniFound)
|
|
|
+ throw new GodotException(Error.FileNotFound);
|
|
|
+ SelfFileSystem = fileSystem;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|