remove old transform sync

This commit is contained in:
Mister_Nebula 2021-08-19 17:27:37 +01:00
parent 28691065a0
commit 988ee05211
3 changed files with 0 additions and 628 deletions

View File

@ -1,264 +0,0 @@
using OWML.Common;
using QSB.Player;
using QSB.Player.TransformSync;
using QSB.Utility;
using QuantumUNET;
using QuantumUNET.Transport;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace QSB.Syncs.TransformSync
{
/*
* Rewrite number : 7
* God has cursed me for my hubris, and my work is never finished.
*/
public abstract class BaseTransformSync : SyncBase
{
private static readonly Dictionary<PlayerInfo, Dictionary<Type, BaseTransformSync>> _storedTransformSyncs = new Dictionary<PlayerInfo, Dictionary<Type, BaseTransformSync>>();
public static T GetPlayers<T>(PlayerInfo player)
where T : BaseTransformSync
{
var dictOfOwnedSyncs = _storedTransformSyncs[player];
var wantedSync = dictOfOwnedSyncs[typeof(T)];
if (wantedSync == default)
{
DebugLog.ToConsole($"Error - _storedTransformSyncs does not contain type:{typeof(T)} under player {player.PlayerId}. Attempting to find manually...", MessageType.Error);
var allSyncs = Resources.FindObjectsOfTypeAll<T>();
wantedSync = allSyncs.First(x => x.Player == player);
if (wantedSync == default)
{
DebugLog.ToConsole($"Error - Could not find type:{typeof(T)} for player {player.PlayerId} manually!", MessageType.Error);
return default;
}
}
return (T)wantedSync;
}
public virtual void Start()
{
var lowestBound = Resources.FindObjectsOfTypeAll<PlayerTransformSync>()
.Where(x => x.NetId.Value <= NetId.Value).OrderBy(x => x.NetId.Value).Last();
NetIdentity.SetRootIdentity(lowestBound.NetIdentity);
DontDestroyOnLoad(gameObject);
_intermediaryTransform = new IntermediaryTransform(transform);
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
if (Player == null)
{
DebugLog.ToConsole($"Error - Player in start of {_logName} was null!", MessageType.Error);
return;
}
if (!_storedTransformSyncs.ContainsKey(Player))
{
_storedTransformSyncs.Add(Player, new Dictionary<Type, BaseTransformSync>());
}
var playerDict = _storedTransformSyncs[Player];
playerDict[GetType()] = this;
}
protected virtual void OnDestroy()
{
if (!HasAuthority && AttachedObject != null)
{
Destroy(AttachedObject.gameObject);
}
QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
if (!QSBPlayerManager.PlayerExists(PlayerId))
{
return;
}
var playerDict = _storedTransformSyncs[Player];
playerDict.Remove(GetType());
}
protected virtual void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isInUniverse)
=> _isInitialized = false;
protected override void Init()
{
if (!QSBSceneManager.IsInUniverse)
{
DebugLog.ToConsole($"Error - {_logName} is being init-ed when not in the universe!", MessageType.Error);
}
if (!HasAuthority && AttachedObject != null)
{
Destroy(AttachedObject.gameObject);
}
AttachedObject = HasAuthority ? InitLocalTransform() : InitRemoteTransform();
_isInitialized = true;
if (QSBCore.DebugMode)
{
DebugBoxManager.CreateBox(AttachedObject.transform, 0, _logName);
}
}
public override bool OnSerialize(QNetworkWriter writer, bool initialState)
{
if (!initialState)
{
if (SyncVarDirtyBits == 0U)
{
writer.WritePackedUInt32(0U);
return false;
}
writer.WritePackedUInt32(1U);
}
SerializeTransform(writer, initialState);
return true;
}
public override void OnDeserialize(QNetworkReader reader, bool initialState)
{
if (!IsServer || !QNetworkServer.localClientActive)
{
if (!initialState)
{
if (reader.ReadPackedUInt32() == 0U)
{
return;
}
}
DeserializeTransform(reader, initialState);
}
}
public override void SerializeTransform(QNetworkWriter writer, bool initialState)
{
if (_intermediaryTransform == null)
{
_intermediaryTransform = new IntermediaryTransform(transform);
}
var worldPos = _intermediaryTransform.GetPosition();
var worldRot = _intermediaryTransform.GetRotation();
writer.Write(worldPos);
SerializeRotation(writer, worldRot);
_prevPosition = worldPos;
_prevRotation = worldRot;
}
public override void DeserializeTransform(QNetworkReader reader, bool initialState)
{
if (!QSBCore.WorldObjectsReady)
{
reader.ReadVector3();
DeserializeRotation(reader);
return;
}
var pos = reader.ReadVector3();
var rot = DeserializeRotation(reader);
if (HasAuthority)
{
return;
}
if (_intermediaryTransform == null)
{
_intermediaryTransform = new IntermediaryTransform(transform);
}
_intermediaryTransform.SetPosition(pos);
_intermediaryTransform.SetRotation(rot);
if (_intermediaryTransform.GetPosition() == Vector3.zero)
{
DebugLog.ToConsole($"Warning - {_logName} at (0,0,0)! - Given position was {pos}", MessageType.Warning);
}
}
protected override bool UpdateTransform()
{
if (HasAuthority)
{
_intermediaryTransform.EncodePosition(AttachedObject.transform.position);
_intermediaryTransform.EncodeRotation(AttachedObject.transform.rotation);
return true;
}
var targetPos = _intermediaryTransform.GetTargetPosition_ParentedToReference();
var targetRot = _intermediaryTransform.GetTargetRotation_ParentedToReference();
if (targetPos != Vector3.zero && _intermediaryTransform.GetTargetPosition_Unparented() != Vector3.zero)
{
if (UseInterpolation)
{
AttachedObject.transform.localPosition = SmartSmoothDamp(AttachedObject.transform.localPosition, targetPos);
AttachedObject.transform.localRotation = QuaternionHelper.SmoothDamp(AttachedObject.transform.localRotation, targetRot, ref _rotationSmoothVelocity, SmoothTime);
}
else
{
AttachedObject.transform.localPosition = targetPos;
AttachedObject.transform.localRotation = targetRot;
}
}
return true;
}
public override bool HasMoved()
{
var displacementMagnitude = (_intermediaryTransform.GetPosition() - _prevPosition).magnitude;
return displacementMagnitude > 1E-03f
|| Quaternion.Angle(_intermediaryTransform.GetRotation(), _prevRotation) > 1E-03f;
}
public void SetReferenceTransform(Transform transform)
{
if (ReferenceTransform == transform)
{
return;
}
ReferenceTransform = transform;
_intermediaryTransform.SetReferenceTransform(transform);
if (AttachedObject == null)
{
DebugLog.ToConsole($"Warning - AttachedObject was null for {_logName} when trying to set reference transform to {transform?.name}. Waiting until not null...", MessageType.Warning);
QSBCore.UnityEvents.RunWhen(
() => AttachedObject != null,
() => ReparentAttachedObject(transform));
return;
}
if (!HasAuthority)
{
ReparentAttachedObject(transform);
}
if (HasAuthority)
{
_intermediaryTransform.EncodePosition(AttachedObject.transform.position);
_intermediaryTransform.EncodeRotation(AttachedObject.transform.rotation);
}
}
private void ReparentAttachedObject(Transform newParent)
{
if (AttachedObject.transform.parent != null && AttachedObject.transform.parent.GetComponent<Sector>() == null)
{
DebugLog.ToConsole($"Warning - Trying to reparent AttachedObject {AttachedObject.name} which wasnt attached to sector!", MessageType.Warning);
}
AttachedObject.transform.SetParent(newParent, true);
AttachedObject.transform.localScale = Vector3.one;
}
}
}

