2021-11-10 23:13:02 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2022-10-08 04:06:17 +00:00
|
|
|
namespace Wabbajack.Services.OSIntegrated;
|
2021-11-10 23:13:02 +00:00
|
|
|
|
|
|
|
public class ResourceSettingsManager
|
|
|
|
{
|
|
|
|
private readonly SettingsManager _manager;
|
|
|
|
private Dictionary<string,ResourceSetting>? _settings;
|
|
|
|
|
|
|
|
public ResourceSettingsManager(SettingsManager manager)
|
|
|
|
{
|
|
|
|
_manager = manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
private SemaphoreSlim _lock = new(1);
|
|
|
|
|
|
|
|
public async Task<ResourceSetting> GetSettings(string name)
|
|
|
|
{
|
|
|
|
|
|
|
|
await _lock.WaitAsync();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_settings ??= await _manager.Load<Dictionary<string, ResourceSetting>>("resource_settings");
|
2023-06-27 14:16:03 +00:00
|
|
|
if (!_settings.ContainsKey(name))
|
2021-11-10 23:13:02 +00:00
|
|
|
{
|
2023-06-27 14:16:03 +00:00
|
|
|
var newSetting = new ResourceSetting
|
|
|
|
{
|
|
|
|
MaxTasks = Environment.ProcessorCount,
|
|
|
|
MaxThroughput = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
_settings.Add(name, newSetting);
|
|
|
|
await SaveSettings(_settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
var setting = _settings[name];
|
|
|
|
return setting;
|
2021-11-10 23:13:02 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
_lock.Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ResourceSetting
|
|
|
|
{
|
|
|
|
public long MaxTasks { get; set; }
|
|
|
|
public long MaxThroughput { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task SaveSettings(Dictionary<string, ResourceSetting> settings)
|
|
|
|
{
|
|
|
|
await _manager.Save("resource_settings", settings);
|
|
|
|
}
|
|
|
|
}
|