From 345f8c456541c7b5bb5aaa805d858acb90f86eef Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 21 May 2025 16:27:52 +0200 Subject: [PATCH] Add debug logs to our expressions --- .../BanquetForFools/BanquetForCyka/Class1.cs | 6 +-- .../BanquetForCyka/ExpressionConfigEntry.cs | 41 ++++++++++++++++--- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/Projects/BanquetForFools/BanquetForCyka/Class1.cs b/Projects/BanquetForFools/BanquetForCyka/Class1.cs index 8cb109f..055427c 100644 --- a/Projects/BanquetForFools/BanquetForCyka/Class1.cs +++ b/Projects/BanquetForFools/BanquetForCyka/Class1.cs @@ -21,9 +21,9 @@ namespace BanquetForCyka { debug = Config.Bind("Debug", "Global Debug", false); debugXp = Config.Bind("Debug", "XP Debug", false); - xpMultiplier = - new ExpressionConfigEntry(Config, "General", "XP Multiplier", "v*1", - "XP Multiplier expression. Use 'v' to represent the original value."); + xpMultiplier = new ExpressionConfigEntry( + Config, "General", "XP Multiplier", "v*1", + "XP Multiplier expression. Use 'v' to represent the original value.", debugXp); Logger.LogInfo("BanquetForCyka loaded"); HarmonyFileLog.Enabled = true; diff --git a/Projects/BanquetForFools/BanquetForCyka/ExpressionConfigEntry.cs b/Projects/BanquetForFools/BanquetForCyka/ExpressionConfigEntry.cs index 04f2140..f6d00dd 100644 --- a/Projects/BanquetForFools/BanquetForCyka/ExpressionConfigEntry.cs +++ b/Projects/BanquetForFools/BanquetForCyka/ExpressionConfigEntry.cs @@ -6,6 +6,7 @@ using UnityEngine; namespace BanquetForCyka { public class ExpressionConfigEntry { private readonly ConfigEntry _configEntry; + private readonly ConfigEntry _debug; private Func _compiledExpression; private string _lastValidExpression; private bool _isValid = true; @@ -18,13 +19,17 @@ namespace BanquetForCyka { // Try to compile the expression try { + LogDebug($"Attempting to compile expression: '{value}'"); var newExpr = ParseExpression(value); _compiledExpression = newExpr; _lastValidExpression = value; _isValid = true; _configEntry.Value = value; - } catch (Exception) { + LogDebug($"Expression '{value}' compiled successfully"); + } catch (Exception e) { _isValid = false; + LogDebug($"Failed to compile expression '{value}': {e.Message}"); + LogDebug($"Stack trace: {e.StackTrace}"); // Don't update the config value if invalid } } @@ -34,33 +39,57 @@ namespace BanquetForCyka { public string LastValidExpression => _lastValidExpression; public ExpressionConfigEntry(ConfigFile config, string section, string key, string defaultValue, - string description) { + string description, ConfigEntry debug = null) { + _debug = debug; + LogDebug($"Initializing ExpressionConfigEntry for {section}.{key}"); _configEntry = config.Bind( section, key, defaultValue, new ConfigDescription(description + "\nUse 'v' to represent the input value. Examples: 'v*4', 'v+13', '(v+5)*2'")); // Initial compilation - _compiledExpression = ParseExpression(_configEntry.Value); - _lastValidExpression = _configEntry.Value; + try { + LogDebug($"Attempting initial compilation of expression: '{_configEntry.Value}'"); + _compiledExpression = ParseExpression(_configEntry.Value); + _lastValidExpression = _configEntry.Value; + LogDebug($"Initial expression '{_configEntry.Value}' compiled successfully"); + } catch (Exception e) { + LogDebug($"Failed to compile initial expression '{_configEntry.Value}': {e.Message}"); + LogDebug($"Stack trace: {e.StackTrace}"); + } // Recompile when config changes _configEntry.SettingChanged += (sender, args) => { try { + LogDebug($"Config changed, attempting to recompile expression: '{_configEntry.Value}'"); _compiledExpression = ParseExpression(_configEntry.Value); _lastValidExpression = _configEntry.Value; _isValid = true; - } catch (Exception) { + LogDebug($"Expression '{_configEntry.Value}' recompiled successfully"); + } catch (Exception e) { _isValid = false; + LogDebug($"Failed to recompile expression '{_configEntry.Value}': {e.Message}"); + LogDebug($"Stack trace: {e.StackTrace}"); } }; } public float Evaluate(float input) { if (!_isValid) { + LogDebug( + $"Expression is invalid, using last valid expression '{_lastValidExpression}' for input {input}"); return input; // Return original value if expression is invalid } - return _compiledExpression(input); + LogDebug($"Evaluating expression '{_configEntry.Value}' with input {input}"); + var result = _compiledExpression(input); + LogDebug($"Expression '{_configEntry.Value}' evaluated: {input} -> {result}"); + return result; + } + + private void LogDebug(string message) { + if (_debug?.Value == true) { + Console.WriteLine($"[ExpressionConfigEntry] {message}"); + } } // Copy of the expression parser from Main