fix variable sync

This commit is contained in:
Mister_Nebula 2021-05-07 14:58:37 +01:00
parent 89191f2bdf
commit 22339f3795
15 changed files with 241 additions and 22 deletions

View File

@ -1959,14 +1959,7 @@ namespace QNetWeaver
var name = typeDefinition.Module.Name;
if (name != Weaver.scriptDef.MainModule.Name && name != Weaver.UnityAssemblyDefinition.MainModule.Name && name != Weaver.QNetAssemblyDefinition.MainModule.Name && name != Weaver.corLib.Name && name != "System.Runtime.dll")
{
Log.Error(string.Concat(new string[]
{
"SyncVar [",
fieldDefinition.FullName,
"] from ",
typeDefinition.Module.ToString(),
" cannot be a different module."
}));
Log.Error($"SyncVar [{fieldDefinition.FullName}] is from an inaccessible module! : [{name}]");
Weaver.fail = true;
return;
}

View File

@ -1,5 +1,6 @@
using OWML.Common;
using OWML.Utils;
using QSB.Animation.Player.Thrusters;
using QSB.Events;
using QSB.Player;
using QSB.Utility;
@ -94,6 +95,7 @@ namespace QSB.Animation.Player
_playerController = body.parent.GetComponent<PlayerCharacterController>();
InitCrouchSync();
InitThrusters();
}
public void InitRemote(Transform body)
@ -118,11 +120,20 @@ namespace QSB.Animation.Player
SetAnimationType(AnimationType.PlayerUnsuited);
InitCrouchSync();
InitThrusters();
ThrusterManager.CreateRemotePlayerVFX(Player);
var ikSync = body.gameObject.AddComponent<PlayerHeadRotationSync>();
QSBCore.UnityEvents.RunWhen(() => Player.CameraBody != null, () => ikSync.Init(Player.CameraBody.transform));
}
private void InitThrusters()
{
Player.JetpackAcceleration = GetComponent<JetpackAccelerationSync>();
var thrusterModel = HasAuthority ? Locator.GetPlayerBody().GetComponent<ThrusterModel>() : null;
Player.JetpackAcceleration.Init(thrusterModel);
}
private void InitCrouchSync()
{
_crouchSync = GetComponent<CrouchSync>();

View File

@ -1,6 +0,0 @@
namespace QSB.Animation.Player.Thrusters.FlamesAndBubbles
{
internal class SetUpThrusterFlames
{
}
}

View File

@ -0,0 +1,38 @@
using QSB.Utility;
using QuantumUNET;
using QuantumUNET.Transport;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
namespace QSB.Animation.Player.Thrusters
{
public class JetpackAccelerationSync : QNetworkBehaviour
{
[SyncVar]
private Vector3 _localAcceleration;
private ThrusterModel _thrusterModel;
public Vector3 LocalAcceleration => _localAcceleration;
public void Init(ThrusterModel model)
=> _thrusterModel = model;
public void Update()
{
if (IsLocalPlayer)
{
SyncLocalAccel();
}
}
private void SyncLocalAccel()
{
if (_thrusterModel != null)
{
_localAcceleration = _thrusterModel.GetLocalAcceleration();
}
}
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace QSB.Animation.Player.Thrusters
{
class RemotePlayerParticlesController : MonoBehaviour
{
}
}

View File

@ -0,0 +1,104 @@
using QSB.Player;
using QSB.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace QSB.Animation.Player.Thrusters
{
class RemoteThrusterWashController : MonoBehaviour
{
private float _raycastDistance = 10f;
private AnimationCurve _emissionDistanceScale;
private AnimationCurve _emissionThrusterScale;
private ParticleSystem _defaultParticleSystem;
private ParticleSystem.MainModule _defaultMainModule;
private ParticleSystem.EmissionModule _defaultEmissionModule;
private float _baseDefaultEmissionRate;
private PlayerInfo _attachedPlayer;
private bool _isReady;
private bool _initialised;
public void InitFromOld(AnimationCurve distanceScale, AnimationCurve thrusterScale, ParticleSystem defaultParticleSystem, PlayerInfo player)
{
DebugLog.DebugWrite($"InitFromOld for player {player.PlayerId}");
_emissionDistanceScale = distanceScale;
_emissionThrusterScale = thrusterScale;
_defaultParticleSystem = defaultParticleSystem;
_attachedPlayer = player;
_isReady = true;
}
private void Init()
{
DebugLog.DebugWrite($"Init for player {_attachedPlayer.PlayerId}");
if (_defaultParticleSystem == null)
{
DebugLog.ToConsole($"Error - DefaultParticleSystem is null!", OWML.Common.MessageType.Error);
return;
}
else
{
DebugLog.DebugWrite($"DefaultParticleSystem OK.");
}
_defaultMainModule = _defaultParticleSystem.main;
_defaultEmissionModule = _defaultParticleSystem.emission;
_baseDefaultEmissionRate = _defaultEmissionModule.rateOverTime.constant;
_initialised = true;
}
private void Update()
{
if (_isReady && !_initialised)
{
Init();
}
if (!_initialised)
{
return;
}
RaycastHit hitInfo = default;
var aboveSurface = false;
var emissionThrusterScale = _emissionThrusterScale.Evaluate(_attachedPlayer.JetpackAcceleration.LocalAcceleration.y);
if (emissionThrusterScale > 0f)
{
aboveSurface = Physics.Raycast(transform.position, transform.forward, out hitInfo, _raycastDistance, OWLayerMask.physicalMask);
}
emissionThrusterScale = (!aboveSurface) ? 0f : (emissionThrusterScale * _emissionDistanceScale.Evaluate(hitInfo.distance));
if (emissionThrusterScale > 0f)
{
var position = hitInfo.point + (hitInfo.normal * 0.25f);
var rotation = Quaternion.LookRotation(hitInfo.normal);
if (!_defaultParticleSystem.isPlaying)
{
DebugLog.DebugWrite($"{_attachedPlayer.PlayerId} play");
_defaultParticleSystem.Play();
}
_defaultEmissionModule.rateOverTimeMultiplier = _baseDefaultEmissionRate * emissionThrusterScale;
_defaultParticleSystem.transform.SetPositionAndRotation(position, rotation);
if (_defaultMainModule.customSimulationSpace != hitInfo.transform)
{
_defaultMainModule.customSimulationSpace = hitInfo.transform;
_defaultParticleSystem.Clear();
}
}
else
{
if (_defaultParticleSystem.isPlaying)
{
DebugLog.DebugWrite($"{_attachedPlayer.PlayerId} stop");
_defaultParticleSystem.Stop(false, ParticleSystemStopBehavior.StopEmitting);
}
}
}
}
}

View File

@ -0,0 +1,49 @@
using OWML.Utils;
using QSB.Player;
using QSB.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace QSB.Animation.Player.Thrusters
{
class ThrusterManager
{
public static GameObject CreateRemotePlayerVFX(PlayerInfo player)
{
DebugLog.DebugWrite($"Create PlayerVFX for player {player.PlayerId}");
var localPlayerVfx = GameObject.Find("PlayerVFX");
var newVfx = UnityEngine.Object.Instantiate(localPlayerVfx);
CreateParticlesController(newVfx);
CreateThrusterWashController(newVfx.transform.Find("ThrusterWash").gameObject, player);
return newVfx;
}
private static void CreateParticlesController(GameObject root)
{
}
private static void CreateThrusterWashController(GameObject root, PlayerInfo player)
{
var old = root.GetComponent<ThrusterWashController>();
var oldDistanceScale = old.GetValue<AnimationCurve>("_emissionDistanceScale");
var oldThrusterScale = old.GetValue<AnimationCurve>("_emissionThrusterScale");
var defaultParticleSystem = old.GetValue<ParticleSystem>("_defaultParticleSystem");
UnityEngine.Object.Destroy(old);
var newObj = root.AddComponent<RemoteThrusterWashController>();
newObj.InitFromOld(oldDistanceScale, oldThrusterScale, defaultParticleSystem, player);
}
private static void CreateThrusterFlameController(GameObject root)
{
}
}
}

View File

@ -1,4 +1,5 @@
using QSB.Animation.Player;
using QSB.Animation.Player.Thrusters;
using QSB.CampfireSync.WorldObjects;
using QSB.Player.TransformSync;
using QSB.ProbeSync;
@ -47,6 +48,7 @@ namespace QSB.Player
public AnimationSync AnimationSync => QSBPlayerManager.GetSyncObject<AnimationSync>(PlayerId);
public bool PlayingInstrument => AnimationSync.CurrentType != AnimationType.PlayerSuited
&& AnimationSync.CurrentType != AnimationType.PlayerUnsuited;
public JetpackAccelerationSync JetpackAcceleration { get; set; }
// Misc
public bool IsInMoon; // TODO : move into PlayerStates?

View File

@ -124,7 +124,10 @@
<Compile Include="Animation\Player\CrouchSync.cs" />
<Compile Include="Animation\Player\Patches\PlayerAnimationPatches.cs" />
<Compile Include="Animation\Player\PlayerHeadRotationSync.cs" />
<Compile Include="Animation\Player\Thrusters\FlamesAndBubbles\SetUpThrusterFlames.cs" />
<Compile Include="Animation\Player\Thrusters\JetpackAccelerationSync.cs" />
<Compile Include="Animation\Player\Thrusters\RemotePlayerParticlesController.cs" />
<Compile Include="Animation\Player\Thrusters\RemoteThrusterWashController.cs" />
<Compile Include="Animation\Player\Thrusters\ThrusterManager.cs" />
<Compile Include="CampfireSync\CampfireManager.cs" />
<Compile Include="CampfireSync\Events\CampfireStateEvent.cs" />
<Compile Include="CampfireSync\Patches\CampfirePatches.cs" />

View File

@ -174,7 +174,7 @@ namespace QSB
GUI.Label(new Rect(220, offset, 200f, 20f), $"Probe Active : {Locator.GetProbe().gameObject.activeInHierarchy}");
offset += _debugLineSpacing;
GUI.Label(new Rect(220, offset, 200f, 20f), $"Player positions :");
GUI.Label(new Rect(220, offset, 200f, 20f), $"Player data :");
offset += _debugLineSpacing;
foreach (var player in QSBPlayerManager.PlayerList.Where(x => x.PlayerStates.IsReady))
{
@ -183,6 +183,8 @@ namespace QSB
GUI.Label(new Rect(220, offset, 400f, 20f), $"- {player.PlayerId} : {networkTransform.transform.localPosition} from {(sector == null ? "NULL" : sector.Name)}");
offset += _debugLineSpacing;
GUI.Label(new Rect(220, offset, 400f, 20f), $"- LocalAccel : {player.JetpackAcceleration?.LocalAcceleration}");
offset += _debugLineSpacing;
}
if (SocketedObjToDebug == -1)

View File

@ -1,6 +1,7 @@
using OWML.Common;
using OWML.Utils;
using QSB.Animation.Player;
using QSB.Animation.Player.Thrusters;
using QSB.DeathSync;
using QSB.Events;
using QSB.Instruments;
@ -59,6 +60,7 @@ namespace QSB
playerPrefab.AddComponent<AnimationSync>();
playerPrefab.AddComponent<CrouchSync>();
playerPrefab.AddComponent<WakeUpSync>();
playerPrefab.AddComponent<JetpackAccelerationSync>();
playerPrefab.AddComponent<InstrumentsManager>();
_shipPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkship.prefab");

View File

@ -37,6 +37,9 @@ namespace QuantumUNET.Components
set => m_LocalPlayerAuthority = value;
}
public QNetworkBehaviour[] GetNetworkBehaviours()
=> m_NetworkBehaviours;
public void SetRootIdentity(QNetworkIdentity newRoot)
{
if (RootIdentity != null)

View File

@ -5,7 +5,7 @@
public static string MsgTypeToString(short value)
{
string result;
if (value < 0 || value > 47)
if (value < 0 || value > 48)
{
result = string.Empty;
}
@ -61,7 +61,8 @@
public const short LobbyAddPlayerFailed = 45;
public const short LobbyReturnToLobby = 46;
public const short ReconnectPlayer = 47;
public const short Highest = 47;
public const short ClientUpdateVars = 48;
public const short Highest = 48;
internal static string[] msgLabels = {
"none",
@ -111,7 +112,8 @@
"LobbySceneLoaded",
"LobbyAddPlayerFailed",
"LobbyReturnToLobby",
"ReconnectPlayer"
"ReconnectPlayer",
"ClientUpdateVars"
};
}
}

View File

@ -57,8 +57,9 @@ namespace QuantumUNET
protected void ClientSendUpdateVars()
{
var writer = new QNetworkWriter();
writer.StartMessage(QMsgType.UpdateVars);
writer.StartMessage(QMsgType.ClientUpdateVars);
writer.Write(NetId);
writer.Write(GetType().Name);
if (OnSerialize(writer, false))
{
ClearAllDirtyBits();

View File

@ -109,7 +109,7 @@ namespace QuantumUNET
internal void RegisterMessageHandlers()
{
m_SimpleServerSimple.RegisterHandlerSafe(QMsgType.UpdateVars, OnUpdateVarsMessage);
m_SimpleServerSimple.RegisterHandlerSafe(QMsgType.ClientUpdateVars, OnUpdateVarsMessage);
m_SimpleServerSimple.RegisterHandlerSafe(QMsgType.Ready, OnClientReadyMessage);
m_SimpleServerSimple.RegisterHandlerSafe(QMsgType.Command, OnCommandMessage);
m_SimpleServerSimple.RegisterHandlerSafe(QMsgType.LocalPlayerTransform, QNetworkTransform.HandleTransform);
@ -124,9 +124,12 @@ namespace QuantumUNET
private static void OnUpdateVarsMessage(QNetworkMessage netMsg)
{
var networkInstanceId = netMsg.Reader.ReadNetworkId();
var typeName = netMsg.Reader.ReadString();
if (instance.m_NetworkScene.GetNetworkIdentity(networkInstanceId, out var networkIdentity))
{
networkIdentity.OnUpdateVars(netMsg.Reader, false);
var allBehaviours = networkIdentity.GetNetworkBehaviours();
var target = allBehaviours.First(x => x.GetType().Name == typeName);
target.OnDeserialize(netMsg.Reader, false);
}
else
{