Fix RegisterSyncField(Type, string) to preserve caller targetType (#880)#881
Open
Fix RegisterSyncField(Type, string) to preserve caller targetType (#880)#881
Conversation
AccessTools.Field walks the inheritance chain, so the returned FieldInfo.ReflectedType collapses to the base type that declared the field. Routing the (Type, string) overload through RegisterSyncField( FieldInfo) silently discarded the caller's concrete Type, causing SyncField to store the base targetType and breaking SyncWorker lookups registered on the concrete subclass (rwmt#880). Keep the caller's Type for the instance path; use the FieldInfo only for existence validation and the static branch. Collapse the RegisterSyncField(FieldInfo) overload into a one-line delegate through (field.ReflectedType, field.Name), preserving its prior behavior.
notfood
approved these changes
Apr 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #880.
MP.RegisterSyncField(Type, string)currently routes throughRegisterSyncField(FieldInfo), which keys everything offFieldInfo.ReflectedType. BecauseAccessTools.Fieldwalks the inheritance chain viaType.GetField, the returnedFieldInfofor a private field declared on a base class comes back withReflectedTypecollapsed to the base type — silently discarding the concreteTypethe caller passed in.Downstream,
SyncFieldstores that basetargetType(SyncField.csL24-30), andDoSyncserializestargetas the base type (L47-52). AnySyncWorkerregistered against the concrete subclass then fails to match, because the wire format now references the base.Concrete case that surfaced this:
Gizmo_Slider.targetValuePctis declared on the baseGizmo_Slider. A compat patch callingMP.RegisterSyncField(concreteSubclass, \"targetValuePct\")ended up registered againstGizmo_Sliderinstead, breaking the subclassSyncWorkerlookup. The Vanilla Gravship Expanded compat patch currently works around this with a reflectionSetValueon the privatetargetTypefield (rwmt/Multiplayer-Compatibility#577); that workaround can be removed once this ships.Fix
RegisterSyncField(Type, string)now looks up theFieldInfoonly to validate existence and branch on static, but passes the caller'stargetTypestraight through toSync.Fieldand thememberPathkey. The internalSync.Field(Type, string, string)path already accepted an arbitraryType— it just wasn't reachable from the public compat API.RegisterSyncField(FieldInfo)is collapsed into a one-line delegate to(field.ReflectedType, field.Name), preserving its prior behavior exactly and eliminating the duplicated switch.FieldInfodeclaration,?? throw new Exception(...)as inSync.Method, K&R braces.No behavior change for callers whose
targetTypeequals the declaring type (the common case), or for any caller of theFieldInfooverload.Test plan
dotnet build Source/Client/Multiplayer.csproj -c Release— 0 errorsSetValueworkaroundRegisterSyncField(Type, string)callers in Multiplayer-Compatibility (Pharmacist, AnimalTab, SRTS Expanded, VEF autocast) — wheretargetTypealready equals the declaring type, behavior is unchangedRegisterSyncField(FieldInfo)callers (CommonSense, VanillaHairExpanded) — routes through the same path as before viaReflectedType