Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions CustomTracks/Backgrounds/VideoBackground.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public VideoBackground(string videoPath)
{
_videoPath = videoPath;
}

public override void SetUpBackground(BGController controller, GameObject bg)
{
DisableParts(bg);
Expand Down Expand Up @@ -56,10 +56,6 @@ public override void OnResume(PauseContext ctx)
public static IEnumerable<YieldInstruction> PlayVideoDelayed(VideoPlayer videoPlayer)
{
yield return new WaitForSeconds(2.4f);

if (videoPlayer != null)
{
videoPlayer.Play();
}
videoPlayer?.Play();
}
}
4 changes: 2 additions & 2 deletions CustomTracks/ChartCompatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ 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);
}

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);
Expand Down
7 changes: 4 additions & 3 deletions CustomTracks/CustomTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -65,9 +65,10 @@ public Coroutines.YieldTask<FSharpResult<TrackAudio, string>> 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);
}

Expand Down
53 changes: 27 additions & 26 deletions CustomTracks/TrackLoader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
Expand All @@ -9,65 +10,65 @@

namespace TrombLoader.CustomTracks;

public class TrackLoader: TrackRegistrationEvent.Listener
public class TrackLoader : TrackRegistrationEvent.Listener
{
private JsonSerializer _serializer = new();

public IEnumerable<TromboneTrack> 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<TromboneTrack> tracks = new List<TromboneTrack>();

var seen = new HashSet<string>();
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<string>();

//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<CustomTrackData>(reader);
dirName = Path.GetDirectoryName(tmbDirectories[i]);
var chartName = dirName.TrimEnd('/');
customLevel = JsonConvert.DeserializeObject<CustomTrackData>(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<CustomTrackData>(reader);

var track = JsonConvert.DeserializeObject<CustomTrackData>(File.ReadAllText(chartPath));
return track?.ToSavedLevel();
}

Expand Down