SongData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using SongCore.Arcache;
  7. using UnityEngine;
  8. namespace SongCore.Data
  9. {
  10. [Serializable]
  11. public class ExtraSongData
  12. {
  13. public Contributor[] contributors; //convert legacy mappers/lighters fields into contributors
  14. public string _customEnvironmentName;
  15. public string _customEnvironmentHash;
  16. public DifficultyData[] _difficulties;
  17. public string _defaultCharacteristic = null;
  18. [Serializable]
  19. public class Contributor
  20. {
  21. public string _role;
  22. public string _name;
  23. public string _iconPath;
  24. [NonSerialized]
  25. public Sprite icon = null;
  26. }
  27. [Serializable]
  28. public class DifficultyData
  29. {
  30. public string _beatmapCharacteristicName;
  31. public BeatmapDifficulty _difficulty;
  32. public string _difficultyLabel;
  33. public RequirementData additionalDifficultyData;
  34. public MapColor _colorLeft;
  35. public MapColor _colorRight;
  36. public MapColor _envColorLeft;
  37. public MapColor _envColorRight;
  38. public MapColor _envColorLeftBoost;
  39. public MapColor _envColorRightBoost;
  40. public MapColor _obstacleColor;
  41. }
  42. [Serializable]
  43. public class RequirementData
  44. {
  45. public string[] _requirements;
  46. public string[] _suggestions;
  47. public string[] _warnings;
  48. public string[] _information;
  49. }
  50. [Serializable]
  51. public class MapColor
  52. {
  53. public float r;
  54. public float g;
  55. public float b;
  56. public MapColor(float r, float g, float b)
  57. {
  58. this.r = r;
  59. this.g = g;
  60. this.b = b;
  61. }
  62. }
  63. public ExtraSongData()
  64. {
  65. }
  66. [Newtonsoft.Json.JsonConstructor]
  67. public ExtraSongData(string levelID, Contributor[] contributors, string customEnvironmentName, string customEnvironmentHash, DifficultyData[] difficulties)
  68. {
  69. // Utilities.Logging.Log("SongData full Ctor");
  70. this.contributors = contributors;
  71. this._customEnvironmentName = customEnvironmentName;
  72. this._customEnvironmentHash = customEnvironmentHash;
  73. this._difficulties = difficulties;
  74. }
  75. public ExtraSongData(string levelID, string songPath)
  76. {
  77. // Utilities.Logging.Log("SongData Ctor");
  78. try
  79. {
  80. if (!ArcacheLoader.ArchiveExists(songPath + "/info.dat")) return;
  81. var infoText = ArcacheLoader.ReadJson(songPath + "/info.dat");
  82. JObject info = JObject.Parse(infoText);
  83. JObject infoData;
  84. List<Contributor> levelContributors = new List<Contributor>();
  85. //Check if song uses legacy value for full song One Saber mode
  86. if (info.TryGetValue("_customData", out var data))
  87. {
  88. infoData = (JObject)data;
  89. if (infoData.TryGetValue("_contributors", out var contributors))
  90. {
  91. levelContributors.AddRange(contributors.ToObject<Contributor[]>());
  92. }
  93. if (infoData.TryGetValue("_customEnvironment", out var customEnvironment))
  94. _customEnvironmentName = (string)customEnvironment;
  95. if (infoData.TryGetValue("_customEnvironmentHash", out var envHash))
  96. _customEnvironmentHash = (string)envHash;
  97. if (infoData.TryGetValue("_defaultCharacteristic", out var defaultChar))
  98. _defaultCharacteristic = (string)defaultChar;
  99. }
  100. contributors = levelContributors.ToArray();
  101. List<DifficultyData> diffData = new List<DifficultyData>();
  102. JArray diffSets = (JArray)info["_difficultyBeatmapSets"];
  103. foreach (JObject diffSet in diffSets)
  104. {
  105. string SetCharacteristic = (string)diffSet["_beatmapCharacteristicName"];
  106. JArray diffBeatmaps = (JArray)diffSet["_difficultyBeatmaps"];
  107. foreach (JObject diffBeatmap in diffBeatmaps)
  108. {
  109. List<string> diffRequirements = new List<string>();
  110. List<string> diffSuggestions = new List<string>();
  111. List<string> diffWarnings = new List<string>();
  112. List<string> diffInfo = new List<string>();
  113. string diffLabel = "";
  114. MapColor diffLeft = null;
  115. MapColor diffRight = null;
  116. MapColor diffEnvLeft = null;
  117. MapColor diffEnvRight = null;
  118. MapColor diffEnvLeftBoost = null;
  119. MapColor diffEnvRightBoost = null;
  120. MapColor diffObstacle = null;
  121. BeatmapDifficulty diffDifficulty = Utilities.Utils.ToEnum((string)diffBeatmap["_difficulty"], BeatmapDifficulty.Normal);
  122. JObject beatmapData;
  123. if (diffBeatmap.TryGetValue("_customData", out var customData))
  124. {
  125. beatmapData = (JObject)customData;
  126. if (beatmapData.TryGetValue("_difficultyLabel", out var difficultyLabel))
  127. diffLabel = (string)difficultyLabel;
  128. //Get difficulty json fields
  129. if (beatmapData.TryGetValue("_colorLeft", out var colorLeft))
  130. {
  131. if (colorLeft.Children().Count() == 3)
  132. {
  133. diffLeft = new MapColor(0, 0, 0);
  134. diffLeft.r = (float)beatmapData["_colorLeft"]["r"];
  135. diffLeft.g = (float)beatmapData["_colorLeft"]["g"];
  136. diffLeft.b = (float)beatmapData["_colorLeft"]["b"];
  137. }
  138. }
  139. if (beatmapData.TryGetValue("_colorRight", out var colorRight))
  140. {
  141. if (colorRight.Children().Count() == 3)
  142. {
  143. diffRight = new MapColor(0, 0, 0);
  144. diffRight.r = (float)beatmapData["_colorRight"]["r"];
  145. diffRight.g = (float)beatmapData["_colorRight"]["g"];
  146. diffRight.b = (float)beatmapData["_colorRight"]["b"];
  147. }
  148. }
  149. if (beatmapData.TryGetValue("_envColorLeft", out var envColorLeft))
  150. {
  151. if (envColorLeft.Children().Count() == 3)
  152. {
  153. diffEnvLeft = new MapColor(0, 0, 0);
  154. diffEnvLeft.r = (float)beatmapData["_envColorLeft"]["r"];
  155. diffEnvLeft.g = (float)beatmapData["_envColorLeft"]["g"];
  156. diffEnvLeft.b = (float)beatmapData["_envColorLeft"]["b"];
  157. }
  158. }
  159. if (beatmapData.TryGetValue("_envColorRight", out var envColorRight))
  160. {
  161. if (envColorRight.Children().Count() == 3)
  162. {
  163. diffEnvRight = new MapColor(0, 0, 0);
  164. diffEnvRight.r = (float)beatmapData["_envColorRight"]["r"];
  165. diffEnvRight.g = (float)beatmapData["_envColorRight"]["g"];
  166. diffEnvRight.b = (float)beatmapData["_envColorRight"]["b"];
  167. }
  168. }
  169. if (beatmapData.TryGetValue("_envColorLeftBoost", out var envColorLeftBoost))
  170. {
  171. if (envColorLeftBoost.Children().Count() == 3)
  172. {
  173. diffEnvLeftBoost = new MapColor(0, 0, 0);
  174. diffEnvLeftBoost.r = (float)beatmapData["_envColorLeftBoost"]["r"];
  175. diffEnvLeftBoost.g = (float)beatmapData["_envColorLeftBoost"]["g"];
  176. diffEnvLeftBoost.b = (float)beatmapData["_envColorLeftBoost"]["b"];
  177. }
  178. }
  179. if (beatmapData.TryGetValue("_envColorRightBoost", out var envColorRightBoost))
  180. {
  181. if (envColorRightBoost.Children().Count() == 3)
  182. {
  183. diffEnvRightBoost = new MapColor(0, 0, 0);
  184. diffEnvRightBoost.r = (float)beatmapData["_envColorRightBoost"]["r"];
  185. diffEnvRightBoost.g = (float)beatmapData["_envColorRightBoost"]["g"];
  186. diffEnvRightBoost.b = (float)beatmapData["_envColorRightBoost"]["b"];
  187. }
  188. }
  189. if (beatmapData.TryGetValue("_obstacleColor", out var obColor))
  190. {
  191. if (obColor.Children().Count() == 3)
  192. {
  193. diffObstacle = new MapColor(0, 0, 0);
  194. diffObstacle.r = (float)beatmapData["_obstacleColor"]["r"];
  195. diffObstacle.g = (float)beatmapData["_obstacleColor"]["g"];
  196. diffObstacle.b = (float)beatmapData["_obstacleColor"]["b"];
  197. }
  198. }
  199. if (beatmapData.TryGetValue("_warnings", out var warnings))
  200. diffWarnings.AddRange(((JArray)warnings).Select(c => (string)c));
  201. if (beatmapData.TryGetValue("_information", out var information))
  202. diffInfo.AddRange(((JArray)information).Select(c => (string)c));
  203. if (beatmapData.TryGetValue("_suggestions", out var suggestions))
  204. diffSuggestions.AddRange(((JArray)suggestions).Select(c => (string)c));
  205. if (beatmapData.TryGetValue("_requirements", out var requirements))
  206. diffRequirements.AddRange(((JArray)requirements).Select(c => (string)c));
  207. }
  208. RequirementData diffReqData = new RequirementData
  209. {
  210. _requirements = diffRequirements.ToArray(),
  211. _suggestions = diffSuggestions.ToArray(),
  212. _information = diffInfo.ToArray(),
  213. _warnings = diffWarnings.ToArray()
  214. };
  215. diffData.Add(new DifficultyData
  216. {
  217. _beatmapCharacteristicName = SetCharacteristic,
  218. _difficulty = diffDifficulty,
  219. _difficultyLabel = diffLabel,
  220. additionalDifficultyData = diffReqData,
  221. _colorLeft = diffLeft,
  222. _colorRight = diffRight,
  223. _envColorLeft = diffEnvLeft,
  224. _envColorRight = diffEnvRight,
  225. _envColorLeftBoost = diffEnvLeftBoost,
  226. _envColorRightBoost = diffEnvRightBoost,
  227. _obstacleColor = diffObstacle
  228. });
  229. }
  230. }
  231. _difficulties = diffData.ToArray();
  232. }
  233. catch (Exception ex)
  234. {
  235. Utilities.Logging.Log($"Error in Level {songPath}: \n {ex}", IPA.Logging.Logger.Level.Error);
  236. }
  237. }
  238. }
  239. }