Skip to content

Commit

Permalink
Changed verb syncing to work closer like IThingHolder
Browse files Browse the repository at this point in the history
One of the changes I've proposed in rwmt#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?
  • Loading branch information
SokyranTheDragon committed Jan 3, 2024
1 parent 7429f3a commit 269709e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 41 deletions.
49 changes: 13 additions & 36 deletions Source/Client/Syncing/Dict/SyncDictRimWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<VerbOwnerType>();
if (ownerType == VerbOwnerType.None) {
return;
}
IVerbOwner verbOwner = null;
if (ownerType == VerbOwnerType.Pawn) {
verbOwner = sync.Read<Pawn>();
}
else if (ownerType == VerbOwnerType.Ability) {
verbOwner = sync.Read<Ability>();
}
else if (ownerType == VerbOwnerType.ThingComp) {
verbOwner = sync.Read<ThingComp>() as IVerbOwner;
}
var verbOwner = sync.Read<IVerbOwner>();
if (verbOwner == null) {
Log.Error($"Multiplayer :: SyncDictionary.Verb: Unknown VerbOwnerType {ownerType}");
return;
}
Expand All @@ -348,6 +317,14 @@ public static class SyncDictRimWorld
}
}, true // implicit
},
{
(ByteWriter data, IVerbOwner obj) => {
WriteWithImpl<IVerbOwner>(data, obj, supportedVerbOwnerTypes);
},
(ByteReader data) => {
return ReadWithImpl<IVerbOwner>(data, supportedVerbOwnerTypes);
}, true // Implicit
},
#endregion

#region AI
Expand Down
12 changes: 7 additions & 5 deletions Source/Client/Syncing/Game/RwImplSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 269709e

Please sign in to comment.