 14575a4a6b
			
		
	
	14575a4a6b
	
	
	
		
			
			* add jwt expiry * start of 0.9 api impl * some stuff idk * some more impl * some cleanup * remove grouppair, add configuration, rework some pair drawing stuff * do some stuff * rework some ui * I don't even know anymore * add cancellationtoken * token bla * ui fixes etc * probably individual adding/removing now working fully as expected * add working report popup * I guess it's more syncshell shit or so * popup shit idk * work out most of the syncshell bullshit I guess * delete some old crap * are we actually getting closer to the end * update pair info stuff * more fixes/adjustments, idk * refactor some things * some rework * some more cleanup * cleanup * make menu buttons w i d e * better icon text buttons * add all syncshell folder and ordering fixes --------- Co-authored-by: rootdarkarchon <root.darkarchon@outlook.com>
		
			
				
	
	
		
			138 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			138 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using MareSynchronos.MareConfiguration.Configurations;
 | |
| using System.Text.Json;
 | |
| 
 | |
| namespace MareSynchronos.MareConfiguration;
 | |
| 
 | |
| public abstract class ConfigurationServiceBase<T> : IDisposable where T : IMareConfiguration
 | |
| {
 | |
|     private readonly CancellationTokenSource _periodicCheckCts = new();
 | |
|     private bool _configIsDirty = false;
 | |
|     private DateTime _configLastWriteTime;
 | |
|     private Lazy<T> _currentConfigInternal;
 | |
| 
 | |
|     protected ConfigurationServiceBase(string configurationDirectory)
 | |
|     {
 | |
|         ConfigurationDirectory = configurationDirectory;
 | |
| 
 | |
|         _ = Task.Run(CheckForConfigUpdatesInternal, _periodicCheckCts.Token);
 | |
|         _ = Task.Run(CheckForDirtyConfigInternal, _periodicCheckCts.Token);
 | |
| 
 | |
|         _currentConfigInternal = LazyConfig();
 | |
|     }
 | |
| 
 | |
|     public string ConfigurationDirectory { get; init; }
 | |
|     public T Current => _currentConfigInternal.Value;
 | |
|     protected abstract string ConfigurationName { get; }
 | |
|     protected string ConfigurationPath => Path.Combine(ConfigurationDirectory, ConfigurationName);
 | |
| 
 | |
|     public void Dispose()
 | |
|     {
 | |
|         Dispose(disposing: true);
 | |
|         GC.SuppressFinalize(this);
 | |
|     }
 | |
| 
 | |
|     public void Save()
 | |
|     {
 | |
|         _configIsDirty = true;
 | |
|     }
 | |
| 
 | |
|     protected virtual void Dispose(bool disposing)
 | |
|     {
 | |
|         _periodicCheckCts.Cancel();
 | |
|         _periodicCheckCts.Dispose();
 | |
|     }
 | |
| 
 | |
|     protected T LoadConfig()
 | |
|     {
 | |
|         T? config;
 | |
|         if (!File.Exists(ConfigurationPath))
 | |
|         {
 | |
|             config = (T)Activator.CreateInstance(typeof(T))!;
 | |
|             Save();
 | |
|         }
 | |
|         else
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 config = JsonSerializer.Deserialize<T>(File.ReadAllText(ConfigurationPath));
 | |
|             }
 | |
|             catch
 | |
|             {
 | |
|                 // config failed to load for some reason
 | |
|                 config = default;
 | |
|             }
 | |
|             if (config == null)
 | |
|             {
 | |
|                 config = (T)Activator.CreateInstance(typeof(T))!;
 | |
|                 Save();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         _configLastWriteTime = GetConfigLastWriteTime();
 | |
|         return config;
 | |
|     }
 | |
| 
 | |
|     protected void SaveDirtyConfig()
 | |
|     {
 | |
|         _configIsDirty = false;
 | |
|         var existingConfigs = Directory.EnumerateFiles(ConfigurationDirectory, ConfigurationName + ".bak.*").Select(c => new FileInfo(c))
 | |
|             .OrderByDescending(c => c.LastWriteTime).ToList();
 | |
|         if (existingConfigs.Skip(10).Any())
 | |
|         {
 | |
|             foreach (var config in existingConfigs.Skip(10).ToList())
 | |
|             {
 | |
|                 config.Delete();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         try
 | |
|         {
 | |
|             File.Copy(ConfigurationPath, ConfigurationPath + ".bak." + DateTime.Now.ToString("yyyyMMddHHmmss"), overwrite: true);
 | |
|         }
 | |
|         catch
 | |
|         {
 | |
|             // ignore if file cannot be backupped once
 | |
|         }
 | |
| 
 | |
|         File.WriteAllText(ConfigurationPath, JsonSerializer.Serialize(Current, new JsonSerializerOptions()
 | |
|         {
 | |
|             WriteIndented = true
 | |
|         }));
 | |
|         _configLastWriteTime = new FileInfo(ConfigurationPath).LastWriteTimeUtc;
 | |
|     }
 | |
| 
 | |
|     private async Task CheckForConfigUpdatesInternal()
 | |
|     {
 | |
|         while (!_periodicCheckCts.IsCancellationRequested)
 | |
|         {
 | |
|             await Task.Delay(TimeSpan.FromSeconds(5), _periodicCheckCts.Token).ConfigureAwait(false);
 | |
| 
 | |
|             var lastWriteTime = GetConfigLastWriteTime();
 | |
|             if (lastWriteTime != _configLastWriteTime)
 | |
|             {
 | |
|                 _currentConfigInternal = LazyConfig();
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private async Task CheckForDirtyConfigInternal()
 | |
|     {
 | |
|         while (!_periodicCheckCts.IsCancellationRequested)
 | |
|         {
 | |
|             if (_configIsDirty)
 | |
|             {
 | |
|                 SaveDirtyConfig();
 | |
|             }
 | |
| 
 | |
|             await Task.Delay(TimeSpan.FromSeconds(1), _periodicCheckCts.Token).ConfigureAwait(false);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private DateTime GetConfigLastWriteTime() => new FileInfo(ConfigurationPath).LastWriteTimeUtc;
 | |
| 
 | |
|     private Lazy<T> LazyConfig()
 | |
|     {
 | |
|         _configLastWriteTime = GetConfigLastWriteTime();
 | |
|         return new Lazy<T>(LoadConfig);
 | |
|     }
 | |
| } |