From e51220f076d060c1a57b5c2535a1fffa5c4e606d Mon Sep 17 00:00:00 2001 From: Zetrith Date: Fri, 5 Apr 2024 12:44:36 +0200 Subject: [PATCH] Sync slot groups and storage groups --- Source/Client/Debug/DebugActions.cs | 4 +- Source/Client/MultiplayerData.cs | 4 +- .../Client/Syncing/Dict/SyncDictRimWorld.cs | 77 ++++++++++++++----- Source/Client/Syncing/RwImplSerialization.cs | 14 ++-- Source/Client/Syncing/RwTypeHelper.cs | 4 +- 5 files changed, 77 insertions(+), 26 deletions(-) diff --git a/Source/Client/Debug/DebugActions.cs b/Source/Client/Debug/DebugActions.cs index e4be8ae0..4da0937e 100644 --- a/Source/Client/Debug/DebugActions.cs +++ b/Source/Client/Debug/DebugActions.cs @@ -136,8 +136,10 @@ public static void DumpSyncTypes() { var dict = new Dictionary() { {"Designator", RwImplSerialization.designatorTypes}, - {"IStoreSettingsParent", RwImplSerialization.storageParents}, + {"IStoreSettingsParent", RwImplSerialization.storageSettingsParent}, {"IPlantToGrowSettable", RwImplSerialization.plantToGrowSettables}, + {"ISlotGroup", RwImplSerialization.slotGroupTypes}, + {"ISlotGroupParent", RwImplSerialization.slotGroupParents}, {"ThingComp", CompSerialization.thingCompTypes}, {"AbilityComp", CompSerialization.abilityCompTypes}, diff --git a/Source/Client/MultiplayerData.cs b/Source/Client/MultiplayerData.cs index 6c9add4a..7233fd50 100644 --- a/Source/Client/MultiplayerData.cs +++ b/Source/Client/MultiplayerData.cs @@ -124,8 +124,10 @@ internal static void CollectDefInfos() dict["AbilityComp"] = GetDefInfo(CompSerialization.abilityCompTypes, TypeHash); dict["WorldObjectComp"] = GetDefInfo(CompSerialization.worldObjectCompTypes, TypeHash); dict["HediffComp"] = GetDefInfo(CompSerialization.hediffCompTypes, TypeHash); - dict["IStoreSettingsParent"] = GetDefInfo(RwImplSerialization.storageParents, TypeHash); + dict["IStoreSettingsParent"] = GetDefInfo(RwImplSerialization.storageSettingsParent, TypeHash); dict["IPlantToGrowSettable"] = GetDefInfo(RwImplSerialization.plantToGrowSettables, TypeHash); + dict["ISlotGroup"] = GetDefInfo(RwImplSerialization.slotGroupTypes, TypeHash); + dict["ISlotGroupParent"] = GetDefInfo(RwImplSerialization.slotGroupParents, TypeHash); dict["Designator"] = GetDefInfo(RwImplSerialization.designatorTypes, TypeHash); dict["DefTypes"] = GetDefInfo(DefSerialization.DefTypes, TypeHash); diff --git a/Source/Client/Syncing/Dict/SyncDictRimWorld.cs b/Source/Client/Syncing/Dict/SyncDictRimWorld.cs index ba0f1e91..00f8701e 100644 --- a/Source/Client/Syncing/Dict/SyncDictRimWorld.cs +++ b/Source/Client/Syncing/Dict/SyncDictRimWorld.cs @@ -1047,6 +1047,65 @@ public static class SyncDictRimWorld }, #endregion + #region Storage + { + (ByteWriter data, IStoreSettingsParent obj) => { + WriteWithImpl(data, obj, storageSettingsParent); + }, + (ByteReader data) => { + return ReadWithImpl(data, storageSettingsParent); + } + }, + { + (ByteWriter data, SlotGroup obj) => { + WriteSync(data, obj.parent); + }, + (ByteReader data) => + { + var parent = ReadSync(data); + return parent.GetSlotGroup(); + } + }, + { + (ByteWriter data, StorageGroup obj) => + { + data.MpContext().map = obj.Map; + WriteSync(data, obj.loadID); + }, + (ByteReader data) => + { + var loadId = data.ReadInt32(); + return data.MpContext().map.storageGroups.groups.Find(g => g.loadID == loadId); + } + }, + { + (ByteWriter data, ISlotGroup obj) => { + WriteWithImpl(data, obj, slotGroupTypes); + }, + (ByteReader data) => { + return ReadWithImpl(data, slotGroupTypes); + } + }, + { + (ByteWriter data, ISlotGroupParent obj) => { + WriteWithImpl(data, obj, slotGroupParents); + }, + (ByteReader data) => { + return ReadWithImpl(data, slotGroupParents); + } + }, + { + (ByteWriter data, IStorageGroupMember obj) => + { + if (obj is Thing thing) + WriteSync(data, thing); + else + throw new SerializationException($"Unknown IStorageGroupMember type: {obj.GetType()}"); + }, + (ByteReader data) => (IStorageGroupMember)ReadSync(data) + }, + #endregion + #region Interfaces { (ByteWriter data, ISelectable obj) => { @@ -1087,14 +1146,6 @@ public static class SyncDictRimWorld }; }, true }, - { - (ByteWriter data, IStoreSettingsParent obj) => { - WriteWithImpl(data, obj, storageParents); - }, - (ByteReader data) => { - return ReadWithImpl(data, storageParents); - } - }, { (ByteWriter data, IPlantToGrowSettable obj) => { WriteWithImpl(data, obj, plantToGrowSettables); @@ -1111,16 +1162,6 @@ public static class SyncDictRimWorld return ReadWithImpl(data, supportedThingHolders); } }, - { - (ByteWriter data, IStorageGroupMember obj) => - { - if (obj is Thing thing) - WriteSync(data, thing); - else - throw new SerializationException($"Unknown IStorageGroupMember type: {obj.GetType()}"); - }, - (ByteReader data) => (IStorageGroupMember)ReadSync(data) - }, #endregion #region Storage diff --git a/Source/Client/Syncing/RwImplSerialization.cs b/Source/Client/Syncing/RwImplSerialization.cs index 64e4e40e..d6b5452e 100644 --- a/Source/Client/Syncing/RwImplSerialization.cs +++ b/Source/Client/Syncing/RwImplSerialization.cs @@ -11,11 +11,13 @@ namespace Multiplayer.Client { public static class RwImplSerialization { - public static Type[] storageParents; - public static Type[] plantToGrowSettables; - public static Type[] designatorTypes; + public static Type[] storageSettingsParent; // IStoreSettingsParent + public static Type[] plantToGrowSettables; // IPlantToGrowSettable + public static Type[] slotGroupTypes; // ISlotGroup + public static Type[] slotGroupParents; // ISlotGroupParent + public static Type[] designatorTypes; // Designator - internal static Type[] supportedThingHolders = + internal static Type[] supportedThingHolders = // IThingHolder { typeof(Map), typeof(Thing), @@ -37,8 +39,10 @@ internal enum VerbOwnerType : byte public static void Init() { - storageParents = TypeUtil.AllImplementationsOrdered(typeof(IStoreSettingsParent)); + storageSettingsParent = TypeUtil.AllImplementationsOrdered(typeof(IStoreSettingsParent)); plantToGrowSettables = TypeUtil.AllImplementationsOrdered(typeof(IPlantToGrowSettable)); + slotGroupTypes = TypeUtil.AllImplementationsOrdered(typeof(ISlotGroup)); + slotGroupParents = TypeUtil.AllImplementationsOrdered(typeof(ISlotGroupParent)); designatorTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(Designator)); } diff --git a/Source/Client/Syncing/RwTypeHelper.cs b/Source/Client/Syncing/RwTypeHelper.cs index 990ebac4..f790cd0d 100644 --- a/Source/Client/Syncing/RwTypeHelper.cs +++ b/Source/Client/Syncing/RwTypeHelper.cs @@ -14,8 +14,10 @@ internal static class RwTypeHelper public static void Init() { - cache[typeof(IStoreSettingsParent)] = RwImplSerialization.storageParents; + cache[typeof(IStoreSettingsParent)] = RwImplSerialization.storageSettingsParent; cache[typeof(IPlantToGrowSettable)] = RwImplSerialization.plantToGrowSettables; + cache[typeof(ISlotGroup)] = RwImplSerialization.slotGroupTypes; + cache[typeof(ISlotGroupParent)] = RwImplSerialization.slotGroupParents; cache[typeof(Designator)] = RwImplSerialization.designatorTypes; cache[typeof(ThingComp)] = CompSerialization.thingCompTypes;