Add debug logs to our expressions
This commit is contained in:
@@ -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;
|
||||
|
@@ -6,6 +6,7 @@ using UnityEngine;
|
||||
namespace BanquetForCyka {
|
||||
public class ExpressionConfigEntry {
|
||||
private readonly ConfigEntry<string> _configEntry;
|
||||
private readonly ConfigEntry<bool> _debug;
|
||||
private Func<float, float> _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<bool> 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
|
||||
|
Reference in New Issue
Block a user