FileSystemPackage.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Godot;
  5. using Zio;
  6. namespace TBL.GodotSharp.Content.Package
  7. {
  8. /// <summary>
  9. /// 基于文件系统的抽象内容包
  10. /// <para>会在内部自动检索配置文件来获取描述符和依赖项信息</para>
  11. /// </summary>
  12. public class FileSystemPackage : PackageNode
  13. {
  14. public override IFileSystem SelfFileSystem { get; }
  15. public sealed override Info SelfInfo { get; }
  16. public sealed override IEnumerable<DependencyInfo> Dependencies { get; }
  17. public FileSystemPackage(IFileSystem fileSystem)
  18. {
  19. Dependencies = null;
  20. var iniFound = false;
  21. foreach (var iniPath in fileSystem.EnumeratePaths(UPath.Root, "*.ini", SearchOption.TopDirectoryOnly, SearchTarget.File))
  22. {
  23. var ini = new ConfigFile();
  24. var err = ini.Parse(fileSystem.ReadAllText(iniPath));
  25. if (err != Error.Ok)
  26. throw new GodotException(err);
  27. iniFound = true;
  28. if (SelfInfo.Name.Empty() && ini.HasSection(nameof(SelfInfo)))
  29. {
  30. SelfInfo = new Info(
  31. ini.GetValue(nameof(SelfInfo), nameof(SelfInfo.Name)) as string,
  32. ini.GetValue(nameof(SelfInfo), nameof(SelfInfo.AuthorInfo)) as string,
  33. ini.GetValue(nameof(SelfInfo), nameof(SelfInfo.VersionInfo)) as string
  34. );
  35. }
  36. if (Dependencies == null && ini.HasSection(nameof(Dependencies)))
  37. {
  38. var dependencies = new List<DependencyInfo>();
  39. foreach (var dependencyName in ini.GetSectionKeys(nameof(Dependencies)))
  40. {
  41. var info = ini.GetValue(nameof(Dependencies), dependencyName) as Godot.Collections.Array;
  42. if (info == null || info.Count < 2)
  43. continue;
  44. var flags = DependencyInfo.FlagsEnum.None;
  45. if (info.Count > 2 && info[2] is string flagsInfo)
  46. {
  47. foreach (var flagInfo in flagsInfo.Split(';'))
  48. if (Enum.TryParse<DependencyInfo.FlagsEnum>(flagInfo.Trim(), out var flag))
  49. flags |= flag;
  50. }
  51. var dependency = new DependencyInfo(new Info(dependencyName, info[0] as string, info[1] as string), flags);
  52. dependencies.Add(dependency);
  53. }
  54. Dependencies = dependencies;
  55. }
  56. if (!SelfInfo.Name.Empty() &&
  57. Dependencies != null)
  58. {
  59. break;
  60. }
  61. }
  62. if (!iniFound)
  63. {
  64. QueueFree();
  65. throw new GodotException(Error.FileNotFound);
  66. }
  67. SelfFileSystem = fileSystem;
  68. }
  69. }
  70. }