Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync serialization refactor #431

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Source/Client/Debug/DebugActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,18 @@ public static void SaveGame()
public static void DumpSyncTypes()
{
var dict = new Dictionary<string, Type[]>() {
{"ThingComp", RwImplSerialization.thingCompTypes},
{"AbilityComp", RwImplSerialization.abilityCompTypes},
{"Designator", RwImplSerialization.designatorTypes},
{"WorldObjectComp", RwImplSerialization.worldObjectCompTypes},
{"HediffComp", RwImplSerialization.hediffCompTypes},
{"IStoreSettingsParent", RwImplSerialization.storageParents},
{"IPlantToGrowSettable", RwImplSerialization.plantToGrowSettables},

{"GameComponent", RwImplSerialization.gameCompTypes},
{"WorldComponent", RwImplSerialization.worldCompTypes},
{"MapComponent", RwImplSerialization.mapCompTypes},
{"ThingComp", CompSerialization.thingCompTypes},
{"AbilityComp", CompSerialization.abilityCompTypes},
{"WorldObjectComp", CompSerialization.worldObjectCompTypes},
{"HediffComp", CompSerialization.hediffCompTypes},

{"GameComponent", CompSerialization.gameCompTypes},
{"WorldComponent", CompSerialization.worldCompTypes},
{"MapComponent", CompSerialization.mapCompTypes},
};

foreach(var kv in dict) {
Expand Down
21 changes: 18 additions & 3 deletions Source/Client/EarlyInit.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using HarmonyLib;
using Multiplayer.Client.Patches;
Expand Down Expand Up @@ -55,10 +56,12 @@ internal static void EarlyPatches(Harmony harmony)

internal static void InitSync()
{
using (DeepProfilerWrapper.Section("Multiplayer SyncSerialization.Init"))
SyncSerialization.Init();
MpReflection.allAssembliesHook = RwAllAssemblies;

using (DeepProfilerWrapper.Section("Multiplayer SyncGame"))
using (DeepProfilerWrapper.Section("Multiplayer RwSerialization.Init"))
RwSerialization.Init();

using (DeepProfilerWrapper.Section("Multiplayer SyncGame.Init"))
SyncGame.Init();

using (DeepProfilerWrapper.Section("Multiplayer Sync register attributes"))
Expand All @@ -68,6 +71,18 @@ internal static void InitSync()
Sync.ValidateAll();
}

private static IEnumerable<Assembly> RwAllAssemblies()
{
yield return Assembly.GetAssembly(typeof(Game));

foreach (ModContentPack mod in LoadedModManager.RunningMods)
foreach (Assembly assembly in mod.assemblies.loadedAssemblies)
yield return assembly;

if (Assembly.GetEntryAssembly() != null)
yield return Assembly.GetEntryAssembly();
}

internal static void LatePatches()
{
if (MpVersion.IsDebug)
Expand Down
2 changes: 1 addition & 1 deletion Source/Client/Multiplayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>11</LangVersion>
<LangVersion>12</LangVersion>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<OutputPath>bin</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
Expand Down
20 changes: 10 additions & 10 deletions Source/Client/MultiplayerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,20 @@ internal static void CollectDefInfos()

int TypeHash(Type type) => GenText.StableStringHash(type.FullName);

dict["ThingComp"] = GetDefInfo(RwImplSerialization.thingCompTypes, TypeHash);
dict["AbilityComp"] = GetDefInfo(RwImplSerialization.abilityCompTypes, TypeHash);
dict["Designator"] = GetDefInfo(RwImplSerialization.designatorTypes, TypeHash);
dict["WorldObjectComp"] = GetDefInfo(RwImplSerialization.worldObjectCompTypes, TypeHash);
dict["HediffComp"] = GetDefInfo(RwImplSerialization.hediffCompTypes, TypeHash);
dict["ThingComp"] = GetDefInfo(CompSerialization.thingCompTypes, TypeHash);
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["IPlantToGrowSettable"] = GetDefInfo(RwImplSerialization.plantToGrowSettables, TypeHash);
dict["Designator"] = GetDefInfo(RwImplSerialization.designatorTypes, TypeHash);
dict["DefTypes"] = GetDefInfo(DefSerialization.DefTypes, TypeHash);

dict["GameComponent"] = GetDefInfo(RwImplSerialization.gameCompTypes, TypeHash);
dict["WorldComponent"] = GetDefInfo(RwImplSerialization.worldCompTypes, TypeHash);
dict["MapComponent"] = GetDefInfo(RwImplSerialization.mapCompTypes, TypeHash);
dict["ISyncSimple"] = GetDefInfo(ImplSerialization.syncSimples, TypeHash);
dict["ISession"] = GetDefInfo(ImplSerialization.sessions, TypeHash);
dict["GameComponent"] = GetDefInfo(CompSerialization.gameCompTypes, TypeHash);
dict["WorldComponent"] = GetDefInfo(CompSerialization.worldCompTypes, TypeHash);
dict["MapComponent"] = GetDefInfo(CompSerialization.mapCompTypes, TypeHash);
dict["ISyncSimple"] = GetDefInfo(ApiSerialization.syncSimples, TypeHash);
dict["ISession"] = GetDefInfo(ApiSerialization.sessions, TypeHash);

dict["PawnBio"] = GetDefInfo(SolidBioDatabase.allBios, b => b.name.GetHashCode());

Expand Down
4 changes: 2 additions & 2 deletions Source/Client/Persistent/RitualData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public void Sync(SyncWorker sync)
sync.Bind(ref extraInfos);

if (sync is WritingSyncWorker writer1)
DelegateSerialization.WriteDelegate(writer1.writer, action);
DelegateSerialization.WriteDelegate(writer1.Writer, action);
else if (sync is ReadingSyncWorker reader)
action = (ActionCallback)DelegateSerialization.ReadDelegate(reader.reader);
action = (ActionCallback)DelegateSerialization.ReadDelegate(reader.Reader);

sync.Bind(ref ritualLabel);
sync.Bind(ref confirmText);
Expand Down
8 changes: 4 additions & 4 deletions Source/Client/Persistent/SessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void WriteSessionData(ByteWriter data)

foreach (var session in semiPersistentSessions)
{
data.WriteUShort((ushort)ImplSerialization.sessions.FindIndex(session.GetType()));
data.WriteUShort((ushort)ApiSerialization.sessions.FindIndex(session.GetType()));
data.WriteInt32(session.SessionId);

try
Expand All @@ -181,13 +181,13 @@ public void ReadSessionData(ByteReader data)
ushort typeIndex = data.ReadUShort();
int sessionId = data.ReadInt32();

if (typeIndex >= ImplSerialization.sessions.Length)
if (typeIndex >= ApiSerialization.sessions.Length)
{
Log.Error($"Received data for ISession type with index out of range: {typeIndex}, session types count: {ImplSerialization.sessions.Length}");
Log.Error($"Received data for ISession type with index out of range: {typeIndex}, session types count: {ApiSerialization.sessions.Length}");
continue;
}

var objType = ImplSerialization.sessions[typeIndex];
var objType = ApiSerialization.sessions[typeIndex];

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Multiplayer.Client;

public static class ImplSerialization
public static class ApiSerialization
{
public static Type[] syncSimples;
public static Type[] sessions;
Expand Down
31 changes: 31 additions & 0 deletions Source/Client/Syncing/CompSerialization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Multiplayer.Client.Util;
using RimWorld;
using RimWorld.Planet;
using Verse;

namespace Multiplayer.Client;

public static class CompSerialization
{
public static Type[] gameCompTypes;
public static Type[] worldCompTypes;
public static Type[] mapCompTypes;

public static Type[] thingCompTypes;
public static Type[] hediffCompTypes;
public static Type[] abilityCompTypes;
public static Type[] worldObjectCompTypes;

public static void Init()
{
thingCompTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(ThingComp));
hediffCompTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(HediffComp));
abilityCompTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(AbilityComp));
worldObjectCompTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(WorldObjectComp));

gameCompTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(GameComponent));
worldCompTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(WorldComponent));
mapCompTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(MapComponent));
}
}
25 changes: 13 additions & 12 deletions Source/Client/Syncing/Dict/SyncDict.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
namespace Multiplayer.Client
namespace Multiplayer.Client;

