Skip to content

Commit

Permalink
Add bulk font replacement, Win32 for file picker
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Jul 15, 2023
1 parent 41ca47a commit 63ef907
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 14 deletions.
41 changes: 40 additions & 1 deletion Bloxstrap/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand Down Expand Up @@ -115,7 +116,7 @@ private void UpdateProgressBar()
if (Dialog is not null)
Dialog.ProgressValue = newProgress;
}

public async Task Run()
{
App.Logger.WriteLine("[Bootstrapper::Run] Running bootstrapper");
Expand Down Expand Up @@ -1035,6 +1036,44 @@ private async Task ApplyModifications()
await response.Content.CopyToAsync(fileStream);
}

// check custom font mod
// instead of replacing the fonts themselves, we'll just alter the font family manifests

string modFontFamiliesFolder = Path.Combine(Directories.Modifications, "content\\fonts\\families");
string customFontLocation = Path.Combine(Directories.Modifications, "content\\fonts\\CustomFont.ttf");

if (File.Exists(customFontLocation))
{
App.Logger.WriteLine("[Bootstrapper::ApplyModifications] Begin font check");

Directory.CreateDirectory(modFontFamiliesFolder);

foreach (string jsonFilePath in Directory.GetFiles(Path.Combine(_versionFolder, "content\\fonts\\families")))
{
string jsonFilename = Path.GetFileName(jsonFilePath);
string modFilepath = Path.Combine(modFontFamiliesFolder, jsonFilename);

if (File.Exists(modFilepath))
continue;

FontFamily? fontFamilyData = JsonSerializer.Deserialize<FontFamily>(File.ReadAllText(jsonFilePath));

if (fontFamilyData is null)
continue;

foreach (FontFace fontFace in fontFamilyData.Faces)
fontFace.AssetId = "rbxasset://fonts/CustomFont.ttf";

File.WriteAllText(modFilepath, JsonSerializer.Serialize(fontFamilyData, new JsonSerializerOptions { WriteIndented = true }));
}

App.Logger.WriteLine("[Bootstrapper::ApplyModifications] End font check");
}
else
{
Directory.Delete(modFontFamiliesFolder, true);
}

foreach (string file in Directory.GetFiles(modFolder, "*.*", SearchOption.AllDirectories))
{
// get relative directory path
Expand Down
20 changes: 20 additions & 0 deletions Bloxstrap/Models/FontFace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Bloxstrap.Models
{
public class FontFace
{
[JsonPropertyName("name")]
public string Name { get; set; } = null!;

[JsonPropertyName("weight")]
public int Weight { get; set; }

[JsonPropertyName("style")]
public string Style { get; set; } = null!;

[JsonPropertyName("assetId")]
public string AssetId { get; set; } = null!;
}
}
14 changes: 14 additions & 0 deletions Bloxstrap/Models/FontFamily.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Bloxstrap.Models
{
public class FontFamily
{
[JsonPropertyName("name")]
public string Name { get; set; } = null!;

[JsonPropertyName("faces")]
public IEnumerable<FontFace> Faces { get; set; } = null!;
}
}
2 changes: 1 addition & 1 deletion Bloxstrap/UI/Elements/Menu/Pages/InstallationPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Margin="0,0,4,0" Text="{Binding InstallLocation, Mode=TwoWay}" />
<ui:Button Grid.Column="1" Margin="4,0,4,0" Height="35" Icon="Folder24" Content="Browse" Command="{Binding BrowseInstallLocationCommand}" />
<ui:Button Grid.Column="2" Margin="4,0,0,0" Height="35" Icon="ArrowUndo24" Content="Reset" Command="{Binding ResetInstallLocationCommand}" />
<ui:Button Grid.Column="2" Margin="4,0,0,0" Height="35" Icon="ArrowCounterclockwise24" Content="Reset" Command="{Binding ResetInstallLocationCommand}" />
</Grid>
</ui:CardExpander>

Expand Down
16 changes: 15 additions & 1 deletion Bloxstrap/UI/Elements/Menu/Pages/ModsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@

<StackPanel x:Name="MiscellaneousOptions">
<TextBlock Text="Miscellaneous" FontSize="16" FontWeight="Medium" Margin="0,16,0,0" />
<ui:CardControl Margin="0,8,0,0">
<ui:CardControl.Header>
<StackPanel>
<TextBlock FontSize="14" Text="Apply custom font" />
<TextBlock Margin="0,2,0,0" FontSize="12" Foreground="{DynamicResource TextFillColorTertiaryBrush}">
Forces every in-game font to be a font that you choose.
</TextBlock>
</StackPanel>
</ui:CardControl.Header>
<StackPanel>
<ui:Button Icon="DocumentAdd16" Content="Choose font..." Appearance="Primary" Command="{Binding ManageCustomFontCommand}" Visibility="{Binding ChooseCustomFontVisibility, Mode=OneWay}" />
<ui:Button Icon="Delete16" Content="Remove applied font" Appearance="Danger" Command="{Binding ManageCustomFontCommand}" Visibility="{Binding DeleteCustomFontVisibility, Mode=OneWay}" />
</StackPanel>
</ui:CardControl>
<ui:CardControl Margin="0,8,0,0">
<ui:CardControl.Header>
<StackPanel>
Expand All @@ -122,7 +136,7 @@
</TextBlock>
</StackPanel>
</ui:CardControl.Header>
<ui:ToggleSwitch IsChecked="{Binding DisableFullscreenOptimizationsEnabled, Mode=TwoWay}" />
<ui:ToggleSwitch IsChecked="{Binding DisableFullscreenOptimizations, Mode=TwoWay}" />
</ui:CardControl>
</StackPanel>
</StackPanel>
Expand Down
18 changes: 10 additions & 8 deletions Bloxstrap/UI/ViewModels/Menu/AppearanceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Windows.Input;
using System.Windows.Media;

Expand Down Expand Up @@ -40,14 +40,16 @@ private void PreviewBootstrapper()

private void BrowseCustomIconLocation()
{
using var dialog = new OpenFileDialog();
dialog.Filter = "Icon files (*.ico)|*.ico|All files (*.*)|*.*";

if (dialog.ShowDialog() == DialogResult.OK)
var dialog = new OpenFileDialog
{
CustomIconLocation = dialog.FileName;
OnPropertyChanged(nameof(CustomIconLocation));
}
Filter = "Icon files|*.ico|All files|*.*"
};

if (dialog.ShowDialog() != true)
return;

CustomIconLocation = dialog.FileName;
OnPropertyChanged(nameof(CustomIconLocation));
}

