Merge pull request #385 from misternebula/fix-quantum-moon

Fix quantum moon
This commit is contained in:
Will Corby 2021-12-02 15:09:02 -08:00 committed by GitHub
commit 82c351e9df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 173 additions and 43 deletions

View File

@ -81,6 +81,13 @@ namespace QSB.QuantumSync
{
if (quantumObject.ControllingPlayer == 0)
{
if (quantumObject.IsEnabled)
{
Popcron.Gizmos.Line((quantumObject as IWorldObject).ReturnObject().transform.position,
QSBPlayerManager.LocalPlayer.Body.transform.position,
Color.magenta * 0.25f);
}
continue;
}

View File

@ -1,4 +1,5 @@
using QSB.WorldSync;
using System.Collections.Generic;
namespace QSB.QuantumSync
{
@ -6,5 +7,8 @@ namespace QSB.QuantumSync
{
uint ControllingPlayer { get; set; }
bool IsEnabled { get; set; }
List<ShapeVisibilityTracker> GetVisibilityTrackers();
List<Shape> GetAttachedShapes();
}
}

View File

@ -1,4 +1,5 @@
using QSB.Player;
using System.Linq;
namespace QSB.QuantumSync.WorldObjects
{
@ -8,7 +9,9 @@ namespace QSB.QuantumSync.WorldObjects
{
ObjectId = id;
AttachedObject = moonObject;
ControllingPlayer = QSBPlayerManager.LocalPlayerId;
ControllingPlayer = QSBCore.IsHost
? QSBPlayerManager.LocalPlayerId
: QSBPlayerManager.PlayerList.OrderBy(x => x.PlayerId).First().PlayerId;
base.Init(moonObject, id);
}
}

View File

@ -1,5 +1,4 @@
using OWML.Common;
using OWML.Utils;
using QSB.Events;
using QSB.Player;
using QSB.Utility;
@ -20,11 +19,8 @@ namespace QSB.QuantumSync.WorldObjects
{
foreach (var shape in GetAttachedShapes())
{
shape.OnShapeActivated -= (Shape s)
=> QSBCore.UnityEvents.FireOnNextUpdate(() => OnEnable(s));
shape.OnShapeDeactivated -= (Shape s)
=> QSBCore.UnityEvents.FireOnNextUpdate(() => OnDisable(s));
shape.OnShapeActivated -= OnEnable;
shape.OnShapeDeactivated -= OnDisable;
}
}
@ -56,13 +52,6 @@ namespace QSB.QuantumSync.WorldObjects
{
break;
}
// Firing next update to give time for shapes to actually be disabled
shape.OnShapeActivated += (Shape s)
=> QSBCore.UnityEvents.FireOnNextUpdate(() => OnEnable(s));
shape.OnShapeDeactivated += (Shape s)
=> QSBCore.UnityEvents.FireOnNextUpdate(() => OnDisable(s));
if (QSBCore.ShowQuantumVisibilityObjects)
{
@ -93,25 +82,49 @@ namespace QSB.QuantumSync.WorldObjects
}
}
if (GetAttachedShapes().Any(x => !x.enabled || !x.active))
QSBCore.UnityEvents.FireInNUpdates(LateInit, 5);
}
private void LateInit()
{
foreach (var shape in GetAttachedShapes())
{
shape.OnShapeActivated += OnEnable;
shape.OnShapeDeactivated += OnDisable;
}
var attachedShapes = GetAttachedShapes();
if (attachedShapes.Count == 0)
{
IsEnabled = false;
return;
}
if (attachedShapes.All(x => x.enabled && x.gameObject.activeInHierarchy && x.active))
{
IsEnabled = true;
}
else
{
ControllingPlayer = 0u;
IsEnabled = false;
}
else
{
IsEnabled = true;
}
}
private List<Shape> GetAttachedShapes()
public List<ShapeVisibilityTracker> GetVisibilityTrackers()
=> AttachedObject?._visibilityTrackers == null
? new()
: AttachedObject._visibilityTrackers.Select(x => (ShapeVisibilityTracker)x).ToList();
public List<Shape> GetAttachedShapes()
{
if (AttachedObject == null)
{
return new List<Shape>();
}
var visibilityTrackers = AttachedObject.GetValue<VisibilityTracker[]>("_visibilityTrackers");
var visibilityTrackers = AttachedObject._visibilityTrackers;
if (visibilityTrackers == null || visibilityTrackers.Length == 0)
{
return new List<Shape>();
@ -124,9 +137,15 @@ namespace QSB.QuantumSync.WorldObjects
}
var totalShapes = new List<Shape>();
foreach (var tracker in visibilityTrackers)
foreach (ShapeVisibilityTracker tracker in visibilityTrackers)
{
var shapes = tracker.GetValue<Shape[]>("_shapes");
if (tracker == null)
{
DebugLog.ToConsole($"Warning - a ShapeVisibilityTracker in {LogName} is null!", MessageType.Warning);
continue;
}
var shapes = tracker._shapes;
totalShapes.AddRange(shapes);
}
@ -135,6 +154,13 @@ namespace QSB.QuantumSync.WorldObjects
private void OnEnable(Shape s)
{
//DebugLog.DebugWrite($"{LogName} enable shape {s.name}");
if (IsEnabled)
{
return;
}
DebugLog.DebugWrite($"{LogName}-{Name} enable", MessageType.Success);
IsEnabled = true;
if (!QSBCore.WorldObjectsReady && !QSBCore.IsHost)
{
@ -154,6 +180,7 @@ namespace QSB.QuantumSync.WorldObjects
private void OnDisable(Shape s)
{
//DebugLog.DebugWrite($"{LogName} disable shape {s.name}");
if (!IsEnabled)
{
return;
@ -164,6 +191,7 @@ namespace QSB.QuantumSync.WorldObjects
return;
}
DebugLog.DebugWrite($"{LogName}-{Name} disable", MessageType.Error);
IsEnabled = false;
if (!QSBCore.WorldObjectsReady && !QSBCore.IsHost)
{

View File

@ -2,6 +2,7 @@
using QSB.Events;
using QSB.Patches;
using QSB.Player;
using QSB.Player.TransformSync;
using QSB.Utility;
using System.Collections.Generic;
using System.Linq;
@ -36,7 +37,9 @@ namespace QSB.RespawnSync
{
if (QSBSceneManager.IsInUniverse)
{
Init(QSBSceneManager.CurrentScene, true);
QSBCore.UnityEvents.RunWhen(
() => PlayerTransformSync.LocalInstance != null,
() => Init(QSBSceneManager.CurrentScene, true));
}
}

View File

@ -58,8 +58,6 @@ namespace QSB.SuspendableSync
{
identity.AssignClientAuthority(newConn);
}
DebugLog.DebugWrite($"{identity.NetId}:{identity.gameObject.name} - set authority to {id}");
}

View File

@ -1,7 +1,9 @@
using QSB.ClientServerStateSync;
using QSB.OrbSync.TransformSync;
using QSB.OrbSync.WorldObjects;
using QSB.Player;
using QSB.QuantumSync;
using QSB.QuantumSync.WorldObjects;
using QSB.ShipSync;
using QSB.ShipSync.TransformSync;
using QSB.ShipSync.WorldObjects;
@ -9,6 +11,7 @@ using QSB.TimeSync;
using QSB.WorldSync;
using System.Linq;
using UnityEngine;
using UnityEngine.Assertions.Must;
namespace QSB.Utility
{
@ -219,6 +222,28 @@ namespace QSB.Utility
}
}
}
WriteLine(4, $"");
WriteLine(4, $"Enabled QuantumObjects :");
foreach (var qo in QSBWorldSync.GetWorldObjects<IQSBQuantumObject>())
{
if (qo.ControllingPlayer != 0)
{
continue;
}
if (qo.IsEnabled)
{
if (qo is not IWorldObject qsbObj)
{
WriteLine(4, $"NULL QSBOBJ", Color.red);
}
else
{
WriteLine(4, $"{qsbObj.Name} ({qsbObj.ObjectId})");
}
}
}
#endregion
}
}