public static class SyncDict
{
public static class SyncDict
internal static SyncWorkerDictionaryTree syncWorkers;

public static void Init()
{
internal static SyncWorkerDictionaryTree syncWorkers;
syncWorkers = SyncWorkerDictionaryTree.Merge(
SyncDictMisc.syncWorkers,
SyncDictRimWorld.syncWorkers,
SyncDictDlc.syncWorkers,
SyncDictMultiplayer.syncWorkers
);

public static void Init()
{
syncWorkers = SyncWorkerDictionaryTree.Merge(
SyncDictMisc.syncWorkers,
SyncDictRimWorld.syncWorkers,
SyncDictDlc.syncWorkers,
SyncDictMultiplayer.syncWorkers
);
}
SyncSerialization.syncTree = syncWorkers;
}
}
2 changes: 1 addition & 1 deletion Source/Client/Syncing/Dict/SyncDictDlc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Multiplayer.Client
{
public static class SyncDictDlc
{
internal static SyncWorkerDictionaryTree syncWorkers = new SyncWorkerDictionaryTree()
internal static SyncWorkerDictionaryTree syncWorkers = new()
{
#region Royalty
{
Expand Down
87 changes: 0 additions & 87 deletions Source/Client/Syncing/Dict/SyncDictFast.cs

This file was deleted.

42 changes: 42 additions & 0 deletions Source/Client/Syncing/Dict/SyncDictMisc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,48 @@ public static class SyncDictMisc
}
},
#endregion

#region Structs
{
(ByteWriter data, Rot4 rot) => data.WriteByte(rot.AsByte),
(ByteReader data) => new Rot4(data.ReadByte())
},
{
(ByteWriter data, IntVec3 vec) => {
if (vec.y < 0) {
data.WriteShort(-1);
}
else {
data.WriteShort((short)vec.y);
data.WriteShort((short)vec.x);
data.WriteShort((short)vec.z);
}
},
(ByteReader data) => {
short y = data.ReadShort();
if (y < 0)
return IntVec3.Invalid;

short x = data.ReadShort();
short z = data.ReadShort();

return new IntVec3(x, y, z);
}
},
{
(SyncWorker sync, ref Vector2 vec) => {
sync.Bind(ref vec.x);
sync.Bind(ref vec.y);
}
},
{
(SyncWorker sync, ref Vector3 vec) => {
sync.Bind(ref vec.x);
sync.Bind(ref vec.y);
sync.Bind(ref vec.z);
}
},
#endregion
};
}
}
Loading
Loading