-
Notifications
You must be signed in to change notification settings - Fork 450
refactor!: remove network variable settings, network behaviour cleanup #1097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4e3e196
refactor!: remove network variable settings, network behaviour cleanup
mattwalsh-unity abfcfc0
remove ensure initialized
mattwalsh-unity a049539
moved init, removed unused var
mattwalsh-unity c1ed83b
Merge branch 'develop' into feature/remove_netvar_settings
mattwalsh-unity 705ec49
refactor!: remove network variable settings, network behaviour cleanup
mattwalsh-unity 764ae72
remove ensure initialized
mattwalsh-unity 0e84dd3
moved init, removed unused var
mattwalsh-unity 08f38f6
whitespace / include fixes
mattwalsh-unity a14a89b
merge fix
mattwalsh-unity f0eb64c
remove unnecessary requires
mattwalsh-unity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -425,7 +425,7 @@ internal void InitializeVariables() | |
sortedFields[i].SetValue(this, instance); | ||
} | ||
|
||
instance.NetworkBehaviour = this; | ||
instance.Initialize(this); | ||
|
||
var instanceNameProperty = fieldType.GetProperty(nameof(NetworkVariableBase.Name)); | ||
var sanitizedVariableName = sortedFields[i].Name.Replace("<", string.Empty).Replace(">k__BackingField", string.Empty); | ||
|
@@ -624,15 +624,15 @@ private bool CouldHaveDirtyNetworkVariables() | |
return false; | ||
} | ||
|
||
internal static void HandleNetworkVariableDeltas(List<NetworkVariableBase> networkVariableList, Stream stream, ulong clientId, NetworkBehaviour logInstance, NetworkManager networkManager) | ||
internal void HandleNetworkVariableDeltas(Stream stream, ulong clientId) | ||
{ | ||
using (var reader = PooledNetworkReader.Get(stream)) | ||
{ | ||
for (int i = 0; i < networkVariableList.Count; i++) | ||
for (int i = 0; i < NetworkVariableFields.Count; i++) | ||
{ | ||
ushort varSize = 0; | ||
|
||
if (networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
if (NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
{ | ||
varSize = reader.ReadUInt16Packed(); | ||
|
||
|
@@ -649,15 +649,15 @@ internal static void HandleNetworkVariableDeltas(List<NetworkVariableBase> netwo | |
} | ||
} | ||
|
||
if (networkManager.IsServer && !networkVariableList[i].CanClientWrite(clientId)) | ||
if (NetworkManager.IsServer && !NetworkVariableFields[i].CanClientWrite(clientId)) | ||
{ | ||
// we are choosing not to fire an exception here, because otherwise a malicious client could use this to crash the server | ||
if (networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
if (NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
{ | ||
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) | ||
{ | ||
NetworkLog.LogWarning($"Client wrote to {typeof(NetworkVariable<>).Name} without permission. => {(logInstance != null ? ($"{nameof(NetworkObjectId)}: {logInstance.NetworkObjectId} - {nameof(NetworkObject.GetNetworkBehaviourOrderIndex)}(): {logInstance.NetworkObject.GetNetworkBehaviourOrderIndex(logInstance)} - VariableIndex: {i}") : string.Empty)}"); | ||
NetworkLog.LogError($"[{networkVariableList[i].GetType().Name}]"); | ||
NetworkLog.LogWarning($"Client wrote to {typeof(NetworkVariable<>).Name} without permission. => {nameof(NetworkObjectId)}: {NetworkObjectId} - {nameof(NetworkObject.GetNetworkBehaviourOrderIndex)}(): {NetworkObject.GetNetworkBehaviourOrderIndex(this)} - VariableIndex: {i}"); | ||
NetworkLog.LogError($"[{NetworkVariableFields[i].GetType().Name}]"); | ||
} | ||
|
||
stream.Position += varSize; | ||
|
@@ -673,32 +673,33 @@ internal static void HandleNetworkVariableDeltas(List<NetworkVariableBase> netwo | |
// - TwoTen | ||
if (NetworkLog.CurrentLogLevel <= LogLevel.Error) | ||
{ | ||
NetworkLog.LogError($"Client wrote to {typeof(NetworkVariable<>).Name} without permission. No more variables can be read. This is critical. => {(logInstance != null ? ($"{nameof(NetworkObjectId)}: {logInstance.NetworkObjectId} - {nameof(NetworkObject.GetNetworkBehaviourOrderIndex)}(): {logInstance.NetworkObject.GetNetworkBehaviourOrderIndex(logInstance)} - VariableIndex: {i}") : string.Empty)}"); | ||
NetworkLog.LogError($"[{networkVariableList[i].GetType().Name}]"); | ||
NetworkLog.LogError($"Client wrote to {typeof(NetworkVariable<>).Name} without permission. No more variables can be read. This is critical. => {nameof(NetworkObjectId)}: {NetworkObjectId} - {nameof(NetworkObject.GetNetworkBehaviourOrderIndex)}(): {NetworkObject.GetNetworkBehaviourOrderIndex(this)} - VariableIndex: {i}"); | ||
NetworkLog.LogError($"[{NetworkVariableFields[i].GetType().Name}]"); | ||
} | ||
|
||
return; | ||
} | ||
long readStartPos = stream.Position; | ||
|
||
networkVariableList[i].ReadDelta(stream, networkManager.IsServer); | ||
networkManager.NetworkMetrics.TrackNetworkVariableDeltaReceived( | ||
NetworkVariableFields[i].ReadDelta(stream, NetworkManager.IsServer); | ||
NetworkManager.NetworkMetrics.TrackNetworkVariableDeltaReceived( | ||
clientId, | ||
logInstance.NetworkObjectId, | ||
logInstance.name, | ||
networkVariableList[i].Name, | ||
logInstance.__getTypeName(), | ||
NetworkObjectId, | ||
name, | ||
NetworkVariableFields[i].Name, | ||
__getTypeName(), | ||
stream.Length); | ||
|
||
(stream as NetworkBuffer).SkipPadBits(); | ||
|
||
if (networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
if (NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
{ | ||
if (stream.Position > (readStartPos + varSize)) | ||
{ | ||
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) | ||
{ | ||
NetworkLog.LogWarning($"Var delta read too far. {stream.Position - (readStartPos + varSize)} bytes. => {(logInstance != null ? ($"{nameof(NetworkObjectId)}: {logInstance.NetworkObjectId} - {nameof(NetworkObject.GetNetworkBehaviourOrderIndex)}(): {logInstance.NetworkObject.GetNetworkBehaviourOrderIndex(logInstance)} - VariableIndex: {i}") : string.Empty)}"); | ||
NetworkLog.LogWarning( | ||
$"Var delta read too far. {stream.Position - (readStartPos + varSize)} bytes. => {nameof(NetworkObjectId)}: {NetworkObjectId} - {nameof(NetworkObject.GetNetworkBehaviourOrderIndex)}(): {NetworkObject.GetNetworkBehaviourOrderIndex(this)} - VariableIndex: {i}"); | ||
} | ||
|
||
stream.Position = readStartPos + varSize; | ||
|
@@ -707,7 +708,8 @@ internal static void HandleNetworkVariableDeltas(List<NetworkVariableBase> netwo | |
{ | ||
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) | ||
{ | ||
NetworkLog.LogWarning($"Var delta read too little. {(readStartPos + varSize) - stream.Position} bytes. => {(logInstance != null ? ($"{nameof(NetworkObjectId)}: {logInstance.NetworkObjectId} - {nameof(NetworkObject.GetNetworkBehaviourOrderIndex)}(): {logInstance.NetworkObject.GetNetworkBehaviourOrderIndex(logInstance)} - VariableIndex: {i}") : string.Empty)}"); | ||
NetworkLog.LogWarning( | ||
$"Var delta read too little. {(readStartPos + varSize) - stream.Position} bytes. => {nameof(NetworkObjectId)}: {NetworkObjectId} - {nameof(NetworkObject.GetNetworkBehaviourOrderIndex)}(): {NetworkObject.GetNetworkBehaviourOrderIndex(this)} - VariableIndex: {i}"); | ||
} | ||
|
||
stream.Position = readStartPos + varSize; | ||
|
@@ -717,20 +719,20 @@ internal static void HandleNetworkVariableDeltas(List<NetworkVariableBase> netwo | |
} | ||
} | ||
|
||
internal static void WriteNetworkVariableData(List<NetworkVariableBase> networkVariableList, Stream stream, ulong clientId, NetworkManager networkManager) | ||
internal void WriteNetworkVariableData(Stream stream, ulong clientId) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same story here, no good reason to make this static |
||
{ | ||
if (networkVariableList.Count == 0) | ||
if (NetworkVariableFields.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
using (var writer = PooledNetworkWriter.Get(stream)) | ||
{ | ||
for (int j = 0; j < networkVariableList.Count; j++) | ||
for (int j = 0; j < NetworkVariableFields.Count; j++) | ||
{ | ||
bool canClientRead = networkVariableList[j].CanClientRead(clientId); | ||
bool canClientRead = NetworkVariableFields[j].CanClientRead(clientId); | ||
|
||
if (networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
if (NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
{ | ||
if (!canClientRead) | ||
{ | ||
|
@@ -744,11 +746,11 @@ internal static void WriteNetworkVariableData(List<NetworkVariableBase> networkV | |
|
||
if (canClientRead) | ||
{ | ||
if (networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
if (NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
{ | ||
using (var varBuffer = PooledNetworkBuffer.Get()) | ||
{ | ||
networkVariableList[j].WriteField(varBuffer); | ||
NetworkVariableFields[j].WriteField(varBuffer); | ||
varBuffer.PadBuffer(); | ||
|
||
writer.WriteUInt16Packed((ushort)varBuffer.Length); | ||
|
@@ -757,28 +759,28 @@ internal static void WriteNetworkVariableData(List<NetworkVariableBase> networkV | |
} | ||
else | ||
{ | ||
networkVariableList[j].WriteField(stream); | ||
NetworkVariableFields[j].WriteField(stream); | ||
writer.WritePadBits(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal static void SetNetworkVariableData(List<NetworkVariableBase> networkVariableList, Stream stream, NetworkManager networkManager) | ||
internal void SetNetworkVariableData(Stream stream) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same story here, cleaner as instance method |
||
{ | ||
if (networkVariableList.Count == 0) | ||
if (NetworkVariableFields.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
using (var reader = PooledNetworkReader.Get(stream)) | ||
{ | ||
for (int j = 0; j < networkVariableList.Count; j++) | ||
for (int j = 0; j < NetworkVariableFields.Count; j++) | ||
{ | ||
ushort varSize = 0; | ||
|
||
if (networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
if (NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
{ | ||
varSize = reader.ReadUInt16Packed(); | ||
|
||
|
@@ -797,10 +799,10 @@ internal static void SetNetworkVariableData(List<NetworkVariableBase> networkVar | |
|
||
long readStartPos = stream.Position; | ||
|
||
networkVariableList[j].ReadField(stream); | ||
NetworkVariableFields[j].ReadField(stream); | ||
reader.SkipPadBits(); | ||
|
||
if (networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
if (NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) | ||
{ | ||
if (stream is NetworkBuffer networkBuffer) | ||
{ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
com.unity.netcode.gameobjects/Runtime/Metrics/NetworkObjectProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#if MULTIPLAYER_TOOLS | ||
#if MULTIPLAYER_TOOLS | ||
using Unity.Multiplayer.Tools; | ||
using UnityEngine; | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't want NetworkBehaviour to be exposed, want tighter control over this