Update Cef 125.0.210, fix PerformanceCounter bugs

This commit is contained in:
Natsumi
2024-06-15 11:53:58 +12:00
parent 2d13d97ae4
commit fe12f615ba
13 changed files with 223 additions and 153 deletions

View File

@@ -40,6 +40,11 @@ namespace VRCX
ProcessMonitor.Instance.ProcessExited += Instance.OnProcessStateChanged;
}
public void Init()
{
// Create Instance before Cef tries to bind it
}
/// <summary>
/// Computes the MD5 hash of the file represented by the specified base64-encoded string.
/// </summary>

View File

@@ -9,20 +9,15 @@ namespace VRCX
public class AppApiVr
{
public static readonly AppApiVr Instance;
private static readonly PerformanceCounter Uptime;
static AppApiVr()
{
Instance = new AppApiVr();
}
try
{
Uptime = new PerformanceCounter("System", "System Up Time");
}
catch
{
Uptime = null;
}
public void Init()
{
// Create Instance before Cef tries to bind it
}
public void VrInit()
@@ -31,13 +26,18 @@ namespace VRCX
MainForm.Instance.Browser.ExecuteScriptAsync("$app.vrInit", "");
}
public void ToggleSystemMonitor(bool enabled)
{
SystemMonitor.Instance.Start(enabled);
}
/// <summary>
/// Returns the current CPU usage as a percentage.
/// </summary>
/// <returns>The current CPU usage as a percentage.</returns>
public float CpuUsage()
{
return CpuMonitor.Instance.CpuUsage;
return SystemMonitor.Instance.CpuUsage;
}
/// <summary>
@@ -56,11 +56,7 @@ namespace VRCX
/// <returns>The number of milliseconds that the system has been running.</returns>
public double GetUptime()
{
if (Uptime == null)
return 0;
Uptime.NextValue();
return TimeSpan.FromSeconds(Uptime.NextValue()).TotalMilliseconds;
return SystemMonitor.Instance.UpTime;
}
/// <summary>

View File

@@ -15,10 +15,10 @@ namespace VRCX
return true;
}
public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
public bool OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
{
if (callback.IsDisposed)
return;
return true;
using (callback)
{
@@ -27,6 +27,8 @@ namespace VRCX
showDialog: true
);
}
return true;
}
public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)

View File

@@ -1,90 +0,0 @@
// Copyright(c) 2019-2022 pypy, Natsumi and individual contributors.
// All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
using System.Diagnostics;
using System.Threading;
namespace VRCX
{
public class CpuMonitor
{
public static readonly CpuMonitor Instance;
public float CpuUsage;
private readonly PerformanceCounter _performanceCounter;
private readonly Timer _timer;
static CpuMonitor()
{
Instance = new CpuMonitor();
}
public CpuMonitor()
{
try
{
_performanceCounter = new PerformanceCounter(
"Processor Information",
"% Processor Utility",
"_Total",
true
);
}
catch
{
}
// fallback
if (_performanceCounter == null)
{
try
{
_performanceCounter = new PerformanceCounter(
"Processor",
"% Processor Time",
"_Total",
true
);
}
catch
{
}
}
_timer = new Timer(TimerCallback, null, -1, -1);
}
internal void Init()
{
_timer.Change(1000, 1000);
}
internal void Exit()
{
lock (this)
{
_timer.Change(-1, -1);
_performanceCounter?.Dispose();
}
}
private void TimerCallback(object state)
{
lock (this)
{
try
{
if (_performanceCounter != null)
{
CpuUsage = _performanceCounter.NextValue();
}
}
catch
{
}
}
}
}
}

View File

