Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
VolcanicArts committed Dec 19, 2022
2 parents f684134 + 36fcdf2 commit 189c15e
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 99 deletions.
4 changes: 2 additions & 2 deletions VRCOSC.Desktop/VRCOSC.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<ApplicationIcon>game.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Version>0.0.0</Version>
<FileVersion>2022.1218.0</FileVersion>
<FileVersion>2022.1219.0</FileVersion>
<Title>VRCOSC</Title>
<Authors>VolcanicArts</Authors>
<Company>VolcanicArts</Company>
<Nullable>enable</Nullable>
<AssemblyVersion>2022.1218.0</AssemblyVersion>
<AssemblyVersion>2022.1219.0</AssemblyVersion>
</PropertyGroup>
<ItemGroup Label="Project References">
<ProjectReference Include="..\VRCOSC.Game\VRCOSC.Game.csproj" />
Expand Down
15 changes: 8 additions & 7 deletions VRCOSC.Game/Modules/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace VRCOSC.Game.Modules;

public partial class GameManager : CompositeComponent
{
private const double openvr_check_interval = 1000;
private const double vrchat_process_check_interval = 5000;
private const int startstop_delay = 250;

Expand Down Expand Up @@ -71,13 +72,7 @@ protected override void UpdateAfterChildren()

protected override void LoadComplete()
{
void initOpenVR() => Task.Run(() =>
{
OpenVRInterface.Init();
Scheduler.Add(initOpenVR);
});

Scheduler.Add(initOpenVR);
Scheduler.AddDelayed(checkForOpenVR, openvr_check_interval, true);
Scheduler.AddDelayed(checkForVRChat, vrchat_process_check_interval, true);

State.BindValueChanged(e => Logger.Log($"{nameof(GameManager)} state changed to {e.NewValue}"));
Expand Down Expand Up @@ -180,6 +175,12 @@ private bool initialiseOscClient()
}
}

private void checkForOpenVR() => Task.Run(() =>
{
static bool isOpenVROpen() => Process.GetProcessesByName("vrmonitor").Any();
if (isOpenVROpen()) OpenVRInterface.Init();
});

