From 1290a62cd5cc41aa5cc7a503f3308f385368670b Mon Sep 17 00:00:00 2001 From: SokyranTheDragon Date: Mon, 26 Aug 2024 02:01:13 +0200 Subject: [PATCH 1/2] Use more `nameof` and use Harmony to access properties rather than methods 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. --- Source/Client/Factions/Blueprints.cs | 4 ++-- Source/Client/MultiplayerStatic.cs | 6 ++--- Source/Client/Patches/ArbiterPatches.cs | 4 ++-- Source/Client/Persistent/RitualPatches.cs | 2 +- Source/Client/Persistent/TradingUI.cs | 2 +- Source/Client/Syncing/Game/SyncFields.cs | 4 ++-- .../Client/Syncing/Game/SyncThingFilters.cs | 12 +++++----- .../Client/Syncing/Game/ThingFilterMarkers.cs | 24 +++++++++---------- Source/Client/Util/MpUI.cs | 2 +- 9 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Source/Client/Factions/Blueprints.cs b/Source/Client/Factions/Blueprints.cs index 55da6117..70357800 100644 --- a/Source/Client/Factions/Blueprints.cs +++ b/Source/Client/Factions/Blueprints.cs @@ -177,8 +177,8 @@ static IEnumerable Transpiler(IEnumerable inst static class SpawnBuildingAsPossiblePatch { static MethodInfo SpawningWipes = AccessTools.Method(typeof(GenSpawn), nameof(GenSpawn.SpawningWipes)); - public static MethodInfo ThingListGet = AccessTools.Method(typeof(List), "get_Item"); - static FieldInfo ThingDefField = AccessTools.Field(typeof(Thing), "def"); + public static MethodInfo ThingListGet = AccessTools.IndexerGetter(typeof(List), [typeof(int)]); + static FieldInfo ThingDefField = AccessTools.Field(typeof(Thing), nameof(Thing.def)); static IEnumerable Transpiler(IEnumerable insts) { diff --git a/Source/Client/MultiplayerStatic.cs b/Source/Client/MultiplayerStatic.cs index 34ce2342..bc638661 100644 --- a/Source/Client/MultiplayerStatic.cs +++ b/Source/Client/MultiplayerStatic.cs @@ -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) }), - ("DesignateThing", new[]{ typeof(Thing) }), + (nameof(DesignatorPatches.DesignateSingleCell), new[]{ typeof(IntVec3) }), + (nameof(DesignatorPatches.DesignateMultiCell), new[]{ typeof(IEnumerable) }), + (nameof(DesignatorPatches.DesignateThing), new[]{ typeof(Thing) }), }; foreach (Type t in typeof(Designator).AllSubtypesAndSelf() diff --git a/Source/Client/Patches/ArbiterPatches.cs b/Source/Client/Patches/ArbiterPatches.cs index 328ae75e..1d17227a 100644 --- a/Source/Client/Patches/ArbiterPatches.cs +++ b/Source/Client/Patches/ArbiterPatches.cs @@ -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) @@ -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 TargetMethods() diff --git a/Source/Client/Persistent/RitualPatches.cs b/Source/Client/Persistent/RitualPatches.cs index e8cff093..0555a370 100644 --- a/Source/Client/Persistent/RitualPatches.cs +++ b/Source/Client/Persistent/RitualPatches.cs @@ -107,7 +107,7 @@ static bool Prefix(Window window) [HarmonyPatch] static class DontClearDialogBeginRitualCache { - private static MethodInfo listClear = AccessTools.Method(typeof(List), "Clear"); + private static MethodInfo listClear = AccessTools.Method(typeof(List), nameof(List.Clear)); static IEnumerable TargetMethods() { diff --git a/Source/Client/Persistent/TradingUI.cs b/Source/Client/Persistent/TradingUI.cs index b0150dfb..f35f8965 100644 --- a/Source/Client/Persistent/TradingUI.cs +++ b/Source/Client/Persistent/TradingUI.cs @@ -451,7 +451,7 @@ static IEnumerable Transpiler(IEnumerable 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)))); diff --git a/Source/Client/Syncing/Game/SyncFields.cs b/Source/Client/Syncing/Game/SyncFields.cs index a0e28313..242dfefa 100644 --- a/Source/Client/Syncing/Game/SyncFields.cs +++ b/Source/Client/Syncing/Game/SyncFields.cs @@ -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)); diff --git a/Source/Client/Syncing/Game/SyncThingFilters.cs b/Source/Client/Syncing/Game/SyncThingFilters.cs index c4b32f53..33078da2 100644 --- a/Source/Client/Syncing/Game/SyncThingFilters.cs +++ b/Source/Client/Syncing/Game/SyncThingFilters.cs @@ -7,7 +7,7 @@ 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; @@ -15,7 +15,7 @@ static bool ThingFilter_SetAllow(StuffCategoryDef cat, bool 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; @@ -23,7 +23,7 @@ static bool ThingFilter_SetAllow(SpecialThingFilterDef sfDef, bool 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; @@ -31,7 +31,7 @@ static bool ThingFilter_SetAllow(ThingDef thingDef, bool allow) return false; } - [MpPrefix(typeof(ThingFilter), "SetAllow", new[] { typeof(ThingCategoryDef), typeof(bool), typeof(IEnumerable), typeof(IEnumerable) })] + [MpPrefix(typeof(ThingFilter), nameof(ThingFilter.SetAllow), [typeof(ThingCategoryDef), typeof(bool), typeof(IEnumerable), typeof(IEnumerable)])] static bool ThingFilter_SetAllow(ThingCategoryDef categoryDef, bool allow) { if (!Multiplayer.ShouldSync || ThingFilterMarkers.DrawnThingFilter == null) return true; @@ -39,7 +39,7 @@ static bool ThingFilter_SetAllow(ThingCategoryDef categoryDef, bool 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; @@ -47,7 +47,7 @@ static bool ThingFilter_SetAllowAll() return false; } - [MpPrefix(typeof(ThingFilter), "SetDisallowAll")] + [MpPrefix(typeof(ThingFilter), nameof(ThingFilter.SetDisallowAll))] static bool ThingFilter_SetDisallowAll() { if (!Multiplayer.ShouldSync || ThingFilterMarkers.DrawnThingFilter == null) return true; diff --git a/Source/Client/Syncing/Game/ThingFilterMarkers.cs b/Source/Client/Syncing/Game/ThingFilterMarkers.cs index f775de22..98651e06 100644 --- a/Source/Client/Syncing/Game/ThingFilterMarkers.cs +++ b/Source/Client/Syncing/Game/ThingFilterMarkers.cs @@ -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; @@ -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))] diff --git a/Source/Client/Util/MpUI.cs b/Source/Client/Util/MpUI.cs index 23244053..4444a9bb 100644 --- a/Source/Client/Util/MpUI.cs +++ b/Source/Client/Util/MpUI.cs @@ -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()); } } From 5e20f7fd2d630306de8a9ac2f7ab343f887da339 Mon Sep 17 00:00:00 2001 From: SokyranTheDragon Date: Tue, 27 Aug 2024 00:04:39 +0200 Subject: [PATCH 2/2] Revert change for `m_RevealCursor`, as it seems to sometimes cause errors Not sure why (I've tried to clear my publicizer cache, etc.). It seems that, for me, Rider is refusing to acknowledge that this field is public. It still compiles fine and without any errors, however this causes annoying errors in code. So for the sake of safety (in case anyone else is going to encounter this issue) - I've decided to just revert this specific change. On top of that, one thing I did not consider at the time - rather than using reflection, we could have just replaced this with `te.m_RevealCursor = true` (if there was no issue after publicizing). --- Source/Client/Util/MpUI.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Client/Util/MpUI.cs b/Source/Client/Util/MpUI.cs index 4444a9bb..23244053 100644 --- a/Source/Client/Util/MpUI.cs +++ b/Source/Client/Util/MpUI.cs @@ -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), nameof(TextEditor.m_RevealCursor)).SetValue(te, true); + AccessTools.Field(typeof(TextEditor), "m_RevealCursor").SetValue(te, true); te.UpdateScrollOffsetIfNeeded(new Event()); } }