Refactor ObjectFieldMultiplier with code style improvements and minor optimizations
This commit is contained in:
@@ -1,6 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using BepInEx.Configuration;
|
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
|
|
||||||
namespace TerraTech {
|
namespace TerraTech {
|
||||||
@@ -17,12 +15,18 @@ namespace TerraTech {
|
|||||||
|
|
||||||
[HarmonyPrefix]
|
[HarmonyPrefix]
|
||||||
[HarmonyPatch(typeof(ModuleShieldGenerator), "OnAttached")]
|
[HarmonyPatch(typeof(ModuleShieldGenerator), "OnAttached")]
|
||||||
static void PostfixCreate(ModuleShieldGenerator __instance) => manager.OnObjectAttached(__instance);
|
static void PostfixCreate(ModuleShieldGenerator __instance) {
|
||||||
|
manager.OnObjectAttached(__instance);
|
||||||
|
}
|
||||||
|
|
||||||
[HarmonyPrefix]
|
[HarmonyPrefix]
|
||||||
[HarmonyPatch(typeof(ModuleShieldGenerator), "OnDetaching")]
|
[HarmonyPatch(typeof(ModuleShieldGenerator), "OnDetaching")]
|
||||||
static void PostfixDestroy(ModuleShieldGenerator __instance) => manager.OnObjectDetached(__instance);
|
static void PostfixDestroy(ModuleShieldGenerator __instance) {
|
||||||
|
manager.OnObjectDetached(__instance);
|
||||||
|
}
|
||||||
|
|
||||||
public static void DoPatch() => manager.ApplyAll();
|
public static void DoPatch() {
|
||||||
|
manager.ApplyAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -39,7 +39,7 @@ namespace TerraTech {
|
|||||||
public void CaptureOriginal() {
|
public void CaptureOriginal() {
|
||||||
originalValue = GetValue();
|
originalValue = GetValue();
|
||||||
if (Main.debug.Value)
|
if (Main.debug.Value)
|
||||||
Console.WriteLine(string.Format("Captured original value for {0}: {1}", fieldName, originalValue));
|
Console.WriteLine("Captured original value for {0}: {1}", fieldName, originalValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Apply() {
|
public void Apply() {
|
||||||
@@ -49,8 +49,7 @@ namespace TerraTech {
|
|||||||
var newValue = (TValue)(originalDynamic * multiplierDynamic);
|
var newValue = (TValue)(originalDynamic * multiplierDynamic);
|
||||||
|
|
||||||
if (Main.debug.Value)
|
if (Main.debug.Value)
|
||||||
Console.WriteLine(string.Format("Applying to {0}: {1} * {2} = {3}", fieldName, originalValue,
|
Console.WriteLine("Applying to {0}: {1} * {2} = {3}", fieldName, originalValue, multiplier.Value, newValue);
|
||||||
multiplier.Value, newValue));
|
|
||||||
|
|
||||||
SetValue(newValue);
|
SetValue(newValue);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -61,7 +60,7 @@ namespace TerraTech {
|
|||||||
|
|
||||||
public void Restore() {
|
public void Restore() {
|
||||||
if (Main.debug.Value)
|
if (Main.debug.Value)
|
||||||
Console.WriteLine(string.Format("Restoring {0} to original value: {1}", fieldName, originalValue));
|
Console.WriteLine("Restoring {0} to original value: {1}", fieldName, originalValue);
|
||||||
SetValue(originalValue);
|
SetValue(originalValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,17 +68,17 @@ namespace TerraTech {
|
|||||||
if (!Main.debug.Value)
|
if (!Main.debug.Value)
|
||||||
return;
|
return;
|
||||||
var currentValue = GetValue();
|
var currentValue = GetValue();
|
||||||
Console.WriteLine(
|
Console.WriteLine("{0} {1}; {2}: {3} (original: {4}, multiplier: {5})", prefix, parentTraverse, fieldName, currentValue, originalValue, multiplier.Value);
|
||||||
$"{prefix} {parentTraverse}; {fieldName}: {currentValue} (original: {originalValue}, multiplier: {multiplier.Value})");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MultipliedObject<T> {
|
public class MultipliedObject<T> {
|
||||||
private readonly Traverse objectTraverse;
|
private readonly Traverse objectTraverse;
|
||||||
private readonly List<MultipliedField<T, float>> fields = new List<MultipliedField<T, float>>();
|
private readonly List<MultipliedField<T, float>> fields;
|
||||||
|
|
||||||
public MultipliedObject(T instance) {
|
public MultipliedObject(T instance) {
|
||||||
objectTraverse = Traverse.Create(instance);
|
objectTraverse = Traverse.Create(instance);
|
||||||
|
fields = new List<MultipliedField<T, float>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddField(string fieldName, ConfigEntry<float> multiplier) {
|
public void AddField(string fieldName, ConfigEntry<float> multiplier) {
|
||||||
@@ -87,37 +86,46 @@ namespace TerraTech {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void CaptureFrom() {
|
public void CaptureFrom() {
|
||||||
fields.ForEach(f => f.CaptureOriginal());
|
foreach (var field in fields) {
|
||||||
|
field.CaptureOriginal();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ApplyTo() {
|
public void ApplyTo() {
|
||||||
fields.ForEach(f => f.Apply());
|
foreach (var field in fields) {
|
||||||
|
field.Apply();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RestoreTo() {
|
public void RestoreTo() {
|
||||||
fields.ForEach(f => f.Restore());
|
foreach (var field in fields) {
|
||||||
|
field.Restore();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LogValues(string prefix) {
|
public void LogValues(string prefix) {
|
||||||
if (!Main.debug.Value)
|
if (!Main.debug.Value)
|
||||||
return;
|
return;
|
||||||
fields.ForEach(f => f.LogValue(prefix));
|
foreach (var field in fields) {
|
||||||
|
field.LogValue(prefix);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MultipliedObjectManager<T>
|
public class MultipliedObjectManager<T>
|
||||||
where T : class {
|
where T : class {
|
||||||
private readonly Dictionary<T, MultipliedObject<T>> managedObjects = new Dictionary<T, MultipliedObject<T>>();
|
private readonly Dictionary<T, MultipliedObject<T>> managedObjects;
|
||||||
private readonly Action<MultipliedObject<T>> configureObject;
|
private readonly Action<MultipliedObject<T>> configureObject;
|
||||||
|
|
||||||
public MultipliedObjectManager(Action<MultipliedObject<T>> configureObject) {
|
public MultipliedObjectManager(Action<MultipliedObject<T>> configureObject) {
|
||||||
this.configureObject = configureObject;
|
this.configureObject = configureObject;
|
||||||
|
managedObjects = new Dictionary<T, MultipliedObject<T>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnObjectAttached(T instance) {
|
public void OnObjectAttached(T instance) {
|
||||||
if (!managedObjects.ContainsKey(instance)) {
|
if (!managedObjects.ContainsKey(instance)) {
|
||||||
if (Main.debug.Value)
|
if (Main.debug.Value)
|
||||||
Console.WriteLine(string.Format("{0}.OnAttached", typeof(T).Name));
|
Console.WriteLine("{0}.OnAttached", typeof(T).Name);
|
||||||
|
|
||||||
var multipliedObject = new MultipliedObject<T>(instance);
|
var multipliedObject = new MultipliedObject<T>(instance);
|
||||||
configureObject(multipliedObject);
|
configureObject(multipliedObject);
|
||||||
@@ -131,9 +139,10 @@ namespace TerraTech {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void OnObjectDetached(T instance) {
|
public void OnObjectDetached(T instance) {
|
||||||
if (managedObjects.TryGetValue(instance, out var multipliedObject)) {
|
MultipliedObject<T> multipliedObject;
|
||||||
|
if (managedObjects.TryGetValue(instance, out multipliedObject)) {
|
||||||
if (Main.debug.Value) {
|
if (Main.debug.Value) {
|
||||||
Console.WriteLine($"{typeof(T).Name}.OnDetaching");
|
Console.WriteLine("{0}.OnDetaching", typeof(T).Name);
|
||||||
multipliedObject.LogValues("Restoring");
|
multipliedObject.LogValues("Restoring");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +154,7 @@ namespace TerraTech {
|
|||||||
|
|
||||||
public void ApplyAll() {
|
public void ApplyAll() {
|
||||||
if (Main.debug.Value)
|
if (Main.debug.Value)
|
||||||
Console.WriteLine($"Modifying {managedObjects.Count} {typeof(T).Name}");
|
Console.WriteLine("Modifying {0} {1}", managedObjects.Count, typeof(T).Name);
|
||||||
foreach (var instance in managedObjects.Keys) {
|
foreach (var instance in managedObjects.Keys) {
|
||||||
RestoreTo(instance);
|
RestoreTo(instance);
|
||||||
ApplyTo(instance);
|
ApplyTo(instance);
|
||||||
@@ -153,13 +162,15 @@ namespace TerraTech {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void ApplyTo(T instance) {
|
private void ApplyTo(T instance) {
|
||||||
if (managedObjects.TryGetValue(instance, out var obj))
|
MultipliedObject<T> obj;
|
||||||
obj.ApplyTo()
|
if (managedObjects.TryGetValue(instance, out obj))
|
||||||
|
obj.ApplyTo();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RestoreTo(T instance) {
|
private void RestoreTo(T instance) {
|
||||||
if (managedObjects.TryGetValue(instance, out var obj))
|
MultipliedObject<T> obj;
|
||||||
obj.RestoreTo()
|
if (managedObjects.TryGetValue(instance, out obj))
|
||||||
|
obj.RestoreTo();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user