private void checkForVRChat()
{
if (!configManager.Get<bool>(VRCOSCSetting.AutoStartStop)) return;
Expand Down
24 changes: 11 additions & 13 deletions VRCOSC.Game/Modules/Modules/HardwareStats/HardwareStatsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed partial class HardwareStatsModule : ChatBoxModule
public override string Description => "Sends hardware stats and displays them in the ChatBox";
public override string Author => "VolcanicArts";
public override ModuleType Type => ModuleType.General;
protected override int DeltaUpdate => 2000;
protected override int DeltaUpdate => 500;

protected override bool DefaultChatBoxDisplay => true;
protected override string DefaultChatBoxFormat => "CPU: $cpuusage$% | GPU: $gpuusage$% RAM: $ramused$GB/$ramtotal$GB";
Expand All @@ -36,17 +36,15 @@ protected override void CreateAttributes()
{
if (!(hardwareStatsProvider?.CanAcceptQueries ?? false)) return null;

hardwareStatsProvider?.Update();

return GetSetting<string>(ChatBoxSetting.ChatBoxFormat)
.Replace("$cpuusage$", hardwareStatsProvider?.CpuUsage.ToString("0.00") ?? "0.00")
.Replace("$gpuusage$", hardwareStatsProvider?.GpuUsage.ToString("0.00") ?? "0.00")
.Replace("$ramusage$", hardwareStatsProvider?.RamUsage.ToString("0.00") ?? "0.00")
.Replace("$cputemp$", hardwareStatsProvider?.CpuTemp.ToString() ?? "0")
.Replace("$gputemp$", hardwareStatsProvider?.GpuTemp.ToString() ?? "0")
.Replace("$ramtotal$", hardwareStatsProvider?.RamTotal.ToString("0.0") ?? "0.0")
.Replace("$ramused$", hardwareStatsProvider?.RamUsed.ToString("0.0") ?? "0.0")
.Replace("$ramavailable$", hardwareStatsProvider?.RamAvailable.ToString("0.0") ?? "0.0");
.Replace("$cpuusage$", hardwareStatsProvider.CpuUsage.ToString("0.00"))
.Replace("$gpuusage$", hardwareStatsProvider.GpuUsage.ToString("0.00"))
.Replace("$ramusage$", hardwareStatsProvider.RamUsage.ToString("0.00"))
.Replace("$cputemp$", hardwareStatsProvider.CpuTemp.ToString())
.Replace("$gputemp$", hardwareStatsProvider.GpuTemp.ToString())
.Replace("$ramtotal$", hardwareStatsProvider.RamTotal.ToString("0.0"))
.Replace("$ramused$", hardwareStatsProvider.RamUsed.ToString("0.0"))
.Replace("$ramavailable$", hardwareStatsProvider.RamAvailable.ToString("0.0"));
}

protected override void OnModuleStart()
Expand All @@ -57,9 +55,9 @@ protected override void OnModuleStart()

protected override void OnModuleUpdate()
{
if (hardwareStatsProvider is null || !hardwareStatsProvider.CanAcceptQueries) return;
if (!(hardwareStatsProvider?.CanAcceptQueries ?? false)) return;

hardwareStatsProvider!.Update();
hardwareStatsProvider.Update();

SendParameter(HardwareStatsParameter.CpuUsage, hardwareStatsProvider.CpuUsage / 100f);
SendParameter(HardwareStatsParameter.GpuUsage, hardwareStatsProvider.GpuUsage / 100f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ protected override void OnModuleStart()

protected override void OnModuleUpdate()
{
if (!HeartRateProvider?.IsConnected ?? false) return;
if (!(HeartRateProvider?.IsConnected ?? false)) return;

((HypeRateProvider)HeartRateProvider!).SendWsHeartBeat();
((HypeRateProvider)HeartRateProvider).SendWsHeartBeat();
if (!receivedHeartRate) SendParameter(HeartrateParameter.Enabled, false);
receivedHeartRate = false;
}
Expand Down
12 changes: 6 additions & 6 deletions VRCOSC.Game/Modules/Modules/Media/MediaModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ protected override void OnFloatParameterReceived(Enum key, float value)
{
switch (key)
{
case MediaParameter.Volume:
mediaProvider.SetVolume(value);
case MediaParameter.Volume when mediaProvider.Controller is not null:
mediaProvider.State.Volume = value;
break;
}
}
Expand All @@ -125,8 +125,8 @@ protected override void OnBoolParameterReceived(Enum key, bool value)
mediaProvider.Controller?.TryChangeShuffleActiveAsync(value);
break;

case MediaParameter.Muted:
mediaProvider.SetMuted(value);
case MediaParameter.Muted when mediaProvider.Controller is not null:
mediaProvider.State.Muted = value;
break;

case MediaParameter.Next when value:
Expand Down Expand Up @@ -163,8 +163,8 @@ private void sendMediaParameters()

private void sendVolumeParameters()
{
SendParameter(MediaParameter.Volume, mediaProvider.GetVolume());
SendParameter(MediaParameter.Muted, mediaProvider.IsMuted());
SendParameter(MediaParameter.Volume, mediaProvider.State.Volume);
SendParameter(MediaParameter.Muted, mediaProvider.State.Muted);
}

private enum MediaSetting
Expand Down
91 changes: 29 additions & 62 deletions VRCOSC.Game/Modules/Modules/Media/MediaProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// See the LICENSE file in the repository root for full license text.

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Windows.Media;
using Windows.Media.Control;
using VRCOSC.Game.Processes;
Expand All @@ -15,8 +12,6 @@ namespace VRCOSC.Game.Modules.Modules.Media;
public class MediaProvider
{
private MediaManager? mediaManager;
private Process? trackedProcess;
private string? lastSender;

public MediaState State { get; private set; } = null!;

Expand All @@ -29,57 +24,21 @@ public void StartMediaHook()
State = new MediaState();

mediaManager = new MediaManager();
mediaManager.OnAnySessionOpened += MediaManager_OnAnySessionOpened;
mediaManager.OnAnySessionClosed += MediaManager_OnAnySessionClosed;
mediaManager.OnFocusedSessionChanged += MediaManager_OnFocusedSessionChanged;
mediaManager.OnAnyPlaybackStateChanged += MediaManager_OnAnyPlaybackStateChanged;
mediaManager.OnAnyMediaPropertyChanged += MediaManager_OnAnyMediaPropertyChanged;
mediaManager.OnFocusedSessionChanged += MediaManager_OnFocusedSessionChanged;
mediaManager.Start();
}

public void StopMediaHook()
{
mediaManager?.Dispose();
mediaManager = null;
lastSender = null;
trackedProcess = null;
}

public void ForceUpdate()
{
if (Controller?.GetPlaybackInfo().PlaybackStatus != GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing) Controller?.TryPlayAsync();
}

private async void MediaManager_OnAnySessionOpened(MediaManager.MediaSession sender)
{
updateTrackedProcess(sender.Id);
await Task.Delay(500);
ForceUpdate();
}

private void MediaManager_OnAnySessionClosed(MediaManager.MediaSession sender)
{
updateTrackedProcess(mediaManager?.CurrentMediaSessions.FirstOrDefault().Value.Id ?? string.Empty);
}

private void MediaManager_OnFocusedSessionChanged(MediaManager.MediaSession sender)
{
updateTrackedProcess(sender.Id);
ForceUpdate();
}

private void MediaManager_OnAnyPlaybackStateChanged(MediaManager.MediaSession sender, GlobalSystemMediaTransportControlsSessionPlaybackInfo args)
{
updateTrackedProcess(sender.Id);

var mediaProperties = sender.ControlSession?.TryGetMediaPropertiesAsync().GetResults();

if (mediaProperties is not null)
{
State.Title = mediaProperties.Title;
State.Artist = mediaProperties.Artist;
}

State.ProcessId = Controller?.SourceAppUserModelId;
State.IsShuffle = args.IsShuffleActive ?? false;
State.RepeatMode = args.AutoRepeatMode ?? 0;
State.Status = args.PlaybackStatus;
Expand All @@ -89,43 +48,51 @@ private void MediaManager_OnAnyPlaybackStateChanged(MediaManager.MediaSession se

private void MediaManager_OnAnyMediaPropertyChanged(MediaManager.MediaSession sender, GlobalSystemMediaTransportControlsSessionMediaProperties args)
{
updateTrackedProcess(sender.Id);

var playbackInfo = sender.ControlSession?.GetPlaybackInfo();
if (playbackInfo is null) return;

State.IsShuffle = playbackInfo.IsShuffleActive ?? false;
State.RepeatMode = playbackInfo.AutoRepeatMode ?? 0;
State.Status = playbackInfo.PlaybackStatus;
State.ProcessId = Controller?.SourceAppUserModelId;
State.Title = args.Title;
State.Artist = args.Artist;

OnMediaUpdate?.Invoke();
}

private void updateTrackedProcess(string senderId)
private async void MediaManager_OnFocusedSessionChanged(MediaManager.MediaSession? sender)
{
if (lastSender is null || lastSender != senderId)
{
trackedProcess = Process.GetProcessesByName(senderId.Replace(".exe", string.Empty)).FirstOrDefault();
lastSender = trackedProcess is null ? null : senderId;
}
}
if (sender is null) return;

public void SetVolume(float percentage) => ProcessExtensions.SetProcessVolume(lastSender ?? string.Empty, percentage);
public void SetMuted(bool muted) => ProcessExtensions.SetProcessMuted(lastSender ?? string.Empty, muted);
var properties = await sender.ControlSession.TryGetMediaPropertiesAsync();
var playbackInfo = sender.ControlSession.GetPlaybackInfo();

State.ProcessId = Controller?.SourceAppUserModelId;
State.Title = properties.Title;
State.Artist = properties.Artist;
State.IsShuffle = playbackInfo.IsShuffleActive ?? false;
State.RepeatMode = playbackInfo.AutoRepeatMode ?? 0;
State.Status = playbackInfo.PlaybackStatus;

public float GetVolume() => ProcessExtensions.RetrieveProcessVolume(lastSender ?? string.Empty);
public bool IsMuted() => ProcessExtensions.IsProcessMuted(lastSender ?? string.Empty);
OnMediaUpdate?.Invoke();
}
}

public class MediaState
{
public string? ProcessId;
public string Title = string.Empty;
public string Artist = string.Empty;
public MediaPlaybackAutoRepeatMode RepeatMode;
public bool IsShuffle;
public GlobalSystemMediaTransportControlsSessionPlaybackStatus Status;
public GlobalSystemMediaTransportControlsSessionTimelineProperties? Position;
public bool IsPlaying => Status == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing;

public float Volume
{
set => ProcessExtensions.SetProcessVolume(ProcessId, value);
get => ProcessExtensions.RetrieveProcessVolume(ProcessId);
}

public bool Muted
{
set => ProcessExtensions.SetProcessMuted(ProcessId, value);
get => ProcessExtensions.IsProcessMuted(ProcessId);
}
}
18 changes: 13 additions & 5 deletions VRCOSC.Game/Processes/ProcessExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,31 @@ public static void SetMainWindowForeground(this Process process)

#region Volume

public static float RetrieveProcessVolume(string processName)
public static float RetrieveProcessVolume(string? processName)
{
return ProcessVolume.GetApplicationVolume(processName) ?? 0f;
if (processName is null) return 1f;

return ProcessVolume.GetApplicationVolume(processName) ?? 1f;
}

public static bool IsProcessMuted(string processName)
public static bool IsProcessMuted(string? processName)
{
if (processName is null) return false;

return ProcessVolume.GetApplicationMute(processName) ?? false;
}

public static void SetProcessVolume(string processName, float percentage)
public static void SetProcessVolume(string? processName, float percentage)
{
if (processName is null) return;

ProcessVolume.SetApplicationVolume(processName, percentage);
}

public static void SetProcessMuted(string processName, bool muted)
public static void SetProcessMuted(string? processName, bool muted)
{
if (processName is null) return;

ProcessVolume.SetApplicationMute(processName, muted);
}

Expand Down
4 changes: 2 additions & 2 deletions VRCOSC.OSC/Client/OscClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ private async void runReceiveLoop()
{
try
{
while (!tokenSource!.Token.IsCancellationRequested)
while (!tokenSource?.Token.IsCancellationRequested ?? false)
{
var message = await receivingClient!.ReceiveOscMessageAsync(tokenSource.Token);
var message = await receivingClient!.ReceiveOscMessageAsync(tokenSource!.Token);
if (message is null) continue;

var data = new OscData
Expand Down

0 comments on commit 189c15e

Please sign in to comment.