diff --git a/Dialogues/DialogueLoader.cs b/Dialogues/DialogueLoader.cs index ff38af9d82..5a99510fef 100644 --- a/Dialogues/DialogueLoader.cs +++ b/Dialogues/DialogueLoader.cs @@ -5,25 +5,31 @@ using System.Linq; using System.Reflection; using System.Text; using System.Text.Json; +using System.Text.RegularExpressions; using CalamityMod.UI.DialogueDisplay; +using CalamityMod.Utilities; using MonoMod.Cil; using MonoMod.RuntimeDetour; +using Terraria; using Terraria.Localization; using Terraria.ModLoader; +using Terraria.ModLoader.Core; namespace CalamityMod.Dialogues; internal record DialogueTextDataEntry( + Mod ProviderMod, string FilePath, string DialogueKey, DialogueTextData Data ); -internal class DialogueLoader : ModSystem +internal partial class DialogueLoader : ModSystem { private const string DialogueFilePrefix = "CalamityDialogue."; private static readonly Dictionary _DialogueLookup = []; + private static readonly Dictionary _Watchers = []; private ILHook _ExtractLocalizationHook; @@ -39,6 +45,35 @@ internal class DialogueLoader : ModSystem } } + public override void PostSetupContent() + { + foreach (var mod in ModLoader.Mods) + { + string path = Path.Combine(ModCompile.ModSourcePath, mod.Name); + if (!Directory.Exists(path)) + continue; + + var watcher = new MainThreadedFileSystemWatcher() + { + Path = path, + Filter = "*.json", + FileNameFilter = CalamityDialogueFileRegex(), + NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite, + IncludeSubdirectories = true + }; + watcher.Changed += (o, arg) => + { + HandleFileUpdate(mod, arg.FullPath); + }; + watcher.Renamed += (o, arg) => + { + HandleFileUpdate(mod, arg.FullPath); + }; + watcher.EnableRaisingEvents = true; + _Watchers[mod] = watcher; + } + } + public override void Unload() { _DialogueLookup.Clear(); @@ -49,6 +84,14 @@ internal class DialogueLoader : ModSystem _ExtractLocalizationHook.Dispose(); _ExtractLocalizationHook = null; } + + foreach (var watcher in _Watchers.Values) + { + watcher.EnableRaisingEvents = false; + watcher.Dispose(); + } + + _Watchers.Clear(); } private static void ExtractDialogueFilesPatch(ILContext il) @@ -148,6 +191,35 @@ internal class DialogueLoader : ModSystem } } + private static void HandleFileUpdate(Mod mod, string filePath) + { + if (!TryGetDialogueFileInfo(filePath, out _, out _, out var dialogueKey)) + return; + + if (!_DialogueLookup.TryGetValue(dialogueKey, out var existingEntry)) + return; + + if (existingEntry.ProviderMod != mod) + return; + + try + { + using var stream = new StreamReader(File.OpenRead(filePath), Encoding.UTF8); + _DialogueLookup[dialogueKey] = existingEntry with + { + Data = JsonSerializer.Deserialize(stream.BaseStream) + }; + + var hotreloadedMessage = $"Dialogue entry has hot reloaded: '{dialogueKey}', from source: '{filePath}'"; + CalamityMod.Log.Info(hotreloadedMessage); + if (!Main.gameMenu) Main.NewText(hotreloadedMessage); + } + catch (Exception e) + { + CalamityMod.Log.Error($"Error while hot reloading DialogueTextData entry ({filePath}): {e}"); + } + } + private static IEnumerable GetDialogueTextDatas(Mod mod, GameCulture targetCulture, bool skipDeserializeData = false) { if (mod == null) @@ -158,19 +230,7 @@ internal class DialogueLoader : ModSystem foreach (var file in mod.File) { - if (!Path.GetExtension(file.Name).Equals(".json", StringComparison.InvariantCultureIgnoreCase)) - continue; - - if (!LocalizationLoader.TryGetCultureAndPrefixFromPath(file.Name, out var culture, out var prefix)) - continue; - - string fileName = Path.GetFileNameWithoutExtension(file.Name); - string dialogueKey; - if (fileName.StartsWith($"{prefix}_{DialogueFilePrefix}", StringComparison.InvariantCultureIgnoreCase)) - dialogueKey = fileName[$"{prefix}_{DialogueFilePrefix}".Length..]; - else if (fileName.StartsWith(DialogueFilePrefix, StringComparison.InvariantCultureIgnoreCase)) - dialogueKey = fileName[DialogueFilePrefix.Length..]; - else + if (!TryGetDialogueFileInfo(file.Name, out var culture, out var prefix, out var dialogueKey)) continue; // Explictly Allow Default Culture File. @@ -194,8 +254,35 @@ internal class DialogueLoader : ModSystem if (data != null || skipDeserializeData) { - yield return new DialogueTextDataEntry(file.Name, dialogueKey, data); + yield return new DialogueTextDataEntry(mod, file.Name, dialogueKey, data); } } } + + private static bool TryGetDialogueFileInfo(string filePath, out GameCulture culture, out string prefix, out string dialogueKey) + { + if (!Path.GetExtension(filePath).Equals(".json", StringComparison.InvariantCultureIgnoreCase)) + goto EXIT_INVALID; + + if (!LocalizationLoader.TryGetCultureAndPrefixFromPath(filePath, out culture, out prefix)) + goto EXIT_INVALID; + + string fileName = Path.GetFileNameWithoutExtension(filePath); + if (fileName.StartsWith($"{prefix}_{DialogueFilePrefix}", StringComparison.InvariantCultureIgnoreCase)) + dialogueKey = fileName[$"{prefix}_{DialogueFilePrefix}".Length..]; + else if (fileName.StartsWith(DialogueFilePrefix, StringComparison.InvariantCultureIgnoreCase)) + dialogueKey = fileName[DialogueFilePrefix.Length..]; + else + goto EXIT_INVALID; + return true; + +EXIT_INVALID: + culture = null; + prefix = null; + dialogueKey = null; + return false; + } + + [GeneratedRegex(@".*?CalamityDialogue\..+?\.jsonc?$", RegexOptions.IgnoreCase)] + private static partial Regex CalamityDialogueFileRegex(); } diff --git a/Utilities/MainThreadedFileSystemWatcher.cs b/Utilities/MainThreadedFileSystemWatcher.cs new file mode 100644 index 0000000000..e282d84798 --- /dev/null +++ b/Utilities/MainThreadedFileSystemWatcher.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.ObjectModel; +using System.IO; +using System.Text.RegularExpressions; +using Terraria; + +namespace CalamityMod.Utilities +{ + internal sealed class MainThreadedFileSystemWatcher : IDisposable + { + public string Path + { + get => _FSW.Path; + set => _FSW.Path = value; + } + + public string Filter + { + get => _FSW.Filter; + set => _FSW.Filter = value; + } + + public Collection Filters + { + get => _FSW.Filters; + } + + public NotifyFilters NotifyFilter + { + get => _FSW.NotifyFilter; + set => _FSW.NotifyFilter = value; + } + + public bool IncludeSubdirectories + { + get => _FSW.IncludeSubdirectories; + set => _FSW.IncludeSubdirectories = value; + } + + public bool EnableRaisingEvents + { + get => _FSW.EnableRaisingEvents; + set => _FSW.EnableRaisingEvents = value; + } + + public Regex FileNameFilter { get; set; } = null; + + public event FileSystemEventHandler Changed; + public event RenamedEventHandler Renamed; + + public TimeSpan ChangedEventCooldown { get; set; } = TimeSpan.FromSeconds(0.1f); + public TimeSpan RenamedEventCooldown { get; set; } = TimeSpan.FromSeconds(0.1f); + + private FileSystemWatcher _FSW; + private DateTime _LastChangedEventDateTime = DateTime.Now; + private DateTime _LastRenamedEventDateTime = DateTime.Now; + private bool disposedValue; + + public MainThreadedFileSystemWatcher() + { + _FSW = new(); + _FSW.Changed += (o, arg) => + { + if (DateTime.Now - _LastChangedEventDateTime < ChangedEventCooldown) + return; + + if (!(FileNameFilter?.IsMatch(System.IO.Path.GetFileName(arg.Name)) ?? true)) + return; + + Main.QueueMainThreadAction(() => Changed?.Invoke(o, arg)); + _LastChangedEventDateTime = DateTime.Now; + }; + + _FSW.Renamed += (o, arg) => + { + if (DateTime.Now - _LastRenamedEventDateTime < RenamedEventCooldown) + return; + + if (!(FileNameFilter?.IsMatch(System.IO.Path.GetFileName(arg.Name)) ?? true)) + return; + + Main.QueueMainThreadAction(() => Renamed?.Invoke(o, arg)); + _LastRenamedEventDateTime = DateTime.Now; + }; + } + + private void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + _FSW?.Dispose(); + } + + _FSW = null; + disposedValue = true; + } + } + + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + } +}