Skip to content

Commit

Permalink
Migrate mod presets to deferred settings system
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Aug 15, 2024
1 parent de82349 commit 53f77f9
Show file tree
Hide file tree
Showing 33 changed files with 546 additions and 313 deletions.
8 changes: 5 additions & 3 deletions Bloxstrap/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Reflection;
using System.Security.Cryptography;
using System.Windows;
using System.Windows.Threading;

using Microsoft.Win32;

using Bloxstrap.Resources;
using Bloxstrap.Models.SettingTasks;
using Bloxstrap.Models.SettingTasks.Base;

namespace Bloxstrap
{
Expand All @@ -29,11 +29,13 @@ public partial class App : Application

public static string Version = Assembly.GetExecutingAssembly().GetName().Version!.ToString()[..^2];

public static readonly MD5 MD5Provider = MD5.Create();

public static NotifyIconWrapper? NotifyIcon { get; set; }

public static readonly Logger Logger = new();

public static readonly Dictionary<string, ISettingTask> PendingSettingTasks = new();
public static readonly Dictionary<string, BaseTask> PendingSettingTasks = new();

public static readonly JsonManager<Settings> Settings = new();

Expand Down
114 changes: 0 additions & 114 deletions Bloxstrap/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -864,72 +864,6 @@ private async Task ApplyModifications()
if (!Directory.Exists(Paths.Modifications))
Directory.CreateDirectory(Paths.Modifications);

// cursors
await CheckModPreset(App.Settings.Prop.CursorType == CursorType.From2006, new Dictionary<string, string>
{
{ @"content\textures\Cursors\KeyboardMouse\ArrowCursor.png", "Cursor.From2006.ArrowCursor.png" },
{ @"content\textures\Cursors\KeyboardMouse\ArrowFarCursor.png", "Cursor.From2006.ArrowFarCursor.png" }
});

await CheckModPreset(App.Settings.Prop.CursorType == CursorType.From2013, new Dictionary<string, string>
{
{ @"content\textures\Cursors\KeyboardMouse\ArrowCursor.png", "Cursor.From2013.ArrowCursor.png" },
{ @"content\textures\Cursors\KeyboardMouse\ArrowFarCursor.png", "Cursor.From2013.ArrowFarCursor.png" }
});

// character sounds
await CheckModPreset(App.Settings.Prop.UseOldDeathSound, @"content\sounds\ouch.ogg", "Sounds.OldDeath.ogg");

await CheckModPreset(App.Settings.Prop.UseOldCharacterSounds, new Dictionary<string, string>
{
{ @"content\sounds\action_footsteps_plastic.mp3", "Sounds.OldWalk.mp3" },
{ @"content\sounds\action_jump.mp3", "Sounds.OldJump.mp3" },
{ @"content\sounds\action_get_up.mp3", "Sounds.OldGetUp.mp3" },
{ @"content\sounds\action_falling.mp3", "Sounds.Empty.mp3" },
{ @"content\sounds\action_jump_land.mp3", "Sounds.Empty.mp3" },
{ @"content\sounds\action_swim.mp3", "Sounds.Empty.mp3" },
{ @"content\sounds\impact_water.mp3", "Sounds.Empty.mp3" }
});

// Mobile.rbxl
await CheckModPreset(App.Settings.Prop.UseOldAvatarBackground, @"ExtraContent\places\Mobile.rbxl", "OldAvatarBackground.rbxl");

// emoji presets are downloaded remotely from github due to how large they are
string contentFonts = Path.Combine(Paths.Modifications, "content\\fonts");
string emojiFontLocation = Path.Combine(contentFonts, "TwemojiMozilla.ttf");
string emojiFontHash = File.Exists(emojiFontLocation) ? MD5Hash.FromFile(emojiFontLocation) : "";

if (App.Settings.Prop.EmojiType == EmojiType.Default && EmojiTypeEx.Hashes.Values.Contains(emojiFontHash))
{
App.Logger.WriteLine(LOG_IDENT, "Reverting to default emoji font");

File.Delete(emojiFontLocation);
}
else if (App.Settings.Prop.EmojiType != EmojiType.Default && emojiFontHash != App.Settings.Prop.EmojiType.GetHash())
{
App.Logger.WriteLine(LOG_IDENT, $"Configuring emoji font as {App.Settings.Prop.EmojiType}");

if (emojiFontHash != "")
File.Delete(emojiFontLocation);

Directory.CreateDirectory(contentFonts);

try
{
var response = await App.HttpClient.GetAsync(App.Settings.Prop.EmojiType.GetUrl());
response.EnsureSuccessStatusCode();
await using var fileStream = new FileStream(emojiFontLocation, FileMode.CreateNew);
await response.Content.CopyToAsync(fileStream);
}
catch (HttpRequestException ex)
{
App.Logger.WriteLine(LOG_IDENT, $"Failed to fetch emoji preset from Github");
App.Logger.WriteException(LOG_IDENT, ex);
Frontend.ShowMessageBox(string.Format(Strings.Bootstrapper_EmojiPresetFetchFailed, App.Settings.Prop.EmojiType), MessageBoxImage.Warning);
App.Settings.Prop.EmojiType = EmojiType.Default;
}
}

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

Expand Down Expand Up @@ -1043,54 +977,6 @@ private async Task ApplyModifications()
App.Logger.WriteLine(LOG_IDENT, $"Finished checking file mods");
}

