Skip to content

Commit 0300b3e

Browse files
committed
Optimise AFK module
1 parent 07d5f54 commit 0300b3e

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

VRCOSC.Game/Processes/ProcessExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static async Task PressKeys(IEnumerable<User32.VirtualKey> keys, int pres
2727
{
2828
var buffer = new char[256];
2929
var handle = User32.GetForegroundWindow();
30-
return User32.GetWindowText(handle, buffer, 256) > 0 ? Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(buffer)) : null;
30+
return User32.GetWindowText(handle, buffer, 256) > 0 ? Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(buffer)).TrimEnd('\u0000') : null;
3131
}
3232

3333
public static void ShowMainWindow(this Process process, User32.WindowShowStyle style) => User32.ShowWindow(process.MainWindowHandle, style);

VRCOSC.Modules/AFK/AFKModule.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// See the LICENSE file in the repository root for full license text.
33

44
using VRCOSC.Game.Modules.ChatBox;
5-
using VRCOSC.Game.OSC.VRChat;
65
using VRCOSC.Game.Processes;
76

87
namespace VRCOSC.Modules.AFK;
@@ -13,7 +12,7 @@ public class AFKModule : ChatBoxModule
1312
public override string Description => "Display text and time since going AFK";
1413
public override string Author => "VolcanicArts";
1514
public override ModuleType Type => ModuleType.General;
16-
protected override TimeSpan DeltaUpdate => VRChatOscConstants.UPDATE_TIME_SPAN;
15+
protected override TimeSpan DeltaUpdate => TimeSpan.FromSeconds(1);
1716

1817
private DateTime? afkBegan;
1918

@@ -38,6 +37,14 @@ protected override void OnModuleStart()
3837
}
3938

4039
protected override void OnModuleUpdate()
40+
{
41+
SetVariableValue(AFKVariable.FocusedWindow, ProcessExtensions.GetActiveWindowTitle() ?? "None");
42+
SetVariableValue(AFKVariable.Duration, afkBegan is null ? null : (DateTime.Now - afkBegan.Value).ToString(@"hh\:mm\:ss"));
43+
SetVariableValue(AFKVariable.Since, afkBegan?.ToString(@"hh\:mm"));
44+
ChangeStateTo(afkBegan is null ? AFKState.NotAFK : AFKState.AFK);
45+
}
46+
47+
protected override void OnPlayerUpdate()
4148
{
4249
if (Player.AFK is null)
4350
{
@@ -48,7 +55,6 @@ protected override void OnModuleUpdate()
4855
if (Player.AFK.Value && afkBegan is null)
4956
{
5057
afkBegan = DateTime.Now;
51-
SetVariableValue(AFKVariable.FocusedWindow, ProcessExtensions.GetActiveWindowTitle() ?? "None");
5258
TriggerEvent(AFKEvent.AFKStarted);
5359
}
5460

@@ -57,10 +63,6 @@ protected override void OnModuleUpdate()
5763
afkBegan = null;
5864
TriggerEvent(AFKEvent.AFKStopped);
5965
}
60-
61-
SetVariableValue(AFKVariable.Duration, afkBegan is null ? null : (DateTime.Now - afkBegan.Value).ToString(@"hh\:mm\:ss"));
62-
SetVariableValue(AFKVariable.Since, afkBegan?.ToString(@"hh\:mm"));
63-
ChangeStateTo(afkBegan is null ? AFKState.NotAFK : AFKState.AFK);
6466
}
6567

6668
private enum AFKVariable

0 commit comments

Comments
 (0)