From 6f1e023012b6e02ff0f718da2b22a28174ce5e76 Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Sat, 18 May 2024 23:23:12 +0100 Subject: [PATCH] font mod: validate chosen file is actually a font --- Bloxstrap/Resources/Strings.Designer.cs | 9 +++++++++ Bloxstrap/Resources/Strings.resx | 3 +++ Bloxstrap/UI/ViewModels/Menu/ModsViewModel.cs | 15 +++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/Bloxstrap/Resources/Strings.Designer.cs b/Bloxstrap/Resources/Strings.Designer.cs index c5faa51..9e16812 100644 --- a/Bloxstrap/Resources/Strings.Designer.cs +++ b/Bloxstrap/Resources/Strings.Designer.cs @@ -2565,6 +2565,15 @@ namespace Bloxstrap.Resources { } } + /// + /// Looks up a localized string similar to The file you have chosen does not appear to be a valid font file.. + /// + public static string Menu_Mods_Misc_CustomFont_Invalid { + get { + return ResourceManager.GetString("Menu.Mods.Misc.CustomFont.Invalid", resourceCulture); + } + } + /// /// Looks up a localized string similar to Remove applied font. /// diff --git a/Bloxstrap/Resources/Strings.resx b/Bloxstrap/Resources/Strings.resx index f1f8ae4..2122862 100644 --- a/Bloxstrap/Resources/Strings.resx +++ b/Bloxstrap/Resources/Strings.resx @@ -967,6 +967,9 @@ Selecting 'No' will ignore this warning and continue installation. Forces every in-game font to be a font that you choose. + + The file you have chosen does not appear to be a valid font file. + Remove applied font diff --git a/Bloxstrap/UI/ViewModels/Menu/ModsViewModel.cs b/Bloxstrap/UI/ViewModels/Menu/ModsViewModel.cs index 15ed80a..f5cbb98 100644 --- a/Bloxstrap/UI/ViewModels/Menu/ModsViewModel.cs +++ b/Bloxstrap/UI/ViewModels/Menu/ModsViewModel.cs @@ -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 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;