SongFolderEntries.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using SongCore.OverrideClasses;
  2. using SongCore.Utilities;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Xml.Linq;
  8. using System.Collections.Concurrent;
  9. namespace SongCore.Data
  10. {
  11. public enum FolderLevelPack { CustomLevels, CustomWIPLevels, NewPack, CachedWIPLevels };
  12. [Serializable]
  13. public class SongFolderEntry
  14. {
  15. public string Name;
  16. public string Path;
  17. public FolderLevelPack Pack;
  18. public string ImagePath;
  19. public bool WIP;
  20. public bool CacheZIPs;
  21. public SongFolderEntry(string name, string path, FolderLevelPack pack, string imagePath = "", bool wip = false, bool cachezips = false)
  22. {
  23. Name = name;
  24. Path = path;
  25. Pack = pack;
  26. ImagePath = imagePath;
  27. WIP = wip;
  28. CacheZIPs = cachezips;
  29. }
  30. }
  31. public class SeperateSongFolder
  32. {
  33. public SongFolderEntry SongFolderEntry { get; private set; }
  34. public ConcurrentDictionary<string, CustomPreviewBeatmapLevel> Levels = new ConcurrentDictionary<string, CustomPreviewBeatmapLevel>();
  35. public SongCoreCustomLevelCollection LevelCollection { get; private set; } = null;
  36. public SongCoreCustomBeatmapLevelPack LevelPack { get; private set; } = null;
  37. public SeperateSongFolder CacheFolder { get; private set; } = null;
  38. public SeperateSongFolder(SongFolderEntry folderEntry, SeperateSongFolder cacheFolder = null)
  39. {
  40. SongFolderEntry = folderEntry;
  41. CacheFolder = cacheFolder;
  42. if (folderEntry.Pack == FolderLevelPack.NewPack)
  43. {
  44. LevelCollection = new SongCoreCustomLevelCollection(Levels.Values.ToArray());
  45. UnityEngine.Sprite image = UI.BasicUI.FolderIcon;
  46. if (!string.IsNullOrEmpty(folderEntry.ImagePath))
  47. {
  48. try
  49. {
  50. var packImage = SongCore.Utilities.Utils.LoadSpriteFromFile(folderEntry.ImagePath);
  51. if (packImage != null)
  52. {
  53. image = packImage;
  54. }
  55. }
  56. catch
  57. {
  58. SongCore.Utilities.Logging.Log($"Failed to Load Image For Seperate Folder \"{folderEntry.Name}\"");
  59. }
  60. }
  61. LevelPack = new SongCoreCustomBeatmapLevelPack(CustomLevelLoader.kCustomLevelPackPrefixId + folderEntry.Name, folderEntry.Name, image, LevelCollection);
  62. }
  63. }
  64. public SeperateSongFolder(SongFolderEntry folderEntry, UnityEngine.Sprite Image)
  65. {
  66. SongFolderEntry = folderEntry;
  67. if (folderEntry.Pack == FolderLevelPack.NewPack)
  68. {
  69. LevelCollection = new SongCoreCustomLevelCollection(Levels.Values.ToArray());
  70. LevelPack = new SongCoreCustomBeatmapLevelPack(CustomLevelLoader.kCustomLevelPackPrefixId + folderEntry.Name, folderEntry.Name, Image, LevelCollection);
  71. }
  72. }
  73. public static List<SeperateSongFolder> ReadSeperateFoldersFromFile(string filePath)
  74. {
  75. List<SeperateSongFolder> result = new List<SeperateSongFolder>();
  76. try
  77. {
  78. XDocument file = XDocument.Load(filePath);
  79. foreach (var item in file.Root.Elements())
  80. {
  81. // Console.WriteLine("Element Name: " + item.Name);
  82. string name = item.Element("Name").Value;
  83. if (name == "Example") continue;
  84. string path = item.Element("Path").Value;
  85. int pack = int.Parse(item.Element("Pack").Value);
  86. string imagePath = "";
  87. var image = item.Element("ImagePath");
  88. if (image != null)
  89. {
  90. imagePath = image.Value;
  91. }
  92. bool isWIP = false;
  93. var wip = item.Element("WIP");
  94. if (wip != null)
  95. {
  96. isWIP = bool.Parse(wip.Value);
  97. }
  98. bool zipCaching = false;
  99. var cachezips = item.Element("CacheZIPs");
  100. if (cachezips != null)
  101. {
  102. zipCaching = bool.Parse(cachezips.Value);
  103. }
  104. SongFolderEntry entry = new SongFolderEntry(name, path, (FolderLevelPack)pack, imagePath, isWIP, zipCaching);
  105. // Console.WriteLine("Entry");
  106. // Console.WriteLine(" " + entry.Name);
  107. // Console.WriteLine(" " + entry.Path);
  108. // Console.WriteLine(" " + entry.Pack);
  109. // Console.WriteLine(" " + entry.WIP);
  110. SeperateSongFolder cachedSeperate = null;
  111. if (zipCaching)
  112. {
  113. FolderLevelPack cachePack;
  114. if ((FolderLevelPack)pack == FolderLevelPack.CustomWIPLevels) cachePack = FolderLevelPack.CachedWIPLevels;
  115. else cachePack = FolderLevelPack.NewPack;
  116. SongFolderEntry cachedSongFolderEntry = new SongFolderEntry(String.Concat("Cached ", name), Path.Combine(path, "Cache"), cachePack, imagePath, isWIP, false);
  117. cachedSeperate = new SeperateSongFolder(cachedSongFolderEntry);
  118. }
  119. SeperateSongFolder seperate = new SeperateSongFolder(entry, cachedSeperate);
  120. result.Add(seperate);
  121. if (cachedSeperate != null) result.Add(cachedSeperate);
  122. }
  123. }
  124. catch
  125. {
  126. Utilities.Logging.Log("Error Reading folders.xml! Make sure the file is properly formatted.", IPA.Logging.Logger.Level.Warning);
  127. }
  128. return result;
  129. }
  130. }
  131. public class ModSeperateSongFolder : SeperateSongFolder
  132. {
  133. public bool AlwaysShow { get; set; } = true;
  134. public ModSeperateSongFolder(SongFolderEntry folderEntry) : base(folderEntry)
  135. {
  136. }
  137. public ModSeperateSongFolder(SongFolderEntry folderEntry, UnityEngine.Sprite Image) : base(folderEntry, Image)
  138. {
  139. }
  140. }
  141. }