123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using BeatLyrics.Common;
- using BeatLyrics.Tool.Models;
- using BeatLyrics.Tool.Utils;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace BeatLyrics.Tool.DataProvider.BeatMaps
- {
- internal static class LocalCustomLevelProvider
- {
- public static LevelInfo[] GetAlLevelInfos(Action<int, int> reportProgress = null)
- {
- var bsDir = DirProvider.BeatSaberDir;
- if (null == bsDir) throw new InvalidOperationException("`BeatSaberDir' NotFound");
- var levelsDir = Path.Combine(bsDir, "Beat Saber_Data", "CustomLevels");
- if (false == Directory.Exists(levelsDir)) throw new DirectoryNotFoundException($"Dir `{levelsDir}' NotFound");
- var dirs = Directory.GetDirectories(levelsDir);
- var ret = new List<LevelInfo>(dirs.Length);
- var loadedCount = 0;
- Parallel.For(0, dirs.Length, index =>
- {
- var dir = dirs[index];
- var item = new LevelInfo { Directory = dir, DirectoryTime = new DirectoryInfo(dir).CreationTime };
- try
- {
- var infoPath = Path.Combine(dir, "info.dat");
- if (File.Exists(infoPath))
- {
- var toHashData = new MemoryStream();
- var infoData = File.ReadAllBytes(infoPath);
- toHashData.Write(infoData, 0, infoData.Length);
- var infoJson = JsonConvert.DeserializeAnonymousType(Encoding.UTF8.GetString(infoData), new
- {
- _coverImageFilename = (string)null,
- _songFilename = (string)null,
- _songName = (string)null,
- _songSubName = (string)null,
- _songAuthorName = (string)null,
- _levelAuthorName = (string)null,
- _difficultyBeatmapSets = new[]
- {
- new
- {
- _difficultyBeatmaps=new []
- {
- new
- {
- _beatmapFilename=(string)null
- }
- }
- }
- },
- });
- item.CoverPath = Path.Combine(dir, infoJson._coverImageFilename);
- item.MediaPath = Path.Combine(dir, infoJson._songFilename);
- item.SongName = infoJson._songName;
- item.SongSubName = infoJson._songSubName;
- item.SongAuthor = infoJson._songAuthorName;
- item.LevelAuthor = infoJson._levelAuthorName;
- var toHash =
- infoJson._difficultyBeatmapSets.SelectMany(p =>
- p._difficultyBeatmaps.Select(r => File.ReadAllBytes(Path.Combine(dir, r._beatmapFilename))))
- .Aggregate(toHashData, (n, p) =>
- {
- n.Write(p, 0, p.Length);
- return n;
- });
- item.Hash = Hasher.Sha1Hex(toHash.ToArray());
- item.ReloadLocalLyricInfo();
- item.IsValid = true;
- }
- else
- {
- item.Error = "file `info.dat' not found";
- }
- }
- catch (Exception e)
- {
- item.Error = e.ToString();
- }
- ret.Add(item);
- reportProgress?.Invoke(dirs.Length, Interlocked.Increment(ref loadedCount));
- });
- return ret.ToArray();
- }
- public static void ReloadLocalLyricInfo(this LevelInfo item)
- {
- var defaultFilePath = DirProvider.GetDefaultFile(item.Hash);
- var defaultFileName = "";
- if (File.Exists(defaultFilePath)) defaultFileName = File.ReadAllText(defaultFilePath);
- var lsDir = DirProvider.GetLyricDir(item.Hash);
- if (Directory.Exists(lsDir))
- {
- item.LocalLyrics = Directory.GetFiles(lsDir).Select(p =>
- {
- var fileName = Path.GetFileName(p);
- return new LocalLyricInfo
- {
- FileName = fileName,
- IsDefault = fileName == defaultFileName
- };
- }).ToArray();
- }
- }
- }
- }
|