first commit

This commit is contained in:
2025-11-01 19:18:39 +10:30
commit d1d2c9f41e
26 changed files with 1052 additions and 0 deletions
@@ -0,0 +1,191 @@
using System.Collections.Concurrent;
using System.Security.Cryptography;
using System.Text.Json;
using FileSnapshot;
using TinfoilVibeServer.Models;
namespace TinfoilVibeServer.Services;
/// <summary>
/// Keeps an inmemory snapshot, watches the filesystem for changes, and
/// only reprocesses a file if its hash changed. The snapshot is
/// automatically regenerated when the configuration changes.
/// </summary>
public sealed class SnapshotService : IDisposable
{
private readonly ConfigManager _config;
private readonly string _jsonPath;
private readonly string _snapshotPath;
private readonly FileSystemWatcher _watcher;
// path -> CachedFile
private readonly ConcurrentDictionary<string, CachedFile> _cache = new();
private string? _currentSnapshotHash;
public SnapshotService(ConfigManager config)
{
_config = config;
_jsonPath = Path.Combine(AppContext.BaseDirectory, config.Settings.SnapshotFile);
_snapshotPath = Path.Combine(AppContext.BaseDirectory, config.Settings.SnapshotBackupFile);
// Initial snapshot
BuildSnapshot();
// Persist a copy for quick load on next run
File.WriteAllText(_snapshotPath, JsonSerializer.Serialize(GetSnapshot()));
// File system watcher
_watcher = new FileSystemWatcher
{
Path = string.Join(Path.PathSeparator, config.Settings.RootDirectories),
IncludeSubdirectories = true,
NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName |
NotifyFilters.Size | NotifyFilters.LastWrite
};
_watcher.Created += OnChanged;
_watcher.Changed += OnChanged;
_watcher.Deleted += OnChanged;
_watcher.Renamed += OnRenamed;
_watcher.EnableRaisingEvents = true;
// React to config changes
_config.OnChange += cfg =>
{
// Reinitialise the watcher with the new root directories
_watcher.Path = string.Join(Path.PathSeparator, cfg.RootDirectories);
_watcher.EnableRaisingEvents = true;
BuildSnapshot(); // rebuild everything
PersistSnapshot();
};
}
private sealed record CachedFile(string Path, string Hash, TitleInfo? Title);
#region File system change handlers
private void OnChanged(object? _, FileSystemEventArgs e) =>
ThrottleSnapshotUpdate();
private void OnRenamed(object? _, RenamedEventArgs e)
{
// Treat rename as delete + create
OnChanged(_, new FileSystemEventArgs(WatcherChangeTypes.Deleted, e.OldFullPath, e.OldName));
OnChanged(_, new FileSystemEventArgs(WatcherChangeTypes.Created, e.FullPath, e.Name));
}
private void ThrottleSnapshotUpdate()
{
// Debounce: only trigger once in a short window
Task.Run(async () =>
{
await Task.Delay(250);
UpdateSnapshot();
});
}
#endregion
/// <summary>
/// Full rebuild called on startup and on config change.
/// </summary>
private void BuildSnapshot()
{
var cfg = _config.Settings;
var entries = new List<FileEntry>();
foreach (var dir in cfg.RootDirectories)
{
foreach (var file in Directory.EnumerateFiles(dir, "*", SearchOption.AllDirectories))
{
var ext = Path.GetExtension(file).ToLowerInvariant();
if (!(cfg.WhitelistExtensions.Contains(ext) || cfg.RomExtensions.Contains(ext)))
continue;
// 1) compute hash
var hash = ComputeHash(file);
// 2) decide if we need to reprocess
if (_cache.TryGetValue(file, out var cached) && cached.Hash == hash)
{
// nothing changed use cached title info
entries.Add(new FileEntry(file, new FileInfo(file).Length, hash, cached.Title));
continue;
}
// 3) extract title if applicable
TitleInfo? title = null;
if (cfg.RomExtensions.Contains(ext))
{
title = NSPExtactor.ExtractFromFile(file);
}
else
{
title = ArchiveHandler.TryExtractTitleInfo(file);
}
// 4) update cache
_cache[file] = new CachedFile(file, hash, title);
// 5) add to snapshot
entries.Add(new FileEntry(file, new FileInfo(file).Length, hash, title));
}
}
// Replace the entire snapshot
lock (_cache)
{
// the snapshot itself is not stored in _cache it's only used for the JSON
}
// we keep entries in a local variable for now
_currentSnapshotHash = ComputeSnapshotHash(entries);
File.WriteAllText(_jsonPath, JsonSerializer.Serialize(entries));
}
private static string ComputeSnapshotHash(IEnumerable<FileEntry> entries)
{
var json = JsonSerializer.Serialize(entries);
using var sha256 = SHA256.Create();
return BitConverter.ToString(sha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(json))).Replace("-", "").ToLowerInvariant();
}
private void UpdateSnapshot()
{
BuildSnapshot();
PersistSnapshot();
}
private void PersistSnapshot()
{
var entries = GetSnapshot();
var newHash = ComputeSnapshotHash(entries);
if (_currentSnapshotHash != newHash)
{
_currentSnapshotHash = newHash;
File.WriteAllText(_jsonPath, JsonSerializer.Serialize(entries));
File.WriteAllText(_snapshotPath, JsonSerializer.Serialize(entries));
}
}
private static string ComputeHash(string filePath)
{
using var sha256 = SHA256.Create();
using var stream = File.OpenRead(filePath);
var hash = sha256.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
public IReadOnlyList<FileEntry> GetSnapshot()
{
var json = File.ReadAllText(_jsonPath);
return JsonSerializer.Deserialize<IReadOnlyList<FileEntry>>(json)!;
}
public void Dispose()
{
_watcher.Dispose();
_config.Dispose();
}
}