private static async Task CheckModPreset(bool condition, string location, string name)
{
string LOG_IDENT = $"Bootstrapper::CheckModPreset.{name}";

string fullLocation = Path.Combine(Paths.Modifications, location);
string fileHash = File.Exists(fullLocation) ? MD5Hash.FromFile(fullLocation) : "";

if (!condition && fileHash == "")
return;

byte[] embeddedData = string.IsNullOrEmpty(name) ? Array.Empty<byte>() : await Resource.Get(name);
string embeddedHash = MD5Hash.FromBytes(embeddedData);

if (!condition)
{
if (fileHash == embeddedHash)
{
App.Logger.WriteLine(LOG_IDENT, $"Deleting '{location}' as preset is disabled, and mod file matches preset");

Filesystem.AssertReadOnly(fullLocation);
File.Delete(fullLocation);
}

return;
}

if (fileHash != embeddedHash)
{
App.Logger.WriteLine(LOG_IDENT, $"Writing '{location}' as preset is enabled, and mod file does not exist or does not match preset");

Directory.CreateDirectory(Path.GetDirectoryName(fullLocation)!);

if (File.Exists(fullLocation))
{
Filesystem.AssertReadOnly(fullLocation);
File.Delete(fullLocation);
}

await File.WriteAllBytesAsync(fullLocation, embeddedData);
}
}

private static async Task CheckModPreset(bool condition, Dictionary<string, string> mapping)
{
foreach (var pair in mapping)
await CheckModPreset(condition, pair.Key, pair.Value);
}

