Make apply only to player option
This commit is contained in:
@@ -14,7 +14,7 @@ namespace TerraTech {
|
||||
return tank.ControllableByLocalPlayer;
|
||||
}
|
||||
|
||||
public static bool IsObjectTank(object obj) {
|
||||
public static Func<object, bool> IsObjectPlayerTank = obj => {
|
||||
if (obj == null)
|
||||
return false;
|
||||
try {
|
||||
@@ -23,8 +23,6 @@ namespace TerraTech {
|
||||
Console.WriteLine("Failed to check if object is a player tank: " + e.Message);
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@@ -11,6 +11,7 @@ namespace TerraTech {
|
||||
private static readonly MultipliedObjectManager<BoosterJet> jetManager =
|
||||
new MultipliedObjectManager<BoosterJet>(ConfigureJetThruster);
|
||||
|
||||
public static ConfigEntry<bool> playerOnly;
|
||||
public static ConfigEntry<float> fanThrustMultiplier;
|
||||
public static ConfigEntry<float> jetThrustMultiplier;
|
||||
|
||||
@@ -18,6 +19,9 @@ namespace TerraTech {
|
||||
float min = 0.01f;
|
||||
float max = 32f;
|
||||
|
||||
playerOnly = config.Bind("Booster", "Player Only", false, new ConfigDescription("Player Only"));
|
||||
playerOnly.SettingChanged += (sender, args) => DoPatch();
|
||||
|
||||
fanThrustMultiplier =
|
||||
config.Bind("Booster", "Fan Thrust Multiplier", 1f,
|
||||
new ConfigDescription("Fan Thrust Multiplier", new AcceptableValueRange<float>(min, max)));
|
||||
@@ -30,13 +34,19 @@ namespace TerraTech {
|
||||
}
|
||||
|
||||
private static void ConfigureFanThruster(MultipliedObject<FanJet> obj) {
|
||||
obj.AddField(new FieldConfiguration<float, float>("m_Force", fanThrustMultiplier));
|
||||
obj.AddField(new FieldConfiguration<float, float>("m_Force", fanThrustMultiplier, ShouldApply));
|
||||
}
|
||||
|
||||
private static void ConfigureJetThruster(MultipliedObject<BoosterJet> obj) {
|
||||
obj.AddField(new FieldConfiguration<float, float>("m_Force", jetThrustMultiplier));
|
||||
obj.AddField(new FieldConfiguration<float, float>("m_Force", jetThrustMultiplier, ShouldApply));
|
||||
}
|
||||
|
||||
private static readonly Func<object, bool> ShouldApply = obj => {
|
||||
if (!playerOnly.Value)
|
||||
return true;
|
||||
return CykUtil.IsObjectPlayerTank(obj);
|
||||
};
|
||||
|
||||
[HarmonyPrefix]
|
||||
[HarmonyPatch(typeof(ModuleBooster), "OnAttached")]
|
||||
static void PostfixCreate(ModuleBooster __instance) {
|
||||
|
@@ -21,6 +21,7 @@ namespace TerraTech {
|
||||
private string _fieldName;
|
||||
private ConfigEntry<TMul> _defaultMultiplier;
|
||||
private Func<object, ConfigEntry<TMul>> _conditionalMultiplier;
|
||||
private Func<object, bool> _applyCondition;
|
||||
|
||||
public string FieldName {
|
||||
get { return _fieldName; }
|
||||
@@ -37,6 +38,11 @@ namespace TerraTech {
|
||||
set { _conditionalMultiplier = value; }
|
||||
}
|
||||
|
||||
public Func<object, bool> ApplyCondition {
|
||||
get { return _applyCondition; }
|
||||
set { _applyCondition = value; }
|
||||
}
|
||||
|
||||
public FieldConfiguration(string fieldName, ConfigEntry<TMul> defaultMultiplier) {
|
||||
_fieldName = fieldName;
|
||||
_defaultMultiplier = defaultMultiplier;
|
||||
@@ -49,28 +55,54 @@ namespace TerraTech {
|
||||
_conditionalMultiplier = conditionalMultiplier;
|
||||
}
|
||||
|
||||
public FieldConfiguration(string fieldName, ConfigEntry<TMul> defaultMultiplier,
|
||||
Func<object, bool> applyCondition) {
|
||||
_fieldName = fieldName;
|
||||
_defaultMultiplier = defaultMultiplier;
|
||||
_applyCondition = applyCondition;
|
||||
}
|
||||
|
||||
public FieldConfiguration(string fieldName, ConfigEntry<TMul> defaultMultiplier,
|
||||
Func<object, ConfigEntry<TMul>> conditionalMultiplier,
|
||||
Func<object, bool> applyCondition) {
|
||||
_fieldName = fieldName;
|
||||
_defaultMultiplier = defaultMultiplier;
|
||||
_conditionalMultiplier = conditionalMultiplier;
|
||||
_applyCondition = applyCondition;
|
||||
}
|
||||
|
||||
public ConfigEntry<TMul> GetMultiplier(object instance) {
|
||||
if (_conditionalMultiplier == null) {
|
||||
return _defaultMultiplier;
|
||||
}
|
||||
return _conditionalMultiplier(instance);
|
||||
}
|
||||
|
||||
public bool ShouldApply(object instance) {
|
||||
if (_applyCondition == null) {
|
||||
return true;
|
||||
}
|
||||
return _applyCondition(instance);
|
||||
}
|
||||
}
|
||||
|
||||
public class MultipliedField<TField, TMul> : IFieldModifier {
|
||||
private readonly string _fieldName;
|
||||
private readonly ConfigEntry<TMul> _multiplier;
|
||||
private readonly Traverse _parentTraverse;
|
||||
private readonly Func<object, bool> _applyCondition;
|
||||
private TField _originalValue;
|
||||
|
||||
public string FieldName {
|
||||
get { return _fieldName; }
|
||||
}
|
||||
|
||||
public MultipliedField(string fieldName, ConfigEntry<TMul> multiplier, Traverse parentTraverse) {
|
||||
public MultipliedField(string fieldName, ConfigEntry<TMul> multiplier, Traverse parentTraverse,
|
||||
Func<object, bool> applyCondition = null) {
|
||||
_fieldName = fieldName;
|
||||
_multiplier = multiplier;
|
||||
_parentTraverse = parentTraverse;
|
||||
_applyCondition = applyCondition;
|
||||
|
||||
if (!parentTraverse.Field(fieldName).FieldExists()) {
|
||||
throw new ArgumentException(
|
||||
@@ -128,6 +160,12 @@ namespace TerraTech {
|
||||
|
||||
public void Apply() {
|
||||
try {
|
||||
if (_applyCondition != null && !_applyCondition(_parentTraverse.GetValue())) {
|
||||
if (Main.debug.Value)
|
||||
Console.WriteLine("Skipping {0}: condition not met", _fieldName);
|
||||
return;
|
||||
}
|
||||
|
||||
var newValue = MultiplyValues(_originalValue, _multiplier.Value);
|
||||
|
||||
if (Main.debug.Value)
|
||||
@@ -160,6 +198,7 @@ namespace TerraTech {
|
||||
private string _fieldName;
|
||||
private ConfigEntry<bool> _value;
|
||||
private Func<object, ConfigEntry<bool>> _conditionalValue;
|
||||
private Func<object, bool> _applyCondition;
|
||||
|
||||
public string FieldName {
|
||||
get { return _fieldName; }
|
||||
@@ -176,6 +215,11 @@ namespace TerraTech {
|
||||
set { _conditionalValue = value; }
|
||||
}
|
||||
|
||||
public Func<object, bool> ApplyCondition {
|
||||
get { return _applyCondition; }
|
||||
set { _applyCondition = value; }
|
||||
}
|
||||
|
||||
public BooleanFieldConfiguration(string fieldName, ConfigEntry<bool> value) {
|
||||
_fieldName = fieldName;
|
||||
_value = value;
|
||||
@@ -188,28 +232,53 @@ namespace TerraTech {
|
||||
_conditionalValue = conditionalValue;
|
||||
}
|
||||
|
||||
public BooleanFieldConfiguration(string fieldName, ConfigEntry<bool> value, Func<object, bool> applyCondition) {
|
||||
_fieldName = fieldName;
|
||||
_value = value;
|
||||
_applyCondition = applyCondition;
|
||||
}
|
||||
|
||||
public BooleanFieldConfiguration(string fieldName, ConfigEntry<bool> value,
|
||||
Func<object, ConfigEntry<bool>> conditionalValue,
|
||||
Func<object, bool> applyCondition) {
|
||||
_fieldName = fieldName;
|
||||
_value = value;
|
||||
_conditionalValue = conditionalValue;
|
||||
_applyCondition = applyCondition;
|
||||
}
|
||||
|
||||
public ConfigEntry<bool> GetValue(object instance) {
|
||||
if (_conditionalValue == null) {
|
||||
return _value;
|
||||
}
|
||||
return _conditionalValue(instance);
|
||||
}
|
||||
|
||||
public bool ShouldApply(object instance) {
|
||||
if (_applyCondition == null) {
|
||||
return true;
|
||||
}
|
||||
return _applyCondition(instance);
|
||||
}
|
||||
}
|
||||
|
||||
public class BooleanField : IFieldModifier {
|
||||
private readonly string _fieldName;
|
||||
private readonly ConfigEntry<bool> _value;
|
||||
private readonly Traverse _parentTraverse;
|
||||
private readonly Func<object, bool> _applyCondition;
|
||||
private bool _originalValue;
|
||||
|
||||
public string FieldName {
|
||||
get { return _fieldName; }
|
||||
}
|
||||
|
||||
public BooleanField(string fieldName, ConfigEntry<bool> value, Traverse parentTraverse) {
|
||||
public BooleanField(string fieldName, ConfigEntry<bool> value, Traverse parentTraverse,
|
||||
Func<object, bool> applyCondition = null) {
|
||||
_fieldName = fieldName;
|
||||
_value = value;
|
||||
_parentTraverse = parentTraverse;
|
||||
_applyCondition = applyCondition;
|
||||
|
||||
if (!parentTraverse.Field(fieldName).FieldExists()) {
|
||||
throw new ArgumentException(
|
||||
@@ -240,6 +309,12 @@ namespace TerraTech {
|
||||
|
||||
public void Apply() {
|
||||
try {
|
||||
if (_applyCondition != null && !_applyCondition(_parentTraverse.GetValue())) {
|
||||
if (Main.debug.Value)
|
||||
Console.WriteLine("Skipping {0}: condition not met", _fieldName);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_value.Value) {
|
||||
if (Main.debug.Value)
|
||||
Console.WriteLine("Applying to {0}: forcing to true", _fieldName);
|
||||
@@ -287,12 +362,12 @@ namespace TerraTech {
|
||||
public void AddField<TField, TMul>(FieldConfiguration<TField, TMul> config) {
|
||||
var multiplier = config.GetMultiplier(_instance);
|
||||
_fields[config.FieldName] =
|
||||
new MultipliedField<TField, TMul>(config.FieldName, multiplier, _objectTraverse);
|
||||
new MultipliedField<TField, TMul>(config.FieldName, multiplier, _objectTraverse, config.ShouldApply);
|
||||
}
|
||||
|
||||
public void AddBooleanField(BooleanFieldConfiguration config) {
|
||||
var value = config.GetValue(_instance);
|
||||
_fields[config.FieldName] = new BooleanField(config.FieldName, value, _objectTraverse);
|
||||
_fields[config.FieldName] = new BooleanField(config.FieldName, value, _objectTraverse, config.ShouldApply);
|
||||
}
|
||||
|
||||
public void CaptureFrom() {
|
||||
@@ -347,7 +422,7 @@ namespace TerraTech {
|
||||
try {
|
||||
_managedObjects.Remove(instance);
|
||||
} catch (Exception e) {
|
||||
Console.WriteLine(String.Format("Error removing instance from _managedObjects: {0}", e));
|
||||
Console.WriteLine("Error removing instance from _managedObjects: {0}", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -376,7 +451,7 @@ namespace TerraTech {
|
||||
ApplyTo(instance);
|
||||
multipliedObject.LogValues("Patched");
|
||||
} catch (Exception e) {
|
||||
Console.WriteLine(String.Format("Error in OnObjectAttached: {0}", e));
|
||||
Console.WriteLine("Error in OnObjectAttached: {0}", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -398,13 +473,13 @@ namespace TerraTech {
|
||||
RestoreTo(instance);
|
||||
multipliedObject.LogValues("Restored");
|
||||
} catch (Exception e) {
|
||||
Console.WriteLine(String.Format("Error restoring values: {0}", e));
|
||||
Console.WriteLine("Error restoring values: {0}", e);
|
||||
}
|
||||
|
||||
SafeRemove(instance);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Console.WriteLine(String.Format("Error in OnObjectDetached: {0}", e));
|
||||
Console.WriteLine("Error in OnObjectDetached: {0}", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -420,7 +495,7 @@ namespace TerraTech {
|
||||
RestoreTo(instance, fieldNames);
|
||||
ApplyTo(instance, fieldNames);
|
||||
} catch (Exception e) {
|
||||
Console.WriteLine(String.Format("Error applying to instance: {0}", e));
|
||||
Console.WriteLine("Error applying to instance: {0}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -436,7 +511,7 @@ namespace TerraTech {
|
||||
if (_managedObjects.TryGetValue(instance, out obj))
|
||||
obj.ApplyTo(fieldNames);
|
||||
} catch (Exception e) {
|
||||
Console.WriteLine(String.Format("Error in ApplyTo: {0}", e));
|
||||
Console.WriteLine("Error in ApplyTo: {0}", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -451,7 +526,7 @@ namespace TerraTech {
|
||||
if (_managedObjects.TryGetValue(instance, out obj))
|
||||
obj.RestoreTo(fieldNames);
|
||||
} catch (Exception e) {
|
||||
Console.WriteLine(String.Format("Error in RestoreTo: {0}", e));
|
||||
Console.WriteLine("Error in RestoreTo: {0}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user