View File

@ -1,220 +0,0 @@
using QSB.Player;
using QSB.SectorSync;
using QSB.SectorSync.WorldObjects;
using QSB.Utility;
using QSB.WorldSync;
using QuantumUNET.Transport;
using UnityEngine;
namespace QSB.Syncs.TransformSync
{
public abstract class SectoredTransformSync : BaseTransformSync, ISectoredSync<Transform>
{
public QSBSector ReferenceSector { get; set; }
public SectorSync.SectorSync SectorSync { get; private set; }
public abstract TargetType Type { get; }
public override bool IgnoreNullReferenceTransform => true;
public override bool IgnoreDisabledAttachedObject => false;
private int _sectorIdWaitingSlot = int.MinValue;
public override void Start()
{
SectorSync = gameObject.AddComponent<SectorSync.SectorSync>();
QSBSectorManager.Instance.SectoredTransformSyncs.Add(this);
base.Start();
}
protected override void OnDestroy()
{
base.OnDestroy();
QSBSectorManager.Instance.SectoredTransformSyncs.Remove(this);
if (SectorSync != null)
{
Destroy(SectorSync);
}
}
protected override void Init()
{
base.Init();
if (!QSBSectorManager.Instance.IsReady)
{
return;
}
if (!HasAuthority)
{
return;
}
QSBCore.UnityEvents.RunWhen(() => SectorSync.IsReady, InitSector);
}
private void InitSector()
{
DebugLog.DebugWrite($"InitSector of {_logName}");
var closestSector = SectorSync.GetClosestSector(AttachedObject.transform);
if (closestSector != null)
{
SetReferenceSector(closestSector);
}
else
{
DebugLog.ToConsole($"Warning - {_logName}'s initial sector was null.", OWML.Common.MessageType.Warning);
}
}
public override void Update()
{
if (_sectorIdWaitingSlot == int.MinValue)
{
base.Update();
return;
}
if (!WorldObjectManager.AllReady)
{
base.Update();
return;
}
var sector = _sectorIdWaitingSlot == -1
? null
: QSBWorldSync.GetWorldFromId<QSBSector>(_sectorIdWaitingSlot);
if (sector != ReferenceSector)
{
if (sector == null)
{
DebugLog.ToConsole($"Error - {PlayerId}.{GetType().Name} got sector of ID -1.", OWML.Common.MessageType.Error);
base.Update();
return;
}
SetReferenceSector(sector);
}
_sectorIdWaitingSlot = int.MinValue;
base.Update();
}
public override void SerializeTransform(QNetworkWriter writer, bool initialState)
{
if (_intermediaryTransform == null)
{
_intermediaryTransform = new IntermediaryTransform(transform);
}
if (!QSBPlayerManager.PlayerExists(PlayerId))
{
writer.Write(-1);
}
else if (!Player.PlayerStates.IsReady)
{
writer.Write(-1);
}
if (ReferenceSector != null)
{
writer.Write(ReferenceSector.ObjectId);
}
else
{
if (_isInitialized)
{
DebugLog.ToConsole($"Warning - ReferenceSector of {PlayerId}.{GetType().Name} is null.", OWML.Common.MessageType.Warning);
}
writer.Write(-1);
}
base.SerializeTransform(writer, initialState);
}
public override void DeserializeTransform(QNetworkReader reader, bool initialState)
{
int sectorId;
if (!QSBCore.WorldObjectsReady)
{
sectorId = reader.ReadInt32();
if (initialState && sectorId != -1)
{
DebugLog.DebugWrite($"{_logName} set waiting sector id:{sectorId}");
_sectorIdWaitingSlot = sectorId;
}
reader.ReadVector3();
DeserializeRotation(reader);
return;
}
sectorId = reader.ReadInt32();
var sector = sectorId == -1
? null
: QSBWorldSync.GetWorldFromId<QSBSector>(sectorId);
if (sector != ReferenceSector)
{
if (sector == null)
{
DebugLog.ToConsole($"Error - {PlayerId}.{GetType().Name} got sector of ID -1.", OWML.Common.MessageType.Error);
base.DeserializeTransform(reader, initialState);
return;
}
SetReferenceSector(sector);
}
base.DeserializeTransform(reader, initialState);
}
protected override bool UpdateTransform()
{
var referenceNull = ReferenceTransform == null || ReferenceSector == null || _intermediaryTransform.GetReferenceTransform() == null;
var sectorManagerReady = QSBSectorManager.Instance.IsReady;
if (!sectorManagerReady)
{
if (referenceNull && HasAuthority)
{
DebugLog.ToConsole($"Warning - Reference was null, but sector manager wasn't ready. " +
$"Transform:{ReferenceTransform == null}, Sector:{ReferenceSector == null}, Intermediary:{_intermediaryTransform.GetReferenceTransform() == null}",
OWML.Common.MessageType.Warning);
}
return base.UpdateTransform();
}
if (!HasAuthority)
{
return base.UpdateTransform();
}
if (referenceNull)
{
var closestSector = SectorSync.GetClosestSector(AttachedObject.transform);
if (closestSector != null)
{
SetReferenceTransform(closestSector.Transform);
}
else
{
if (SectorSync.IsReady)
{
DebugLog.ToConsole($"Error - No closest sector found to {PlayerId}.{GetType().Name}!", OWML.Common.MessageType.Error);
}
return base.UpdateTransform();
}
}
return base.UpdateTransform();
}
public void SetReferenceSector(QSBSector sector)
{
ReferenceSector = sector;
SetReferenceTransform(sector?.Transform);
}
}
}

