Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyGalbreath committed Feb 16, 2024
1 parent 4dbd8d0 commit 5f20b56
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
10 changes: 0 additions & 10 deletions FreedomUnitsModSystem.cs

This file was deleted.

24 changes: 14 additions & 10 deletions src/FreedomUnitsMod.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection;
Expand All @@ -17,13 +18,15 @@
namespace FreedomUnits;

[HarmonyPatch]
public class FreedomUnitsMod : ModSystem {
private static readonly Regex TEMPERATURE_REGEX = new(@"(-?\d+(?:\.|,)?\d*)( ?)(°C|deg)");
[SuppressMessage("ReSharper", "UnusedType.Global")]
public partial class FreedomUnitsMod : ModSystem {
[GeneratedRegex("(-?\\d+(?:\\.|,)?\\d*)( ?)(°C|deg)")]
private static partial Regex TemperatureRegex();

private static GuiDialog? _hudDebugScreen;
private static ICoreAPI? _api;

private Harmony? harmony;
private Harmony? _harmony;

public override bool ShouldLoad(EnumAppSide side) {
return side.IsClient();
Expand All @@ -32,26 +35,27 @@ public class FreedomUnitsMod : ModSystem {
public override void StartClientSide(ICoreClientAPI api) {
_api = api;

harmony = new Harmony(Mod.Info.ModID);
harmony.PatchAll(Assembly.GetExecutingAssembly());
_harmony = new Harmony(Mod.Info.ModID);
_harmony.PatchAll(Assembly.GetExecutingAssembly());
}

private static bool IsDebugHugText(string text) {
return ((_hudDebugScreen ??= ((ClientMain)_api?.World!).GetField<List<GuiDialog>>("LoadedGuis")!.FirstOrDefault(dlg => dlg is HudDebugScreen))?.IsOpened() ?? false) && text.Contains("Yaw: ") && text.Contains("Facing: ");
}

public override void Dispose() {
harmony?.UnpatchAll(Mod.Info.ModID);
_harmony?.UnpatchAll(Mod.Info.ModID);

_hudDebugScreen = null;
_api = null;
}

[HarmonyPrefix]
[HarmonyPatch(typeof(TextDrawUtil), "Lineize", typeof(Context), typeof(string), typeof(EnumLinebreakBehavior), typeof(TextFlowPath[]), typeof(double), typeof(double), typeof(double), typeof(bool))]
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public static void PreLineize(ref string text) {
string original = text;
text = TEMPERATURE_REGEX.Replace(text, match => {
text = TemperatureRegex().Replace(text, match => {
if (match.Groups[3].Value.Normalize() is not ("°C" or "deg") || IsDebugHugText(original)) {
return match.Value;
}
Expand All @@ -65,17 +69,17 @@ public class FreedomUnitsMod : ModSystem {
try {
return $"{float.Parse(match.Groups[1].Value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture) * 9F / 5F + (delta ? 0 : 32):0.#}°F";
}
catch (FormatException) {
} catch (FormatException) {
return match.Value;
}
});
}

[HarmonyTranspiler]
[HarmonyPatch(typeof(GuiDialogBlockEntityFirepit), "SetupDialog")]
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public static IEnumerable<CodeInstruction> TranspileSetupDialog(IEnumerable<CodeInstruction> instructions) {
var codes = new List<CodeInstruction>(instructions);
List<CodeInstruction> codes = new(instructions);

for (int i = 0; i < codes.Count; i++) {
if (codes[i].opcode == OpCodes.Ldc_R8 && (codes[i].operand?.ToString()?.Equals("60") ?? false)) {
Expand Down

0 comments on commit 5f20b56

Please sign in to comment.