refactor: centralise template parsing

This commit is contained in:
Anna 2024-02-17 23:40:58 -05:00
parent a162429ea4
commit 3f84307b61
Signed by: anna
GPG Key ID: D0943384CD9F87D1
3 changed files with 34 additions and 25 deletions

View File

@ -3,8 +3,6 @@ using System.Drawing.Imaging;
using Blake3;
using Dalamud.Game.Command;
using Newtonsoft.Json;
using Scriban;
using Scriban.Parsing;
using WebP.Net;
namespace Screenie;
@ -101,17 +99,14 @@ internal class Command : IDisposable {
private FileStream OpenFile(string ext, ScreenshotMetadata meta) {
Directory.CreateDirectory(this.Plugin.Config.SaveDirectory);
var template = Template.Parse(
this.Plugin.Config.SaveFileNameFormat,
parserOptions: new ParserOptions {
LiquidFunctionsToScriban = true,
}
);
var fileName = template.Render(meta);
var fileName = this.Plugin.Config.SaveFileNameTemplate.Render(meta);
var path = Path.Join(this.Plugin.Config.SaveDirectory, fileName);
path += $".{ext}";
var parent = Path.Join(path, "..");
Directory.CreateDirectory(parent);
return new FileStream(path, FileMode.Create, FileAccess.Write);
}
}

View File

@ -1,5 +1,7 @@
using System.Drawing.Imaging;
using Dalamud.Configuration;
using Scriban;
using Scriban.Parsing;
namespace Screenie;
@ -11,6 +13,28 @@ public class Configuration : IPluginConfiguration {
public Format SaveFormat = Format.Png;
public int SaveFormatData = 90;
public string SaveFileNameFormat = "{{ captured_at_local | date.to_string '%Y/%m/[%H:%M:%S]' }} {{ active_character.name }} - {{ location }}";
private int _templateHashCode;
private Template? _template;
internal Template SaveFileNameTemplate {
get {
var currentHash = this.SaveFileNameFormat.GetHashCode();
if (currentHash == this._templateHashCode && this._template != null) {
return this._template;
}
this._templateHashCode = currentHash;
this._template = Template.Parse(
this.SaveFileNameFormat,
parserOptions: new ParserOptions {
LiquidFunctionsToScriban = true,
}
);
return this._template;
}
}
}
public enum Format {

View File

@ -6,8 +6,6 @@ using Dalamud.Interface.ImGuiFileDialog;
using Dalamud.Interface.Utility;
using ImGuiNET;
using Screenie.Util;
using Scriban;
using Scriban.Parsing;
namespace Screenie.Ui;
@ -15,11 +13,10 @@ internal class PluginUi : IDisposable {
private Plugin Plugin { get; }
private FileDialogManager FileDialogManager { get; }
private ScreenshotMetadata Metadata { get; set; }
private Template? Template { get; set; }
internal bool Visible;
private Stopwatch _metaUpdate = Stopwatch.StartNew();
private readonly Stopwatch _metaUpdate = Stopwatch.StartNew();
internal PluginUi(Plugin plugin) {
this.Plugin = plugin;
@ -94,19 +91,12 @@ internal class PluginUi : IDisposable {
ImGui.TextUnformatted("Filename format");
ImGui.SetNextItemWidth(-1);
var templateChanged = ImGui.InputText("##filename-format", ref this.Plugin.Config.SaveFileNameFormat, 1024);
anyChanged |= templateChanged;
if (this.Template == null || templateChanged) {
this.Template = Template.Parse(
this.Plugin.Config.SaveFileNameFormat,
parserOptions: new ParserOptions {
LiquidFunctionsToScriban = true,
}
);
}
anyChanged |= ImGui.InputText("##filename-format", ref this.Plugin.Config.SaveFileNameFormat, 1024);
if (this.Template != null) {
ImGui.TextUnformatted(this.Template.Render(this.Metadata));
try {
ImGui.TextUnformatted(this.Plugin.Config.SaveFileNameTemplate.Render(this.Metadata));
} catch (Exception ex) {
ImGui.TextUnformatted($"Invalid template: {ex.Message}");
}
if (anyChanged) {