@@ -65,7 +65,7 @@ namespace VRCX
if (m_Client == null && m_Active)
{
m_Client = new DiscordRpcClient(DiscordAppId);
if (m_Client.Initialize() == false)
if (!m_Client.Initialize())
{
m_Client.Dispose();
m_Client = null;
@@ -117,18 +117,18 @@ namespace VRCX
public void SetText(string details, string state)
{
if (m_Client != null && !m_Lock.IsReadLockHeld)
if (m_Client == null || m_Lock.IsReadLockHeld)
return;
m_Lock.EnterWriteLock();
try
{
m_Lock.EnterWriteLock();
try
{
m_Presence.Details = LimitByteLength(details, 127);
m_Presence.State = LimitByteLength(state, 127);
}
finally
{
m_Lock.ExitWriteLock();
}
m_Presence.Details = LimitByteLength(details, 127);
m_Presence.State = LimitByteLength(state, 127);
}
finally
{
m_Lock.ExitWriteLock();
}
}
@@ -137,17 +137,15 @@ namespace VRCX
m_Lock.EnterWriteLock();
try
{
if (string.IsNullOrEmpty(largeKey) == true &&
string.IsNullOrEmpty(smallKey) == true)
if (string.IsNullOrEmpty(largeKey) &&
string.IsNullOrEmpty(smallKey))
{
m_Presence.Assets = null;
}
else
{
if (m_Presence.Assets == null)
m_Presence.Assets = new Assets();
if (m_Presence.Party == null)
m_Presence.Party = new Party();
m_Presence.Assets ??= new Assets();
m_Presence.Party ??= new Party();
m_Presence.Assets.LargeImageKey = largeKey;
m_Presence.Assets.LargeImageText = largeText;
m_Presence.Assets.SmallImageKey = smallKey;
@@ -155,15 +153,15 @@ namespace VRCX
m_Presence.Party.ID = partyId;
m_Presence.Party.Size = partySize;
m_Presence.Party.Max = partyMax;
Button[] Buttons = { };
Button[] buttons = [];
if (!string.IsNullOrEmpty(buttonUrl))
{
Buttons = new Button[]
{
new Button() { Label = buttonText, Url = buttonUrl }
};
buttons =
[
new Button { Label = buttonText, Url = buttonUrl }
];
}
m_Presence.Buttons = Buttons;
m_Presence.Buttons = buttons;
if (DiscordAppId != appId)
{
DiscordAppId = appId;
@@ -181,7 +179,7 @@ namespace VRCX
m_Lock.ExitWriteLock();
}
}
public void SetTimestamps(double startUnixMilliseconds, double endUnixMilliseconds)
{
var _startUnixMilliseconds = (ulong)startUnixMilliseconds;
@@ -196,11 +194,7 @@ namespace VRCX
}
else
{
if (m_Presence.Timestamps == null)
{
m_Presence.Timestamps = new Timestamps();
}
m_Presence.Timestamps ??= new Timestamps();
m_Presence.Timestamps.StartUnixMilliseconds = _startUnixMilliseconds;
if (_endUnixMilliseconds == 0)

View File

@@ -144,7 +144,8 @@ namespace VRCX
ProcessMonitor.Instance.Init();
VRCXStorage.Load();
SQLiteLegacy.Instance.Init();
CpuMonitor.Instance.Init();
AppApi.Instance.Init();
AppApiVr.Instance.Init();
Discord.Instance.Init();
WorldDBManager.Instance.Init();
WebApi.Instance.Init();
@@ -166,7 +167,7 @@ namespace VRCX
WorldDBManager.Instance.Stop();
Discord.Instance.Exit();
CpuMonitor.Instance.Exit();
SystemMonitor.Instance.Exit();
VRCXStorage.Save();
SQLiteLegacy.Instance.Exit();
ProcessMonitor.Instance.Exit();

151
Dotnet/SystemMonitor.cs Normal file
View File

@@ -0,0 +1,151 @@
// Copyright(c) 2019-2022 pypy, Natsumi and individual contributors.
// All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
using System;
using System.Diagnostics;
using System.Threading;
using NLog;
namespace VRCX
{
public class SystemMonitor
{
public static readonly SystemMonitor Instance;
public float CpuUsage;
public double UpTime;
private bool _enabled;
private PerformanceCounter _performanceCounterCpuUsage;
private PerformanceCounter _performanceCounterUpTime;
private Thread _thread;
private static readonly NLog.Logger logger = NLog.LogManager.GetLogger("VRCX");
static SystemMonitor()
{
Instance = new SystemMonitor();
}
public void Start(bool enabled)
{
if (enabled == _enabled)
return;
_enabled = enabled;
if (enabled)
StartThread();
else
Exit();
}
internal void Exit()
{
CpuUsage = 0;
UpTime = 0;
try
{
if (_thread != null)
{
_thread.Interrupt();
_thread.Join();
_thread = null;
}
}
catch (ThreadInterruptedException)
{
}
_performanceCounterCpuUsage?.Dispose();
_performanceCounterCpuUsage = null;
_performanceCounterUpTime?.Dispose();
_performanceCounterUpTime = null;
}
private void StartThread()
{
Exit();
try
{
_performanceCounterCpuUsage = new PerformanceCounter(
"Processor Information",
"% Processor Utility",
"_Total",
true
);
_performanceCounterCpuUsage?.NextValue();
}
catch (Exception ex)
{
logger.Warn($"Failed to create \"Processor Utility\" PerformanceCounter ${ex}");
}
// fallback
if (_performanceCounterCpuUsage == null)
{
try
{
_performanceCounterCpuUsage = new PerformanceCounter(
"Processor",
"% Processor Time",
"_Total",
true
);
_performanceCounterCpuUsage?.NextValue();
}
catch (Exception ex)
{
logger.Warn($"Failed to create \"Processor Time\" PerformanceCounter ${ex}");
}
}
try
{
_performanceCounterUpTime = new PerformanceCounter("System", "System Up Time");
_performanceCounterUpTime?.NextValue();
}
catch
{
logger.Warn("Failed to create \"System Up Time\" PerformanceCounter");
}
if (_performanceCounterCpuUsage == null &&
_performanceCounterUpTime == null)
{
logger.Error("Failed to create any PerformanceCounter");
return;
}
logger.Info("SystemMonitor started");
_thread = new Thread(ThreadProc)
{
IsBackground = true
};
_thread.Start();
}
private void ThreadProc()
{
try
{
while (_enabled)
{
if (_performanceCounterCpuUsage != null)
CpuUsage = _performanceCounterCpuUsage.NextValue();
if (_performanceCounterUpTime != null)
UpTime = TimeSpan.FromSeconds(_performanceCounterUpTime.NextValue()).TotalMilliseconds;
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
logger.Warn($"SystemMonitor thread exception: {ex}");
}
Exit();
}
}
}