Skip to content

Commit

Permalink
2.6.2 - Spam Timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ShayBox committed Feb 21, 2024
1 parent 27675bd commit 285b00e
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 278 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ members = [
]

[workspace.package]
version = "2.6.1"
version = "2.6.2"
authors = ["Shayne Hartford <[email protected]>"]
edition = "2021"
readme = "README.md"
Expand Down
83 changes: 42 additions & 41 deletions plugin-control/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use std::{collections::HashMap, net::UdpSocket};
use anyhow::Result;
use rosc::{decoder::MTU, OscMessage, OscPacket, OscType};
use windows::Media::{
Control::{
GlobalSystemMediaTransportControlsSession,
GlobalSystemMediaTransportControlsSessionManager as GSMTCSM,
},
Control::GlobalSystemMediaTransportControlsSessionManager as GSMTCSM,
MediaPlaybackAutoRepeatMode,
};

Expand All @@ -18,6 +15,7 @@ use windows::Media::{
#[tokio::main(flavor = "current_thread")]
pub async extern "Rust" fn load(socket: UdpSocket) -> Result<()> {
let manager = GSMTCSM::RequestAsync()?.await?;
let mut previous_parameters = HashMap::new();
let mut buf = [0u8; MTU];

loop {
Expand Down Expand Up @@ -85,47 +83,50 @@ pub async extern "Rust" fn load(socket: UdpSocket) -> Result<()> {
session.TryChangePlaybackPositionAsync(playback_position)
}
_ => {
let _ = try_sync_media_state(&socket, &session);
continue;
}
}?
.await?;
}
}

/// Try to synchronize the media session state to the `VRChat` menu parameters
fn try_sync_media_state(
socket: &UdpSocket,
session: &GlobalSystemMediaTransportControlsSession,
) -> Result<()> {
let playback_info = session.GetPlaybackInfo()?;
let mut parameters = HashMap::new();
let playback_info = session.GetPlaybackInfo()?;
let mut parameters = HashMap::new();

if let Ok(playback_status) = playback_info.PlaybackStatus() {
let play = OscType::Bool(playback_status.0 == 4);
if previous_parameters.get("Play") != Some(&play) {
parameters.insert("Play", play.clone());
previous_parameters.insert("Play", play);
}
}

if let Ok(playback_status) = playback_info.PlaybackStatus() {
parameters.insert("Play", OscType::Bool(playback_status.0 == 4));
}
if let Ok(shuffle_ref) = playback_info.IsShuffleActive() {
if let Ok(shuffle) = shuffle_ref.Value() {
let shuffle = OscType::Bool(shuffle);
if previous_parameters.get("Shuffle") != Some(&shuffle) {
parameters.insert("Shuffle", shuffle.clone());
previous_parameters.insert("Shuffle", shuffle);
}
}
}

if let Ok(shuffle_ref) = playback_info.IsShuffleActive() {
if let Ok(shuffle) = shuffle_ref.Value() {
parameters.insert("Shuffle", OscType::Bool(shuffle));
}
}
if let Ok(repeat_mode_ref) = playback_info.AutoRepeatMode() {
if let Ok(repeat_mode) = repeat_mode_ref.Value() {
let repeat = OscType::Int(repeat_mode.0);
if previous_parameters.get("Repeat") != Some(&repeat) {
parameters.insert("Repeat", repeat.clone());
previous_parameters.insert("Repeat", repeat);
}
}
}

if let Ok(repeat_mode_ref) = playback_info.AutoRepeatMode() {
if let Ok(repeat_mode) = repeat_mode_ref.Value() {
parameters.insert("Repeat", OscType::Int(repeat_mode.0));
}
}
for (param, arg) in parameters {
let packet = OscPacket::Message(OscMessage {
addr: format!("/avatar/parameters/VRCOSC/Media/{param}"),
args: vec![arg],
});

for (param, arg) in parameters {
let packet = OscPacket::Message(OscMessage {
addr: format!("/avatar/parameters/VRCOSC/Media/{param}"),
args: vec![arg],
});
let msg_buf = rosc::encoder::encode(&packet)?;
socket.send(&msg_buf)?;
}

let msg_buf = rosc::encoder::encode(&packet)?;
socket.send(&msg_buf)?;
continue;
}
}?
.await?;
}

Ok(())
}
1 change: 0 additions & 1 deletion plugin-spotify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition.workspace = true

[lib]
name = "spotify"
path = "lib.rs"
crate-type = ["cdylib"]

[dependencies]
Expand Down
85 changes: 85 additions & 0 deletions plugin-spotify/src/chatbox.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use anyhow::{bail, Context, Result};
use async_ffi::async_ffi;
use ferrispot::{
model::{playback::PlayingType, track::FullTrack},
prelude::*,
};
use terminal_link::Link;
use tokio::runtime::Handle;

use crate::{LYRICS, SPOTIFY};

#[no_mangle]
#[async_ffi(?Send)]
#[allow(clippy::unnecessary_wraps)]
#[allow(clippy::needless_pass_by_value)]
async extern "Rust" fn chat(
mut chatbox: String,
mut console: String,
handle: Handle,
) -> Result<(String, String)> {
let _enter = handle.enter();
let config = crate::config()?;
let spotify = SPOTIFY.get().context("Spotify is Authenticating...")?;
let mut lyrics = LYRICS.get().context("Lyrics is Authenticating...")?.clone();

let current_item = spotify
.currently_playing_item()
.send_async()
.await?
.context("None")?;

let public_item = current_item.public_playing_item().context("None")?;
let PlayingType::Track(track) = public_item.item() else {
bail!("None")
};

if config.enable_lyrics {
if let Ok(color_lyrics) = lyrics.get_color_lyrics(track.id().as_str()).await {
let words = color_lyrics
.lyrics
.lines
.iter()
.rev()
.try_find(|line| {
u64::try_from(public_item.progress().as_millis())
.map(|progress| line.start_time_ms < progress)
})
.iter()
.flatten()
.map(|line| line.words.clone())
.collect::<Vec<_>>()
.join(" ");

if !words.is_empty() && words != "♪" {
chatbox = words.clone();
console = words;

return Ok((chatbox, console));
}
};
}

replace(&mut chatbox, track);
replace(&mut console, track);

let href = track.external_urls().spotify.clone().context("None")?;
let link = Link::new(&console, &href);
Ok((chatbox, link.to_string()))
}

fn replace(message: &mut String, track: &FullTrack) {
let id = &track.id().to_string();
let song = track.name();
let artists = track
.artists()
.iter()
.map(CommonArtistInformation::name)
.collect::<Vec<_>>()
.join(", ");

*message = message
.replace("{id}", id)
.replace("{song}", song)
.replace("{artists}", &artists);
}
Loading

0 comments on commit 285b00e

Please sign in to comment.