|
| 1 | +#if SCENE_MANAGEMENT_SCENE_HANDLE_NO_INT_CONVERSION |
| 2 | +using System; |
| 3 | +#endif |
| 4 | +using UnityEngine.SceneManagement; |
| 5 | + |
| 6 | +namespace Unity.Netcode |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// The scene map type <see cref="SceneMap"/>. |
| 10 | + /// </summary> |
| 11 | + public enum MapTypes |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Denotes the server to client scene map type. |
| 15 | + /// </summary> |
| 16 | + ServerToClient, |
| 17 | + /// <summary> |
| 18 | + /// Denotes the client to server scene map type. |
| 19 | + /// </summary> |
| 20 | + ClientToServer |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Provides the status of a loaded scene |
| 25 | + /// </summary> |
| 26 | + public struct SceneMap : INetworkSerializable |
| 27 | + { |
| 28 | + /// <summary> |
| 29 | + /// The scene mapping type <see cref="MapTypes"/>. |
| 30 | + /// </summary> |
| 31 | + public MapTypes MapType; |
| 32 | + /// <summary> |
| 33 | + /// The <see cref="UnityEngine.SceneManagement.Scene"/> struct of the scene mapped. |
| 34 | + /// </summary> |
| 35 | + public Scene Scene; |
| 36 | + /// <summary> |
| 37 | + /// When true, the scene is present. |
| 38 | + /// </summary> |
| 39 | + public bool ScenePresent; |
| 40 | + /// <summary> |
| 41 | + /// The name of the scene |
| 42 | + /// </summary> |
| 43 | + public string SceneName; |
| 44 | + |
| 45 | +#if SCENE_MANAGEMENT_SCENE_HANDLE_NO_INT_CONVERSION |
| 46 | + /// <summary> |
| 47 | + /// The scene's server handle (a.k.a network scene handle) |
| 48 | + /// </summary> |
| 49 | + /// <remarks> |
| 50 | + /// This is deprecated in favor of ServerSceneHandle |
| 51 | + /// </remarks> |
| 52 | + [Obsolete("Int representation of a SceneHandle is deprecated, please use SceneHandle instead. (UnityUpgradable) -> ServerSceneHandle")] |
| 53 | +#else |
| 54 | + /// <summary> |
| 55 | + /// The scene's server handle (a.k.a network scene handle) |
| 56 | + /// </summary> |
| 57 | +#endif |
| 58 | + public int ServerHandle; |
| 59 | + |
| 60 | +#if SCENE_MANAGEMENT_SCENE_HANDLE_NO_INT_CONVERSION |
| 61 | + /// <summary> |
| 62 | + /// The mapped handled. This could be the ServerHandle or LocalHandle depending upon context (client or server). |
| 63 | + /// </summary> |
| 64 | + /// <remarks> |
| 65 | + /// This is deprecated in favor of MappedLocalSceneHandle |
| 66 | + /// </remarks> |
| 67 | + [Obsolete("Int representation of a SceneHandle is deprecated, please use SceneHandle instead. (UnityUpgradable) -> MappedLocalSceneHandle")] |
| 68 | +#else |
| 69 | + /// <summary> |
| 70 | + /// The mapped handled. This could be the ServerHandle or LocalHandle depending upon context (client or server). |
| 71 | + /// </summary> |
| 72 | +#endif |
| 73 | + public int MappedLocalHandle; |
| 74 | + |
| 75 | +#if SCENE_MANAGEMENT_SCENE_HANDLE_NO_INT_CONVERSION |
| 76 | + /// <summary> |
| 77 | + /// The local handle of the scene. |
| 78 | + /// </summary> |
| 79 | + /// <remarks> |
| 80 | + /// This is deprecated in favor of LocalSceneHandle |
| 81 | + /// </remarks> |
| 82 | + [Obsolete("Int representation of a SceneHandle is deprecated, please use SceneHandle instead. (UnityUpgradable) -> LocalSceneHandle")] |
| 83 | +#else |
| 84 | + /// <summary> |
| 85 | + /// The local handle of the scene. |
| 86 | + /// </summary> |
| 87 | +#endif |
| 88 | + public int LocalHandle; |
| 89 | + |
| 90 | +#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE |
| 91 | + /// <summary> |
| 92 | + /// The scene's server handle (a.k.a network scene handle) |
| 93 | + /// </summary> |
| 94 | + public SceneHandle ServerSceneHandle; |
| 95 | + /// <summary> |
| 96 | + /// The mapped handled. This could be the ServerSceneHandle or LocalSceneHandle depending upon context (client or server). |
| 97 | + /// </summary> |
| 98 | + public SceneHandle MappedLocalSceneHandle; |
| 99 | + /// <summary> |
| 100 | + /// The local handle of the scene. |
| 101 | + /// </summary> |
| 102 | + public SceneHandle LocalSceneHandle; |
| 103 | +#endif |
| 104 | + |
| 105 | + private NetworkSceneHandle m_ServerHandle; |
| 106 | + private NetworkSceneHandle m_MappedLocalHandle; |
| 107 | + private NetworkSceneHandle m_LocalHandle; |
| 108 | + |
| 109 | + internal SceneMap(MapTypes mapType, Scene scene, bool isScenePresent, NetworkSceneHandle serverHandle, NetworkSceneHandle mappedLocalHandle) |
| 110 | + { |
| 111 | + MapType = mapType; |
| 112 | + Scene = scene; |
| 113 | + ScenePresent = isScenePresent; |
| 114 | + SceneName = isScenePresent ? scene.name : "Not Present"; |
| 115 | + |
| 116 | + m_ServerHandle = serverHandle; |
| 117 | + m_MappedLocalHandle = mappedLocalHandle; |
| 118 | + m_LocalHandle = new NetworkSceneHandle(scene.handle); |
| 119 | + |
| 120 | +#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE |
| 121 | + ServerSceneHandle = serverHandle; |
| 122 | + MappedLocalSceneHandle = mappedLocalHandle; |
| 123 | + LocalSceneHandle = scene.handle; |
| 124 | +#endif |
| 125 | + |
| 126 | + |
| 127 | +#pragma warning disable CS0618 // Type or member is obsolete |
| 128 | +#if SCENE_MANAGEMENT_SCENE_HANDLE_MUST_USE_ULONG |
| 129 | + ServerHandle = (int)Server.GetRawData(); |
| 130 | + MappedLocalHandle = (int)MappedLocal.GetRawData(); |
| 131 | + LocalHandle = (int)Local.GetRawData(); |
| 132 | +#else |
| 133 | + ServerHandle = m_ServerHandle.GetRawData(); |
| 134 | + MappedLocalHandle = m_MappedLocalHandle.GetRawData(); |
| 135 | + LocalHandle = m_LocalHandle.GetRawData(); |
| 136 | +#endif |
| 137 | +#pragma warning restore CS0618 // Type or member is obsolete |
| 138 | + } |
| 139 | + |
| 140 | + /// <inheritdoc cref="INetworkSerializable.NetworkSerialize{T}(BufferSerializer{T})"/> |
| 141 | + public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter |
| 142 | + { |
| 143 | + serializer.SerializeValue(ref MapType); |
| 144 | + serializer.SerializeValue(ref ScenePresent); |
| 145 | + if (serializer.IsReader) |
| 146 | + { |
| 147 | + SceneName = "Not Present"; |
| 148 | + } |
| 149 | + if (ScenePresent) |
| 150 | + { |
| 151 | + serializer.SerializeValue(ref SceneName); |
| 152 | +#pragma warning disable CS0618 // Type or member is obsolete |
| 153 | + serializer.SerializeValue(ref LocalHandle); |
| 154 | + } |
| 155 | + serializer.SerializeValue(ref ServerHandle); |
| 156 | + serializer.SerializeValue(ref MappedLocalHandle); |
| 157 | +#pragma warning restore CS0618 // Type or member is obsolete |
| 158 | + |
| 159 | + |
| 160 | +#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE |
| 161 | + // Ensure the SceneHandles are valid to be serialized |
| 162 | + if (serializer.IsWriter) |
| 163 | + { |
| 164 | + if (m_LocalHandle.IsEmpty() && LocalSceneHandle != SceneHandle.None) |
| 165 | + { |
| 166 | + m_LocalHandle = LocalSceneHandle; |
| 167 | + } |
| 168 | + if (m_ServerHandle.IsEmpty() && ServerSceneHandle != SceneHandle.None) |
| 169 | + { |
| 170 | + m_ServerHandle = ServerSceneHandle; |
| 171 | + } |
| 172 | + if (m_MappedLocalHandle.IsEmpty() && MappedLocalSceneHandle != SceneHandle.None) |
| 173 | + { |
| 174 | + m_MappedLocalHandle = MappedLocalSceneHandle; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // Serialize the INetworkSerializable representations |
| 179 | + serializer.SerializeValue(ref m_LocalHandle); |
| 180 | + serializer.SerializeValue(ref m_ServerHandle); |
| 181 | + serializer.SerializeValue(ref m_MappedLocalHandle); |
| 182 | + |
| 183 | + // If we're reading, convert back into the raw SceneHandle |
| 184 | + if (serializer.IsReader) |
| 185 | + { |
| 186 | + ServerSceneHandle = m_ServerHandle; |
| 187 | + ServerSceneHandle = m_LocalHandle; |
| 188 | + ServerSceneHandle = m_MappedLocalHandle; |
| 189 | + } |
| 190 | +#endif |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments