Add debug logs to our expressions
This commit is contained in:
@@ -21,9 +21,9 @@ namespace BanquetForCyka {
|
|||||||
debug = Config.Bind("Debug", "Global Debug", false);
|
debug = Config.Bind("Debug", "Global Debug", false);
|
||||||
debugXp = Config.Bind("Debug", "XP Debug", false);
|
debugXp = Config.Bind("Debug", "XP Debug", false);
|
||||||
|
|
||||||
xpMultiplier =
|
xpMultiplier = new ExpressionConfigEntry(
|
||||||
new ExpressionConfigEntry(Config, "General", "XP Multiplier", "v*1",
|
Config, "General", "XP Multiplier", "v*1",
|
||||||
"XP Multiplier expression. Use 'v' to represent the original value.");
|
"XP Multiplier expression. Use 'v' to represent the original value.", debugXp);
|
||||||
|
|
||||||
Logger.LogInfo("BanquetForCyka loaded");
|
Logger.LogInfo("BanquetForCyka loaded");
|
||||||
HarmonyFileLog.Enabled = true;
|
HarmonyFileLog.Enabled = true;
|
||||||
|
@@ -6,6 +6,7 @@ using UnityEngine;
|
|||||||
namespace BanquetForCyka {
|
namespace BanquetForCyka {
|
||||||
public class ExpressionConfigEntry {
|
public class ExpressionConfigEntry {
|
||||||
private readonly ConfigEntry<string> _configEntry;
|
private readonly ConfigEntry<string> _configEntry;
|
||||||
|
private readonly ConfigEntry<bool> _debug;
|
||||||
private Func<float, float> _compiledExpression;
|
private Func<float, float> _compiledExpression;
|
||||||
private string _lastValidExpression;
|
private string _lastValidExpression;
|
||||||
private bool _isValid = true;
|
private bool _isValid = true;
|
||||||
@@ -18,13 +19,17 @@ namespace BanquetForCyka {
|
|||||||
|
|
||||||
// Try to compile the expression
|
// Try to compile the expression
|
||||||
try {
|
try {
|
||||||
|
LogDebug($"Attempting to compile expression: '{value}'");
|
||||||
var newExpr = ParseExpression(value);
|
var newExpr = ParseExpression(value);
|
||||||
_compiledExpression = newExpr;
|
_compiledExpression = newExpr;
|
||||||
_lastValidExpression = value;
|
_lastValidExpression = value;
|
||||||
_isValid = true;
|
_isValid = true;
|
||||||
_configEntry.Value = value;
|
_configEntry.Value = value;
|
||||||
} catch (Exception) {
|
LogDebug($"Expression '{value}' compiled successfully");
|
||||||
|
} catch (Exception e) {
|
||||||
_isValid = false;
|
_isValid = false;
|
||||||
|
LogDebug($"Failed to compile expression '{value}': {e.Message}");
|
||||||
|
LogDebug($"Stack trace: {e.StackTrace}");
|
||||||
// Don't update the config value if invalid
|
// Don't update the config value if invalid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,33 +39,57 @@ namespace BanquetForCyka {
|
|||||||
public string LastValidExpression => _lastValidExpression;
|
public string LastValidExpression => _lastValidExpression;
|
||||||
|
|
||||||
public ExpressionConfigEntry(ConfigFile config, string section, string key, string defaultValue,
|
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(
|
_configEntry = config.Bind(
|
||||||
section, key, defaultValue,
|
section, key, defaultValue,
|
||||||
new ConfigDescription(description +
|
new ConfigDescription(description +
|
||||||
"\nUse 'v' to represent the input value. Examples: 'v*4', 'v+13', '(v+5)*2'"));
|
"\nUse 'v' to represent the input value. Examples: 'v*4', 'v+13', '(v+5)*2'"));
|
||||||
|
|
||||||
// Initial compilation
|
// Initial compilation
|
||||||
_compiledExpression = ParseExpression(_configEntry.Value);
|
try {
|
||||||
_lastValidExpression = _configEntry.Value;
|
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
|
// Recompile when config changes
|
||||||
_configEntry.SettingChanged += (sender, args) => {
|
_configEntry.SettingChanged += (sender, args) => {
|
||||||
try {
|
try {
|
||||||
|
LogDebug($"Config changed, attempting to recompile expression: '{_configEntry.Value}'");
|
||||||
_compiledExpression = ParseExpression(_configEntry.Value);
|
_compiledExpression = ParseExpression(_configEntry.Value);
|
||||||
_lastValidExpression = _configEntry.Value;
|
_lastValidExpression = _configEntry.Value;
|
||||||
_isValid = true;
|
_isValid = true;
|
||||||
} catch (Exception) {
|
LogDebug($"Expression '{_configEntry.Value}' recompiled successfully");
|
||||||
|
} catch (Exception e) {
|
||||||
_isValid = false;
|
_isValid = false;
|
||||||
|
LogDebug($"Failed to recompile expression '{_configEntry.Value}': {e.Message}");
|
||||||
|
LogDebug($"Stack trace: {e.StackTrace}");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public float Evaluate(float input) {
|
public float Evaluate(float input) {
|
||||||
if (!_isValid) {
|
if (!_isValid) {
|
||||||
|
LogDebug(
|
||||||
|
$"Expression is invalid, using last valid expression '{_lastValidExpression}' for input {input}");
|
||||||
return input; // Return original value if expression is invalid
|
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
|
// Copy of the expression parser from Main
|
||||||
|
Reference in New Issue
Block a user