diff --git a/CustomTracks/Backgrounds/VideoBackground.cs b/CustomTracks/Backgrounds/VideoBackground.cs index 1c5a64b..b9d9778 100644 --- a/CustomTracks/Backgrounds/VideoBackground.cs +++ b/CustomTracks/Backgrounds/VideoBackground.cs @@ -14,7 +14,7 @@ public VideoBackground(string videoPath) { _videoPath = videoPath; } - + public override void SetUpBackground(BGController controller, GameObject bg) { DisableParts(bg); @@ -56,10 +56,6 @@ public override void OnResume(PauseContext ctx) public static IEnumerable PlayVideoDelayed(VideoPlayer videoPlayer) { yield return new WaitForSeconds(2.4f); - - if (videoPlayer != null) - { - videoPlayer.Play(); - } + videoPlayer?.Play(); } } \ No newline at end of file diff --git a/CustomTracks/ChartCompatibility.cs b/CustomTracks/ChartCompatibility.cs index dd93958..3e3f89e 100644 --- a/CustomTracks/ChartCompatibility.cs +++ b/CustomTracks/ChartCompatibility.cs @@ -36,7 +36,7 @@ public override int ReadJson(JsonReader reader, Type objectType, int existingVal if (reader.TokenType is JsonToken.Float) { var chartName = (string) serializer.Context.Context; - _logger.LogWarning($"Chart '{chartName}' has invalid type {reader.TokenType.ToString()} on field {_fieldName} (expected an integer)"); + _logger.LogWarning($"Chart '{chartName}' has invalid type {reader.TokenType} on field {_fieldName} (expected an integer)"); } return (int) Convert.ToDouble(reader.Value); @@ -44,7 +44,7 @@ public override int ReadJson(JsonReader reader, Type objectType, int existingVal if (reader.TokenType != JsonToken.Integer) { - throw new JsonException($"Expected number, got {reader.TokenType.ToString()}"); + throw new JsonException($"Expected number, got {reader.TokenType}"); } return Convert.ToInt32(reader.Value); diff --git a/CustomTracks/CustomTrack.cs b/CustomTracks/CustomTrack.cs index dcca86f..dcb28a1 100644 --- a/CustomTracks/CustomTrack.cs +++ b/CustomTracks/CustomTrack.cs @@ -38,7 +38,7 @@ public CustomTrack(string folderPath, CustomTrackData data, TrackLoader loader) public SavedLevel LoadChart() { - return _loader?.ShouldReloadChart() == true ? _loader.ReloadTrack(this) : _data.ToSavedLevel(); + return (bool)_loader?.ShouldReloadChart() ? _loader.ReloadTrack(this) : _data.ToSavedLevel(); } public LoadedTromboneTrack LoadTrack() @@ -65,9 +65,10 @@ public Coroutines.YieldTask> LoadClip() private AbstractBackground LoadBackground() { - if (File.Exists(Path.Combine(folderPath, "bg.trombackground"))) + var possibleBackgroundPath = Path.Combine(folderPath, "bg.trombackground"); + if (File.Exists(possibleBackgroundPath)) { - var bundle = AssetBundle.LoadFromFile(Path.Combine(folderPath, "bg.trombackground")); + var bundle = AssetBundle.LoadFromFile(possibleBackgroundPath); return new CustomBackground(bundle, folderPath); } diff --git a/CustomTracks/TrackLoader.cs b/CustomTracks/TrackLoader.cs index cdb7c01..c926957 100644 --- a/CustomTracks/TrackLoader.cs +++ b/CustomTracks/TrackLoader.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.Serialization; @@ -9,65 +10,65 @@ namespace TrombLoader.CustomTracks; -public class TrackLoader: TrackRegistrationEvent.Listener +public class TrackLoader : TrackRegistrationEvent.Listener { - private JsonSerializer _serializer = new(); - public IEnumerable OnRegisterTracks() { + Stopwatch sw = Stopwatch.StartNew(); CreateMissingDirectories(); - var songs = Directory.GetFiles(Globals.GetCustomSongsPath(), "song.tmb", SearchOption.AllDirectories) - .Concat(Directory.GetFiles(BepInEx.Paths.PluginPath, "song.tmb", SearchOption.AllDirectories)) - .Select(i => Path.GetDirectoryName(i)); + List tracks = new List(); - var seen = new HashSet(); - foreach (var songFolder in songs) - { - var chartPath = Path.Combine(songFolder, Globals.defaultChartName); - var chartName = Path.GetFileName(songFolder.TrimEnd('/')); - if (!File.Exists(chartPath)) continue; + //Non recursive method is faster but requires stricter custom songs folder structure + //var songDirectories = Directory.GetDirectories(Globals.GetCustomSongsPath()); - using var stream = File.OpenText(chartPath); - using var reader = new JsonTextReader(stream); + //Only recursively check inside of the custom songs folder + var tmbDirectories = Directory.GetFiles(Globals.GetCustomSongsPath(), "song.tmb", SearchOption.AllDirectories); + var seen = new HashSet(); + //For some reasons more thread ends up being much slower even than single threading, but using a low thread count makes it much faster + for(int i = 0; i < tmbDirectories.Length; i++) + { CustomTrackData customLevel; + string dirName; try { - _serializer.Context = new StreamingContext(StreamingContextStates.File, chartName); - customLevel = _serializer.Deserialize(reader); + dirName = Path.GetDirectoryName(tmbDirectories[i]); + var chartName = dirName.TrimEnd('/'); + customLevel = JsonConvert.DeserializeObject(File.ReadAllText(tmbDirectories[i]), new JsonSerializerSettings() + { + Context = new StreamingContext(StreamingContextStates.File, chartName) + }) ?? throw new Exception("Deserializer returned unexpected null value."); } catch (Exception exc) { - Plugin.LogWarning($"Unable to deserialize JSON of custom chart: {chartPath}"); + Plugin.LogWarning($"Unable to deserialize JSON of custom chart: {tmbDirectories[i]}"); Plugin.LogWarning(exc.Message); continue; } - if (customLevel == null) continue; - if (seen.Add(customLevel.trackRef)) { Plugin.LogDebug($"Found custom chart: {customLevel.trackRef}"); - yield return new CustomTrack(songFolder, customLevel, this); + tracks.Add(new CustomTrack(dirName, customLevel, this)); } else { Plugin.LogWarning( - $"Skipping folder {chartPath} as its trackref '{customLevel.trackRef}' was already loaded!"); + $"Skipping folder {dirName} as its trackref '{customLevel.trackRef}' was already loaded!"); } - } + }; + sw.Stop(); + Plugin.LogInfo($"{tracks.Count} charts were loaded in {sw.Elapsed.TotalMilliseconds:0.00}ms"); + return tracks.Where(x => x != null); } public SavedLevel ReloadTrack(CustomTrack existing) { var chartPath = Path.Combine(existing.folderPath, Globals.defaultChartName); - using var stream = File.OpenText(chartPath); - using var reader = new JsonTextReader(stream); - - var track = _serializer.Deserialize(reader); + var track = JsonConvert.DeserializeObject(File.ReadAllText(chartPath)); return track?.ToSavedLevel(); }