using System;
using System.IO;
using System.Reactive.Linq;
using System.Text.RegularExpressions;
using ReactiveUI;
using Splat;
using StarsAssistant.Helpers;
namespace StarsAssistant.Services;
public partial class CSVDataLoader : IEnableLogger
{
///
/// Reference to the game metadata, retrieved by DI
///
protected Services.Game Game = Locator.Current.GetService()!;
[GeneratedRegex(@".*\.(?[pf])(?\d)+$")]
private static partial Regex MyRegex();
protected Regex FileTypeRegEx = MyRegex();
public CSVDataLoader()
{
}
public void StartPlanetCSVWatcher()
{
// string[] filters = { "*.p*", "*.f*", "*.map" };
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(Services.Game.Player);
}
}