Skip to content

Commit

Permalink
refactor: do some code cleanup/updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyvion committed May 23, 2024
1 parent 4a3b1dc commit de4ec5d
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace FarmingHysteresis.VanillaPlantsExpandedMorePlants.Patch;

/// <summary>
/// Add Farming Hysteresis gizmos to the growing zone
/// Add Farming Hysteresis gizmos to the Zone_GrowingAquatic
/// </summary>
[HarmonyPatch(typeof(Zone_GrowingAquatic), nameof(Zone_GrowingAquatic.GetGizmos))]
internal static class Zone_GrowingAquatic_GetGizmos
Expand All @@ -29,7 +29,7 @@ g is Command_Toggle t &&
}

/// <summary>
/// Add Farming Hysteresis gizmos to any Building_PlantGrower
/// Add Farming Hysteresis gizmos to the Zone_GrowingSandy
/// </summary>
[HarmonyPatch(typeof(Zone_GrowingSandy), nameof(Zone_GrowingSandy.GetGizmos))]
internal static class Zone_GrowingSandy_GetGizmos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ namespace FarmingHysteresis.VanillaPlantsExpandedMorePlants.Patch;
[HarmonyPatch(typeof(Zone), nameof(Zone.GetInspectTabs))]
internal static class Zone_GetInspectTabs
{
private static readonly ITab[] ITabs = new ITab[1]
{
private static readonly ITab[] ITabs =
[
new ITab_Hysteresis()
};
];

private static IEnumerable<InspectTabBase> Postfix(IEnumerable<InspectTabBase> values, Zone __instance)
{
Expand Down
4 changes: 2 additions & 2 deletions Source/FarmingHysteresis/Command_Decrement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ internal class Command_Decrement : Command_Action
public override GizmoResult GizmoOnGUI(Vector2 loc, float maxWidth, GizmoRenderParms parms)
{
GizmoResult result = base.GizmoOnGUI(loc, maxWidth, parms);
Rect rect = new Rect(loc.x, loc.y, GetWidth(maxWidth), 75f);
Rect position = new Rect(rect.x + rect.width - GenUI.SmallIconSize, rect.y, GenUI.SmallIconSize, GenUI.SmallIconSize);
Rect rect = new(loc.x, loc.y, GetWidth(maxWidth), 75f);
Rect position = new(rect.x + rect.width - GenUI.SmallIconSize, rect.y, GenUI.SmallIconSize, GenUI.SmallIconSize);
Texture2D image = ContentFinder<Texture2D>.Get("UI/Overlays/Arrow");

// The arrow ordinarily points up. So let's render it upside down.
Expand Down
4 changes: 2 additions & 2 deletions Source/FarmingHysteresis/Command_Increment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ internal class Command_Increment : Command_Action
public override GizmoResult GizmoOnGUI(Vector2 loc, float maxWidth, GizmoRenderParms parms)
{
GizmoResult result = base.GizmoOnGUI(loc, maxWidth, parms);
Rect rect = new Rect(loc.x, loc.y, GetWidth(maxWidth), 75f);
Rect position = new Rect(rect.x + rect.width - GenUI.SmallIconSize, rect.y, GenUI.SmallIconSize, GenUI.SmallIconSize);
Rect rect = new(loc.x, loc.y, GetWidth(maxWidth), 75f);
Rect position = new(rect.x + rect.width - GenUI.SmallIconSize, rect.y, GenUI.SmallIconSize, GenUI.SmallIconSize);
Texture2D image = ContentFinder<Texture2D>.Get("UI/Overlays/Arrow");

GUI.DrawTexture(position, image);
Expand Down
2 changes: 1 addition & 1 deletion Source/FarmingHysteresis/Core/FarmingHysteresisMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace FarmingHysteresis
{
public class FarmingHysteresisMod : Mod
{
private ModContentPack content;
private readonly ModContentPack content;

public FarmingHysteresisMod(ModContentPack content) : base(content)
{
Expand Down
49 changes: 22 additions & 27 deletions Source/FarmingHysteresis/Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ public class Settings : ModSettings
private static bool _showOldCommands = false;
private static bool _showHysteresisMainTab = true;

internal static int DefaultHysteresisLowerBound { get => _defaultHysteresisLowerBound; set => _defaultHysteresisLowerBound = value; }
internal static int DefaultHysteresisUpperBound { get => _defaultHysteresisUpperBound; set => _defaultHysteresisUpperBound = value; }
internal static bool EnabledByDefault { get => _enabledByDefault; set => _enabledByDefault = value; }
internal static bool UseGlobalValuesByDefault { get => _useGlobalValuesByDefault; set => _useGlobalValuesByDefault = value; }
internal static bool CountAllOnMap { get => _countAllOnMap; set => _countAllOnMap = value; }
internal static HysteresisMode HysteresisMode { get => _hysteresisMode; set => _hysteresisMode = value; }
internal static bool ShowOldCommands { get => _showOldCommands; set => _showOldCommands = value; }
internal static bool ShowHysteresisMainTab { get => _showHysteresisMainTab; set => _showHysteresisMainTab = value; }

internal static bool ControlSowing => _hysteresisMode == HysteresisMode.Sowing || _hysteresisMode == HysteresisMode.SowingAndHarvesting;
internal static bool ControlHarvesting => _hysteresisMode == HysteresisMode.Harvesting || _hysteresisMode == HysteresisMode.SowingAndHarvesting;
public static int DefaultHysteresisLowerBound { get => _defaultHysteresisLowerBound; internal set => _defaultHysteresisLowerBound = value; }
public static int DefaultHysteresisUpperBound { get => _defaultHysteresisUpperBound; internal set => _defaultHysteresisUpperBound = value; }
public static bool EnabledByDefault { get => _enabledByDefault; internal set => _enabledByDefault = value; }
public static bool UseGlobalValuesByDefault { get => _useGlobalValuesByDefault; internal set => _useGlobalValuesByDefault = value; }
public static bool CountAllOnMap { get => _countAllOnMap; internal set => _countAllOnMap = value; }
public static HysteresisMode HysteresisMode { get => _hysteresisMode; internal set => _hysteresisMode = value; }
public static bool ShowOldCommands { get => _showOldCommands; internal set => _showOldCommands = value; }
public static bool ShowHysteresisMainTab { get => _showHysteresisMainTab; internal set => _showHysteresisMainTab = value; }

public static bool ControlSowing => _hysteresisMode == HysteresisMode.Sowing || _hysteresisMode == HysteresisMode.SowingAndHarvesting;
public static bool ControlHarvesting => _hysteresisMode == HysteresisMode.Harvesting || _hysteresisMode == HysteresisMode.SowingAndHarvesting;

public override void ExposeData()
{
Expand All @@ -48,15 +48,15 @@ public override void ExposeData()

public static void DoSettingsWindowContents(Rect inRect)
{
Listing_Standard listingStandard = new Listing_Standard();
Listing_Standard listingStandard = new();
listingStandard.Begin(inRect);

listingStandard.CheckboxLabeled("FarmingHysteresis.EnabledByDefault".Translate(), ref _enabledByDefault);
listingStandard.CheckboxLabeled("FarmingHysteresis.UseGlobalValuesByDefault".Translate(), ref _useGlobalValuesByDefault);

// Calculate where the CountAllOnMap checkbox will go
var textHeight = Text.CalcHeight("FarmingHysteresis.CountAllOnMap".Translate(), listingStandard.ColumnWidth);
Rect textRect = new Rect(
Rect textRect = new(
Traverse.Create(listingStandard).Field<float>("curX").Value,
Traverse.Create(listingStandard).Field<float>("curY").Value,
listingStandard.ColumnWidth,
Expand Down Expand Up @@ -94,8 +94,8 @@ public static void DoSettingsWindowContents(Rect inRect)
TextAnchor.MiddleLeft))
{
#endif
List<FloatMenuOption> list = new()
{
List<FloatMenuOption> list =
[
new FloatMenuOption(
"FarmingHysteresis.Control".Translate("FarmingHysteresis.Sowing".Translate()),
() => _hysteresisMode = HysteresisMode.Sowing),
Expand All @@ -105,7 +105,7 @@ public static void DoSettingsWindowContents(Rect inRect)
new FloatMenuOption(
"FarmingHysteresis.Control".Translate("FarmingHysteresis.SowingAndHarvesting".Translate()),
() => _hysteresisMode = HysteresisMode.SowingAndHarvesting)
};
];
Find.WindowStack.Add(new FloatMenu(list));
}

Expand All @@ -129,18 +129,13 @@ public static class HysteresisModeExtensions
{
public static string AsString(this HysteresisMode mode)
{
switch (mode)
return mode switch
{
case HysteresisMode.Sowing:
return "FarmingHysteresis.Sowing".Translate();
case HysteresisMode.Harvesting:
return "FarmingHysteresis.Harvesting".Translate();
case HysteresisMode.SowingAndHarvesting:
return "FarmingHysteresis.SowingAndHarvesting".Translate();

default:
throw new Exception($"Uncovered HysteresisMode: {mode}");
}
HysteresisMode.Sowing => "FarmingHysteresis.Sowing".Translate(),
HysteresisMode.Harvesting => "FarmingHysteresis.Harvesting".Translate(),
HysteresisMode.SowingAndHarvesting => "FarmingHysteresis.SowingAndHarvesting".Translate(),
_ => throw new Exception($"Uncovered HysteresisMode: {mode}"),
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ private static void ThrowError(IPlantToGrowSettable plantGrower, string method)

internal static bool GetAllowSow(this IPlantToGrowSettable plantGrower)
{
var def = GetControlDefForPlantGrower(plantGrower, nameof(SetHysteresisControlState));
var def = GetControlDefForPlantGrower(plantGrower, nameof(GetAllowSow));
return def.GetAllowSow(plantGrower);
}

internal static bool GetAllowHarvest(this IPlantToGrowSettable plantGrower)
{
var def = GetControlDefForPlantGrower(plantGrower, nameof(SetHysteresisControlState));
var def = GetControlDefForPlantGrower(plantGrower, nameof(GetAllowHarvest));
return def.GetAllowHarvest(plantGrower);
}

internal static FarmingHysteresisData GetFarmingHysteresisData(this IPlantToGrowSettable plantGrower)
{
return dataTable.GetValue(plantGrower, (pg) => new FarmingHysteresisData(new System.WeakReference<IPlantToGrowSettable>(pg)));
return dataTable.GetValue(plantGrower, (pg) => new FarmingHysteresisData(pg));
}
}
58 changes: 32 additions & 26 deletions Source/FarmingHysteresis/FarmingHysteresisData.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.CompilerServices;
using FarmingHysteresis.Extensions;
using RimWorld;
using Verse;
Expand All @@ -20,19 +19,19 @@ public void ExposeData()

internal class FarmingHysteresisData : IBoundedValueAccessor
{
private System.WeakReference<IPlantToGrowSettable> zoneWeakReference;
private readonly System.WeakReference<IPlantToGrowSettable> _plantGrowerWeakReference;

private bool enabled;
private BoundValues bounds;
public LatchMode latchMode;
private bool _enabled;
private BoundValues _bounds;

public LatchMode latchMode;
public bool useGlobalValues;

public FarmingHysteresisData(System.WeakReference<IPlantToGrowSettable> weakReference)
public FarmingHysteresisData(IPlantToGrowSettable plantGrower)
{
zoneWeakReference = weakReference;
enabled = Settings.EnabledByDefault;
bounds = new BoundValues
_plantGrowerWeakReference = new(plantGrower);
_enabled = Settings.EnabledByDefault;
_bounds = new BoundValues
{
Upper = Settings.DefaultHysteresisUpperBound,
Lower = Settings.DefaultHysteresisLowerBound
Expand All @@ -43,25 +42,28 @@ public FarmingHysteresisData(System.WeakReference<IPlantToGrowSettable> weakRefe

internal void ExposeData()
{
Scribe_Values.Look(ref enabled, "farmingHysteresisEnabled", Settings.EnabledByDefault, true);
Scribe_Values.Look(ref _enabled, "farmingHysteresisEnabled", Settings.EnabledByDefault, true);
Scribe_Deep.Look(
ref bounds,
ref _bounds,
"farmingHysteresisBounds"
);
if (bounds == null)
_bounds ??= new BoundValues
{
bounds = new BoundValues
{
Upper = Settings.DefaultHysteresisUpperBound,
Lower = Settings.DefaultHysteresisLowerBound
};
}
Upper = Settings.DefaultHysteresisUpperBound,
Lower = Settings.DefaultHysteresisLowerBound
};

#if v1_5
#else
if (Scribe.mode == LoadSaveMode.LoadingVars)
{
TransferOldBounds();
}
#endif

Scribe_Values.Look(ref latchMode, "farmingHysteresisLatchMode", LatchMode.Unknown, true);
#if v1_5
#else
if (Scribe.mode == LoadSaveMode.LoadingVars)
{
// Ignore obsolete warning (612) since we're explicitly
Expand All @@ -79,26 +81,30 @@ internal void ExposeData()
}
#pragma warning restore 612
}
#endif
Scribe_Values.Look(ref useGlobalValues, "farmingHysteresisUseGlobalValues", Settings.UseGlobalValuesByDefault, true);

#if v1_5
#else
void TransferOldBounds()
{
int lowerBound = 0;
int upperBound = 0;
Scribe_Values.Look(ref lowerBound, "farmingHysteresisLowerBound", 0);
if (lowerBound != 0)
{
bounds.Lower = lowerBound;
_bounds.Lower = lowerBound;
}
Scribe_Values.Look(ref upperBound, "farmingHysteresisUpperBound", 0);
if (upperBound != 0)
{
bounds.Upper = upperBound;
_bounds.Upper = upperBound;
}
}
#endif
}

BoundValues IBoundedValueAccessor.BoundValueRaw => bounds;
BoundValues IBoundedValueAccessor.BoundValueRaw => _bounds;

private IBoundedValueAccessor GetBoundedValueAccessor()
{
Expand All @@ -109,7 +115,7 @@ private IBoundedValueAccessor GetBoundedValueAccessor()
}
else
{
if (zoneWeakReference.TryGetTarget(out var zone))
if (_plantGrowerWeakReference.TryGetTarget(out var zone))
{
var (harvestedThingDef, _) = zone.PlantHarvestInfo();
if (harvestedThingDef == null)
Expand Down Expand Up @@ -167,18 +173,18 @@ public int UpperBound

internal bool Enabled
{
get { return enabled; }
get { return _enabled; }
}

internal void Enable(IPlantToGrowSettable plantToGrowSettable)
{
enabled = true;
_enabled = true;
UpdateLatchModeAndHandling(plantToGrowSettable);
}

internal void Disable(IPlantToGrowSettable plantToGrowSettable)
{
enabled = false;
_enabled = false;
}

internal void UpdateLatchModeAndHandling(IPlantToGrowSettable plantToGrowSettable)
Expand Down Expand Up @@ -250,7 +256,7 @@ internal void UpdateLatchModeAndHandling(IPlantToGrowSettable plantToGrowSettabl

internal void DisableDueToMissingHarvestedThingDef(IPlantToGrowSettable plantToGrowSettable, ThingDef plantDef)
{
enabled = false;
_enabled = false;
if (plantToGrowSettable is Zone zone)
{
FarmingHysteresisMod.Warning($"Zone '{zone.label}' has a plant type without a harvestable product ({plantDef.label}). Disabling farming hysteresis.");
Expand Down
Loading

0 comments on commit de4ec5d

Please sign in to comment.