Skip to content

Commit

Permalink
Add custom track data API
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgehog1029 committed Jun 17, 2024
1 parent 60dcd5c commit c98e589
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
12 changes: 12 additions & 0 deletions CustomTracks/CustomTrack.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO;
using BaboonAPI.Hooks.Tracks;
using BaboonAPI.Utility;
using JetBrains.Annotations;
using Microsoft.FSharp.Core;
using TrombLoader.CustomTracks.Backgrounds;
using TrombLoader.Helpers;
Expand Down Expand Up @@ -36,6 +37,17 @@ public CustomTrack(string folderPath, CustomTrackData data, TrackLoader loader)
_loader = loader;
}

/// <summary>
/// Get custom data for an identifier
/// </summary>
/// <param name="identifier">Key for the data property</param>
/// <returns>Custom data for the specified identifier, if present on the track, otherwise null</returns>
[CanBeNull]
public ExtraData GetCustomData(Identifier identifier)
{
return _data.custom_data.TryGetValue(identifier, out var data) ? new ExtraData(identifier, data) : null;
}

public SavedLevel LoadChart()
{
return _loader.LoadChartData(folderPath, _data);
Expand Down
4 changes: 3 additions & 1 deletion CustomTracks/CustomTrackData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace TrombLoader.CustomTracks;

Expand All @@ -18,4 +18,6 @@ public class CustomTrackData
[JsonRequired] public int difficulty;
[JsonRequired] public float tempo;
public string backgroundMovement = "none";

public Dictionary<Identifier, JObject> custom_data = new();
}
19 changes: 19 additions & 0 deletions CustomTracks/ExtraData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json.Linq;

namespace TrombLoader.CustomTracks;

public class ExtraData
{
private Identifier _identifier;
private JObject _contents;

public ExtraData(Identifier identifier, JObject contents)
{
_identifier = identifier;
_contents = contents;
}

public Identifier Identifier => _identifier;
public JObject GetJsonObject() => _contents;
public JToken this[string key] => _contents[key];
}
54 changes: 54 additions & 0 deletions CustomTracks/Identifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace TrombLoader.CustomTracks;

public class Identifier
{
private static readonly Regex IdentifierRegex = new(@"^([a-zA-Z_-]+):([a-zA-Z0-9_-]+)$");

public string Namespace { get; }
public string Path { get; }

public Identifier(string @namespace, string path)
{
Namespace = @namespace;
Path = path;
}

[JsonConstructor]
public Identifier(string identifier)
{
var m = IdentifierRegex.Match(identifier);
if (!m.Success) throw new ArgumentException("Invalid identifier string: " + identifier);

Namespace = m.Captures[0].Value;
Path = m.Captures[1].Value;
}

public override string ToString() => $"{Namespace}:{Path}";

public override int GetHashCode()
{
unchecked
{
return (Namespace.GetHashCode() * 397) ^ Path.GetHashCode();
}
}

protected bool Equals(Identifier other)
{
return Namespace == other.Namespace && Path == other.Path;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Identifier)obj);
}

public static Identifier Parse(string identifier) => new(identifier);
}

0 comments on commit c98e589

Please sign in to comment.