Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix casting error when using a child class of LavaTrack #144

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions src/Node/LavaNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ await OnUpdateReceived.Invoke(new UpdateEventArg<TLavaPlayer, TLavaTrack> {

var lavaTrack = default(TLavaTrack);
if (root.TryGetProperty("track", out var trackElement)) {
lavaTrack = (TLavaTrack)TrackDecoder.Decode($"{trackElement}");
lavaTrack = TrackDecoder.Decode($"{trackElement}", player.Track);
}

var type = $"{root.GetProperty("type")}";
Expand Down Expand Up @@ -534,4 +534,4 @@ private Task OnVoiceServerUpdatedAsync(SocketVoiceServer voiceServer) {
});
}
}
}
}
11 changes: 5 additions & 6 deletions src/Player/Decoder/TrackDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public readonly struct TrackDecoder {
/// Decodes the hash for the specified track.
/// </summary>
/// <param name="trackHash">Track's hash.</param>
/// <param name="track">LavaTrack</param>
/// <returns>
/// <see cref="LavaTrack" />
/// </returns>
public static LavaTrack Decode(string trackHash) {
public static TLavaTrack Decode<TLavaTrack>(string trackHash, TLavaTrack track) where TLavaTrack : LavaTrack {
Span<byte> hashBuffer = stackalloc byte[trackHash.Length];
Encoding.ASCII.GetBytes(trackHash, hashBuffer);
Base64.DecodeFromUtf8InPlace(hashBuffer, out var bytesWritten);
Expand All @@ -33,8 +34,8 @@ public static LavaTrack Decode(string trackHash) {
? javaReader.Read<sbyte>()
: 1;

var track = new LavaTrack(
trackHash,
return LavaTrack.Initialize(track,
hash: trackHash,
title: javaReader.ReadString(),
author: javaReader.ReadString(),
duration: javaReader.Read<long>(),
Expand All @@ -46,8 +47,6 @@ public static LavaTrack Decode(string trackHash) {
position: default,
canSeek: true,
source: default);

return track;
}

/// <summary>
Expand Down Expand Up @@ -116,4 +115,4 @@ public static Task<IEnumerable<LavaTrack>> DecodeAsync(NodeConfiguration nodeCon
VictoriaExtensions.LavaTrackConverter);
}
}
}
}
28 changes: 27 additions & 1 deletion src/Player/LavaTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,32 @@
Source = lavaTrack.Source;
}

internal static TLavaTrack Initialize<TLavaTrack>(TLavaTrack? lavaTrack,

Check warning on line 121 in src/Player/LavaTrack.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 121 in src/Player/LavaTrack.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
string hash, string id, string title, string author,
string url, TimeSpan position, long duration,
bool canSeek, bool isStream, string source) where TLavaTrack : LavaTrack
{
if (lavaTrack is null)
return (TLavaTrack)Activator.CreateInstance(
typeof(TLavaTrack),
new LavaTrack(hash, id, title, author, url, position, duration, canSeek, isStream, source));

lavaTrack.Hash = hash ?? throw new ArgumentNullException(nameof(hash));
lavaTrack.Id = id ?? throw new ArgumentNullException(nameof(id));
lavaTrack.Title = title ?? throw new ArgumentNullException(nameof(title));
lavaTrack.Author = author ?? throw new ArgumentNullException(nameof(author));
lavaTrack.Url = url ?? throw new ArgumentNullException(nameof(url));
lavaTrack.Position = position;
lavaTrack.Duration = duration < TimeSpan.MaxValue.Ticks
? TimeSpan.FromMilliseconds(duration)
: TimeSpan.MaxValue;
lavaTrack.CanSeek = canSeek;
lavaTrack.IsStream = isStream;
lavaTrack.Source = source;

return lavaTrack;
}

/// <summary>
///
/// </summary>
Expand All @@ -133,4 +159,4 @@
return $"{Author} {Title}";
}
}
}
}
Loading