Minimal validation for DevPlugin and ThirdRepo (#1176)

This commit is contained in:
Ottermandias 2023-04-06 21:37:47 +02:00 committed by GitHub
parent c71f876fb1
commit d0914133b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 24 deletions

View file

@ -1,6 +1,7 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
@ -55,13 +56,14 @@ public class DevPluginsSettingsEntry : SettingsEntry
public override void Draw()
{
ImGui.Text(this.Name);
using var id = ImRaii.PushId("devPluginLocation");
ImGui.TextUnformatted(this.Name);
if (this.devPluginLocationsChanged)
{
using (ImRaii.PushColor(ImGuiCol.Text, ImGuiColors.HealerGreen))
{
ImGui.SameLine();
ImGui.Text(Loc.Localize("DalamudSettingsChanged", "(Changed)"));
ImGui.TextUnformatted(Loc.Localize("DalamudSettingsChanged", "(Changed)"));
}
}
@ -77,13 +79,13 @@ public class DevPluginsSettingsEntry : SettingsEntry
ImGui.Separator();
ImGui.Text("#");
ImGui.TextUnformatted("#");
ImGui.NextColumn();
ImGui.Text("Path");
ImGui.TextUnformatted("Path");
ImGui.NextColumn();
ImGui.Text("Enabled");
ImGui.TextUnformatted("Enabled");
ImGui.NextColumn();
ImGui.Text(string.Empty);
ImGui.TextUnformatted(string.Empty);
ImGui.NextColumn();
ImGui.Separator();
@ -95,10 +97,10 @@ public class DevPluginsSettingsEntry : SettingsEntry
{
var isEnabled = devPluginLocationSetting.IsEnabled;
ImGui.PushID($"devPluginLocation_{devPluginLocationSetting.Path}");
id.Push(devPluginLocationSetting.Path);
ImGui.SetCursorPosX(ImGui.GetCursorPosX() + (ImGui.GetColumnWidth() / 2) - 8 - (ImGui.CalcTextSize(locNumber.ToString()).X / 2));
ImGui.Text(locNumber.ToString());
ImGui.TextUnformatted(locNumber.ToString());
ImGui.NextColumn();
ImGui.SetNextItemWidth(-1);
@ -115,6 +117,11 @@ public class DevPluginsSettingsEntry : SettingsEntry
this.devPluginLocationAddError = Loc.Localize("DalamudDevPluginLocationExists", "Location already exists.");
Task.Delay(5000).ContinueWith(t => this.devPluginLocationAddError = string.Empty);
}
else if (!ValidDevPluginPath(path))
{
this.devPluginLocationAddError = Loc.Localize("DalamudDevPluginInvalid", "The entered value is not a valid path to a potential Dev Plugin.\nDid you mean to enter it as a custom plugin repository in the fields below instead?");
Task.Delay(5000).ContinueWith(t => this.devPluginLocationAddError = string.Empty);
}
else
{
devPluginLocationSetting.Path = path;
@ -133,7 +140,7 @@ public class DevPluginsSettingsEntry : SettingsEntry
locationToRemove = devPluginLocationSetting;
}
ImGui.PopID();
id.Pop();
ImGui.NextColumn();
ImGui.Separator();
@ -150,7 +157,7 @@ public class DevPluginsSettingsEntry : SettingsEntry
}
ImGui.SetCursorPosX(ImGui.GetCursorPosX() + (ImGui.GetColumnWidth() / 2) - 8 - (ImGui.CalcTextSize(locNumber.ToString()).X / 2));
ImGui.Text(locNumber.ToString());
ImGui.TextUnformatted(locNumber.ToString());
ImGui.NextColumn();
ImGui.SetNextItemWidth(-1);
ImGui.InputText("##devPluginLocationInput", ref this.devPluginTempLocation, 300);
@ -164,6 +171,11 @@ public class DevPluginsSettingsEntry : SettingsEntry
this.devPluginLocationAddError = Loc.Localize("DalamudDevPluginLocationExists", "Location already exists.");
Task.Delay(5000).ContinueWith(t => this.devPluginLocationAddError = string.Empty);
}
else if (!ValidDevPluginPath(this.devPluginTempLocation))
{
this.devPluginLocationAddError = Loc.Localize("DalamudDevPluginInvalid", "The entered value is not a valid path to a potential Dev Plugin.\nDid you mean to enter it as a custom plugin repository in the fields below instead?");
Task.Delay(5000).ContinueWith(t => this.devPluginLocationAddError = string.Empty);
}
else
{
this.devPluginLocations.Add(new DevPluginLocationSettings
@ -183,4 +195,7 @@ public class DevPluginsSettingsEntry : SettingsEntry
ImGuiHelpers.SafeTextColoredWrapped(new Vector4(1, 0, 0, 1), this.devPluginLocationAddError);
}
}
private static bool ValidDevPluginPath(string path)
=> Path.IsPathRooted(path) && (Path.GetExtension(path) == ".dll" || !Path.Exists(path) || Directory.Exists(path));
}

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
@ -51,13 +51,14 @@ public class ThirdRepoSettingsEntry : SettingsEntry
public override void Draw()
{
ImGui.Text(Loc.Localize("DalamudSettingsCustomRepo", "Custom Plugin Repositories"));
using var id = ImRaii.PushId("thirdRepo");
ImGui.TextUnformatted(Loc.Localize("DalamudSettingsCustomRepo", "Custom Plugin Repositories"));
if (this.thirdRepoListChanged)
{
using (ImRaii.PushColor(ImGuiCol.Text, ImGuiColors.HealerGreen))
{
ImGui.SameLine();
ImGui.Text(Loc.Localize("DalamudSettingsChanged", "(Changed)"));
ImGui.TextUnformatted(Loc.Localize("DalamudSettingsChanged", "(Changed)"));
}
}
@ -77,20 +78,20 @@ public class ThirdRepoSettingsEntry : SettingsEntry
ImGui.Separator();
ImGui.Text("#");
ImGui.TextUnformatted("#");
ImGui.NextColumn();
ImGui.Text("URL");
ImGui.TextUnformatted("URL");
ImGui.NextColumn();
ImGui.Text("Enabled");
ImGui.TextUnformatted("Enabled");
ImGui.NextColumn();
ImGui.Text(string.Empty);
ImGui.TextUnformatted(string.Empty);
ImGui.NextColumn();
ImGui.Separator();
ImGui.Text("0");
ImGui.TextUnformatted("0");
ImGui.NextColumn();
ImGui.Text("XIVLauncher");
ImGui.TextUnformatted("XIVLauncher");
ImGui.NextColumn();
ImGui.NextColumn();
ImGui.NextColumn();
@ -103,10 +104,10 @@ public class ThirdRepoSettingsEntry : SettingsEntry
{
var isEnabled = thirdRepoSetting.IsEnabled;
ImGui.PushID($"thirdRepo_{thirdRepoSetting.Url}");
id.Push(thirdRepoSetting.Url);
ImGui.SetCursorPosX(ImGui.GetCursorPosX() + (ImGui.GetColumnWidth() / 2) - 8 - (ImGui.CalcTextSize(repoNumber.ToString()).X / 2));
ImGui.Text(repoNumber.ToString());
ImGui.TextUnformatted(repoNumber.ToString());
ImGui.NextColumn();
ImGui.SetNextItemWidth(-1);
@ -123,6 +124,11 @@ public class ThirdRepoSettingsEntry : SettingsEntry
this.thirdRepoAddError = Loc.Localize("DalamudThirdRepoExists", "Repo already exists.");
Task.Delay(5000).ContinueWith(t => this.thirdRepoAddError = string.Empty);
}
else if (!ValidThirdPartyRepoUrl(url))
{
this.thirdRepoAddError = Loc.Localize("DalamudThirdRepoNotUrl", "The entered address is not a valid URL.\nDid you mean to enter it as a DevPlugin in the fields above instead?");
Task.Delay(5000).ContinueWith(t => this.thirdRepoAddError = string.Empty);
}
else
{
thirdRepoSetting.Url = url;
@ -145,7 +151,7 @@ public class ThirdRepoSettingsEntry : SettingsEntry
repoToRemove = thirdRepoSetting;
}
ImGui.PopID();
id.Pop();
ImGui.NextColumn();
ImGui.Separator();
@ -162,7 +168,7 @@ public class ThirdRepoSettingsEntry : SettingsEntry
}
ImGui.SetCursorPosX(ImGui.GetCursorPosX() + (ImGui.GetColumnWidth() / 2) - 8 - (ImGui.CalcTextSize(repoNumber.ToString()).X / 2));
ImGui.Text(repoNumber.ToString());
ImGui.TextUnformatted(repoNumber.ToString());
ImGui.NextColumn();
ImGui.SetNextItemWidth(-1);
ImGui.InputText("##thirdRepoUrlInput", ref this.thirdRepoTempUrl, 300);
@ -177,6 +183,11 @@ public class ThirdRepoSettingsEntry : SettingsEntry
this.thirdRepoAddError = Loc.Localize("DalamudThirdRepoExists", "Repo already exists.");
Task.Delay(5000).ContinueWith(t => this.thirdRepoAddError = string.Empty);
}
else if (!ValidThirdPartyRepoUrl(this.thirdRepoTempUrl))
{
this.thirdRepoAddError = Loc.Localize("DalamudThirdRepoNotUrl", "The entered address is not a valid URL.\nDid you mean to enter it as a DevPlugin in the fields above instead?");
Task.Delay(5000).ContinueWith(t => this.thirdRepoAddError = string.Empty);
}
else
{
this.thirdRepoList.Add(new ThirdPartyRepoSettings
@ -196,4 +207,8 @@ public class ThirdRepoSettingsEntry : SettingsEntry
ImGuiHelpers.SafeTextColoredWrapped(new Vector4(1, 0, 0, 1), this.thirdRepoAddError);
}
}
private static bool ValidThirdPartyRepoUrl(string url)
=> Uri.TryCreate(url, UriKind.Absolute, out var uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttps || uriResult.Scheme == Uri.UriSchemeHttp);
}