Skip to content

Commit e80a937

Browse files
Merge branch 'develop-2.0.0' into fix/networkvariable-collections-can-be-modified-without-write-permissions
2 parents 1f78274 + 058481b commit e80a937

File tree

9 files changed

+266
-50
lines changed

9 files changed

+266
-50
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1515

1616
### Fixed
1717

18+
- Fixed issue with the in-scene network prefab instance update menu tool where it was not properly updating scenes when invoked on the root prefab instance. (#3092)
1819
- Fixed issue where a newly synchronizing client would be synchronized with the current `NetworkVariable` values always which could cause issues with collections if there were any pending state updates. Now, when initially synchronizing a client, if a `NetworkVariable` has a pending state update it will serialize the previously known value(s) to the synchronizing client so when the pending updates are sent they aren't duplicate values on the newly connected client side. (#3081)
1920
- Fixed issue where changing ownership would mark every `NetworkVariable` dirty. Now, it will only mark any `NetworkVariable` with owner read permissions as dirty and will send/flush any pending updates to all clients prior to sending the change in ownership message. (#3081)
2021
- Fixed issue with `NetworkVariable` collections where transferring ownership to another client would not update the new owner's previous value to the most current value which could cause the last/previous added value to be detected as a change when adding or removing an entry (as long as the entry removed was not the last/previously added value). (#3081)

com.unity.netcode.gameobjects/Editor/NetworkManagerEditor.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public class NetworkManagerEditor : NetcodeEditorBase<NetworkManager>
4848
private SerializedProperty m_NetworkProfileMetrics;
4949
private SerializedProperty m_NetworkMessageMetrics;
5050

51+
#if CMB_SERVICE_DEVELOPMENT
52+
private SerializedProperty m_MajorVersion;
53+
private SerializedProperty m_MinorVersion;
54+
private SerializedProperty m_PatchVersion;
55+
#endif
56+
5157
private NetworkManager m_NetworkManager;
5258
private bool m_Initialized;
5359

@@ -120,8 +126,11 @@ private void Initialize()
120126
#if MULTIPLAYER_TOOLS
121127
m_NetworkMessageMetrics = m_NetworkConfigProperty.FindPropertyRelative("NetworkMessageMetrics");
122128
#endif
123-
124-
129+
#if CMB_SERVICE_DEVELOPMENT
130+
m_MajorVersion = serializedObject.FindProperty(nameof(NetworkManager.MajorVersion));
131+
m_MinorVersion = serializedObject.FindProperty(nameof(NetworkManager.MinorVersion));
132+
m_PatchVersion = serializedObject.FindProperty(nameof(NetworkManager.PatchVersion));
133+
#endif
125134
m_RpcHashSizeProperty = m_NetworkConfigProperty.FindPropertyRelative("RpcHashSize");
126135
m_PrefabsList = m_NetworkConfigProperty
127136
.FindPropertyRelative(nameof(NetworkConfig.Prefabs))
@@ -161,6 +170,11 @@ private void CheckNullProperties()
161170
#if MULTIPLAYER_TOOLS
162171
m_NetworkMessageMetrics = m_NetworkConfigProperty.FindPropertyRelative("NetworkMessageMetrics");
163172
#endif
173+
#if CMB_SERVICE_DEVELOPMENT
174+
m_MajorVersion = serializedObject.FindProperty(nameof(NetworkManager.MajorVersion));
175+
m_MinorVersion = serializedObject.FindProperty(nameof(NetworkManager.MinorVersion));
176+
m_PatchVersion = serializedObject.FindProperty(nameof(NetworkManager.PatchVersion));
177+
#endif
164178

165179
m_RpcHashSizeProperty = m_NetworkConfigProperty.FindPropertyRelative("RpcHashSize");
166180
m_PrefabsList = m_NetworkConfigProperty
@@ -173,10 +187,18 @@ private void DisplayNetworkManagerProperties()
173187
if (!m_NetworkManager.IsServer && !m_NetworkManager.IsClient)
174188
{
175189
serializedObject.Update();
190+
176191
EditorGUILayout.PropertyField(m_RunInBackgroundProperty);
177192
EditorGUILayout.PropertyField(m_LogLevelProperty);
193+
EditorGUILayout.Space();
178194

195+
#if CMB_SERVICE_DEVELOPMENT
196+
EditorGUILayout.LabelField("Version:", EditorStyles.boldLabel);
197+
EditorGUILayout.PropertyField(m_MajorVersion);
198+
EditorGUILayout.PropertyField(m_MinorVersion);
199+
EditorGUILayout.PropertyField(m_PatchVersion);
179200
EditorGUILayout.Space();
201+
#endif
180202
EditorGUILayout.LabelField("Network Settings", EditorStyles.boldLabel);
181203
#if MULTIPLAYER_SERVICES_SDK_INSTALLED
182204
EditorGUILayout.PropertyField(m_NetworkTopologyProperty);

com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,8 +1463,6 @@ private bool ShouldSynchronizeHalfFloat(ulong targetClientId)
14631463
// For test logging purposes
14641464
internal NetworkTransformState SynchronizeState;
14651465

1466-
// DANGO-TODO: We will want to remove this when we migrate NetworkTransforms to a dedicated internal message
1467-
private const ushort k_NetworkTransformStateMagic = 0xf48d;
14681466
#endregion
14691467

14701468
#region ONSYNCHRONIZE
@@ -1494,14 +1492,6 @@ protected override void OnSynchronize<T>(ref BufferSerializer<T> serializer)
14941492

14951493
if (serializer.IsWriter)
14961494
{
1497-
// DANGO-TODO: This magic value is sent to the server in order to identify the network transform.
1498-
// The server discards it before forwarding synchronization data to other clients.
1499-
if (NetworkManager.DistributedAuthorityMode && NetworkManager.CMBServiceConnection)
1500-
{
1501-
var writer = serializer.GetFastBufferWriter();
1502-
writer.WriteValueSafe(k_NetworkTransformStateMagic);
1503-
}
1504-
15051495
SynchronizeState.IsTeleportingNextFrame = true;
15061496
var transformToCommit = transform;
15071497
// If we are using Half Float Precision, then we want to only synchronize the authority's m_HalfPositionState.FullPosition in order for

com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,16 +564,20 @@ private void SendConnectionRequest()
564564
var message = new ConnectionRequestMessage
565565
{
566566
CMBServiceConnection = NetworkManager.CMBServiceConnection,
567-
TickRate = NetworkManager.NetworkConfig.TickRate,
568-
EnableSceneManagement = NetworkManager.NetworkConfig.EnableSceneManagement,
569-
570567
// Since only a remote client will send a connection request, we should always force the rebuilding of the NetworkConfig hash value
571568
ConfigHash = NetworkManager.NetworkConfig.GetConfig(false),
572569
ShouldSendConnectionData = NetworkManager.NetworkConfig.ConnectionApproval,
573570
ConnectionData = NetworkManager.NetworkConfig.ConnectionData,
574571
MessageVersions = new NativeArray<MessageVersionData>(MessageManager.MessageHandlers.Length, Allocator.Temp)
575572
};
576573

574+
if (NetworkManager.CMBServiceConnection)
575+
{
576+
message.ClientConfig.NGOVersion = NetworkManager.GetNGOVersion();
577+
message.ClientConfig.TickRate = NetworkManager.NetworkConfig.TickRate;
578+
message.ClientConfig.EnableSceneManagement = NetworkManager.NetworkConfig.EnableSceneManagement;
579+
}
580+
577581
for (int index = 0; index < MessageManager.MessageHandlers.Length; index++)
578582
{
579583
if (MessageManager.MessageTypes[index] != null)

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using UnityEngine;
66
#if UNITY_EDITOR
77
using UnityEditor;
8+
using PackageInfo = UnityEditor.PackageManager.PackageInfo;
89
#endif
910
using UnityEngine.SceneManagement;
1011
using Debug = UnityEngine.Debug;
@@ -885,6 +886,30 @@ internal T Value
885886

886887
internal Override<ushort> PortOverride;
887888

889+
890+
[HideInInspector]
891+
[SerializeField]
892+
[Range(0, 255)]
893+
internal byte MajorVersion;
894+
[HideInInspector]
895+
[SerializeField]
896+
[Range(0, 255)]
897+
internal byte MinorVersion;
898+
[HideInInspector]
899+
[SerializeField]
900+
[Range(0, 255)]
901+
internal byte PatchVersion;
902+
903+
internal NGOVersion GetNGOVersion()
904+
{
905+
return new NGOVersion()
906+
{
907+
Major = MajorVersion,
908+
Minor = MinorVersion,
909+
Patch = PatchVersion
910+
};
911+
}
912+
888913
#if UNITY_EDITOR
889914
internal static INetworkManagerHelper NetworkManagerHelper;
890915

@@ -911,13 +936,39 @@ protected virtual void OnValidateComponent()
911936

912937
}
913938

939+
private PackageInfo GetPackageInfo(string packageName)
940+
{
941+
return AssetDatabase.FindAssets("package").Select(AssetDatabase.GUIDToAssetPath).Where(x => AssetDatabase.LoadAssetAtPath<TextAsset>(x) != null).Select(PackageInfo.FindForAssetPath).Where(x => x != null).First(x => x.name == packageName);
942+
}
943+
944+
private void SetPackageVersion()
945+
{
946+
var packageInfo = GetPackageInfo("com.unity.netcode.gameobjects");
947+
if (packageInfo != null)
948+
{
949+
var versionSplit = packageInfo.version.Split(".");
950+
if (versionSplit.Length == 3)
951+
{
952+
MajorVersion = byte.Parse(versionSplit[0]);
953+
MinorVersion = byte.Parse(versionSplit[1]);
954+
PatchVersion = byte.Parse(versionSplit[2]);
955+
}
956+
}
957+
}
958+
914959
internal void OnValidate()
915960
{
916961
if (NetworkConfig == null)
917962
{
918963
return; // May occur when the component is added
919964
}
920965

966+
#if !CMB_SERVICE_DEVELOPMENT
967+
SetPackageVersion();
968+
#else
969+
Debug.Log($"Major:({MajorVersion}) Minor({MinorVersion}) Patch({PatchVersion})");
970+
#endif
971+
921972
if (GetComponentInChildren<NetworkObject>() != null)
922973
{
923974
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@ internal void RefreshAllPrefabInstances()
113113
}
114114

115115
// Handle updating the currently active scene
116-
var networkObjects = FindObjectsByType<NetworkObject>(FindObjectsInactive.Include, FindObjectsSortMode.None);
117-
foreach (var networkObject in networkObjects)
118-
{
119-
networkObject.OnValidate();
120-
}
121116
NetworkObjectRefreshTool.ProcessActiveScene();
122117

123118
// Refresh all build settings scenes
@@ -130,14 +125,14 @@ internal void RefreshAllPrefabInstances()
130125
continue;
131126
}
132127
// Add the scene to be processed
133-
NetworkObjectRefreshTool.ProcessScene(editorScene.path, false);
128+
NetworkObjectRefreshTool.ProcessScene(editorScene.path, true);
134129
}
135130

136131
// Process all added scenes
137132
NetworkObjectRefreshTool.ProcessScenes();
138133
}
139134

140-
private void OnValidate()
135+
internal void OnValidate()
141136
{
142137
// do NOT regenerate GlobalObjectIdHash for NetworkPrefabs while Editor is in PlayMode
143138
if (EditorApplication.isPlaying && !string.IsNullOrEmpty(gameObject.scene.name))
@@ -229,6 +224,7 @@ private void CheckForInScenePlaced()
229224
if (sourceAsset != null && sourceAsset.GlobalObjectIdHash != 0 && InScenePlacedSourceGlobalObjectIdHash != sourceAsset.GlobalObjectIdHash)
230225
{
231226
InScenePlacedSourceGlobalObjectIdHash = sourceAsset.GlobalObjectIdHash;
227+
EditorUtility.SetDirty(this);
232228
}
233229
IsSceneObject = true;
234230
}

com.unity.netcode.gameobjects/Runtime/Core/NetworkObjectRefreshTool.cs

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Text;
6+
using UnityEditor;
57
using UnityEditor.SceneManagement;
68
using UnityEngine;
79
using UnityEngine.SceneManagement;
@@ -21,6 +23,28 @@ internal class NetworkObjectRefreshTool
2123

2224
internal static Action AllScenesProcessed;
2325

26+
internal static NetworkObject PrefabNetworkObject;
27+
28+
internal static void LogInfo(string msg, bool append = false)
29+
{
30+
if (!append)
31+
{
32+
s_Log.AppendLine(msg);
33+
}
34+
else
35+
{
36+
s_Log.Append(msg);
37+
}
38+
}
39+
40+
internal static void FlushLog()
41+
{
42+
Debug.Log(s_Log.ToString());
43+
s_Log.Clear();
44+
}
45+
46+
private static StringBuilder s_Log = new StringBuilder();
47+
2448
internal static void ProcessScene(string scenePath, bool processScenes = true)
2549
{
2650
if (!s_ScenesToUpdate.Contains(scenePath))
@@ -29,14 +53,18 @@ internal static void ProcessScene(string scenePath, bool processScenes = true)
2953
{
3054
EditorSceneManager.sceneOpened += EditorSceneManager_sceneOpened;
3155
EditorSceneManager.sceneSaved += EditorSceneManager_sceneSaved;
56+
s_Log.Clear();
57+
LogInfo("NetworkObject Refresh Scenes to Process:");
3258
}
59+
LogInfo($"[{scenePath}]", true);
3360
s_ScenesToUpdate.Add(scenePath);
3461
}
3562
s_ProcessScenes = processScenes;
3663
}
3764

3865
internal static void ProcessActiveScene()
3966
{
67+
FlushLog();
4068
var activeScene = SceneManager.GetActiveScene();
4169
if (s_ScenesToUpdate.Contains(activeScene.path) && s_ProcessScenes)
4270
{
@@ -54,10 +82,12 @@ internal static void ProcessScenes()
5482
}
5583
else
5684
{
85+
s_ProcessScenes = false;
5786
s_CloseScenes = false;
5887
EditorSceneManager.sceneSaved -= EditorSceneManager_sceneSaved;
5988
EditorSceneManager.sceneOpened -= EditorSceneManager_sceneOpened;
6089
AllScenesProcessed?.Invoke();
90+
FlushLog();
6191
}
6292
}
6393

@@ -68,9 +98,8 @@ private static void FinishedProcessingScene(Scene scene, bool refreshed = false)
6898
// Provide a log of all scenes that were modified to the user
6999
if (refreshed)
70100
{
71-
Debug.Log($"Refreshed and saved updates to scene: {scene.name}");
101+
LogInfo($"Refreshed and saved updates to scene: {scene.name}");
72102
}
73-
s_ProcessScenes = false;
74103
s_ScenesToUpdate.Remove(scene.path);
75104

76105
if (scene != SceneManager.GetActiveScene())
@@ -88,24 +117,41 @@ private static void EditorSceneManager_sceneSaved(Scene scene)
88117

89118
private static void SceneOpened(Scene scene)
90119
{
120+
LogInfo($"Processing scene {scene.name}:");
91121
if (s_ScenesToUpdate.Contains(scene.path))
92122
{
93123
if (s_ProcessScenes)
94124
{
95-
if (!EditorSceneManager.MarkSceneDirty(scene))
96-
{
97-
Debug.Log($"Scene {scene.name} did not get marked as dirty!");
98-
FinishedProcessingScene(scene);
99-
}
100-
else
125+
var prefabInstances = PrefabUtility.FindAllInstancesOfPrefab(PrefabNetworkObject.gameObject);
126+
127+
if (prefabInstances.Length > 0)
101128
{
102-
EditorSceneManager.SaveScene(scene);
129+
var instancesSceneLoadedSpecific = prefabInstances.Where((c) => c.scene == scene).ToList();
130+
131+
if (instancesSceneLoadedSpecific.Count > 0)
132+
{
133+
foreach (var prefabInstance in instancesSceneLoadedSpecific)
134+
{
135+
prefabInstance.GetComponent<NetworkObject>().OnValidate();
136+
}
137+
138+
if (!EditorSceneManager.MarkSceneDirty(scene))
139+
{
140+
LogInfo($"Scene {scene.name} did not get marked as dirty!");
141+
FinishedProcessingScene(scene);
142+
}
143+
else
144+
{
145+
LogInfo($"Changes detected and applied!");
146+
EditorSceneManager.SaveScene(scene);
147+
}
148+
return;
149+
}
103150
}
104151
}
105-
else
106-
{
107-
FinishedProcessingScene(scene);
108-
}
152+
153+
LogInfo($"No changes required.");
154+
FinishedProcessingScene(scene);
109155
}
110156
}
111157

0 commit comments

Comments
 (0)