font mod: validate chosen file is actually a font

This commit is contained in:
pizzaboxer 2024-05-18 23:23:12 +01:00
parent 2aa5367e1f
commit 6f1e023012
No known key found for this signature in database
GPG Key ID: 59D4A1DBAD0F2BA8
3 changed files with 27 additions and 0 deletions

View File

@ -2565,6 +2565,15 @@ namespace Bloxstrap.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to The file you have chosen does not appear to be a valid font file..
/// </summary>
public static string Menu_Mods_Misc_CustomFont_Invalid {
get {
return ResourceManager.GetString("Menu.Mods.Misc.CustomFont.Invalid", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Remove applied font.
/// </summary>

View File

@ -967,6 +967,9 @@ Selecting 'No' will ignore this warning and continue installation.</value>
<data name="Menu.Mods.Misc.CustomFont.Description" xml:space="preserve">
<value>Forces every in-game font to be a font that you choose.</value>
</data>
<data name="Menu.Mods.Misc.CustomFont.Invalid" xml:space="preserve">
<value>The file you have chosen does not appear to be a valid font file.</value>
</data>
<data name="Menu.Mods.Misc.CustomFont.Remove" xml:space="preserve">
<value>Remove applied font</value>
</data>

View File

@ -13,6 +13,13 @@ namespace Bloxstrap.UI.ViewModels.Menu
private bool _usingCustomFont => App.IsFirstRun && App.CustomFontLocation is not null || !App.IsFirstRun && File.Exists(Paths.CustomFont);
private readonly Dictionary<string, byte[]> FontHeaders = new()
{
{ "ttf", new byte[4] { 0x00, 0x01, 0x00, 0x00 } },
{ "otf", new byte[4] { 0x4F, 0x54, 0x54, 0x4F } },
{ "ttc", new byte[4] { 0x74, 0x74, 0x63, 0x66 } }
};
private void ManageCustomFont()
{
if (_usingCustomFont)
@ -32,6 +39,14 @@ namespace Bloxstrap.UI.ViewModels.Menu
if (dialog.ShowDialog() != true)
return;
string type = dialog.FileName.Substring(dialog.FileName.Length-3, 3).ToLower();
if (!File.ReadAllBytes(dialog.FileName).Take(4).SequenceEqual(FontHeaders[type]))
{
Frontend.ShowMessageBox(Resources.Strings.Menu_Mods_Misc_CustomFont_Invalid, MessageBoxImage.Error);
return;
}
if (App.IsFirstRun)
{
App.CustomFontLocation = dialog.FileName;