View File

@ -1,144 +0,0 @@
using OWML.Common;
using QSB.Player.TransformSync;
using QSB.Utility;
using QuantumUNET.Transport;
using System.Linq;
using UnityEngine;
namespace QSB.Syncs.TransformSync
{
public abstract class UnparentedBaseTransformSync : SyncBase
{
public override bool IgnoreDisabledAttachedObject => false;
public override bool IgnoreNullReferenceTransform => false;
public virtual void Start()
{
var lowestBound = Resources.FindObjectsOfTypeAll<PlayerTransformSync>()
.Where(x => x.NetId.Value <= NetId.Value).OrderBy(x => x.NetId.Value).Last();
NetIdentity.SetRootIdentity(lowestBound.NetIdentity);
DontDestroyOnLoad(gameObject);
_intermediaryTransform = new IntermediaryTransform(transform);
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
}
protected virtual void OnDestroy()
{
DebugLog.DebugWrite($"OnDestroy {_logName}");
if (!HasAuthority && AttachedObject != null)
{
Destroy(AttachedObject.gameObject);
}
QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
}
protected void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isInUniverse) =>
_isInitialized = false;
protected override void Init()
{
AttachedObject = HasAuthority ? InitLocalTransform() : InitRemoteTransform();
_isInitialized = true;
}
public override void SerializeTransform(QNetworkWriter writer, bool initialState)
{
if (_intermediaryTransform == null)
{
_intermediaryTransform = new IntermediaryTransform(transform);
}
var worldPos = _intermediaryTransform.GetPosition();
var worldRot = _intermediaryTransform.GetRotation();
writer.Write(worldPos);
SerializeRotation(writer, worldRot);
_prevPosition = worldPos;
_prevRotation = worldRot;
}
public override void DeserializeTransform(QNetworkReader reader, bool initialState)
{
if (!QSBCore.WorldObjectsReady)
{
reader.ReadVector3();
DeserializeRotation(reader);
return;
}
var pos = reader.ReadVector3();
var rot = DeserializeRotation(reader);
if (HasAuthority)
{
return;
}
if (_intermediaryTransform == null)
{
_intermediaryTransform = new IntermediaryTransform(transform);
}
_intermediaryTransform.SetPosition(pos);
_intermediaryTransform.SetRotation(rot);
if (_intermediaryTransform.GetPosition() == Vector3.zero)
{
DebugLog.ToConsole($"Warning - {PlayerId}.{GetType().Name} at (0,0,0)! - Given position was {pos}", MessageType.Warning);
}
}
protected override bool UpdateTransform()
{
if (HasAuthority)
{
_intermediaryTransform.EncodePosition(AttachedObject.transform.position);
_intermediaryTransform.EncodeRotation(AttachedObject.transform.rotation);
return true;
}
var targetPos = _intermediaryTransform.GetTargetPosition_Unparented();
var targetRot = _intermediaryTransform.GetTargetRotation_Unparented();
if (targetPos != Vector3.zero && _intermediaryTransform.GetTargetPosition_ParentedToReference() != Vector3.zero)
{
if (UseInterpolation)
{
AttachedObject.transform.position = SmartSmoothDamp(AttachedObject.transform.position, targetPos);
AttachedObject.transform.rotation = QuaternionHelper.SmoothDamp(AttachedObject.transform.rotation, targetRot, ref _rotationSmoothVelocity, SmoothTime);
}
else
{
AttachedObject.transform.position = targetPos;
AttachedObject.transform.rotation = targetRot;
}
}
return true;
}
public override bool HasMoved()
{
var displacementMagnitude = (_intermediaryTransform.GetPosition() - _prevPosition).magnitude;
return displacementMagnitude > 1E-03f
|| Quaternion.Angle(_intermediaryTransform.GetRotation(), _prevRotation) > 1E-03f;
}
public void SetReferenceTransform(Transform transform)
{
if (ReferenceTransform == transform)
{
return;
}
ReferenceTransform = transform;
_intermediaryTransform.SetReferenceTransform(transform);
if (HasAuthority)
{
_intermediaryTransform.EncodePosition(AttachedObject.transform.position);
_intermediaryTransform.EncodeRotation(AttachedObject.transform.rotation);
}
}
}
}