Skip to content

Commit 7bf5a7b

Browse files
l-2-jBanane9
andauthored
Bytes formatting IEC/Decimals option (#51)
* Update Finnish storage unit translations At `t` (tavua) is least better than whatever `it` was supposed to stand for * Use IEC byte names for finnish storage units * Remove loop from byte suffix retrieval * IEC/Decimal bytes format option * Adjust config names, add locales, make format refresh when IEC is changed --------- Co-authored-by: ljoonal <5854483+ljoonal@users.noreply.github.com> Co-authored-by: Arne Kiesewetter <banane9@live.de>
1 parent b0bf334 commit 7bf5a7b

8 files changed

Lines changed: 123 additions & 58 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using MonkeyLoader.Configuration;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace CommunityBugFixCollection
7+
{
8+
internal sealed class BugFixOptions : ConfigSection
9+
{
10+
private static readonly DefiningConfigKey<bool> _useIecByteFormat = new("UseIecByteFormat", "Whether to format bytes using IEC as opposed to decimal format when <i>LocalizedByteFormatting</i> is enabled.", () => true);
11+
12+
/// <inheritdoc/>
13+
public override string Description => "Contains the settings for the few fixes that offer them.";
14+
15+
/// <inheritdoc/>
16+
public override string Id => "Options";
17+
18+
/// <summary>
19+
/// Gets whether <see cref="LocalizedByteFormatting"/> should format bytes using IEC or decimal format.
20+
/// </summary>
21+
public bool UseIecByteFormat => _useIecByteFormat.GetValue();
22+
23+
/// <inheritdoc/>
24+
public override Version Version { get; } = new(1, 0, 0);
25+
}
26+
}

CommunityBugFixCollection/CommunityBugFixCollection.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1111
<PackageId>CommunityBugFixCollection</PackageId>
1212
<Title>Community Bug-Fix Collection</Title>
13-
<Authors>Banane9; Nytra; art0007i; LeCloutPanda; goat; __Choco__</Authors>
13+
<Authors>Banane9; Nytra; art0007i; LeCloutPanda; goat; __Choco__; LJ</Authors>
1414
<Version>0.5.0-beta</Version>
1515
<Description>This MonkeyLoader mod for Resonite that fixes various small Resonite-issues that are still open.</Description>
1616
<PackageReadmeFile>README.md</PackageReadmeFile>
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Collections.ObjectModel;
4-
using System.Text;
1+
using System.Diagnostics.CodeAnalysis;
52

63
namespace CommunityBugFixCollection
74
{
5+
[SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Usernames")]
86
internal static class Contributors
97
{
8+
public static string[] __Choco__ { get; } = ["__Choco__"];
9+
1010
public static string[] Art0007i { get; } = ["art0007i"];
1111

1212
public static string[] Banane9 { get; } = ["Banane9"];
@@ -15,8 +15,8 @@ internal static class Contributors
1515

1616
public static string[] LeCloutPanda { get; } = ["LeCloutPanda"];
1717

18-
public static string[] Nytra { get; } = ["Nytra"];
18+
public static string[] LJ { get; } = ["LJ"];
1919

20-
public static string[] __Choco__ { get; } = ["__Choco__"];
20+
public static string[] Nytra { get; } = ["Nytra"];
2121
}
2222
}

CommunityBugFixCollection/Locale/de.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"CommunityBugFixCollection.ImportMultipleAudioFiles.Description": "Macht es möglich mehrere Audiodateien auf einmal zu importieren.",
3131
"CommunityBugFixCollection.ImportWebFilesAsUrls.Description": "Sorgt dafür, dass URLs zu Textdateien oder Resonite Packages nicht importiert werden, statt als Hyperlink aufzutauchen.",
3232
"CommunityBugFixCollection.IndependentlyScaleDirectCursor.Description": "Verhindert, dass der direkte Cursor sehr groß wird, wenn er an einem deutlich nährem Objekt hängt als der echte Cursor.",
33-
"CommunityBugFixCollection.LocalizedByteFormatting.Description": "Lokalisiert die Speichereinheiten (MiB, GiB, etc.), inbsesondere bei der StorageUsageStatus Komponente.",
33+
"CommunityBugFixCollection.LocalizedByteFormatting.Description": "Lokalisiert die Speichereinheiten (MiB, GiB, etc.), inbsesondere bei der StorageUsageStatus Komponente. Es gibt eine Einstellung dafür, die Bytes nach IEC (Faktor 1024) statt dezimal (Faktor 1000) zu formatieren.",
3434
"CommunityBugFixCollection.LongerWorldLoadingFailIndication.Description": "Lässt den Welt-Ladefortschritts-Indikator frühestens nach 20s verschwinden, falls der Vorgang fehlgeschlagen ist.",
3535
"CommunityBugFixCollection.NaNtEqual.Description": "Sorgt dafür, dass NaN float / double Werte sich bei den == und != ProtoFlux Nodes sowie bei der ValueEqualityDriver Komponente niemals gleichen.",
3636
"CommunityBugFixCollection.NoLossOfColorProfile.Description": "Verhindert, dass Farbprofile nicht bei allen Berechnungen erhalten bleiben.",
@@ -44,6 +44,11 @@
4444
"CommunityBugFixCollection.TiltedUIAlignment.Description": "Kippt die UI-fokussierte Kamera, um UIX-Renderprobleme zum umgehen.",
4545
"CommunityBugFixCollection.StationaryGrabWorldActivation.Description": "Verhindert, dass die Welt-Greifen Fortbewegung den Spieler bei jeder Aktivierung bewegt.",
4646
"CommunityBugFixCollection.ValidQuaternionInputs.Description": "Lässt das ProtoFlux-Tool valide float und double Quaternions spawnen.",
47-
"CommunityBugFixCollection.UserInspectorAsNonHost.Description": "Sorgt dafür, dass Benutzer-Inspektoren bereits präsente Benutzer auch in Sessions anzeigen, in denen man nicht der Host ist."
47+
"CommunityBugFixCollection.UserInspectorAsNonHost.Description": "Sorgt dafür, dass Benutzer-Inspektoren bereits präsente Benutzer auch in Sessions anzeigen, in denen man nicht der Host ist.",
48+
49+
"CommunityBugFixCollection.Config.Options.Name": "Optionen",
50+
"CommunityBugFixCollection.Config.Options.Description": "Enthällt die Einstellungen für die wenigen Fixes die diese anbieten.",
51+
"CommunityBugFixCollection.Config.Options.UseIecByteFormat.Name": "IEC Byte-Format Benutzen",
52+
"CommunityBugFixCollection.Config.Options.UseIecByteFormat.Description": "Ob Bytes nach IEC (Faktor 1024) statt dezimal (Faktor 1000) formatiert werden sollen, wenn <i>LocalizedByteFormatting</i> aktiviert ist."
4853
}
4954
}

