From eb3929a5bbc3584c77ac53a78fe6fd55e45cb55d Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Tue, 18 Jan 2022 10:06:15 -0800 Subject: [PATCH] generic variable syncer --- MirrorWeaver/Weaver/Extensions.cs | 4 ++++ QSB/Utility/VariableSync/BaseVariableSyncer.cs | 17 +++++++++++++++-- QSB/Utility/VariableSync/BoolVariableSyncer.cs | 15 ++------------- QSB/Utility/VariableSync/FloatVariableSyncer.cs | 15 ++------------- .../VariableSync/Vector3VariableSyncer.cs | 14 ++------------ 5 files changed, 25 insertions(+), 40 deletions(-) diff --git a/MirrorWeaver/Weaver/Extensions.cs b/MirrorWeaver/Weaver/Extensions.cs index 5ba4718a..8b424555 100644 --- a/MirrorWeaver/Weaver/Extensions.cs +++ b/MirrorWeaver/Weaver/Extensions.cs @@ -22,7 +22,11 @@ namespace Mirror.Weaver public static bool IsDerivedFrom(this TypeReference tr, Type baseClass) { + if (tr == null) + return false; TypeDefinition td = tr.Resolve(); + if (td == null) + return false; if (!td.IsClass) return false; diff --git a/QSB/Utility/VariableSync/BaseVariableSyncer.cs b/QSB/Utility/VariableSync/BaseVariableSyncer.cs index 7906bb50..140e73df 100644 --- a/QSB/Utility/VariableSync/BaseVariableSyncer.cs +++ b/QSB/Utility/VariableSync/BaseVariableSyncer.cs @@ -1,7 +1,20 @@ -namespace QSB.Utility.VariableSync +using Mirror; +using System; +using System.Collections.Generic; + +namespace QSB.Utility.VariableSync { - public abstract class BaseVariableSyncer : QSBNetworkBehaviour + public abstract class BaseVariableSyncer : QSBNetworkBehaviour { protected override float SendInterval => 0.1f; + + private T _prevValue; + [NonSerialized] + public T Value; + + protected override bool HasChanged() => !EqualityComparer.Default.Equals(_prevValue, Value); + protected override void UpdatePrevData() => _prevValue = Value; + protected override void Serialize(NetworkWriter writer, bool initialState) => writer.Write(Value); + protected override void Deserialize(NetworkReader reader, bool initialState) => Value = reader.Read(); } } diff --git a/QSB/Utility/VariableSync/BoolVariableSyncer.cs b/QSB/Utility/VariableSync/BoolVariableSyncer.cs index 0a7ed44e..e3525d5c 100644 --- a/QSB/Utility/VariableSync/BoolVariableSyncer.cs +++ b/QSB/Utility/VariableSync/BoolVariableSyncer.cs @@ -1,17 +1,6 @@ -using Mirror; -using System; - -namespace QSB.Utility.VariableSync +namespace QSB.Utility.VariableSync { - public class BoolVariableSyncer : BaseVariableSyncer + public class BoolVariableSyncer : BaseVariableSyncer { - private bool _prevValue; - [NonSerialized] - public bool Value; - - protected override bool HasChanged() => Value != _prevValue; - protected override void UpdatePrevData() => _prevValue = Value; - protected override void Serialize(NetworkWriter writer, bool initialState) => writer.Write(Value); - protected override void Deserialize(NetworkReader reader, bool initialState) => Value = reader.Read(); } } diff --git a/QSB/Utility/VariableSync/FloatVariableSyncer.cs b/QSB/Utility/VariableSync/FloatVariableSyncer.cs index 7903f479..39206e50 100644 --- a/QSB/Utility/VariableSync/FloatVariableSyncer.cs +++ b/QSB/Utility/VariableSync/FloatVariableSyncer.cs @@ -1,17 +1,6 @@ -using Mirror; -using System; - -namespace QSB.Utility.VariableSync +namespace QSB.Utility.VariableSync { - public class FloatVariableSyncer : BaseVariableSyncer + public class FloatVariableSyncer : BaseVariableSyncer { - private float _prevValue; - [NonSerialized] - public float Value; - - protected override bool HasChanged() => Value != _prevValue; - protected override void UpdatePrevData() => _prevValue = Value; - protected override void Serialize(NetworkWriter writer, bool initialState) => writer.Write(Value); - protected override void Deserialize(NetworkReader reader, bool initialState) => Value = reader.Read(); } } diff --git a/QSB/Utility/VariableSync/Vector3VariableSyncer.cs b/QSB/Utility/VariableSync/Vector3VariableSyncer.cs index 66748086..9f21ebc1 100644 --- a/QSB/Utility/VariableSync/Vector3VariableSyncer.cs +++ b/QSB/Utility/VariableSync/Vector3VariableSyncer.cs @@ -1,18 +1,8 @@ -using Mirror; -using System; -using UnityEngine; +using UnityEngine; namespace QSB.Utility.VariableSync { - public class Vector3VariableSyncer : BaseVariableSyncer + public class Vector3VariableSyncer : BaseVariableSyncer { - private Vector3 _prevValue; - [NonSerialized] - public Vector3 Value; - - protected override bool HasChanged() => Value != _prevValue; - protected override void UpdatePrevData() => _prevValue = Value; - protected override void Serialize(NetworkWriter writer, bool initialState) => writer.Write(Value); - protected override void Deserialize(NetworkReader reader, bool initialState) => Value = reader.Read(); } }