public AppearanceViewModel(Page page)
Expand Down
46 changes: 43 additions & 3 deletions Bloxstrap/UI/ViewModels/Menu/ModsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,56 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;

using Bloxstrap.Enums;
using Bloxstrap.Extensions;

using Microsoft.Win32;

using CommunityToolkit.Mvvm.Input;

namespace Bloxstrap.UI.ViewModels.Menu
{
public class ModsViewModel
public class ModsViewModel : INotifyPropertyChanged
{
public ICommand OpenModsFolderCommand => new RelayCommand(OpenModsFolder);
public event PropertyChangedEventHandler? PropertyChanged;
public void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

private void OpenModsFolder() => Process.Start("explorer.exe", Directories.Modifications);

private string _customFontLocation = Path.Combine(Directories.Modifications, "content\\fonts\\CustomFont.ttf");
private bool _usingCustomFont => File.Exists(_customFontLocation);

private void ManageCustomFont()
{
if (_usingCustomFont)
{
File.Delete(_customFontLocation);
}
else
{
var dialog = new OpenFileDialog
{
Filter = "Font files|*.ttf;*.otf|All files|*.*"
};

if (dialog.ShowDialog() != true)
return;

Directory.CreateDirectory(Path.GetDirectoryName(_customFontLocation)!);
File.Copy(dialog.FileName, _customFontLocation);
}

OnPropertyChanged(nameof(ChooseCustomFontVisibility));
OnPropertyChanged(nameof(DeleteCustomFontVisibility));
}

public ICommand OpenModsFolderCommand => new RelayCommand(OpenModsFolder);

public bool OldDeathSoundEnabled
{
get => App.Settings.Prop.UseOldDeathSound;
Expand Down Expand Up @@ -50,7 +85,12 @@ public string SelectedEmojiType
set => App.Settings.Prop.EmojiType = EmojiTypes[value];
}

public bool DisableFullscreenOptimizationsEnabled
public Visibility ChooseCustomFontVisibility => _usingCustomFont ? Visibility.Collapsed : Visibility.Visible;
public Visibility DeleteCustomFontVisibility => _usingCustomFont ? Visibility.Visible : Visibility.Collapsed;

public ICommand ManageCustomFontCommand => new RelayCommand(ManageCustomFont);

public bool DisableFullscreenOptimizations
{
get => App.Settings.Prop.DisableFullscreenOptimizations;
set => App.Settings.Prop.DisableFullscreenOptimizations = value;
Expand Down

0 comments on commit 63ef907

Please sign in to comment.