Skip to content

Commit

Permalink
Revert "IVerbOwner, ISelectable - support all implementations by defa…
Browse files Browse the repository at this point in the history
…ult"

This reverts commit 6a96199.
  • Loading branch information
SokyranTheDragon committed Jan 7, 2024
1 parent 6a96199 commit f1e4095
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 59 deletions.
2 changes: 0 additions & 2 deletions Source/Client/MultiplayerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ internal static void CollectDefInfos()
dict["HediffComp"] = GetDefInfo(RwImplSerialization.hediffCompTypes, TypeHash);
dict["IStoreSettingsParent"] = GetDefInfo(RwImplSerialization.storageParents, TypeHash);
dict["IPlantToGrowSettable"] = GetDefInfo(RwImplSerialization.plantToGrowSettables, TypeHash);
dict["IVerbOwner"] = GetDefInfo(RwImplSerialization.verbOwners, TypeHash);
dict["ISelectable"] = GetDefInfo(RwImplSerialization.selectables, TypeHash);
dict["DefTypes"] = GetDefInfo(DefSerialization.DefTypes, TypeHash);

dict["GameComponent"] = GetDefInfo(RwImplSerialization.gameCompTypes, TypeHash);
Expand Down
138 changes: 85 additions & 53 deletions Source/Client/Syncing/Dict/SyncDictRimWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,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 Expand Up @@ -1013,61 +1021,45 @@ public static class SyncDictRimWorld
},
#endregion

#region Storage
{
(ByteWriter data, StorageSettings storage) => {
WriteSync(data, storage.owner);
},
(ByteReader data) => {
IStoreSettingsParent parent = ReadSync<IStoreSettingsParent>(data);
return parent?.GetStoreSettings();
}
},
{
(ByteWriter data, StorageGroup group) => WriteSync(data, group.members.First()),
(ByteReader data) => ReadSync<IStorageGroupMember>(data).Group
},
#endregion

#region Letters
{
(ByteWriter data, Letter letter) => {
WriteSync(data, letter.ID);
},
(ByteReader data) =>
{
var id = data.ReadInt32();
return (Letter)Find.Archive.ArchivablesListForReading.Find(a => a is Letter l && l.ID == id);
}, true
},
#endregion

#region PassingShip
{
(ByteWriter data, PassingShip ship) =>
{
WriteSync(data, ship.Map);
if (ship.Map != null) data.WriteInt32(ship.loadID);
},
(ByteReader data) =>
{
var map = ReadSync<Map>(data);
if (map == null) return null;

var id = data.ReadInt32();
return map.passingShipManager.passingShips.FirstOrDefault(s => s.loadID == id);
}, true // Implicit
},
#endregion

#region Interfaces
{
(ByteWriter data, ISelectable obj) => {
WriteWithImpl<ISelectable>(data, obj, selectables);
if (obj == null)
{
WriteSync(data, ISelectableImpl.None);
}
else if (obj is Thing thing)
{
WriteSync(data, ISelectableImpl.Thing);
WriteSync(data, thing);
}
else if (obj is Zone zone)
{
WriteSync(data, ISelectableImpl.Zone);
WriteSync(data, zone);
}
else if (obj is WorldObject worldObj)
{
WriteSync(data, ISelectableImpl.WorldObject);
WriteSync(data, worldObj);
}
else
{
throw new SerializationException($"Unknown ISelectable type: {obj.GetType()}");
}
},
(ByteReader data) => {
return ReadWithImpl<ISelectable>(data, selectables);
}, true // Implicit
ISelectableImpl impl = ReadSync<ISelectableImpl>(data);

return impl switch
{
ISelectableImpl.None => null,
ISelectableImpl.Thing => ReadSync<Thing>(data),
ISelectableImpl.Zone => ReadSync<Zone>(data),
ISelectableImpl.WorldObject => ReadSync<WorldObject>(data),
_ => throw new Exception($"Unknown ISelectable {impl}")
};
}, true
},
{
(ByteWriter data, IStoreSettingsParent obj) => {
Expand Down Expand Up @@ -1103,12 +1095,52 @@ public static class SyncDictRimWorld
},
(ByteReader data) => (IStorageGroupMember)ReadSync<Thing>(data)
},

#endregion

#region Storage
{
(ByteWriter data, IVerbOwner obj) => {
WriteWithImpl<IVerbOwner>(data, obj, verbOwners);
(ByteWriter data, StorageSettings storage) => {
WriteSync(data, storage.owner);
},
(ByteReader data) => {
return ReadWithImpl<IVerbOwner>(data, verbOwners);
IStoreSettingsParent parent = ReadSync<IStoreSettingsParent>(data);
return parent?.GetStoreSettings();
}
},
{
(ByteWriter data, StorageGroup group) => WriteSync(data, group.members.First()),
(ByteReader data) => ReadSync<IStorageGroupMember>(data).Group
},
#endregion

#region Letters
{
(ByteWriter data, Letter letter) => {
WriteSync(data, letter.ID);
},
(ByteReader data) =>
{
var id = data.ReadInt32();
return (Letter)Find.Archive.ArchivablesListForReading.Find(a => a is Letter l && l.ID == id);
}, true
},
#endregion

#region PassingShip
{
(ByteWriter data, PassingShip ship) =>
{
WriteSync(data, ship.Map);
if (ship.Map != null) data.WriteInt32(ship.loadID);
},
(ByteReader data) =>
{
var map = ReadSync<Map>(data);
if (map == null) return null;

var id = data.ReadInt32();
return map.passingShipManager.passingShips.FirstOrDefault(s => s.loadID == id);
}, true // Implicit
},
#endregion
Expand Down
17 changes: 13 additions & 4 deletions Source/Client/Syncing/Game/RwImplSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public static class RwImplSerialization
{
public static Type[] storageParents;
public static Type[] plantToGrowSettables;
public static Type[] verbOwners;
public static Type[] selectables;

public static Type[] thingCompTypes;
public static Type[] hediffCompTypes;
Expand All @@ -34,12 +32,23 @@ 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
}

public static void Init()
{
storageParents = TypeUtil.AllImplementationsOrdered(typeof(IStoreSettingsParent));
plantToGrowSettables = TypeUtil.AllImplementationsOrdered(typeof(IPlantToGrowSettable));
verbOwners = TypeUtil.AllImplementationsOrdered(typeof(IVerbOwner));
selectables = TypeUtil.AllImplementationsOrdered(typeof(ISelectable));

thingCompTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(ThingComp));
hediffCompTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(HediffComp));
Expand Down

0 comments on commit f1e4095

Please sign in to comment.