Improve ObjectFieldMultiplier with naming conventions and documentation
This commit is contained in:
@@ -4,101 +4,111 @@ using BepInEx.Configuration;
|
||||
using HarmonyLib;
|
||||
|
||||
namespace TerraTech {
|
||||
public class MultipliedField<TObject, TValue> {
|
||||
private readonly string fieldName;
|
||||
private readonly ConfigEntry<TValue> multiplier;
|
||||
private readonly Traverse parentTraverse;
|
||||
private TValue originalValue;
|
||||
/// <summary>
|
||||
/// Represents a field that can be multiplied by a configurable value
|
||||
/// </summary>
|
||||
/// <typeparam name="TValue">The type of the field value</typeparam>
|
||||
public class MultipliedField<TValue> {
|
||||
private readonly string _fieldName;
|
||||
private readonly ConfigEntry<TValue> _multiplier;
|
||||
private readonly Traverse _parentTraverse;
|
||||
private TValue _originalValue;
|
||||
|
||||
public MultipliedField(string fieldName, ConfigEntry<TValue> multiplier, Traverse parentTraverse) {
|
||||
this.fieldName = fieldName;
|
||||
this.multiplier = multiplier;
|
||||
this.parentTraverse = parentTraverse;
|
||||
_fieldName = fieldName;
|
||||
_multiplier = multiplier;
|
||||
_parentTraverse = parentTraverse;
|
||||
|
||||
if (!parentTraverse.Field(fieldName).FieldExists()) {
|
||||
throw new ArgumentException(
|
||||
string.Format("Field {0} does not exist on {1}", fieldName, parentTraverse.ToString()));
|
||||
string.Format("Field {0} does not exist on {1}", fieldName, parentTraverse));
|
||||
}
|
||||
}
|
||||
|
||||
public TValue GetValue() {
|
||||
var value = parentTraverse.Field(fieldName).GetValue();
|
||||
var value = _parentTraverse.Field(_fieldName).GetValue();
|
||||
if (value == null)
|
||||
throw new InvalidOperationException(string.Format("Field {0} returned null", fieldName));
|
||||
throw new InvalidOperationException(string.Format("Field {0} returned null", _fieldName));
|
||||
return (TValue)value;
|
||||
}
|
||||
|
||||
public void SetValue(TValue value) {
|
||||
parentTraverse.Field(fieldName).SetValue(value);
|
||||
_parentTraverse.Field(_fieldName).SetValue(value);
|
||||
var verifyValue = GetValue();
|
||||
if (!verifyValue.Equals(value))
|
||||
throw new InvalidOperationException(
|
||||
string.Format("Field {0} set to {1} but read back as {2}", fieldName, value, verifyValue));
|
||||
string.Format("Field {0} set to {1} but read back as {2}", _fieldName, value, verifyValue));
|
||||
}
|
||||
|
||||
public void CaptureOriginal() {
|
||||
originalValue = GetValue();
|
||||
_originalValue = GetValue();
|
||||
if (Main.debug.Value)
|
||||
Console.WriteLine("Captured original value for {0}: {1}", fieldName, originalValue);
|
||||
Console.WriteLine("Captured original value for {0}: {1}", _fieldName, _originalValue);
|
||||
}
|
||||
|
||||
public void Apply() {
|
||||
try {
|
||||
dynamic originalDynamic = originalValue;
|
||||
dynamic multiplierDynamic = multiplier.Value;
|
||||
dynamic originalDynamic = _originalValue;
|
||||
dynamic multiplierDynamic = _multiplier.Value;
|
||||
var newValue = (TValue)(originalDynamic * multiplierDynamic);
|
||||
|
||||
if (Main.debug.Value)
|
||||
Console.WriteLine("Applying to {0}: {1} * {2} = {3}", fieldName, originalValue, multiplier.Value, newValue);
|
||||
Console.WriteLine("Applying to {0}: {1} * {2} = {3}", _fieldName, _originalValue, _multiplier.Value,
|
||||
newValue);
|
||||
|
||||
SetValue(newValue);
|
||||
} catch (Exception e) {
|
||||
throw new InvalidOperationException(string.Format("Failed to apply multiplication to {0}", fieldName),
|
||||
throw new InvalidOperationException(string.Format("Failed to apply multiplication to {0}", _fieldName),
|
||||
e);
|
||||
}
|
||||
}
|
||||
|
||||
public void Restore() {
|
||||
if (Main.debug.Value)
|
||||
Console.WriteLine("Restoring {0} to original value: {1}", fieldName, originalValue);
|
||||
SetValue(originalValue);
|
||||
Console.WriteLine("Restoring {0} to original value: {1}", _fieldName, _originalValue);
|
||||
SetValue(_originalValue);
|
||||
}
|
||||
|
||||
public void LogValue(string prefix) {
|
||||
if (!Main.debug.Value)
|
||||
return;
|
||||
var currentValue = GetValue();
|
||||
Console.WriteLine("{0} {1}; {2}: {3} (original: {4}, multiplier: {5})", prefix, parentTraverse, fieldName, currentValue, originalValue, multiplier.Value);
|
||||
Console.WriteLine("{0} {1}; {2}: {3} (original: {4}, multiplier: {5})", prefix, _parentTraverse, _fieldName,
|
||||
currentValue, _originalValue, _multiplier.Value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents an object with multiple fields that can be multiplied
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the object being managed</typeparam>
|
||||
public class MultipliedObject<T> {
|
||||
private readonly Traverse objectTraverse;
|
||||
private readonly List<MultipliedField<T, float>> fields;
|
||||
private readonly Traverse _objectTraverse;
|
||||
private readonly List<MultipliedField<float>> _fields;
|
||||
|
||||
public MultipliedObject(T instance) {
|
||||
objectTraverse = Traverse.Create(instance);
|
||||
fields = new List<MultipliedField<T, float>>();
|
||||
_objectTraverse = Traverse.Create(instance);
|
||||
_fields = new List<MultipliedField<float>>();
|
||||
}
|
||||
|
||||
public void AddField(string fieldName, ConfigEntry<float> multiplier) {
|
||||
fields.Add(new MultipliedField<T, float>(fieldName, multiplier, objectTraverse));
|
||||
_fields.Add(new MultipliedField<float>(fieldName, multiplier, _objectTraverse));
|
||||
}
|
||||
|
||||
public void CaptureFrom() {
|
||||
foreach (var field in fields) {
|
||||
foreach (var field in _fields) {
|
||||
field.CaptureOriginal();
|
||||
}
|
||||
}
|
||||
|
||||
public void ApplyTo() {
|
||||
foreach (var field in fields) {
|
||||
foreach (var field in _fields) {
|
||||
field.Apply();
|
||||
}
|
||||
}
|
||||
|
||||
public void RestoreTo() {
|
||||
foreach (var field in fields) {
|
||||
foreach (var field in _fields) {
|
||||
field.Restore();
|
||||
}
|
||||
}
|
||||
@@ -106,31 +116,35 @@ namespace TerraTech {
|
||||
public void LogValues(string prefix) {
|
||||
if (!Main.debug.Value)
|
||||
return;
|
||||
foreach (var field in fields) {
|
||||
foreach (var field in _fields) {
|
||||
field.LogValue(prefix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Manages a collection of objects whose fields can be multiplied
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of objects being managed</typeparam>
|
||||
public class MultipliedObjectManager<T>
|
||||
where T : class {
|
||||
private readonly Dictionary<T, MultipliedObject<T>> managedObjects;
|
||||
private readonly Action<MultipliedObject<T>> configureObject;
|
||||
private readonly Dictionary<T, MultipliedObject<T>> _managedObjects;
|
||||
private readonly Action<MultipliedObject<T>> _configureObject;
|
||||
|
||||
public MultipliedObjectManager(Action<MultipliedObject<T>> configureObject) {
|
||||
this.configureObject = configureObject;
|
||||
managedObjects = new Dictionary<T, MultipliedObject<T>>();
|
||||
_configureObject = configureObject;
|
||||
_managedObjects = new Dictionary<T, MultipliedObject<T>>();
|
||||
}
|
||||
|
||||
public void OnObjectAttached(T instance) {
|
||||
if (!managedObjects.ContainsKey(instance)) {
|
||||
if (!_managedObjects.ContainsKey(instance)) {
|
||||
if (Main.debug.Value)
|
||||
Console.WriteLine("{0}.OnAttached", typeof(T).Name);
|
||||
|
||||
var multipliedObject = new MultipliedObject<T>(instance);
|
||||
configureObject(multipliedObject);
|
||||
_configureObject(multipliedObject);
|
||||
multipliedObject.CaptureFrom();
|
||||
managedObjects.Add(instance, multipliedObject);
|
||||
_managedObjects.Add(instance, multipliedObject);
|
||||
multipliedObject.LogValues("Patching");
|
||||
|
||||
ApplyTo(instance);
|
||||
@@ -140,7 +154,7 @@ namespace TerraTech {
|
||||
|
||||
public void OnObjectDetached(T instance) {
|
||||
MultipliedObject<T> multipliedObject;
|
||||
if (managedObjects.TryGetValue(instance, out multipliedObject)) {
|
||||
if (_managedObjects.TryGetValue(instance, out multipliedObject)) {
|
||||
if (Main.debug.Value) {
|
||||
Console.WriteLine("{0}.OnDetaching", typeof(T).Name);
|
||||
multipliedObject.LogValues("Restoring");
|
||||
@@ -148,14 +162,14 @@ namespace TerraTech {
|
||||
|
||||
RestoreTo(instance);
|
||||
multipliedObject.LogValues("Restored");
|
||||
managedObjects.Remove(instance);
|
||||
_managedObjects.Remove(instance);
|
||||
}
|
||||
}
|
||||
|
||||
public void ApplyAll() {
|
||||
if (Main.debug.Value)
|
||||
Console.WriteLine("Modifying {0} {1}", managedObjects.Count, typeof(T).Name);
|
||||
foreach (var instance in managedObjects.Keys) {
|
||||
Console.WriteLine("Modifying {0} {1}", _managedObjects.Count, typeof(T).Name);
|
||||
foreach (var instance in _managedObjects.Keys) {
|
||||
RestoreTo(instance);
|
||||
ApplyTo(instance);
|
||||
}
|
||||
@@ -163,13 +177,13 @@ namespace TerraTech {
|
||||
|
||||
private void ApplyTo(T instance) {
|
||||
MultipliedObject<T> obj;
|
||||
if (managedObjects.TryGetValue(instance, out obj))
|
||||
if (_managedObjects.TryGetValue(instance, out obj))
|
||||
obj.ApplyTo();
|
||||
}
|
||||
|
||||
private void RestoreTo(T instance) {
|
||||
MultipliedObject<T> obj;
|
||||
if (managedObjects.TryGetValue(instance, out obj))
|
||||
if (_managedObjects.TryGetValue(instance, out obj))
|
||||
obj.RestoreTo();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user