Skip to content

Commit

Permalink
#207 Fix Room is not serializable. (#214)
Browse files Browse the repository at this point in the history
Also added validation for all other arguments for their serialization.
  • Loading branch information
zuev93 authored Aug 11, 2023
1 parent 502b1c9 commit 646a755
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
63 changes: 60 additions & 3 deletions MultiplayerMod/Core/Patch/PatchTargetResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using MultiplayerMod.Core.Logging;
using MultiplayerMod.Platform.Steam.Network.Messaging.Surrogates;
using UnityEngine;

namespace MultiplayerMod.Core.Patch;

Expand All @@ -13,10 +16,16 @@ public class PatchTargetResolver {
private readonly Dictionary<Type, List<string>> targets;
private readonly IEnumerable<Type> baseTypes;
private readonly Assembly assembly = Assembly.GetAssembly(typeof(global::Game));
private bool checkArgumentsSerializable;

public PatchTargetResolver(Dictionary<Type, List<string>> targets, IEnumerable<Type> baseTypes) {
private PatchTargetResolver(
Dictionary<Type, List<string>> targets,
IEnumerable<Type> baseTypes,
bool checkArgumentsSerializable
) {
this.targets = targets;
this.baseTypes = baseTypes;
this.checkArgumentsSerializable = checkArgumentsSerializable;
}

public IEnumerable<MethodBase> Resolve() {
Expand Down Expand Up @@ -58,8 +67,11 @@ public IEnumerable<MethodBase> Resolve() {

private MethodBase GetMethodOrSetter(Type type, string methodName, Type? interfaceType) {
var methodInfo = GetMethod(type, methodName, interfaceType);
if (methodInfo != null)
if (methodInfo != null) {
if (checkArgumentsSerializable)
ValidateArguments(methodInfo);
return methodInfo;
}

var property = GetSetter(type, methodName, interfaceType);
if (property != null)
Expand Down Expand Up @@ -112,10 +124,50 @@ private List<Type> GetImplementedInterfaces(IEnumerable<Type> interfaceTypes, Ty
.Where(interfaceType => interfaceType.IsAssignableFrom(type))
.ToList();

private void ValidateArguments(MethodBase? methodBase) {
if (methodBase == null) return;

var parameters = methodBase.GetParameters();
foreach (var parameterInfo in parameters) {
var paramType = parameterInfo.ParameterType;
ValidateTypeIsSerializable(methodBase, paramType);
}
}

private void ValidateTypeIsSerializable(MethodBase methodBase, Type checkType) {
if (checkType.IsInterface) {
var implementations = assembly.GetTypes()
.Where(
type => type.IsClass && checkType.IsAssignableFrom(type)
).ToList();
foreach (var implementation in implementations) {
ValidateTypeIsSerializable(methodBase, implementation);
}
return;
}
if (checkType.IsEnum) {
return;
}
var isTypeSerializable = checkType.IsDefined(typeof(SerializableAttribute), false);
var isSurrogateExists = SerializationSurrogates.Selector.GetSurrogate(
checkType,
new StreamingContext(StreamingContextStates.All),
out ISurrogateSelector _
) != null;
var gameObjectOrKMono =
checkType.IsSubclassOf(typeof(GameObject)) || checkType.IsSubclassOf(typeof(KMonoBehaviour));
if (isTypeSerializable || isSurrogateExists || gameObjectOrKMono) return;

var message = $"{checkType} is not serializable (method {methodBase}.";
log.Error(message);
throw new Exception(message);
}

public class Builder {

private readonly Dictionary<Type, List<string>> targets = new();
private readonly List<Type> baseTypes = new();
private bool checkArgumentsSerializable;

private List<string> GetTargets(Type type) {
if (targets.TryGetValue(type, out var methods))
Expand All @@ -136,7 +188,12 @@ public Builder AddBaseType(Type type) {
return this;
}

public PatchTargetResolver Build() => new(targets, baseTypes);
public Builder CheckArgumentsSerializable(bool check) {
checkArgumentsSerializable = check;
return this;
}

public PatchTargetResolver Build() => new(targets, baseTypes, checkArgumentsSerializable);

}

Expand Down
4 changes: 3 additions & 1 deletion MultiplayerMod/Game/Mechanics/Objects/ObjectEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public static class ObjectEvents {
)
.AddMethods(typeof(ISidescreenButtonControl), nameof(ISidescreenButtonControl.OnSidescreenButtonPressed))
.AddMethods(typeof(IUserControlledCapacity), nameof(IUserControlledCapacity.UserMaxCapacity))
.AddMethods(typeof(Assignable), nameof(Assignable.Assign), nameof(Assignable.Unassign)).AddMethods(
.AddMethods(typeof(Assignable), nameof(Assignable.Assign), nameof(Assignable.Unassign))
.AddMethods(
typeof(AccessControl),
nameof(AccessControl.SetPermission),
nameof(AccessControl.ClearPermission),
Expand Down Expand Up @@ -116,6 +117,7 @@ public static class ObjectEvents {
// )
.AddBaseType(typeof(KMonoBehaviour))
.AddBaseType(typeof(StateMachine.Instance))
.CheckArgumentsSerializable(true)
.Build();

// ReSharper disable once UnusedMember.Local
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

namespace MultiplayerMod.Platform.Steam.Network.Messaging.Surrogates;

public class RoomSurrogate : ISerializationSurrogate, ISurrogateType {

public Type Type => typeof(Room);

public void GetObjectData(object obj, SerializationInfo info, StreamingContext context) {
var room = obj as Room;
var allIds = new List<KPrefabID>(room!.primary_buildings);
allIds.AddRange(room.buildings);
allIds.AddRange(room.plants);
var firstGo = allIds.First().gameObject!;
var cell = Grid.PosToCell(firstGo);
info.AddValue("cell", cell);
}

public object? SetObjectData(
object obj,
SerializationInfo info,
StreamingContext context,
ISurrogateSelector selector
) {
var cell = info.GetInt32("cell");
return global::Game.Instance.roomProber.GetCavityForCell(cell)?.room;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static SerializationSurrogates() {
Selector.Add(new CarePackageInstanceDataSurrogate());
Selector.Add(new ComplexRecipeSurrogate());
Selector.Add(new MinionStartingStatsSurrogate());
Selector.Add(new RoomSurrogate());
Selector.Add(new SpaceDestinationSurrogate());
Selector.Add(new SpiceGrinderSurrogate());
}
Expand Down

0 comments on commit 646a755

Please sign in to comment.