View File

@ -17,6 +17,8 @@ namespace QSB.Utility.VariableSync
public abstract void WriteData(QNetworkWriter writer);
public abstract void ReadData(QNetworkReader writer);
public bool Ready => _ready;
public virtual void Awake()
{
QNetworkServer.instance.m_SimpleServerSimple.RegisterHandlerSafe(short.MaxValue, HandleVariable);

View File

@ -5,10 +5,11 @@ namespace QSB.Utility.VariableSync
{
public class BoolVariableSyncer : BaseVariableSyncer
{
public VariableReference<bool> ValueToSync { get; private set; } = new();
public VariableReference<bool> ValueToSync { get; private set; }
public void Init(Func<bool> getter, Action<bool> setter)
{
ValueToSync = new(this);
ValueToSync.Getter = getter;
ValueToSync.Setter = setter;
_ready = true;
@ -18,9 +19,27 @@ namespace QSB.Utility.VariableSync
=> _ready = false;
public override void WriteData(QNetworkWriter writer)
=> writer.Write(ValueToSync.Value);
{
if (Ready)
{
writer.Write(ValueToSync.Value);
}
else
{
writer.Write(default(bool));
}
}
public override void ReadData(QNetworkReader writer)
=> ValueToSync.Value = writer.ReadBoolean();
public override void ReadData(QNetworkReader reader)
{
if (Ready)
{
ValueToSync.Value = reader.ReadBoolean();
}
else
{
reader.ReadBoolean();
}
}
}
}

View File

@ -5,10 +5,11 @@ namespace QSB.Utility.VariableSync
{
public class FloatVariableSyncer : BaseVariableSyncer
{
public VariableReference<float> ValueToSync { get; private set; } = new();
public VariableReference<float> ValueToSync { get; private set; }
public void Init(Func<float> getter, Action<float> setter)
{
ValueToSync = new(this);
ValueToSync.Getter = getter;
ValueToSync.Setter = setter;
_ready = true;
@ -18,9 +19,27 @@ namespace QSB.Utility.VariableSync
=> _ready = false;
public override void WriteData(QNetworkWriter writer)
=> writer.Write(ValueToSync.Value);
{
if (Ready)
{
writer.Write(ValueToSync.Value);
}
else
{
writer.Write(default(float));
}
}
public override void ReadData(QNetworkReader writer)
=> ValueToSync.Value = writer.ReadSingle();
public override void ReadData(QNetworkReader reader)
{
if (Ready)
{
ValueToSync.Value = reader.ReadSingle();
}
else
{
reader.ReadSingle();
}
}
}
}

View File

@ -1,14 +1,17 @@
using QSB.Player;
using QSB.Player.TransformSync;
using System;
using System;
namespace QSB.Utility.VariableSync
{
public class VariableReference<T>
{
private BaseVariableSyncer _owner;
public Func<T> Getter;
public Action<T> Setter;
public VariableReference(BaseVariableSyncer owner)
=> _owner = owner;
public T Value
{
get
@ -19,7 +22,7 @@ namespace QSB.Utility.VariableSync
}
else
{
if (PlayerTransformSync.LocalInstance != null && QSBPlayerManager.LocalPlayer.IsReady)
if (_owner.Ready)
{
DebugLog.ToConsole($"Warning - Getter is null!", OWML.Common.MessageType.Warning);
}
@ -35,7 +38,7 @@ namespace QSB.Utility.VariableSync
}
else
{
if (PlayerTransformSync.LocalInstance != null && QSBPlayerManager.LocalPlayer.IsReady)
if (_owner.Ready)
{
DebugLog.ToConsole($"Warning - Setter is null!", OWML.Common.MessageType.Warning);
}

View File

@ -6,10 +6,11 @@ namespace QSB.Utility.VariableSync
{
public class Vector3VariableSyncer : BaseVariableSyncer
{
public VariableReference<Vector3> ValueToSync { get; private set; } = new();
public VariableReference<Vector3> ValueToSync { get; private set; }
public void Init(Func<Vector3> getter, Action<Vector3> setter)
{
ValueToSync = new(this);
ValueToSync.Getter = getter;
ValueToSync.Setter = setter;
_ready = true;
@ -19,9 +20,27 @@ namespace QSB.Utility.VariableSync
=> _ready = false;
public override void WriteData(QNetworkWriter writer)
=> writer.Write(ValueToSync.Value);
{
if (Ready)
{
writer.Write(ValueToSync.Value);
}
else
{
writer.Write(default(Vector3));
}
}
public override void ReadData(QNetworkReader writer)
=> ValueToSync.Value = writer.ReadVector3();
public override void ReadData(QNetworkReader reader)
{
if (Ready)
{
ValueToSync.Value = reader.ReadVector3();
}
else
{
reader.ReadVector3();
}
}
}
}