using System; using System.Collections.Generic; using BlockOutReplica.Domain; using UnityEngine; namespace BlockOutReplica.Infrastructure { public static class OriginalLevelRepository { private const string LevelRoot = "OriginalLevels/"; private static IReadOnlyList cachedLevelIds; public static LevelDefinition LoadLevelOne() => Load("t140-level-1"); public static IReadOnlyList GetLevelIds() { if (cachedLevelIds != null) return cachedLevelIds; TextAsset[] assets = Resources.LoadAll("OriginalLevels"); List ids = new List(assets.Length); for (int i = 0; i < assets.Length; i++) ids.Add(assets[i].name); ids.Sort(CompareLevelIds); cachedLevelIds = ids; return cachedLevelIds; } public static LevelDefinition Load(string levelId) { if (string.IsNullOrWhiteSpace(levelId) || levelId.Contains("/") || levelId.Contains("\\")) throw new ArgumentException("Level id must be a plain resource name.", nameof(levelId)); string resourcePath = LevelRoot + levelId; TextAsset asset = Resources.Load(resourcePath); if (asset == null) throw new InvalidOperationException("Missing Resources/" + resourcePath + ".json"); return OriginalLevelJsonParser.Parse(asset.text); } private static int CompareLevelIds(string left, string right) { SplitLevelId(left, out string leftFamily, out int leftNumber); SplitLevelId(right, out string rightFamily, out int rightNumber); int family = string.CompareOrdinal(leftFamily, rightFamily); return family != 0 ? family : leftNumber.CompareTo(rightNumber); } private static void SplitLevelId(string id, out string family, out int number) { int marker = id.LastIndexOf("-level-", StringComparison.Ordinal); if (marker < 0 || !int.TryParse(id.Substring(marker + 7), out number)) { family = id; number = int.MaxValue; return; } family = id.Substring(0, marker); } } }