Enhance ModuleShieldGeneratorManager with robust error handling and debug logging
This commit is contained in:
@@ -14,23 +14,61 @@ namespace TerraTech {
|
|||||||
this.fieldName = fieldName;
|
this.fieldName = fieldName;
|
||||||
this.multiplier = multiplier;
|
this.multiplier = multiplier;
|
||||||
this.parentTraverse = parentTraverse;
|
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 TValue GetValue() {
|
||||||
public void SetValue(TValue value) => parentTraverse.Field(fieldName).SetValue(value);
|
var value = parentTraverse.Field(fieldName).GetValue();
|
||||||
public void CaptureOriginal() => originalValue = GetValue();
|
if (value == null)
|
||||||
public void Restore() => SetValue(originalValue);
|
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() {
|
public void Apply() {
|
||||||
dynamic originalDynamic = originalValue;
|
try {
|
||||||
dynamic multiplierDynamic = multiplier.Value;
|
dynamic originalDynamic = originalValue;
|
||||||
SetValue((TValue)(originalDynamic * multiplierDynamic));
|
dynamic multiplierDynamic = multiplier.Value;
|
||||||
|
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) {
|
public void LogValue(string prefix) {
|
||||||
if (!Main.debug.Value)
|
if (!Main.debug.Value)
|
||||||
return;
|
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) {
|
private void ApplyTo(T instance) if (managedObjects.TryGetValue(instance, out var obj)) obj.ApplyTo();
|
||||||
if (managedObjects.TryGetValue(instance, out var obj))
|
private void RestoreTo(T instance) if (managedObjects.TryGetValue(instance, out var obj)) obj.RestoreTo();
|
||||||
obj.ApplyTo();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void RestoreTo(T instance) {
|
|
||||||
if (managedObjects.TryGetValue(instance, out var obj))
|
|
||||||
obj.RestoreTo();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HarmonyPatch]
|
[HarmonyPatch]
|
||||||
|
Reference in New Issue
Block a user