CommunityBugFixCollection/Locale/en.json

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"localeCode": "en",
3-
"authors": [ "Banane9", "__Choco__", "Nytra" ],
3+
"authors": [
4+
"Banane9",
5+
"__Choco__",
6+
"Nytra",
7+
"LJ"
8+
],
49
"messages": {
510
"CommunityBugFixCollection.Name": "Community Bug-Fix Collection",
611
"CommunityBugFixCollection.Description": "This mod contains fixes for various small Resonite Issues that are still open.",
@@ -30,7 +35,7 @@
3035
"CommunityBugFixCollection.ImportMultipleAudioFiles.Description": "Fixes it not being possible to import multiple audio clips at once.",
3136
"CommunityBugFixCollection.ImportWebFilesAsUrls.Description": "Fixes URLs to text files or Resonite Packages failing to import instead of appearing as a hyperlink.",
3237
"CommunityBugFixCollection.IndependentlyScaleDirectCursor.Description": "Fixes direct cursor size becoming very large when snapped to an object much closer than the true cursor.",
33-
"CommunityBugFixCollection.LocalizedByteFormatting.Description": "Localizes the storage units (MiB, GiB, etc.), in particular for the StorageUsageStatus component.",
38+
"CommunityBugFixCollection.LocalizedByteFormatting.Description": "Localizes the storage units (MiB, GiB, etc.), in particular for the StorageUsageStatus component. There's an option to use IEC format (factor 1024) over decimal (factor 1000).",
3439
"CommunityBugFixCollection.LongerWorldLoadingFailIndication.Description": "Only lets the World Load Progress Indicator disappear after 20s or more if the process failed.",
3540
"CommunityBugFixCollection.NaNtEqual.Description": "Makes NaN floats / doubles never equal to each other for the ProtoFlux == and != nodes, as well as the ValueEqualityDriver component.",
3641
"CommunityBugFixCollection.NoLossOfColorProfile.Description": "Fixes Color Profile not being preserved on all operations.",
@@ -47,11 +52,23 @@
4752
"CommunityBugFixCollection.ValidQuaternionInputs.Description": "Makes the ProtoFlux Tool spawn valid float and double quaternion inputs.",
4853
"CommunityBugFixCollection.ValueModDecimal.Description": "Adds a zero check to the Decimal ValueMod ProtoFlux node to prevent DIV/0 crashes",
4954

55+
"CommunityBugFixCollection.Config.Options.Name": "Options",
56+
"CommunityBugFixCollection.Config.Options.Description": "Contains the settings for the few fixes that offer them.",
57+
"CommunityBugFixCollection.Config.Options.UseIecByteFormat.Name": "Use IEC Byte Format",
58+
"CommunityBugFixCollection.Config.Options.UseIecByteFormat.Description": "Whether to format bytes using IEC (factor 1024) as opposed to decimal format (factor 1000) when <i>LocalizedByteFormatting</i> is enabled.",
59+
60+
"CommunityBugFixCollection.StorageUnits.Bi": "B",
61+
"CommunityBugFixCollection.StorageUnits.kiB": "KiB",
62+
"CommunityBugFixCollection.StorageUnits.MiB": "MiB",
63+
"CommunityBugFixCollection.StorageUnits.GiB": "GiB",
64+
"CommunityBugFixCollection.StorageUnits.TiB": "TiB",
65+
"CommunityBugFixCollection.StorageUnits.PiB": "PiB",
66+
5067
"CommunityBugFixCollection.StorageUnits.B": "B",
51-
"CommunityBugFixCollection.StorageUnits.kB": "KiB",
52-
"CommunityBugFixCollection.StorageUnits.MB": "MiB",
53-
"CommunityBugFixCollection.StorageUnits.GB": "GiB",
54-
"CommunityBugFixCollection.StorageUnits.TB": "TiB",
55-
"CommunityBugFixCollection.StorageUnits.PB": "PiB"
68+
"CommunityBugFixCollection.StorageUnits.kB": "kB",
69+
"CommunityBugFixCollection.StorageUnits.MB": "MB",
70+
"CommunityBugFixCollection.StorageUnits.GB": "GB",
71+
"CommunityBugFixCollection.StorageUnits.TB": "TB",
72+
"CommunityBugFixCollection.StorageUnits.PB": "PB"
5673
}
5774
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"localeCode": "fi",
3-
"authors": [ "Banane9" ],
3+
"authors": [ "LJ" ],
44
"messages": {
55
"CommunityBugFixCollection.StorageUnits.B": "t",
6-
"CommunityBugFixCollection.StorageUnits.kB": "Kit",
7-
"CommunityBugFixCollection.StorageUnits.MB": "Mit",
8-
"CommunityBugFixCollection.StorageUnits.GB": "Git",
9-
"CommunityBugFixCollection.StorageUnits.TB": "Tit",
10-
"CommunityBugFixCollection.StorageUnits.PB": "Pit"
6+
"CommunityBugFixCollection.StorageUnits.kB": "kt",
7+
"CommunityBugFixCollection.StorageUnits.MB": "Mt",
8+
"CommunityBugFixCollection.StorageUnits.GB": "Gt",
9+
"CommunityBugFixCollection.StorageUnits.TB": "Tt",
10+
"CommunityBugFixCollection.StorageUnits.PB": "Pt"
1111
}
1212
}

