Skip to content

Commit

Permalink
Use more nameof and use Harmony to access properties rather than me…
Browse files Browse the repository at this point in the history
…thods

String constant, wherever possible (assuming I haven't missed some), were replaced by the use of `nameof`.

Replaced places where properties are accessed with `AccessTools.PropertyGetter` or `AccessTools.IndexerGetter`, rather than `AccessTools.Method` by using `get_` in the method name.

Also, I've changed array initialization to use collection expressions in `SyncThingFilters` class, since I was already modifying it.
  • Loading branch information
SokyranTheDragon committed Aug 26, 2024
1 parent f0221d0 commit 1290a62
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Source/Client/Factions/Blueprints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
static class SpawnBuildingAsPossiblePatch
{
static MethodInfo SpawningWipes = AccessTools.Method(typeof(GenSpawn), nameof(GenSpawn.SpawningWipes));
public static MethodInfo ThingListGet = AccessTools.Method(typeof(List<Thing>), "get_Item");
static FieldInfo ThingDefField = AccessTools.Field(typeof(Thing), "def");
public static MethodInfo ThingListGet = AccessTools.IndexerGetter(typeof(List<Thing>), [typeof(int)]);
static FieldInfo ThingDefField = AccessTools.Field(typeof(Thing), nameof(Thing.def));

static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> insts)
{
Expand Down
6 changes: 3 additions & 3 deletions Source/Client/MultiplayerStatic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ void TryPatch(MethodBase original, HarmonyMethod prefix = null, HarmonyMethod po
{
var designatorFinalizer = AccessTools.Method(typeof(DesignatorPatches), nameof(DesignatorPatches.DesignateFinalizer));
var designatorMethods = new[] {
("DesignateSingleCell", new[]{ typeof(IntVec3) }),
("DesignateMultiCell", new[]{ typeof(IEnumerable<IntVec3>) }),
("DesignateThing", new[]{ typeof(Thing) }),
(nameof(DesignatorPatches.DesignateSingleCell), new[]{ typeof(IntVec3) }),
(nameof(DesignatorPatches.DesignateMultiCell), new[]{ typeof(IEnumerable<IntVec3>) }),
(nameof(DesignatorPatches.DesignateThing), new[]{ typeof(Thing) }),
};

foreach (Type t in typeof(Designator).AllSubtypesAndSelf()
Expand Down
4 changes: 2 additions & 2 deletions Source/Client/Patches/ArbiterPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static class GUISkinArbiter_Patch
{
static MethodBase TargetMethod()
{
return AccessTools.Method(typeof(GUI), "get_" + nameof(GUI.skin));
return AccessTools.PropertyGetter(typeof(GUI), nameof(GUI.skin));
}

static bool Prefix(ref GUISkin __result)
Expand All @@ -28,7 +28,7 @@ static bool Prefix(ref GUISkin __result)
[HarmonyPatch]
static class RenderTextureCreatePatch
{
static MethodInfo IsCreated = AccessTools.Method(typeof(RenderTexture), "IsCreated");
static MethodInfo IsCreated = AccessTools.Method(typeof(RenderTexture), nameof(RenderTexture.IsCreated));
static FieldInfo ArbiterField = AccessTools.Field(typeof(Multiplayer), nameof(Multiplayer.arbiterInstance));

static IEnumerable<MethodBase> TargetMethods()
Expand Down
2 changes: 1 addition & 1 deletion Source/Client/Persistent/RitualPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static bool Prefix(Window window)
[HarmonyPatch]
static class DontClearDialogBeginRitualCache
{
private static MethodInfo listClear = AccessTools.Method(typeof(List<Precept_Role>), "Clear");
private static MethodInfo listClear = AccessTools.Method(typeof(List<Precept_Role>), nameof(List<Precept_Role>.Clear));

static IEnumerable<MethodBase> TargetMethods()
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Client/Persistent/TradingUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> e, M

for (int i = 0; i < 2; i++)
{
int getAllTradeables = finder.Forward(OpCodes.Callvirt, AccessTools.Method(typeof(TradeDeal), "get_AllTradeables"));
int getAllTradeables = finder.Forward(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(TradeDeal), nameof(TradeDeal.AllTradeables)));

insts.RemoveRange(getAllTradeables - 1, 2);
insts.Insert(getAllTradeables - 1, new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(TradingWindow), nameof(TradingWindow.AllTradeables))));
Expand Down
4 changes: 2 additions & 2 deletions Source/Client/Syncing/Game/SyncFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public static void Init()
SyncFactionAcceptRoyalFavor = Sync.Field(typeof(Faction), nameof(Faction.allowRoyalFavorRewards));
SyncFactionAcceptGoodwill = Sync.Field(typeof(Faction), nameof(Faction.allowGoodwillRewards));

SyncThingFilterHitPoints = Sync.Field(typeof(ThingFilterContext), "Filter/AllowedHitPointsPercents").SetBufferChanges();
SyncThingFilterQuality = Sync.Field(typeof(ThingFilterContext), "Filter/AllowedQualityLevels").SetBufferChanges();
SyncThingFilterHitPoints = Sync.Field(typeof(ThingFilterContext), $"{nameof(ThingFilterContext.Filter)}/{nameof(ThingFilter.AllowedHitPointsPercents)}").SetBufferChanges();
SyncThingFilterQuality = Sync.Field(typeof(ThingFilterContext), $"{nameof(ThingFilterContext.Filter)}/{nameof(ThingFilter.AllowedQualityLevels)}").SetBufferChanges();

SyncBillPaused = Sync.Field(typeof(Bill_Production), nameof(Bill_Production.paused)).SetBufferChanges();
SyncBillSuspended = Sync.Field(typeof(Bill), nameof(Bill.suspended));
Expand Down
12 changes: 6 additions & 6 deletions Source/Client/Syncing/Game/SyncThingFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,47 @@ namespace Multiplayer.Client;

public static class SyncThingFilters
{
[MpPrefix(typeof(ThingFilter), "SetAllow", new[] { typeof(StuffCategoryDef), typeof(bool) })]
[MpPrefix(typeof(ThingFilter), nameof(ThingFilter.SetAllow), [typeof(StuffCategoryDef), typeof(bool)])]
static bool ThingFilter_SetAllow(StuffCategoryDef cat, bool allow)
{
if (!Multiplayer.ShouldSync || ThingFilterMarkers.DrawnThingFilter == null) return true;
AllowStuffCat_Helper(ThingFilterMarkers.DrawnThingFilter, cat, allow);
return false;
}

[MpPrefix(typeof(ThingFilter), "SetAllow", new[] { typeof(SpecialThingFilterDef), typeof(bool) })]
[MpPrefix(typeof(ThingFilter), nameof(ThingFilter.SetAllow), [typeof(SpecialThingFilterDef), typeof(bool)])]
static bool ThingFilter_SetAllow(SpecialThingFilterDef sfDef, bool allow)
{
if (!Multiplayer.ShouldSync || ThingFilterMarkers.DrawnThingFilter == null) return true;
AllowSpecial_Helper(ThingFilterMarkers.DrawnThingFilter, sfDef, allow);
return false;
}

[MpPrefix(typeof(ThingFilter), "SetAllow", new[] { typeof(ThingDef), typeof(bool) })]
[MpPrefix(typeof(ThingFilter), nameof(ThingFilter.SetAllow), [typeof(ThingDef), typeof(bool)])]
static bool ThingFilter_SetAllow(ThingDef thingDef, bool allow)
{
if (!Multiplayer.ShouldSync || ThingFilterMarkers.DrawnThingFilter == null) return true;
AllowThing_Helper(ThingFilterMarkers.DrawnThingFilter, thingDef, allow);
return false;
}

[MpPrefix(typeof(ThingFilter), "SetAllow", new[] { typeof(ThingCategoryDef), typeof(bool), typeof(IEnumerable<ThingDef>), typeof(IEnumerable<SpecialThingFilterDef>) })]
[MpPrefix(typeof(ThingFilter), nameof(ThingFilter.SetAllow), [typeof(ThingCategoryDef), typeof(bool), typeof(IEnumerable<ThingDef>), typeof(IEnumerable<SpecialThingFilterDef>)])]
static bool ThingFilter_SetAllow(ThingCategoryDef categoryDef, bool allow)
{
if (!Multiplayer.ShouldSync || ThingFilterMarkers.DrawnThingFilter == null) return true;
AllowCategory_Helper(ThingFilterMarkers.DrawnThingFilter, categoryDef, allow);
return false;
}

[MpPrefix(typeof(ThingFilter), "SetAllowAll")]
[MpPrefix(typeof(ThingFilter), nameof(ThingFilter.SetAllowAll))]
static bool ThingFilter_SetAllowAll()
{
if (!Multiplayer.ShouldSync || ThingFilterMarkers.DrawnThingFilter == null) return true;
AllowAll_Helper(ThingFilterMarkers.DrawnThingFilter);
return false;
}

[MpPrefix(typeof(ThingFilter), "SetDisallowAll")]
[MpPrefix(typeof(ThingFilter), nameof(ThingFilter.SetDisallowAll))]
static bool ThingFilter_SetDisallowAll()
{
if (!Multiplayer.ShouldSync || ThingFilterMarkers.DrawnThingFilter == null) return true;
Expand Down
24 changes: 12 additions & 12 deletions Source/Client/Syncing/Game/ThingFilterMarkers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static ThingFilterContext DrawnThingFilter
}
}

[MpPrefix(typeof(ITab_Storage), "FillTab")]
[MpPrefix(typeof(ITab_Storage), nameof(ITab_Storage.FillTab))]
static void TabStorageFillTab_Prefix(ITab_Storage __instance)
{
var selThing = __instance.SelObject;
Expand All @@ -37,42 +37,42 @@ static void TabStorageFillTab_Prefix(ITab_Storage __instance)
DrawnThingFilter = new TabStorageWrapper(selParent);
}

[MpPostfix(typeof(ITab_Storage), "FillTab")]
[MpPostfix(typeof(ITab_Storage), nameof(ITab_Storage.FillTab))]
static void TabStorageFillTab_Postfix() => DrawnThingFilter = null;

[MpPrefix(typeof(Dialog_BillConfig), "DoWindowContents")]
[MpPrefix(typeof(Dialog_BillConfig), nameof(Dialog_BillConfig.DoWindowContents))]
static void BillConfig_Prefix(Dialog_BillConfig __instance) =>
DrawnThingFilter = new BillConfigWrapper(__instance.bill);

[MpPostfix(typeof(Dialog_BillConfig), "DoWindowContents")]
[MpPostfix(typeof(Dialog_BillConfig), nameof(Dialog_BillConfig.DoWindowContents))]
static void BillConfig_Postfix() => DrawnThingFilter = null;

[MpPrefix(typeof(Dialog_ManageApparelPolicies), "DoContentsRect")]
[MpPrefix(typeof(Dialog_ManageApparelPolicies), nameof(Dialog_ManageApparelPolicies.DoContentsRect))]
static void ManageOutfit_Prefix(Dialog_ManageApparelPolicies __instance) =>
DrawnThingFilter = new OutfitWrapper(__instance.SelectedPolicy);

[MpPostfix(typeof(Dialog_ManageApparelPolicies), "DoContentsRect")]
[MpPostfix(typeof(Dialog_ManageApparelPolicies), nameof(Dialog_ManageApparelPolicies.DoContentsRect))]
static void ManageOutfit_Postfix() => DrawnThingFilter = null;

[MpPrefix(typeof(Dialog_ManageFoodPolicies), "DoContentsRect")]
[MpPrefix(typeof(Dialog_ManageFoodPolicies), nameof(Dialog_ManageFoodPolicies.DoContentsRect))]
static void ManageFoodRestriction_Prefix(Dialog_ManageFoodPolicies __instance) =>
DrawnThingFilter = new FoodRestrictionWrapper(__instance.SelectedPolicy);

[MpPostfix(typeof(Dialog_ManageFoodPolicies), "DoContentsRect")]
[MpPostfix(typeof(Dialog_ManageFoodPolicies), nameof(Dialog_ManageFoodPolicies.DoContentsRect))]
static void ManageFoodRestriction_Postfix() => DrawnThingFilter = null;

[MpPrefix(typeof(ITab_PenAutoCut), "FillTab")]
[MpPrefix(typeof(ITab_PenAutoCut), nameof(ITab_PenAutoCut.FillTab))]
static void TabPenAutocutFillTab_Prefix(ITab_PenAutoCut __instance) =>
DrawnThingFilter = new PenAutocutWrapper(__instance.SelectedCompAnimalPenMarker);

[MpPostfix(typeof(ITab_PenAutoCut), "FillTab")]
[MpPostfix(typeof(ITab_PenAutoCut), nameof(ITab_PenAutoCut.FillTab))]
static void TabPenAutocutFillTab_Postfix() => DrawnThingFilter = null;

[MpPrefix(typeof(ITab_PenAnimals), "FillTab")]
[MpPrefix(typeof(ITab_PenAnimals), nameof(ITab_PenAnimals.FillTab))]
static void TabPenAnimalsFillTab_Prefix(ITab_PenAnimals __instance) =>
DrawnThingFilter = new PenAnimalsWrapper(__instance.SelectedCompAnimalPenMarker);

[MpPostfix(typeof(ITab_PenAnimals), "FillTab")]
[MpPostfix(typeof(ITab_PenAnimals), nameof(ITab_PenAnimals.FillTab))]
static void TabPenAnimalsFillTab_Postfix() => DrawnThingFilter = null;

[MpPrefix(typeof(ITab_WindTurbineAutoCut), nameof(ITab_WindTurbineAutoCut.FillTab))]
Expand Down
2 changes: 1 addition & 1 deletion Source/Client/Util/MpUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public static void DoPasswordField(Rect rect, string controlName, ref string pas
{
te.scrollOffset = scrollOffset;
te.text = new string(PassChar, password.Length);
AccessTools.Field(typeof(TextEditor), "m_RevealCursor").SetValue(te, true);
AccessTools.Field(typeof(TextEditor), nameof(TextEditor.m_RevealCursor)).SetValue(te, true);
te.UpdateScrollOffsetIfNeeded(new Event());
}
}
Expand Down

0 comments on commit 1290a62

Please sign in to comment.