clean-branch #1

Merged
king merged 5 commits from clean-branch into master 2024-12-27 11:02:15 +08:00
Showing only changes of commit f65d05402a - Show all commits
+28 -1
View File
@@ -22,8 +22,17 @@ namespace ResourceMonitorService
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
var builder = WebApplication.CreateBuilder(); var builder = WebApplication.CreateBuilder();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins",
builder => builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
});
builder.Services.AddControllers().AddNewtonsoftJson(); builder.Services.AddControllers().AddNewtonsoftJson();
var app = builder.Build(); var app = builder.Build();
// Apply CORS policy to allow all origins
app.UseCors("AllowAllOrigins");
app.MapGet("/api/resource-usage", async context => app.MapGet("/api/resource-usage", async context =>
{ {
@@ -50,7 +59,7 @@ namespace ResourceMonitorService
await context.Response.WriteAsync(json); await context.Response.WriteAsync(json);
}); });
app.RunAsync(stoppingToken); _ = app.RunAsync(stoppingToken);
await Task.Delay(Timeout.Infinite, stoppingToken); await Task.Delay(Timeout.Infinite, stoppingToken);
} }
@@ -68,11 +77,17 @@ namespace ResourceMonitorService
private object GetCpuUsage() private object GetCpuUsage()
{ {
#pragma warning disable CA1416 // Validate platform compatibility
var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
cpuCounter.NextValue(); cpuCounter.NextValue();
#pragma warning restore CA1416 // Validate platform compatibility
Thread.Sleep(1000); // Wait a second to get a valid reading Thread.Sleep(1000); // Wait a second to get a valid reading
#pragma warning disable CA1416 // Validate platform compatibility
var usage = cpuCounter.NextValue(); var usage = cpuCounter.NextValue();
#pragma warning restore CA1416 // Validate platform compatibility
if (usage > 80) if (usage > 80)
{ {
// Get the current processes and sort them by CPU usage in descending order // Get the current processes and sort them by CPU usage in descending order
@@ -111,20 +126,30 @@ namespace ResourceMonitorService
private float GetRamUsage() private float GetRamUsage()
{ {
#pragma warning disable CA1416 // Validate platform compatibility
var ramCounter = new PerformanceCounter("Memory", "Available MBytes"); var ramCounter = new PerformanceCounter("Memory", "Available MBytes");
#pragma warning restore CA1416 // Validate platform compatibility
var totalMemory = GetTotalPhysicalMemory(); var totalMemory = GetTotalPhysicalMemory();
#pragma warning disable CA1416 // Validate platform compatibility
var availableMemory = ramCounter.NextValue() * 1024 * 1024; var availableMemory = ramCounter.NextValue() * 1024 * 1024;
#pragma warning restore CA1416 // Validate platform compatibility
return (float)(totalMemory - availableMemory) / totalMemory * 100; return (float)(totalMemory - availableMemory) / totalMemory * 100;
} }
private ulong GetTotalPhysicalMemory() private ulong GetTotalPhysicalMemory()
{ {
ulong totalMemory = 0; ulong totalMemory = 0;
#pragma warning disable CA1416 // Validate platform compatibility
var searcher = new ManagementObjectSearcher("SELECT TotalPhysicalMemory FROM Win32_ComputerSystem"); var searcher = new ManagementObjectSearcher("SELECT TotalPhysicalMemory FROM Win32_ComputerSystem");
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
foreach (var obj in searcher.Get()) foreach (var obj in searcher.Get())
{ {
#pragma warning disable CA1416 // Validate platform compatibility
totalMemory = (ulong)obj["TotalPhysicalMemory"]; totalMemory = (ulong)obj["TotalPhysicalMemory"];
#pragma warning restore CA1416 // Validate platform compatibility
} }
#pragma warning restore CA1416 // Validate platform compatibility
return totalMemory; return totalMemory;
} }
@@ -160,7 +185,9 @@ namespace ResourceMonitorService
{ {
try try
{ {
#pragma warning disable CS8602 // Dereference of a possibly null reference.
var filePath = process.MainModule.FileName; var filePath = process.MainModule.FileName;
#pragma warning restore CS8602 // Dereference of a possibly null reference.
if (filePath.Contains(@"\steamapps\common\")) if (filePath.Contains(@"\steamapps\common\"))
{ {
// Extract the game directory name // Extract the game directory name