CommunityBugFixCollection/Locale/fr.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"authors": [ "Banane9" ],
44
"messages": {
55
"CommunityBugFixCollection.StorageUnits.B": "o",
6-
"CommunityBugFixCollection.StorageUnits.kB": "Kio",
7-
"CommunityBugFixCollection.StorageUnits.MB": "Mio",
8-
"CommunityBugFixCollection.StorageUnits.GB": "Gio",
9-
"CommunityBugFixCollection.StorageUnits.TB": "Tio",
10-
"CommunityBugFixCollection.StorageUnits.PB": "Pio"
6+
"CommunityBugFixCollection.StorageUnits.kB": "Ko",
7+
"CommunityBugFixCollection.StorageUnits.MB": "Mo",
8+
"CommunityBugFixCollection.StorageUnits.GB": "Go",
9+
"CommunityBugFixCollection.StorageUnits.TB": "To",
10+
"CommunityBugFixCollection.StorageUnits.PB": "Po"
1111
}
1212
}

CommunityBugFixCollection/LocalizedByteFormatting.cs

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Elements.Core;
22
using FrooxEngine;
33
using HarmonyLib;
4+
using MonkeyLoader.Configuration;
45
using MonkeyLoader.Resonite;
56
using MonkeyLoader.Resonite.Locale;
67
using System;
@@ -10,47 +11,41 @@
1011
using System.Text;
1112
using System.Threading.Tasks;
1213

