Enhance ModuleShieldGeneratorManager with robust error handling and debug logging
This commit is contained in:
@@ -14,23 +14,61 @@ namespace TerraTech {
|
||||
this.fieldName = fieldName;
|
||||
this.multiplier = multiplier;
|
||||
this.parentTraverse = parentTraverse;
|
||||
|
||||
if (!parentTraverse.Field(fieldName).FieldExists()) {
|
||||
throw new ArgumentException($"Field {fieldName} does not exist on {parentTraverse.ToString()}");
|
||||
}
|
||||
}
|
||||
|
||||
public TValue GetValue() => (TValue)parentTraverse.Field(fieldName).GetValue();
|
||||
public void SetValue(TValue value) => parentTraverse.Field(fieldName).SetValue(value);
|
||||
public void CaptureOriginal() => originalValue = GetValue();
|
||||
public void Restore() => SetValue(originalValue);
|
||||
public TValue GetValue() {
|
||||
var value = parentTraverse.Field(fieldName).GetValue();
|
||||
if (value == null)
|
||||
throw new InvalidOperationException($"Field {fieldName} returned null");
|
||||
return (TValue)value;
|
||||
}
|
||||
|
||||
public void SetValue(TValue value) {
|
||||
if (value == null)
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
parentTraverse.Field(fieldName).SetValue(value);
|
||||
var verifyValue = GetValue();
|
||||
if (!verifyValue.Equals(value))
|
||||
throw new InvalidOperationException($"Field {fieldName} set to {value} but read back as {verifyValue}");
|
||||
}
|
||||
|
||||
public void CaptureOriginal() {
|
||||
originalValue = GetValue();
|
||||
if (Main.debug.Value)
|
||||
Console.WriteLine($"Captured original value for {fieldName}: {originalValue}");
|
||||
}
|
||||
|
||||
public void Apply() {
|
||||
try {
|
||||
dynamic originalDynamic = originalValue;
|
||||
dynamic multiplierDynamic = multiplier.Value;
|
||||
SetValue((TValue)(originalDynamic * multiplierDynamic));
|
||||
var newValue = (TValue)(originalDynamic * multiplierDynamic);
|
||||
|
||||
if (Main.debug.Value)
|
||||
Console.WriteLine($"Applying to {fieldName}: {originalValue} * {multiplier.Value} = {newValue}");
|
||||
|
||||
SetValue(newValue);
|
||||
} catch (Exception e) {
|
||||
throw new InvalidOperationException($"Failed to apply multiplication to {fieldName}", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void Restore() {
|
||||
if (Main.debug.Value)
|
||||
Console.WriteLine($"Restoring {fieldName} to original value: {originalValue}");
|
||||
SetValue(originalValue);
|
||||
}
|
||||
|
||||
public void LogValue(string prefix) {
|
||||
if (!Main.debug.Value)
|
||||
return;
|
||||
Console.WriteLine("{0} {1}; {2}: {3}", prefix, parentTraverse.ToString(), fieldName, GetValue());
|
||||
var currentValue = GetValue();
|
||||
Console.WriteLine(
|
||||
$"{prefix} {parentTraverse}; {fieldName}: {currentValue} (original: {originalValue}, multiplier: {multiplier.Value})");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,15 +136,8 @@ namespace TerraTech {
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyTo(T instance) {
|
||||
if (managedObjects.TryGetValue(instance, out var obj))
|
||||
obj.ApplyTo();
|
||||
}
|
||||
|
||||
private void RestoreTo(T instance) {
|
||||
if (managedObjects.TryGetValue(instance, out var obj))
|
||||
obj.RestoreTo();
|
||||
}
|
||||
private void ApplyTo(T instance) if (managedObjects.TryGetValue(instance, out var obj)) obj.ApplyTo();
|
||||
private void RestoreTo(T instance) if (managedObjects.TryGetValue(instance, out var obj)) obj.RestoreTo();
|
||||
}
|
||||
|
||||
[HarmonyPatch]
|
||||
|
Reference in New Issue
Block a user