WIP: Themes v2 #24
No files matched your search
+13
-13
@@ -1329,24 +1329,24 @@ pub struct PresenceButton {
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PresenceActivityPayload {
|
||||
pub state: Option<String>,
|
||||
pub details: Option<String>,
|
||||
pub large_image_key: Option<String>,
|
||||
pub large_image_text: Option<String>,
|
||||
pub small_image_key: Option<Option<String>>,
|
||||
pub small_image_text: Option<Option<String>>,
|
||||
pub button1: Option<Option<presence::PresenceButton>>,
|
||||
pub button2: Option<Option<presence::PresenceButton>>,
|
||||
pub show_timestamp: Option<bool>,
|
||||
pub state: String,
|
||||
pub details: String,
|
||||
pub large_image_key: String,
|
||||
pub large_image_text: String,
|
||||
pub small_image_key: Option<String>,
|
||||
pub small_image_text: Option<String>,
|
||||
pub button1: Option<presence::PresenceButton>,
|
||||
pub button2: Option<presence::PresenceButton>,
|
||||
pub show_timestamp: bool,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn presence_update_activity(activity: PresenceActivityPayload) {
|
||||
presence::update_activity(
|
||||
activity.state.as_deref(),
|
||||
activity.details.as_deref(),
|
||||
activity.large_image_key.as_deref(),
|
||||
activity.large_image_text.as_deref(),
|
||||
&activity.state,
|
||||
&activity.details,
|
||||
&activity.large_image_key,
|
||||
&activity.large_image_text,
|
||||
activity.small_image_key,
|
||||
activity.small_image_text,
|
||||
activity.button1,
|
||||
|
||||
+47
-64
@@ -4,6 +4,7 @@ use std::sync::Mutex as StdMutex;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use tauri::{AppHandle, RunEvent};
|
||||
use tokio::sync::{mpsc, oneshot};
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio::time::{Duration, interval};
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
@@ -57,19 +58,15 @@ enum PresenceCommand {
|
||||
IsConnected(oneshot::Sender<bool>),
|
||||
}
|
||||
|
||||
enum ReconnectResult {
|
||||
Connected(Box<AsyncDiscordIpcClient>),
|
||||
Failed,
|
||||
}
|
||||
|
||||
struct PresenceActor {
|
||||
receiver: mpsc::Receiver<PresenceCommand>,
|
||||
reconnect_rx: mpsc::Receiver<ReconnectResult>,
|
||||
reconnect_tx: mpsc::Sender<ReconnectResult>,
|
||||
reconnect_rx: mpsc::Receiver<Box<AsyncDiscordIpcClient>>,
|
||||
reconnect_tx: mpsc::Sender<Box<AsyncDiscordIpcClient>>,
|
||||
reconnect_task: Option<JoinHandle<()>>,
|
||||
client: Option<AsyncDiscordIpcClient>,
|
||||
data: PresenceData,
|
||||
start_timestamp: i64,
|
||||
reconnecting: bool,
|
||||
should_connect: bool,
|
||||
}
|
||||
|
||||
impl PresenceActor {
|
||||
@@ -85,10 +82,11 @@ impl PresenceActor {
|
||||
receiver,
|
||||
reconnect_rx,
|
||||
reconnect_tx,
|
||||
reconnect_task: None,
|
||||
client: None,
|
||||
data: PresenceData::default(),
|
||||
start_timestamp: start,
|
||||
reconnecting: false,
|
||||
should_connect: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,18 +110,13 @@ impl PresenceActor {
|
||||
}
|
||||
}
|
||||
|
||||
Some(result) = self.reconnect_rx.recv() => {
|
||||
self.reconnecting = false;
|
||||
match result {
|
||||
ReconnectResult::Connected(client) => {
|
||||
Some(client) = self.reconnect_rx.recv() => {
|
||||
self.reconnect_task = None;
|
||||
if self.should_connect {
|
||||
log::info!(target: "launcher", "Presence: reconnect succeeded");
|
||||
self.client = Some(*client);
|
||||
self.push_presence().await;
|
||||
}
|
||||
ReconnectResult::Failed => {
|
||||
log::warn!(target: "launcher", "Presence: all reconnect attempts exhausted");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_ = heartbeat.tick() => {
|
||||
@@ -134,15 +127,19 @@ impl PresenceActor {
|
||||
}
|
||||
|
||||
async fn cmd_connect(&mut self, resp: oneshot::Sender<bool>) {
|
||||
self.should_connect = true;
|
||||
if self.client.is_some() {
|
||||
let _ = resp.send(true);
|
||||
return;
|
||||
}
|
||||
if self.reconnect_task.is_some() {
|
||||
let _ = resp.send(false);
|
||||
return;
|
||||
}
|
||||
|
||||
match try_connect_once().await {
|
||||
Some(client) => {
|
||||
self.client = Some(client);
|
||||
self.reconnecting = false;
|
||||
self.push_presence().await;
|
||||
let _ = resp.send(true);
|
||||
}
|
||||
@@ -158,6 +155,10 @@ impl PresenceActor {
|
||||
}
|
||||
|
||||
async fn cmd_disconnect(&mut self, resp: oneshot::Sender<()>) {
|
||||
self.should_connect = false;
|
||||
if let Some(task) = self.reconnect_task.take() {
|
||||
task.abort();
|
||||
}
|
||||
if let Some(mut client) = self.client.take() {
|
||||
let _ = client.clear_activity().await;
|
||||
log::info!(target: "launcher", "Presence: disconnected");
|
||||
@@ -206,37 +207,37 @@ impl PresenceActor {
|
||||
e
|
||||
);
|
||||
self.client = None;
|
||||
self.should_connect = true;
|
||||
self.spawn_reconnect_task();
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_reconnect_task(&mut self) {
|
||||
if self.reconnecting {
|
||||
if self.reconnect_task.is_some() || !self.should_connect {
|
||||
return;
|
||||
}
|
||||
self.reconnecting = true;
|
||||
|
||||
let tx = self.reconnect_tx.clone();
|
||||
tokio::spawn(async move {
|
||||
const MAX_ATTEMPTS: u32 = 5;
|
||||
for attempt in 1..=MAX_ATTEMPTS {
|
||||
let delay = Duration::from_secs(5 * attempt as u64);
|
||||
let task = tokio::spawn(async move {
|
||||
let mut attempt: u32 = 0;
|
||||
loop {
|
||||
attempt = attempt.saturating_add(1);
|
||||
let delay = Duration::from_secs((5 * u64::from(attempt)).min(30));
|
||||
log::info!(
|
||||
target: "launcher",
|
||||
"Presence: reconnect attempt {}/{} in {:?}…",
|
||||
"Presence: reconnect attempt {} in {:?}…",
|
||||
attempt,
|
||||
MAX_ATTEMPTS,
|
||||
delay
|
||||
);
|
||||
tokio::time::sleep(delay).await;
|
||||
|
||||
if let Some(client) = try_connect_once().await {
|
||||
let _ = tx.send(ReconnectResult::Connected(Box::new(client))).await;
|
||||
let _ = tx.send(Box::new(client)).await;
|
||||
return;
|
||||
}
|
||||
}
|
||||
let _ = tx.send(ReconnectResult::Failed).await;
|
||||
});
|
||||
self.reconnect_task = Some(task);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,45 +303,27 @@ pub async fn has_presence() -> bool {
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn update_activity(
|
||||
state: Option<&str>,
|
||||
details: Option<&str>,
|
||||
large_image_key: Option<&str>,
|
||||
large_image_text: Option<&str>,
|
||||
small_image_key: Option<Option<String>>,
|
||||
small_image_text: Option<Option<String>>,
|
||||
button1: Option<Option<PresenceButton>>,
|
||||
button2: Option<Option<PresenceButton>>,
|
||||
show_timestamp: Option<bool>,
|
||||
state: &str,
|
||||
details: &str,
|
||||
large_image_key: &str,
|
||||
large_image_text: &str,
|
||||
small_image_key: Option<String>,
|
||||
small_image_text: Option<String>,
|
||||
button1: Option<PresenceButton>,
|
||||
button2: Option<PresenceButton>,
|
||||
show_timestamp: bool,
|
||||
) {
|
||||
let data = {
|
||||
let mut guard = PRESENCE_DATA.lock().unwrap();
|
||||
if let Some(s) = state {
|
||||
guard.state = s.to_string();
|
||||
}
|
||||
if let Some(d) = details {
|
||||
guard.details = d.to_string();
|
||||
}
|
||||
if let Some(img) = large_image_key {
|
||||
guard.large_image_key = img.to_string();
|
||||
}
|
||||
if let Some(txt) = large_image_text {
|
||||
guard.large_image_text = txt.to_string();
|
||||
}
|
||||
if let Some(key) = small_image_key {
|
||||
guard.small_image_key = key;
|
||||
}
|
||||
if let Some(text) = small_image_text {
|
||||
guard.small_image_text = text;
|
||||
}
|
||||
if let Some(b1) = button1 {
|
||||
guard.dynamic_button = b1;
|
||||
}
|
||||
if let Some(b2) = button2 {
|
||||
guard.secondary_button = b2;
|
||||
}
|
||||
if let Some(ts) = show_timestamp {
|
||||
guard.show_timestamp = ts;
|
||||
}
|
||||
guard.state = state.to_string();
|
||||
guard.details = details.to_string();
|
||||
guard.large_image_key = large_image_key.to_string();
|
||||
guard.large_image_text = large_image_text.to_string();
|
||||
guard.small_image_key = small_image_key;
|
||||
guard.small_image_text = small_image_text;
|
||||
guard.dynamic_button = button1;
|
||||
guard.secondary_button = button2;
|
||||
guard.show_timestamp = show_timestamp;
|
||||
guard.clone()
|
||||
};
|
||||
|
||||
|
||||
@@ -12,13 +12,11 @@
|
||||
DISCORD_PRESENCE_PRESETS,
|
||||
interpolatePresenceString,
|
||||
loadDiscordPresenceConfig,
|
||||
pushIdlePresenceFromConfig,
|
||||
saveDiscordPresenceConfig,
|
||||
type DiscordPresenceConfig,
|
||||
type PresenceVariableContext,
|
||||
} from '$lib/discordPresenceConfig';
|
||||
import { discordPresence, launcherVersion } from '$lib/global';
|
||||
import * as presence from '$lib/presence';
|
||||
import { currentUser } from '$lib/userAuthentication';
|
||||
import {
|
||||
Check,
|
||||
@@ -206,10 +204,14 @@
|
||||
user?: Parameters<typeof currentUser.set>[0];
|
||||
info?: Parameters<typeof currentUserInfo.set>[0];
|
||||
launcherVersion?: string;
|
||||
discordPresence?: boolean;
|
||||
}>('sync-theme-editor-global-data', (event) => {
|
||||
if (event.payload?.user) currentUser.set(event.payload.user);
|
||||
if (event.payload?.info) currentUserInfo.set(event.payload.info);
|
||||
if (event.payload?.launcherVersion) launcherVersion.set(event.payload.launcherVersion);
|
||||
if (typeof event.payload?.discordPresence === 'boolean') {
|
||||
discordPresence.set(event.payload.discordPresence);
|
||||
}
|
||||
});
|
||||
await emit('request-theme-editor-global-data');
|
||||
} catch (err) {
|
||||
@@ -469,18 +471,9 @@
|
||||
try {
|
||||
await saveDiscordPresenceConfig(workingConfig);
|
||||
initialConfigJson = JSON.stringify(workingConfig);
|
||||
if ($discordPresence && (await presence.isConnected())) {
|
||||
await pushIdlePresenceFromConfig(
|
||||
{
|
||||
user: $currentUser,
|
||||
version: $launcherVersion || undefined,
|
||||
rank: simRank,
|
||||
pp: simPp,
|
||||
accuracy: simAccuracy,
|
||||
},
|
||||
workingConfig
|
||||
);
|
||||
}
|
||||
// The main window owns the live player stats and its presence config
|
||||
// store, so let it refresh and push the updated activity immediately.
|
||||
await emit('discord-presence-config-saved');
|
||||
sileo.success({ title: 'Discord Rich Presence Saved', description: 'Activity updated' });
|
||||
await handleClose();
|
||||
} catch (err) {
|
||||
|
||||
@@ -247,6 +247,8 @@ export type IdlePresenceParams = {
|
||||
rank?: number | string;
|
||||
pp?: number | string;
|
||||
accuracy?: number | string;
|
||||
mode?: string;
|
||||
action?: string;
|
||||
};
|
||||
|
||||
export type PlayingPresenceParams = {
|
||||
@@ -282,6 +284,8 @@ export const pushIdlePresenceFromConfig = async (
|
||||
rank: params?.rank,
|
||||
pp: params?.pp,
|
||||
accuracy: params?.accuracy,
|
||||
mode: params?.mode,
|
||||
action: params?.action ?? 'In Launcher',
|
||||
version: params?.version,
|
||||
profileUrl: params?.user?.id ? `https://ez-pp.farm/u/${params.user.id}` : 'https://ez-pp.farm',
|
||||
};
|
||||
|
||||
+9
-9
@@ -6,15 +6,15 @@ export type PresenceButtonPayload = {
|
||||
};
|
||||
|
||||
export type PresenceActivityPayload = {
|
||||
state?: string;
|
||||
details?: string;
|
||||
largeImageKey?: string;
|
||||
largeImageText?: string;
|
||||
smallImageKey?: string | null;
|
||||
smallImageText?: string | null;
|
||||
button1?: PresenceButtonPayload | null;
|
||||
button2?: PresenceButtonPayload | null;
|
||||
showTimestamp?: boolean;
|
||||
state: string;
|
||||
details: string;
|
||||
largeImageKey: string;
|
||||
largeImageText: string;
|
||||
smallImageKey: string | null;
|
||||
smallImageText: string | null;
|
||||
button1: PresenceButtonPayload | null;
|
||||
button2: PresenceButtonPayload | null;
|
||||
showTimestamp: boolean;
|
||||
};
|
||||
|
||||
export const connect = async () => await invoke('presence_connect');
|
||||
|
||||
@@ -248,6 +248,7 @@
|
||||
osuStream: get(osuStream),
|
||||
serverPing: get(serverPing),
|
||||
launcherVersion: get(launcherVersion),
|
||||
discordPresence: get(discordPresence),
|
||||
});
|
||||
} catch {}
|
||||
};
|
||||
|
||||
+53
-22
@@ -21,6 +21,7 @@
|
||||
import { config } from '$lib/config';
|
||||
import { currentUserInfo } from '$lib/data';
|
||||
import {
|
||||
discordPresenceCustomConfig,
|
||||
loadDiscordPresenceConfig,
|
||||
pushIdlePresenceFromConfig,
|
||||
pushPlayingPresenceFromConfig,
|
||||
@@ -90,7 +91,6 @@
|
||||
startOpenTabletDriver,
|
||||
stopOpenTabletDriver,
|
||||
} from '$lib/osuUtil';
|
||||
import * as presence from '$lib/presence';
|
||||
import {
|
||||
downloadAndInstallCommunityTheme,
|
||||
fetchCommunityThemeDetails,
|
||||
@@ -297,6 +297,10 @@
|
||||
let activeThemesTab = $state<'library' | 'workshop'>('library');
|
||||
let themeSavedUnlisten: (() => void) | undefined;
|
||||
let themePublishedUnlisten: (() => void) | undefined;
|
||||
let presenceConfigUnlisten: (() => void) | undefined;
|
||||
let presenceConfigReady = $state(false);
|
||||
let idlePresenceTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
let lastIdlePresenceSignature = '';
|
||||
let refreshingThemes = $state(false);
|
||||
let reloadingActiveTheme = $state(false);
|
||||
let revealedNsfw = $state<string[]>([]);
|
||||
@@ -1125,6 +1129,46 @@
|
||||
};
|
||||
|
||||
const currentStats = $derived($currentUserInfo?.stats?.[selectedGamemode]);
|
||||
const currentPresenceContext = () => ({
|
||||
rank: currentStats?.rank,
|
||||
pp: currentStats?.pp,
|
||||
accuracy: currentStats ? `${currentStats.acc.toFixed(2)}%` : undefined,
|
||||
mode: getGamemodeName(modeIntToStr(selectedMode), typeIntToStr(selectedType)),
|
||||
action: 'In Launcher',
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (!$discordPresence || !presenceConfigReady || $launching) {
|
||||
lastIdlePresenceSignature = '';
|
||||
return;
|
||||
}
|
||||
|
||||
const user = $currentUser;
|
||||
const version = $launcherVersion || undefined;
|
||||
const context = currentPresenceContext();
|
||||
const cfg = $discordPresenceCustomConfig;
|
||||
const signature = JSON.stringify({
|
||||
userId: user?.id,
|
||||
username: user?.name,
|
||||
version,
|
||||
context,
|
||||
config: cfg,
|
||||
});
|
||||
if (signature === lastIdlePresenceSignature) return;
|
||||
|
||||
idlePresenceTimer = setTimeout(() => {
|
||||
idlePresenceTimer = undefined;
|
||||
lastIdlePresenceSignature = signature;
|
||||
void pushIdlePresenceFromConfig({ user, version, ...context }, cfg);
|
||||
}, 100);
|
||||
|
||||
return () => {
|
||||
if (idlePresenceTimer !== undefined) {
|
||||
clearTimeout(idlePresenceTimer);
|
||||
idlePresenceTimer = undefined;
|
||||
}
|
||||
};
|
||||
});
|
||||
let recentActivityIndex = $state(0);
|
||||
const userEvents = $derived($currentUserInfo?.events ?? []);
|
||||
const safeEventIndex = $derived(
|
||||
@@ -1584,9 +1628,7 @@
|
||||
let presenceUpdater: number | undefined = undefined;
|
||||
const beatmapCoverValidity = new SvelteMap<string, boolean>();
|
||||
|
||||
const isPresenceConnected = await presence.isConnected();
|
||||
|
||||
if ($discordPresence && isPresenceConnected) {
|
||||
if ($discordPresence) {
|
||||
let osuDetected = false;
|
||||
presenceUpdater = window.setInterval(async () => {
|
||||
if (!osuDetected) {
|
||||
@@ -1709,14 +1751,6 @@
|
||||
fadeGlobalVolume(0, $custom_theme_volume, 1500);
|
||||
if (presenceUpdater) {
|
||||
window.clearInterval(presenceUpdater);
|
||||
try {
|
||||
await pushIdlePresenceFromConfig({
|
||||
user: $currentUser,
|
||||
version: $launcherVersion || undefined,
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
await new Promise((res) => setTimeout(res, 1000));
|
||||
/* await replaceUIFiles(osuPath, true); */
|
||||
@@ -2009,18 +2043,14 @@
|
||||
}
|
||||
})();
|
||||
|
||||
loadDiscordPresenceConfig().then((cfg) => {
|
||||
if ($discordPresence) {
|
||||
pushIdlePresenceFromConfig(
|
||||
{
|
||||
user: $currentUser,
|
||||
version: $launcherVersion || undefined,
|
||||
},
|
||||
cfg
|
||||
).catch(() => {});
|
||||
}
|
||||
loadDiscordPresenceConfig().then(() => {
|
||||
presenceConfigReady = true;
|
||||
});
|
||||
|
||||
listen('discord-presence-config-saved', async () => {
|
||||
await loadDiscordPresenceConfig();
|
||||
}).then((unlisten) => (presenceConfigUnlisten = unlisten));
|
||||
|
||||
animate(ezppLogo, {
|
||||
opacity: [0, 1],
|
||||
scale: [0.95, 1],
|
||||
@@ -2075,6 +2105,7 @@
|
||||
window.clearInterval(animateInterval);
|
||||
themeSavedUnlisten?.();
|
||||
themePublishedUnlisten?.();
|
||||
presenceConfigUnlisten?.();
|
||||
stopThemeScript();
|
||||
};
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user