private async Task DownloadPackage(Package package)
{
string LOG_IDENT = $"Bootstrapper::DownloadPackage.{package.Name}";
Expand Down
5 changes: 5 additions & 0 deletions Bloxstrap/Enums/CursorType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
{
public enum CursorType
{
[EnumSort(Order = 1)]
[EnumName(FromTranslation = "Common.Default")]
Default,

[EnumSort(Order = 3)]
From2006,

[EnumSort(Order = 2)]
From2013
}
}
12 changes: 0 additions & 12 deletions Bloxstrap/Extensions/CursorTypeEx.cs

This file was deleted.

11 changes: 1 addition & 10 deletions Bloxstrap/Extensions/EmojiTypeEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@
{
static class EmojiTypeEx
{
public static IReadOnlyCollection<EmojiType> Selections => new EmojiType[]
{
EmojiType.Default,
EmojiType.Catmoji,
EmojiType.Windows11,
EmojiType.Windows10,
EmojiType.Windows8
};

public static IReadOnlyDictionary<EmojiType, string> Filenames => new Dictionary<EmojiType, string>
{
{ EmojiType.Catmoji, "Catmoji.ttf" },
Expand All @@ -34,7 +25,7 @@ public static string GetUrl(this EmojiType emojiType)
if (emojiType == EmojiType.Default)
return "";

return $"https://github.com/bloxstraplabs/rbxcustom-fontemojis/releases/download/my-phone-is-78-percent/{Filenames[emojiType]}";
return $"https://github.com/bloxstraplabs/rbxcustom-fontemoji/releases/download/my-phone-is-78-percent/{Filenames[emojiType]}";
}
}
}
4 changes: 0 additions & 4 deletions Bloxstrap/FastFlagManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using Bloxstrap.Enums.FlagPresets;
using System.Windows.Forms;

using Windows.Win32;
using Windows.Win32.Graphics.Gdi;

namespace Bloxstrap
{
Expand Down
1 change: 1 addition & 0 deletions Bloxstrap/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
global using Bloxstrap.Models.BloxstrapRPC;
global using Bloxstrap.Models.RobloxApi;
global using Bloxstrap.Models.Manifest;
global using Bloxstrap.Resources;
global using Bloxstrap.UI;
global using Bloxstrap.Utility;
3 changes: 2 additions & 1 deletion Bloxstrap/JsonManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Bloxstrap
{
public class JsonManager<T> where T : new()
public class JsonManager<T> where T : class, new()
{
public T Prop { get; set; } = new();

public virtual string FileLocation => Path.Combine(Paths.Base, $"{typeof(T).Name}.json");

private string LOG_IDENT_CLASS => $"JsonManager<{typeof(T).Name}>";
Expand Down
13 changes: 13 additions & 0 deletions Bloxstrap/Models/Attributes/EnumSortAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bloxstrap.Models.Attributes
{
class EnumSortAttribute : Attribute
{
public int Order { get; set; }
}
}
40 changes: 40 additions & 0 deletions Bloxstrap/Models/ModPresetFileData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Security.Cryptography;
using System.Windows.Markup;

namespace Bloxstrap.Models
{
public class ModPresetFileData
{
public string FilePath { get; private set; }

public string FullFilePath => Path.Combine(Paths.Modifications, FilePath);

public FileStream FileStream => File.OpenRead(FullFilePath);

public string ResourceIdentifier { get; private set; }

public Stream ResourceStream => Resource.GetStream(ResourceIdentifier);

public byte[] ResourceHash { get; private set; }

public ModPresetFileData(string contentPath, string resource)
{
FilePath = contentPath;
ResourceIdentifier = resource;

using var stream = ResourceStream;
ResourceHash = App.MD5Provider.ComputeHash(stream);
}

public bool HashMatches()
{
if (!File.Exists(FullFilePath))
return false;

using var fileStream = FileStream;
var fileHash = App.MD5Provider.ComputeHash(fileStream);

return fileHash.SequenceEqual(ResourceHash);
}
}
}
21 changes: 21 additions & 0 deletions Bloxstrap/Models/SettingTasks/Base/BaseTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bloxstrap.Models.SettingTasks.Base
{
public abstract class BaseTask
{
public string Name { get; private set; }

public abstract bool Changed { get; }

public BaseTask(string prefix, string name) => Name = $"{prefix}.{name}";

public override string ToString() => Name;

public abstract void Execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,28 @@
using System.Text;
using System.Threading.Tasks;

namespace Bloxstrap.Models.SettingTasks
namespace Bloxstrap.Models.SettingTasks.Base
{
public class BaseTask : ISettingTask
public abstract class BoolBaseTask : BaseTask
{
private bool _originalState;

private bool _newState;

public string Name { get; set; } = "";
private bool _newState;

public bool OriginalState
public virtual bool OriginalState
{
get
{
return _originalState;
}
get => _originalState;

set
set
{
_originalState = value;
_newState = value;
}
}

public bool NewState
public virtual bool NewState
{
get
{
return _newState;
}
get => _newState;

set
{
Expand All @@ -42,6 +34,8 @@ public bool NewState
}
}

public virtual void Execute() => throw new NotImplementedException();
public override bool Changed => NewState != OriginalState;

public BoolBaseTask(string prefix, string name) : base(prefix, name) { }
}
}
Loading

0 comments on commit 53f77f9

Please sign in to comment.