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-22 16:54:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Regex to match fs watcher results to Stars file types.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Precompiled RegEx</returns>
|
2024-09-18 18:34:30 +00:00
|
|
|
[GeneratedRegex(@".*\.(?<type>[pf])(?<player>\d)+$")]
|
|
|
|
private static partial Regex MyRegex();
|
|
|
|
|
2024-09-22 16:54:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// RegEx Instance to match Stars file types.
|
|
|
|
/// </summary>
|
2024-09-18 18:34:30 +00:00
|
|
|
protected Regex FileTypeRegEx = MyRegex();
|
|
|
|
|
2024-09-22 16:54:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Instance of the FSWatcher, to which we subscribe.
|
|
|
|
/// </summary>
|
|
|
|
protected IObservable<FileSystemEventArgs> Watcher;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Active subscription to FsWatcher.
|
|
|
|
/// </summary>
|
|
|
|
protected IDisposable? Subscription;
|
2024-09-17 18:20:42 +00:00
|
|
|
|
2024-09-22 16:54:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Construct the instance and prepare our FS Watcher.
|
|
|
|
/// </summary>
|
|
|
|
public CSVDataLoader()
|
2024-09-17 18:20:42 +00:00
|
|
|
{
|
2024-09-18 18:34:30 +00:00
|
|
|
// string[] filters = { "*.p*", "*.f*", "*.map" };
|
2024-09-22 16:54:16 +00:00
|
|
|
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-22 16:54:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Start the CSV watcher, and capture our disposable, so that we can shut
|
|
|
|
/// down the watcher if needed.
|
|
|
|
/// </summary>
|
|
|
|
public void StartCSVWatcher()
|
|
|
|
{
|
|
|
|
if (Subscription != null)
|
|
|
|
throw new InvalidOperationException("CSV Watcher is active, can't start it again.");
|
|
|
|
|
|
|
|
Subscription = Watcher.Subscribe(fsEvent => this.LoadFile(fsEvent.FullPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Subscription to File Processing, called by our subscription to the watcher.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="fileName">The File to process</param>
|
|
|
|
protected void LoadFile(string fileName)
|
2024-09-18 18:34:30 +00:00
|
|
|
{
|
|
|
|
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}");
|
2024-09-22 16:54:16 +00:00
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case "p":
|
|
|
|
var loader = new CSV.PlanetLoader();
|
|
|
|
loader.ImportForRace(Services.Game.Player);
|
2024-09-26 20:18:50 +00:00
|
|
|
|
|
|
|
PlanetManager manager = Locator.Current.GetService<PlanetManager>()!;
|
|
|
|
manager.InitFromDatabase();
|
2024-09-22 16:54:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
this.Log().Warn($"Planet loader got unknown file type ${type}. Ignoring file.");
|
|
|
|
break;
|
|
|
|
}
|
2024-09-17 18:20:42 +00:00
|
|
|
}
|
|
|
|
}
|