13-
#pragma warning disable MHA008 // Assignment to non-ref patch method argument
14-
1514
namespace CommunityBugFixCollection
1615
{
1716
[HarmonyPatch]
1817
[HarmonyPatchCategory(nameof(LocalizedByteFormatting))]
19-
internal sealed class LocalizedByteFormatting : ResoniteAsyncEventHandlerMonkey<LocalizedByteFormatting, LocaleLoadingEvent>
18+
internal sealed class LocalizedByteFormatting : ConfiguredResoniteAsyncEventHandlerMonkey<LocalizedByteFormatting, BugFixOptions, LocaleLoadingEvent>
2019
{
21-
public override IEnumerable<string> Authors => Contributors.Banane9;
20+
private static readonly ConditionalWeakTable<StorageUsageStatus, CultureInfo> _lastCultureByStorageStatus = new();
21+
22+
public override IEnumerable<string> Authors { get; } = [.. Contributors.Banane9, .. Contributors.LJ];
2223

2324
public override bool CanBeDisabled => true;
2425

2526
public override int Priority => HarmonyLib.Priority.Last;
2627

27-
[HarmonyPrefix]
28-
[HarmonyPatch(typeof(UnitFormatting), nameof(UnitFormatting.FormatBytes))]
29-
private static bool UnitFormatBytesPrefix(double bytes, int decimalPlaces, ref string __result)
28+
protected override Task Handle(LocaleLoadingEvent eventData)
3029
{
31-
if (!Enabled)
32-
return true;
30+
Engine.Current.GlobalCoroutineManager.RunInSeconds(2, _lastCultureByStorageStatus.Clear);
3331

34-
var format = $"F{decimalPlaces}";
35-
var absoluteBytes = MathX.Abs(bytes);
36-
var culture = Settings.GetActiveSetting<LocaleSettings>()?.ActiveCulture ?? CultureInfo.CurrentCulture;
32+
return Task.CompletedTask;
33+
}
3734

38-
foreach (var suffix in UnitFormatting.suffixes)
39-
{
40-
if (absoluteBytes < 1024.0 || suffix == UnitFormatting.suffixes[^1])
41-
{
42-
__result = $"{bytes.ToString(format, culture)} {Mod.GetMessageInCurrent($"StorageUnits.{suffix}")}";
43-
return false;
44-
}
45-
46-
bytes /= 1024;
47-
absoluteBytes /= 1024;
48-
}
35+
protected override bool OnEngineReady()
36+
{
37+
ConfigSection.ItemChanged += ConfigSectionItemChanged;
4938

50-
return false;
39+
return base.OnEngineReady();
5140
}
5241

53-
private static readonly ConditionalWeakTable<StorageUsageStatus, CultureInfo> _lastCultureByStorageStatus = new();
42+
protected override bool OnShutdown(bool applicationExiting)
43+
{
44+
if (!applicationExiting)
45+
ConfigSection.ItemChanged -= ConfigSectionItemChanged;
46+
47+
return base.OnShutdown(applicationExiting);
48+
}
5449

5550
[HarmonyPrefix]
5651
[HarmonyPatch(typeof(StorageUsageStatus), nameof(StorageUsageStatus.OnCommonUpdate))]
@@ -128,13 +123,35 @@ private static bool StorageUsageStatusOnCommonUpdatePrefix(StorageUsageStatus __
128123
return false;
129124
}
130125

131-
protected override Task Handle(LocaleLoadingEvent eventData)
126+
[HarmonyPrefix]
127+
[HarmonyPatch(typeof(UnitFormatting), nameof(UnitFormatting.FormatBytes))]
128+
private static bool UnitFormatBytesPrefix(double bytes, int decimalPlaces, ref string __result)
132129
{
133-
Engine.Current.GlobalCoroutineManager.RunInSeconds(2, _lastCultureByStorageStatus.Clear);
130+
if (!Enabled)
131+
return true;
134132

135-
return Task.CompletedTask;
133+
var format = $"F{decimalPlaces}";
134+
var culture = Settings.GetActiveSetting<LocaleSettings>()?.ActiveCulture ?? CultureInfo.CurrentCulture;
135+
136+
// Select base and divisor for suffix determination
137+
var baseNum = ConfigSection.UseIecByteFormat ? 2 : 10;
138+
var divNum = ConfigSection.UseIecByteFormat ? 10 : 3;
139+
140+
// Either `2^(10*n)` or `10^(3*n)`, but also limited to max unit index.
141+
var index = MathX.Min(MathX.FloorToUInt(MathX.Log(MathX.Abs(bytes), baseNum) / divNum), (uint)(UnitFormatting.suffixes.Length - 1));
142+
var suffix = UnitFormatting.suffixes[index];
143+
144+
if (ConfigSection.UseIecByteFormat)
145+
suffix = suffix.Insert(suffix.Length - 1, "i");
146+
147+
// AKA scaled bytes in IEC/decimal format
148+
var numToFormat = bytes / MathX.Pow(baseNum, divNum * index);
149+
__result = $"{numToFormat.ToString(format, culture)} {Mod.GetMessageInCurrent($"StorageUnits.{suffix}")}";
150+
151+
return false;
136152
}
137-
}
138-
}
139153

140-
#pragma warning restore MHA008 // Assignment to non-ref patch method argument
154+
private void ConfigSectionItemChanged(object sender, IConfigKeyChangedEventArgs configKeyChangedEventArgs)
155+
=> Engine.Current.GlobalCoroutineManager.RunInSeconds(0, _lastCultureByStorageStatus.Clear);
156+
}
157+
}

0 commit comments

Comments
 (0)