-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Version 0.6.0.4, fix Def serialization and join-point related desyncs
- Loading branch information
Showing
23 changed files
with
299 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using HarmonyLib; | ||
using Multiplayer.Common; | ||
using Verse; | ||
|
||
namespace Multiplayer.Client | ||
{ | ||
public static class DefSerialization | ||
{ | ||
public static Type[] DefTypes { get; private set; } | ||
private static Dictionary<Type, Type> hashableType = new(); | ||
|
||
public static void Init() | ||
{ | ||
DefTypes = ImplSerialization.AllSubclassesNonAbstractOrdered(typeof(Def)); | ||
|
||
foreach (var defType in DefTypes) | ||
{ | ||
var hashable = defType; | ||
for (var t = defType; t != typeof(Def); t = t.BaseType) | ||
if (!t.IsAbstract) | ||
hashable = t; | ||
|
||
hashableType[defType] = hashable; | ||
} | ||
} | ||
|
||
private static Dictionary<Type, MethodInfo> methodCache = new(); | ||
|
||
public static Def GetDef(Type defType, ushort hash) | ||
{ | ||
return (Def)methodCache.AddOrGet( | ||
hashableType[defType], | ||
static t => typeof(DefDatabase<>).MakeGenericType(t).GetMethod("GetByShortHash") | ||
).Invoke(null, new[] { (object)hash }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using HarmonyLib; | ||
using Multiplayer.Common; | ||
using Verse; | ||
|
||
namespace Multiplayer.Client | ||
{ | ||
public static class ExposableSerialization | ||
{ | ||
private static readonly MethodInfo ReadExposableDefinition = | ||
AccessTools.Method(typeof(ScribeUtil), nameof(ScribeUtil.ReadExposable)); | ||
|
||
private static Dictionary<Type, MethodInfo> readExposableCache = new(); | ||
|
||
public static IExposable ReadExposable(Type type, byte[] data) | ||
{ | ||
return (IExposable)readExposableCache.AddOrGet(type, newType => ReadExposableDefinition.MakeGenericMethod(newType)).Invoke(null, new[] { (object)data, null }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Multiplayer.Common; | ||
using RimWorld; | ||
using RimWorld.Planet; | ||
using Verse; | ||
|
||
namespace Multiplayer.Client | ||
{ | ||
public static class ImplSerialization | ||
{ | ||
public static Type[] storageParents; | ||
public static Type[] plantToGrowSettables; | ||
|
||
public static Type[] thingCompTypes; | ||
public static Type[] abilityCompTypes; | ||
public static Type[] designatorTypes; | ||
public static Type[] worldObjectCompTypes; | ||
|
||
public static Type[] gameCompTypes; | ||
public static Type[] worldCompTypes; | ||
public static Type[] mapCompTypes; | ||
|
||
internal static Type[] supportedThingHolders = | ||
{ | ||
typeof(Map), | ||
typeof(Thing), | ||
typeof(ThingComp), | ||
typeof(WorldObject), | ||
typeof(WorldObjectComp) | ||
}; | ||
|
||
internal enum ISelectableImpl : byte | ||
{ | ||
None, Thing, Zone, WorldObject | ||
} | ||
|
||
internal enum VerbOwnerType : byte | ||
{ | ||
None, Pawn, Ability, CompEquippable, CompReloadable | ||
} | ||
|
||
public static void Init() | ||
{ | ||
storageParents = AllImplementationsOrdered(typeof(IStoreSettingsParent)); | ||
plantToGrowSettables = AllImplementationsOrdered(typeof(IPlantToGrowSettable)); | ||
|
||
thingCompTypes = AllSubclassesNonAbstractOrdered(typeof(ThingComp)); | ||
abilityCompTypes = AllSubclassesNonAbstractOrdered(typeof(AbilityComp)); | ||
designatorTypes = AllSubclassesNonAbstractOrdered(typeof(Designator)); | ||
worldObjectCompTypes = AllSubclassesNonAbstractOrdered(typeof(WorldObjectComp)); | ||
|
||
gameCompTypes = AllSubclassesNonAbstractOrdered(typeof(GameComponent)); | ||
worldCompTypes = AllSubclassesNonAbstractOrdered(typeof(WorldComponent)); | ||
mapCompTypes = AllSubclassesNonAbstractOrdered(typeof(MapComponent)); | ||
} | ||
|
||
internal static T ReadWithImpl<T>(ByteReader data, IList<Type> impls) where T : class | ||
{ | ||
ushort impl = data.ReadUShort(); | ||
if (impl == ushort.MaxValue) return null; | ||
return (T)SyncSerialization.ReadSyncObject(data, impls[impl]); | ||
} | ||
|
||
internal static void WriteWithImpl<T>(ByteWriter data, object obj, IList<Type> impls) where T : class | ||
{ | ||
if (obj == null) | ||
{ | ||
data.WriteUShort(ushort.MaxValue); | ||
return; | ||
} | ||
|
||
GetImpl(obj, impls, out Type implType, out int impl); | ||
|
||
if (implType == null) | ||
throw new SerializationException($"Unknown {typeof(T)} implementation type {obj.GetType()}"); | ||
|
||
data.WriteUShort((ushort)impl); | ||
SyncSerialization.WriteSyncObject(data, obj, implType); | ||
} | ||
|
||
internal static void GetImpl(object obj, IList<Type> impls, out Type type, out int index) | ||
{ | ||
type = null; | ||
index = -1; | ||
|
||
if (obj == null) return; | ||
|
||
for (int i = 0; i < impls.Count; i++) | ||
{ | ||
if (impls[i].IsAssignableFrom(obj.GetType())) | ||
{ | ||
type = impls[i]; | ||
index = i; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public static Type[] AllImplementationsOrdered(Type type) | ||
{ | ||
return type.AllImplementing() | ||
.OrderBy(t => t.IsInterface) | ||
.ThenBy(t => t.Name) | ||
.ToArray(); | ||
} | ||
|
||
public static Type[] AllSubclassesNonAbstractOrdered(Type type) { | ||
return type | ||
.AllSubclassesNonAbstract() | ||
.OrderBy(t => t.Name) | ||
.ToArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.