sa/Stars Assistant/Services/CSVDataLoader.cs

59 lines
1.7 KiB
C#
Raw Normal View History

using System;
2024-09-17 18:20:42 +00:00
using System.IO;
using System.Reactive.Linq;
using System.Text.RegularExpressions;
using ReactiveUI;
using Splat;
2024-09-17 18:20:42 +00:00
using StarsAssistant.Helpers;
namespace StarsAssistant.Services;
public partial class CSVDataLoader : IEnableLogger
{
/// <summary>
/// Reference to the game metadata, retrieved by DI
/// </summary>
2024-09-17 18:20:42 +00:00
protected Services.Game Game = Locator.Current.GetService<Services.Game>()!;
[GeneratedRegex(@".*\.(?<type>[pf])(?<player>\d)+$")]
private static partial Regex MyRegex();
protected Regex FileTypeRegEx = MyRegex();
public CSVDataLoader()
{
2024-09-17 18:20:42 +00:00
}
2024-09-17 18:20:42 +00:00
public void StartPlanetCSVWatcher()
{
// string[] filters = { "*.p*", "*.f*", "*.map" };
2024-09-17 18:20:42 +00:00
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);
2024-09-17 18:20:42 +00:00
watcher.Subscribe(fsEvent => this.LoadPlanetFile(fsEvent.FullPath));
}
protected void LoadPlanetFile(string fileName)
{
Match m = FileTypeRegEx.Match(fileName);
if (! m.Success)
2024-09-17 18:20:42 +00:00
{
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);
2024-09-17 18:20:42 +00:00
}
}