|
| 1 | +using System; |
| 2 | +using System.Reflection; |
| 3 | +using Elements.Core; |
| 4 | +using FrooxEngine; |
| 5 | +using FrooxEngine.Undo; |
| 6 | +using HarmonyLib; |
| 7 | +using ResoniteModLoader; |
| 8 | +using System.Globalization; |
| 9 | +using System.Data; |
| 10 | + |
| 11 | +namespace Numantics { |
| 12 | + public class Numantics : ResoniteMod { |
| 13 | + internal const string VERSION_CONSTANT = "1.0.0"; |
| 14 | + public override string Name => "Numantics"; |
| 15 | + public override string Author => "NalaTheThird"; |
| 16 | + public override string Version => VERSION_CONSTANT; |
| 17 | + public override string Link => "https://github.com/nalathethird/R-Numantics"; |
| 18 | + |
| 19 | + [AutoRegisterConfigKey] |
| 20 | + private static readonly ModConfigurationKey<bool> EnableValueFieldMath = |
| 21 | + new ModConfigurationKey<bool>("enable_math", "Enable math processing in input fields", () => true); |
| 22 | + |
| 23 | + [AutoRegisterConfigKey] |
| 24 | + private static readonly ModConfigurationKey<bool> IncludeStrings = |
| 25 | + new ModConfigurationKey<bool>("include_strings", "Allow math to be calculated in string fields - slightly dangerous, be careful!", () => false); |
| 26 | + |
| 27 | + [AutoRegisterConfigKey] |
| 28 | + private static readonly ModConfigurationKey<bool> VerboseLogging = |
| 29 | + new ModConfigurationKey<bool>("verbose_logging", "Enable verbose logging for debugging", () => false); |
| 30 | + |
| 31 | + private static ModConfiguration Config; |
| 32 | + |
| 33 | + public override void OnEngineInit() { |
| 34 | + Config = GetConfiguration(); |
| 35 | + Config.Save(true); |
| 36 | + |
| 37 | + var harmony = new Harmony("com.nalathethird.Numantics"); |
| 38 | + harmony.PatchAll(); |
| 39 | + Msg("Harmony patches applied - happy mathing!"); |
| 40 | + } |
| 41 | + |
| 42 | + [HarmonyPatch(typeof(TextEditor))] |
| 43 | + [HarmonyPatch("OnFinished")] |
| 44 | + class TextEditor_OnFinished_Patch { |
| 45 | + static void Prefix(TextEditor __instance) { |
| 46 | + try { |
| 47 | + bool verbose = Config?.GetValue(VerboseLogging) ?? false; |
| 48 | + |
| 49 | + if (verbose) Msg("OnFinished called"); |
| 50 | + |
| 51 | + if (__instance?.Text?.Target == null) { |
| 52 | + if (verbose) Msg("TextEditor or Text.Target is null, skipping"); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (Config == null) { |
| 57 | + Warn("Config is null, cannot proceed"); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (!Config.GetValue(EnableValueFieldMath)) { |
| 62 | + if (verbose) Msg("Math evaluation is disabled in config"); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + string text = __instance.Text.Target.Text; |
| 67 | + if (verbose) Msg($"Input text: '{text}'"); |
| 68 | + |
| 69 | + if (string.IsNullOrWhiteSpace(text)) { |
| 70 | + if (verbose) Msg("Text is null or whitespace, skipping"); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + string exprText = text |
| 75 | + .Replace("x", "*") |
| 76 | + .Replace("d", "/") |
| 77 | + .Replace("a", "+") |
| 78 | + .Replace("s", "-"); |
| 79 | + |
| 80 | + if (verbose && exprText != text) { |
| 81 | + Msg($" Replaced operators: '{text}' => '{exprText}'"); |
| 82 | + } |
| 83 | + |
| 84 | + if (TryEvaluateExpression(exprText, out string result, verbose)) { |
| 85 | + Msg($"SUCCESS - Evaluated '{text}' => '{result}'"); |
| 86 | + |
| 87 | + __instance.Text.Target.Text = result; |
| 88 | + |
| 89 | + if (verbose) Msg($"Updated Text.Target.Text to '{result}'"); |
| 90 | + } else { |
| 91 | + if (verbose) Msg($"Not a math expression or evaluation failed: '{exprText}'"); |
| 92 | + } |
| 93 | + } catch (Exception e) { |
| 94 | + Error($"Exception in OnFinished patch: {e.Message}"); |
| 95 | + Error($"Stack trace: {e.StackTrace}"); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static bool TryEvaluateExpression(string input, out string result, bool verbose) { |
| 100 | + result = input; |
| 101 | + |
| 102 | + if (!(input.Contains("+") || input.Contains("-") || input.Contains("*") || input.Contains("/"))) { |
| 103 | + if (verbose) Msg("No math operators found in input"); |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + try { |
| 108 | + if (verbose) Msg($"Attempting to parse expression: '{input}'"); |
| 109 | + var table = new DataTable(); |
| 110 | + var evaluated = table.Compute(input, ""); |
| 111 | + double resultValue = Convert.ToDouble(evaluated); |
| 112 | + |
| 113 | + if (verbose) Msg($"Evaluated to: {resultValue}"); |
| 114 | + |
| 115 | + result = resultValue.ToString(CultureInfo.InvariantCulture); |
| 116 | + return true; |
| 117 | + } catch (Exception ex) { |
| 118 | + Warn($"Expression evaluation failed for '{input}': {ex.Message}"); |
| 119 | + if (verbose) Warn($"Exception type: {ex.GetType().Name}"); |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments