mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-18 05:47:43 +01:00
feat: add sanitizer
This commit is contained in:
parent
09e651b3ab
commit
4504bb049f
6 changed files with 230 additions and 1 deletions
|
|
@ -13,6 +13,7 @@ using Dalamud.Game.ClientState;
|
|||
using Dalamud.Game.Command;
|
||||
using Dalamud.Game.Internal;
|
||||
using Dalamud.Interface;
|
||||
using Dalamud.Plugin.Sanitizer;
|
||||
|
||||
namespace Dalamud.Plugin
|
||||
{
|
||||
|
|
@ -48,6 +49,7 @@ namespace Dalamud.Plugin
|
|||
this.pluginName = pluginName;
|
||||
this.configs = configs;
|
||||
|
||||
this.Sanitizer = new Sanitizer.Sanitizer(this.Data.Language);
|
||||
this.UiLanguage = this.dalamud.Configuration.LanguageOverride;
|
||||
dalamud.LocalizationManager.OnLocalizationChanged += this.OnLocalizationChanged;
|
||||
}
|
||||
|
|
@ -132,6 +134,11 @@ namespace Dalamud.Plugin
|
|||
/// </summary>
|
||||
public string UiLanguage { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets serializer class with functions to remove special characters from strings.
|
||||
/// </summary>
|
||||
public ISanitizer Sanitizer { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the action that should be executed when any plugin sends a message.
|
||||
/// </summary>
|
||||
|
|
|
|||
40
Dalamud/Plugin/Sanitizer/ISanitizer.cs
Normal file
40
Dalamud/Plugin/Sanitizer/ISanitizer.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Dalamud.Plugin.Sanitizer
|
||||
{
|
||||
/// <summary>
|
||||
/// Sanitize strings to remove soft hyphens and other special characters.
|
||||
/// </summary>
|
||||
public interface ISanitizer
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a sanitized string using current clientLanguage.
|
||||
/// </summary>
|
||||
/// <param name="unsanitizedString">An unsanitized string to sanitize.</param>
|
||||
/// <returns>A sanitized string.</returns>
|
||||
string Sanitize(string unsanitizedString);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a sanitized string using request clientLanguage.
|
||||
/// </summary>
|
||||
/// <param name="unsanitizedString">An unsanitized string to sanitize.</param>
|
||||
/// <param name="clientLanguage">Target language for sanitized strings.</param>
|
||||
/// <returns>A sanitized string.</returns>
|
||||
string Sanitize(string unsanitizedString, ClientLanguage clientLanguage);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of sanitized strings using current clientLanguage.
|
||||
/// </summary>
|
||||
/// <param name="unsanitizedStrings">List of unsanitized string to sanitize.</param>
|
||||
/// <returns>A list of sanitized strings.</returns>
|
||||
IEnumerable<string> Sanitize(IEnumerable<string> unsanitizedStrings);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of sanitized strings using requested clientLanguage.
|
||||
/// </summary>
|
||||
/// <param name="unsanitizedStrings">List of unsanitized string to sanitize.</param>
|
||||
/// <param name="clientLanguage">Target language for sanitized strings.</param>
|
||||
/// <returns>A list of sanitized strings.</returns>
|
||||
IEnumerable<string> Sanitize(IEnumerable<string> unsanitizedStrings, ClientLanguage clientLanguage);
|
||||
}
|
||||
}
|
||||
113
Dalamud/Plugin/Sanitizer/Sanitizer.cs
Normal file
113
Dalamud/Plugin/Sanitizer/Sanitizer.cs
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Dalamud.Plugin.Sanitizer
|
||||
{
|
||||
/// <summary>
|
||||
/// Sanitize strings to remove soft hyphens and other special characters.
|
||||
/// </summary>
|
||||
public class Sanitizer : ISanitizer
|
||||
{
|
||||
private readonly KeyValuePair<string, string> softHyphen = new KeyValuePair<string, string>("\u0002\u0016\u0001\u0003", string.Empty);
|
||||
private readonly KeyValuePair<string, string> emphasisOpen = new KeyValuePair<string, string>("\u0002\u001A\u0003", string.Empty);
|
||||
private readonly KeyValuePair<string, string> emphasisClose = new KeyValuePair<string, string>("\u0002\u001A\u0001\u0003", string.Empty);
|
||||
private readonly KeyValuePair<string, string> indent = new KeyValuePair<string, string>("\u0002\u001D\u0001\u0003", string.Empty);
|
||||
private readonly KeyValuePair<string, string> dagger = new KeyValuePair<string, string>("\u0020\u2020", string.Empty);
|
||||
private readonly KeyValuePair<string, string> ligatureOE = new KeyValuePair<string, string>("\u0153", "\u006F\u0065");
|
||||
private readonly List<KeyValuePair<string, string>> defaultSanitizationList;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Sanitizer"/> class.
|
||||
/// </summary>
|
||||
/// <param name="clientLanguage">Default clientLanguage for sanitizing strings.</param>
|
||||
public Sanitizer(ClientLanguage clientLanguage)
|
||||
{
|
||||
this.defaultSanitizationList = this.BuildSanitizationList(clientLanguage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a sanitized string using current clientLanguage.
|
||||
/// </summary>
|
||||
/// <param name="unsanitizedString">An unsanitized string to sanitize.</param>
|
||||
/// <returns>A sanitized string.</returns>
|
||||
public string Sanitize(string unsanitizedString)
|
||||
{
|
||||
return this.defaultSanitizationList == null ? unsanitizedString : ApplySanitizationList(unsanitizedString, this.defaultSanitizationList);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a sanitized string using request clientLanguage.
|
||||
/// </summary>
|
||||
/// <param name="unsanitizedString">An unsanitized string to sanitize.</param>
|
||||
/// <param name="clientLanguage">Target language for sanitized strings.</param>
|
||||
/// <returns>A sanitized string.</returns>
|
||||
public string Sanitize(string unsanitizedString, ClientLanguage clientLanguage)
|
||||
{
|
||||
var newSanitizationList = this.BuildSanitizationList(clientLanguage);
|
||||
return newSanitizationList == null ? unsanitizedString : ApplySanitizationList(unsanitizedString, newSanitizationList);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of sanitized strings using current clientLanguage.
|
||||
/// </summary>
|
||||
/// <param name="unsanitizedStrings">List of unsanitized string to sanitize.</param>
|
||||
/// <returns>A list of sanitized strings.</returns>
|
||||
public IEnumerable<string> Sanitize(IEnumerable<string> unsanitizedStrings)
|
||||
{
|
||||
return this.defaultSanitizationList == null ? unsanitizedStrings.Select(unsanitizedString => unsanitizedString) :
|
||||
unsanitizedStrings.Select(unsanitizedString => ApplySanitizationList(unsanitizedString, this.defaultSanitizationList));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of sanitized strings using requested clientLanguage.
|
||||
/// </summary>
|
||||
/// <param name="unsanitizedStrings">List of unsanitized string to sanitize.</param>
|
||||
/// <param name="clientLanguage">Target language for sanitized strings.</param>
|
||||
/// <returns>A list of sanitized strings.</returns>
|
||||
public IEnumerable<string> Sanitize(IEnumerable<string> unsanitizedStrings, ClientLanguage clientLanguage)
|
||||
{
|
||||
var newSanitizationList = this.BuildSanitizationList(clientLanguage);
|
||||
return newSanitizationList == null ? unsanitizedStrings.Select(unsanitizedString => unsanitizedString) :
|
||||
unsanitizedStrings.Select(unsanitizedString => ApplySanitizationList(unsanitizedString, newSanitizationList));
|
||||
}
|
||||
|
||||
private static string ApplySanitizationList(string unsanitizedString, IEnumerable<KeyValuePair<string, string>> sanitizationList)
|
||||
{
|
||||
var sanitizedValue = new StringBuilder(unsanitizedString);
|
||||
foreach (var item in sanitizationList) sanitizedValue.Replace(item.Key, item.Value);
|
||||
return sanitizedValue.ToString();
|
||||
}
|
||||
|
||||
private List<KeyValuePair<string, string>> BuildSanitizationList(ClientLanguage clientLanguage)
|
||||
{
|
||||
switch (clientLanguage)
|
||||
{
|
||||
case ClientLanguage.Japanese:
|
||||
break;
|
||||
case ClientLanguage.English:
|
||||
break;
|
||||
case ClientLanguage.German:
|
||||
return new List<KeyValuePair<string, string>>
|
||||
{
|
||||
this.softHyphen,
|
||||
this.emphasisOpen,
|
||||
this.emphasisClose,
|
||||
this.dagger,
|
||||
};
|
||||
case ClientLanguage.French:
|
||||
return new List<KeyValuePair<string, string>>
|
||||
{
|
||||
this.softHyphen,
|
||||
this.indent,
|
||||
this.ligatureOE,
|
||||
};
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue