Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pose Auto Save: sanitise paths to ensure they are valid on FS. #164

Open
wants to merge 1 commit into
base: v0.3/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions Ktisis/Editor/Posing/AutoSave/PoseAutoSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,20 @@ public void Configure(Configuration cfg) {
this._timer.Enabled = this._cfg.Enabled;
}

private void OnElapsed(object? sender, ElapsedEventArgs e) {
private async void OnElapsed(object? sender, ElapsedEventArgs e) {
if (!this.Posing.IsValid) {
this._timer.Stop();
return;
}

if (this._cfg.Enabled && this.Posing.IsEnabled)
this._framework.RunOnFrameworkThread(this.Save);
if (!this._cfg.Enabled || !this.Posing.IsEnabled)
return;

try {
await this._framework.RunOnFrameworkThread(this.Save);
} catch (Exception err) {
Ktisis.Log.Error($"Failed to save poses:\n{err}");
}
}

public void Save() {
Expand All @@ -88,9 +94,10 @@ public void Save() {
if (chara.Pose == null) continue;

var dupeCt = 1;
var path = Path.Combine(folder, $"{chara.Name}.pose");
var name = this._format.StripInvalidChars(chara.Name);
var path = Path.Combine(folder, $"{name}.pose");
while (Path.Exists(path))
path = Path.Combine(folder, $"{chara.Name} ({++dupeCt}).pose");
path = Path.Combine(folder, $"{name} ({++dupeCt}).pose");

var serializer = new JsonFileSerializer();
var file = new EntityPoseConverter(chara.Pose).SaveFile();
Expand Down
15 changes: 10 additions & 5 deletions Ktisis/Services/Data/FormatService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using Dalamud.Plugin.Services;
Expand Down Expand Up @@ -53,20 +54,24 @@ public string Replace(string path) {
};

private string GetPlayerName() {
return this._client.LocalPlayer?.Name.ToString() ?? "Unknown";
return this.StripInvalidChars(this._client.LocalPlayer?.Name.ToString() ?? "Unknown");
}

private string GetCurrentWorld() {
return this._client.LocalPlayer?.CurrentWorld.GameData?.Name.ToString() ?? "Unknown";
return this.StripInvalidChars(this._client.LocalPlayer?.CurrentWorld.GameData?.Name.ToString() ?? "Unknown");
}

private string GetHomeWorld() {
return this._client.LocalPlayer?.HomeWorld.GameData?.Name.ToString() ?? "Unknown";
return this.StripInvalidChars(this._client.LocalPlayer?.HomeWorld.GameData?.Name.ToString() ?? "Unknown");
}

private string GetZone() {
return this._data.GetExcelSheet<TerritoryType>()?
return this.StripInvalidChars(this._data.GetExcelSheet<TerritoryType>()?
.GetRow(this._client.TerritoryType)?.PlaceName.Value?.Name
.ToString() ?? "Unknown";
.ToString() ?? "Unknown");
}

public string StripInvalidChars(string str) {
return Path.GetInvalidFileNameChars().Aggregate(str, (current, c) => current.Replace(c, '_'));
}
}