Skip to content

Commit

Permalink
Generalize WithImpl serialization
Browse files Browse the repository at this point in the history
Make SyncSerialization into a non-static class for easier testing
Move Def serialization to RW sync tree
Remove serialization type changers
Fix parent-child relations in SyncWorker trees
  • Loading branch information
Zetrith committed Apr 10, 2024
1 parent 8fcf76f commit b5a01c3
Show file tree
Hide file tree
Showing 28 changed files with 535 additions and 601 deletions.
9 changes: 3 additions & 6 deletions Source/Client/Debug/DebugActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,6 @@ public static void SaveGame()
public static void DumpSyncTypes()
{
var dict = new Dictionary<string, Type[]>() {
{"Designator", RwImplSerialization.designatorTypes},
{"IStoreSettingsParent", RwImplSerialization.storageSettingsParent},
{"IPlantToGrowSettable", RwImplSerialization.plantToGrowSettables},
{"ISlotGroup", RwImplSerialization.slotGroupTypes},
{"ISlotGroupParent", RwImplSerialization.slotGroupParents},

{"ThingComp", CompSerialization.thingCompTypes},
{"AbilityComp", CompSerialization.abilityCompTypes},
{"WorldObjectComp", CompSerialization.worldObjectCompTypes},
Expand All @@ -151,6 +145,9 @@ public static void DumpSyncTypes()
{"MapComponent", CompSerialization.mapCompTypes},
};

foreach (var explicitImplType in Multiplayer.serialization.explicitImplTypes)
dict[explicitImplType.Name] = Multiplayer.serialization.TypeHelper!.GetImplementations(explicitImplType).ToArray();

foreach(var kv in dict) {
Log.Warning($"== {kv.Key} ==");
Log.Message(
Expand Down
16 changes: 2 additions & 14 deletions Source/Client/EarlyInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ internal static void EarlyPatches(Harmony harmony)

internal static void InitSync()
{
MpReflection.allAssembliesHook = RwAllAssemblies;

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

SyncDict.Init();

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

Expand All @@ -71,18 +71,6 @@ 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
19 changes: 16 additions & 3 deletions Source/Client/Multiplayer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
Expand All @@ -25,6 +26,8 @@ public static class Multiplayer
public static MultiplayerGame game;
public static MultiplayerSession session;

public static Common.SyncSerialization serialization;

public static MultiplayerServer LocalServer { get; set; }
public static Thread localServerThread;

Expand Down Expand Up @@ -112,10 +115,8 @@ public static void InitMultiplayer()
settings = MultiplayerLoader.Multiplayer.instance!.GetSettings<MpSettings>();

EarlyInit.ProcessEnvironment();

SyncDict.Init();

EarlyInit.EarlyPatches(harmony);
MpReflection.allAssembliesHook = RwAllAssemblies;
EarlyInit.InitSync();
CheckInterfaceVersions();

Expand Down Expand Up @@ -219,5 +220,17 @@ public static void StopMultiplayer()
Application.Quit();
}
}

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();
}
}
}
12 changes: 5 additions & 7 deletions Source/Client/MultiplayerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,14 @@ 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.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);

dict["GameComponent"] = GetDefInfo(CompSerialization.gameCompTypes, TypeHash);
dict["WorldComponent"] = GetDefInfo(CompSerialization.worldCompTypes, TypeHash);
dict["MapComponent"] = GetDefInfo(CompSerialization.mapCompTypes, TypeHash);

foreach (var explicitImplType in Multiplayer.serialization.explicitImplTypes)
dict[explicitImplType.Name] =
GetDefInfo(Multiplayer.serialization.TypeHelper.GetImplementations(explicitImplType), TypeHash);

dict["ISyncSimple"] = GetDefInfo(ApiSerialization.syncSimples, TypeHash);
dict["ISession"] = GetDefInfo(ApiSerialization.sessions, TypeHash);

Expand Down
2 changes: 1 addition & 1 deletion Source/Client/Patches/Designators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static Exception DesignateFinalizer(Exception __exception)
private static void WriteData(ByteWriter data, DesignatorMode mode, Designator designator)
{
SyncSerialization.WriteSync(data, mode);
SyncSerialization.WriteSyncObject(data, designator, designator.GetType());
SyncSerialization.WriteSync(data, designator);

// Read at MapAsyncTimeComp.SetDesignatorState
// The reading side affects global state so these can't be SyncWorkers
Expand Down
4 changes: 2 additions & 2 deletions Source/Client/Persistent/SessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void WriteSessionData(ByteWriter data)

try
{
session.Sync(new WritingSyncWorker(data));
session.Sync(new WritingSyncWorker(data, Multiplayer.serialization));
}
catch (Exception e)
{
Expand Down Expand Up @@ -194,7 +194,7 @@ public void ReadSessionData(ByteReader data)
if (Activator.CreateInstance(objType, Map) is SemiPersistentSession session)
{
session.SessionId = sessionId;
session.Sync(new ReadingSyncWorker(data));
session.Sync(new ReadingSyncWorker(data, Multiplayer.serialization));
semiPersistentSessions.Add(session);
allSessions.Add(session);
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Client/Syncing/ApiSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class ApiSerialization

public static void Init()
{
syncSimples = TypeUtil.AllImplementationsOrdered(typeof(ISyncSimple));
sessions = TypeUtil.AllImplementationsOrdered(typeof(Session));
syncSimples = TypeCache.AllImplementationsOrdered(typeof(ISyncSimple));
sessions = TypeCache.AllImplementationsOrdered(typeof(Session));
}
}
14 changes: 7 additions & 7 deletions Source/Client/Syncing/CompSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public static class CompSerialization

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

gameCompTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(GameComponent));
worldCompTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(WorldComponent));
mapCompTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(MapComponent));
gameCompTypes = TypeCache.AllSubclassesNonAbstractOrdered(typeof(GameComponent));
worldCompTypes = TypeCache.AllSubclassesNonAbstractOrdered(typeof(WorldComponent));
mapCompTypes = TypeCache.AllSubclassesNonAbstractOrdered(typeof(MapComponent));
}
}
2 changes: 1 addition & 1 deletion Source/Client/Syncing/DefSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class DefSerialization

public static void Init()
{
DefTypes = TypeUtil.AllSubclassesNonAbstractOrdered(typeof(Def));
DefTypes = TypeCache.AllSubclassesNonAbstractOrdered(typeof(Def));

foreach (var defType in DefTypes)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Client/Syncing/Dict/SyncDict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public static void Init()
SyncDictMultiplayer.syncWorkers
);

SyncSerialization.syncTree = syncWorkers;
Multiplayer.serialization.syncTree = syncWorkers;
}
}
Loading

0 comments on commit b5a01c3

Please sign in to comment.