Hook up fswatcher to csv parser, needs proper filename parsing.

This commit is contained in:
2024-09-18 20:34:30 +02:00
parent 802180a26e
commit 275e6de228
6 changed files with 82 additions and 65 deletions

View File

@ -1,41 +1,58 @@
using System;
using System.IO;
using System.Reactive.Linq;
using System.Text.RegularExpressions;
using ReactiveUI;
using Splat;
using StarsAssistant.Helpers;
namespace StarsAssistant.Services;
public class CSVDataLoader
public partial class CSVDataLoader : IEnableLogger
{
/// <summary>
/// Reference to the game metadata, retrieved by DI
/// </summary>
protected Services.Game Game = Locator.Current.GetService<Services.Game>()!;
public void CSVDataLoader()
[GeneratedRegex(@".*\.(?<type>[pf])(?<player>\d)+$")]
private static partial Regex MyRegex();
protected Regex FileTypeRegEx = MyRegex();
public CSVDataLoader()
{
}
public void StartPlanetCSVWatcher()
{
// TODO: which scheduler for Throttle?
var watcher = FsWatcher
.ObserveFileSystem(Game.GamePath, new string[] { Game.PlanetFileSearchPattern });
.ThrottleAndDistinct(2);
/*
watcher.Subscribe(x =>
{
Console.WriteLine($"{DateTime.Now.ToLongTimeString()} got {x.Count()} events:");
// string[] filters = { "*.p*", "*.f*", "*.map" };
foreach (var fsEvent in x)
{
Console.WriteLine($"{DateTime.Now.ToLongTimeString()} {i++} {fsEvent.FullPath} - {fsEvent.ChangeType}");
}
});
*/
var watcher = FsWatcher
.ObserveFileSystem(Game.GamePath, [ Game.PlanetFileSearchPattern ])
.ThrottleAndDistinct(2, RxApp.TaskpoolScheduler)
.Log(this, $"{DateTime.Now.ToLongTimeString()} FsEvent", fsEvent => $"{fsEvent.FullPath} {fsEvent.ChangeType}")
.ObserveOn(RxApp.TaskpoolScheduler);
watcher.Subscribe(fsEvent => this.LoadPlanetFile(fsEvent.FullPath));
}
protected void LoadPlanetFile(string fileName)
{
Match m = FileTypeRegEx.Match(fileName);
if (! m.Success)
{
this.Log().Error($"Failed to parse {fileName} to identify what we are looking at. Ignoring file.");
return;
}
string type = m.Groups["type"].Value;
string player = m.Groups["player"].Value;
this.Log().Debug($"Got file type {type} for player {player}");
var loader = new CSV.PlanetLoader();
loader.ImportForRace(Model.Race.Player);
}
}