2024-09-15 20:10:44 +00:00
|
|
|
using System;
|
2024-09-17 18:20:42 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Reactive.Linq;
|
2024-09-18 18:34:30 +00:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using ReactiveUI;
|
2024-09-15 20:10:44 +00:00
|
|
|
using Splat;
|
2024-09-17 18:20:42 +00:00
|
|
|
using StarsAssistant.Helpers;
|
2024-09-15 20:10:44 +00:00
|
|
|
|
|
|
|
namespace StarsAssistant.Services;
|
|
|
|
|
|
|
|
|
2024-09-18 18:34:30 +00:00
|
|
|
public partial class CSVDataLoader : IEnableLogger
|
2024-09-15 20:10:44 +00:00
|
|
|
{
|
|
|
|
/// <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>()!;
|
2024-09-15 20:10:44 +00:00
|
|
|
|
2024-09-18 18:34:30 +00:00
|
|
|
[GeneratedRegex(@".*\.(?<type>[pf])(?<player>\d)+$")]
|
|
|
|
private static partial Regex MyRegex();
|
|
|
|
|
|
|
|
protected Regex FileTypeRegEx = MyRegex();
|
|
|
|
|
|
|
|
public CSVDataLoader()
|
2024-09-15 20:10:44 +00:00
|
|
|
{
|
2024-09-17 18:20:42 +00:00
|
|
|
|
2024-09-15 20:10:44 +00:00
|
|
|
}
|
2024-09-17 18:20:42 +00:00
|
|
|
|
|
|
|
public void StartPlanetCSVWatcher()
|
|
|
|
{
|
2024-09-18 18:34:30 +00:00
|
|
|
// string[] filters = { "*.p*", "*.f*", "*.map" };
|
|
|
|
|
2024-09-17 18:20:42 +00:00
|
|
|
var watcher = FsWatcher
|
2024-09-18 18:34:30 +00:00
|
|
|
.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
|
|
|
|
2024-09-18 18:34:30 +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
|
|
|
{
|
2024-09-18 18:34:30 +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();
|
2024-09-21 12:40:13 +00:00
|
|
|
loader.ImportForRace(Services.Game.Player);
|
2024-09-17 18:20:42 +00:00
|
|
|
}
|
|
|
|
}
|