From 269709ea6f1aba464ebee20d7aa8482c1df31960 Mon Sep 17 00:00:00 2001 From: SokyranTheDragon Date: Wed, 3 Jan 2024 18:55:45 +0100 Subject: [PATCH] Changed verb syncing to work closer like IThingHolder One of the changes I've proposed in #411, without any API implementation. List of changes: - `VerbOwnerType` enum was removed and replaced by `supportedVerbOwnerTypes` array - The array includes `typeof(Thing)` in stead of `typeof(Pawn)` for increased compatibility - `IVerbOwner` sync worker entry was added - `Verb` sync worker entry was modified to sync the owner as `IVerbOwner` Those changes should result in greater compatibility, as new supported `IVerbOwner` types can now be added to the array and synced using their own sync workers. This should also end up simplifying `Verb` sync worker going forward, as we won't have to expand it anymore in the future - only the array of supported types. Things that I did not include, but we may want to potentially consider: - Add more vanilla types to the list of supported verb owners, which could include: - `HediffComp` (specifically for `HediffComp_VerbGiver`) - however, in vanilla RW they don't have gizmos, but a mod could add a comp that adds one - `Pawn_MeleeVerbs_TerrainSource` - likely will never gizmos, probably will be completely pointless to include - `Pawn_NativeVerbs` - same as above - Automatically including all subtypes of `IVerbOwner` which have an explicit sync worker - Would simplify mod compat, as mods would (likely) never need to modify the list of supported verb owners - Could have potentially unintended consequences? --- .../Client/Syncing/Dict/SyncDictRimWorld.cs | 49 +++++-------------- .../Syncing/Game/RwImplSerialization.cs | 12 +++-- 2 files changed, 20 insertions(+), 41 deletions(-) diff --git a/Source/Client/Syncing/Dict/SyncDictRimWorld.cs b/Source/Client/Syncing/Dict/SyncDictRimWorld.cs index 75c234b7..522b14b8 100644 --- a/Source/Client/Syncing/Dict/SyncDictRimWorld.cs +++ b/Source/Client/Syncing/Dict/SyncDictRimWorld.cs @@ -295,46 +295,15 @@ public static class SyncDictRimWorld (SyncWorker sync, ref Verb verb) => { if (sync.isWriting) { - if (verb.DirectOwner is Pawn pawn) { - sync.Write(VerbOwnerType.Pawn); - sync.Write(pawn); - } - else if (verb.DirectOwner is Ability ability) { - sync.Write(VerbOwnerType.Ability); - sync.Write(ability); - } - else if (verb.DirectOwner is ThingComp thingComp) { - sync.Write(VerbOwnerType.ThingComp); - sync.Write(thingComp); - } - else { - Log.Error($"Multiplayer :: SyncDictionary.Verb: Unknown DirectOwner {verb.loadID} {verb.DirectOwner}"); - sync.Write(VerbOwnerType.None); - return; - } - - sync.Write(verb.loadID); + sync.Write(verb.DirectOwner); + // No reason to sync loadID if the owner is null + if (verb.DirectOwner != null) + sync.Write(verb.loadID); } else { - var ownerType = sync.Read(); - if (ownerType == VerbOwnerType.None) { - return; - } - - IVerbOwner verbOwner = null; - if (ownerType == VerbOwnerType.Pawn) { - verbOwner = sync.Read(); - } - else if (ownerType == VerbOwnerType.Ability) { - verbOwner = sync.Read(); - } - else if (ownerType == VerbOwnerType.ThingComp) { - verbOwner = sync.Read() as IVerbOwner; - } - + var verbOwner = sync.Read(); if (verbOwner == null) { - Log.Error($"Multiplayer :: SyncDictionary.Verb: Unknown VerbOwnerType {ownerType}"); return; } @@ -348,6 +317,14 @@ public static class SyncDictRimWorld } }, true // implicit }, + { + (ByteWriter data, IVerbOwner obj) => { + WriteWithImpl(data, obj, supportedVerbOwnerTypes); + }, + (ByteReader data) => { + return ReadWithImpl(data, supportedVerbOwnerTypes); + }, true // Implicit + }, #endregion #region AI diff --git a/Source/Client/Syncing/Game/RwImplSerialization.cs b/Source/Client/Syncing/Game/RwImplSerialization.cs index 7016ab11..43911054 100644 --- a/Source/Client/Syncing/Game/RwImplSerialization.cs +++ b/Source/Client/Syncing/Game/RwImplSerialization.cs @@ -32,17 +32,19 @@ public static class RwImplSerialization typeof(WorldObjectComp) }; + internal static Type[] supportedVerbOwnerTypes = + { + typeof(Thing), + typeof(Ability), + typeof(ThingComp), + }; + // ReSharper disable once InconsistentNaming internal enum ISelectableImpl : byte { None, Thing, Zone, WorldObject } - internal enum VerbOwnerType : byte - { - None, Pawn, Ability, ThingComp - } - public static void Init() { storageParents = TypeUtil.AllImplementationsOrdered(typeof(IStoreSettingsParent));