1+ // NetSim Implementation compilation boilerplate
2+ // All references to UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED should be defined in the same way,
3+ // as any discrepancies are likely to result in build failures
4+ #if UNITY_EDITOR || ( DEVELOPMENT_BUILD && ! UNITY_MP_TOOLS_NETSIM_DISABLED_IN_DEVELOP ) || ( ! DEVELOPMENT_BUILD && UNITY_MP_TOOLS_NETSIM_ENABLED_IN_RELEASE )
5+ #define UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
6+ #endif
7+
18using System ;
29using System . Collections . Generic ;
310using UnityEngine ;
@@ -343,6 +350,9 @@ public struct SimulatorParameters
343350 /// - packet jitter (variances in latency, see: https://en.wikipedia.org/wiki/Jitter)
344351 /// - packet drop rate (packet loss)
345352 /// </summary>
353+ #if UTP_TRANSPORT_2_0_ABOVE
354+ [ Obsolete ( "DebugSimulator is no longer supported and has no effect. Use Network Simulator from the Multiplayer Tools package." , false ) ]
355+ #endif
346356 public SimulatorParameters DebugSimulator = new SimulatorParameters
347357 {
348358 PacketDelayMS = 0 ,
@@ -359,6 +369,10 @@ private struct PacketLossCache
359369 public float PacketLoss ;
360370 } ;
361371
372+ internal static event Action < int , NetworkDriver > TransportInitialized ;
373+ internal static event Action < int > TransportDisposed ;
374+ internal NetworkDriver NetworkDriver => m_Driver ;
375+
362376 private PacketLossCache m_PacketLossCache = new PacketLossCache ( ) ;
363377
364378 private State m_State = State . Disconnected ;
@@ -402,6 +416,8 @@ private void InitDriver()
402416 out m_UnreliableFragmentedPipeline ,
403417 out m_UnreliableSequencedFragmentedPipeline ,
404418 out m_ReliableSequencedPipeline ) ;
419+
420+ TransportInitialized ? . Invoke ( GetInstanceID ( ) , NetworkDriver ) ;
405421 }
406422
407423 private void DisposeInternals ( )
@@ -419,6 +435,8 @@ private void DisposeInternals()
419435 }
420436
421437 m_SendQueue . Clear ( ) ;
438+
439+ TransportDisposed ? . Invoke ( GetInstanceID ( ) ) ;
422440 }
423441
424442 private NetworkPipeline SelectSendPipeline ( NetworkDelivery delivery )
@@ -652,6 +670,9 @@ public void SetConnectionData(NetworkEndpoint endPoint, NetworkEndpoint listenEn
652670 /// <param name="packetDelay">Packet delay in milliseconds.</param>
653671 /// <param name="packetJitter">Packet jitter in milliseconds.</param>
654672 /// <param name="dropRate">Packet drop percentage.</param>
673+ #if UTP_TRANSPORT_2_0_ABOVE
674+ [ Obsolete ( "SetDebugSimulatorParameters is no longer supported and has no effect. Use Network Simulator from the Multiplayer Tools package." , false ) ]
675+ #endif
655676 public void SetDebugSimulatorParameters ( int packetDelay , int packetJitter , int dropRate )
656677 {
657678 if ( m_Driver . IsCreated )
@@ -1329,7 +1350,25 @@ public override void Shutdown()
13291350 m_ServerClientId = 0 ;
13301351 }
13311352
1332- private void ConfigureSimulator ( )
1353+ #if UTP_TRANSPORT_2_0_ABOVE
1354+ private void ConfigureSimulatorForUtp2 ( )
1355+ {
1356+ // As DebugSimulator is deprecated, the 'packetDelayMs', 'packetJitterMs' and 'packetDropPercentage'
1357+ // parameters are set to the default and are supposed to be changed using Network Simulator tool instead.
1358+ m_NetworkSettings . WithSimulatorStageParameters (
1359+ maxPacketCount : 300 , // TODO Is there any way to compute a better value?
1360+ maxPacketSize : NetworkParameterConstants . MTU ,
1361+ packetDelayMs : 0 ,
1362+ packetJitterMs : 0 ,
1363+ packetDropPercentage : 0 ,
1364+ randomSeed : DebugSimulatorRandomSeed ?? ( uint ) System . Diagnostics . Stopwatch . GetTimestamp ( )
1365+ , mode : ApplyMode . AllPackets
1366+ ) ;
1367+
1368+ m_NetworkSettings . WithNetworkSimulatorParameters ( ) ;
1369+ }
1370+ #else
1371+ private void ConfigureSimulatorForUtp1 ( )
13331372 {
13341373 m_NetworkSettings . WithSimulatorStageParameters (
13351374 maxPacketCount : 300 , // TODO Is there any way to compute a better value?
@@ -1338,11 +1377,9 @@ private void ConfigureSimulator()
13381377 packetJitterMs : DebugSimulator . PacketJitterMS ,
13391378 packetDropPercentage : DebugSimulator . PacketDropRate ,
13401379 randomSeed : DebugSimulatorRandomSeed ?? ( uint ) System . Diagnostics . Stopwatch . GetTimestamp ( )
1341- #if UTP_TRANSPORT_2_0_ABOVE
1342- , mode : ApplyMode . AllPackets
1343- #endif
13441380 ) ;
13451381 }
1382+ #endif
13461383
13471384 /// <summary>
13481385 /// Creates the internal NetworkDriver
@@ -1357,14 +1394,14 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
13571394 out NetworkPipeline unreliableSequencedFragmentedPipeline ,
13581395 out NetworkPipeline reliableSequencedPipeline )
13591396 {
1360- #if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1361- #if ! UTP_TRANSPORT_2_0_ABOVE
1397+ #if MULTIPLAYER_TOOLS_1_0_0_PRE_7 && ! UTP_TRANSPORT_2_0_ABOVE
13621398 NetworkPipelineStageCollection . RegisterPipelineStage ( new NetworkMetricsPipelineStage ( ) ) ;
13631399#endif
1364- #endif
13651400
1366- #if UNITY_EDITOR || DEVELOPMENT_BUILD
1367- ConfigureSimulator ( ) ;
1401+ #if UTP_TRANSPORT_2_0_ABOVE && UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
1402+ ConfigureSimulatorForUtp2 ( ) ;
1403+ #elif ! UTP_TRANSPORT_2_0_ABOVE && ( UNITY_EDITOR || DEVELOPMENT_BUILD )
1404+ ConfigureSimulatorForUtp1 ( ) ;
13681405#endif
13691406
13701407 m_NetworkSettings . WithNetworkConfigParameters (
@@ -1395,42 +1432,53 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
13951432 driver = NetworkDriver . Create ( m_NetworkSettings ) ;
13961433#endif
13971434
1398- #if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1399- #if UTP_TRANSPORT_2_0_ABOVE
1400- driver . RegisterPipelineStage < NetworkMetricsPipelineStage > ( new NetworkMetricsPipelineStage ( ) ) ;
1435+ #if MULTIPLAYER_TOOLS_1_0_0_PRE_7 && UTP_TRANSPORT_2_0_ABOVE
1436+ driver . RegisterPipelineStage ( new NetworkMetricsPipelineStage ( ) ) ;
14011437#endif
1438+
1439+ #if ! UTP_TRANSPORT_2_0_ABOVE
1440+ SetupPipelinesForUtp1 ( driver ,
1441+ out unreliableFragmentedPipeline ,
1442+ out unreliableSequencedFragmentedPipeline ,
1443+ out reliableSequencedPipeline ) ;
1444+ #else
1445+ SetupPipelinesForUtp2 ( driver ,
1446+ out unreliableFragmentedPipeline ,
1447+ out unreliableSequencedFragmentedPipeline ,
1448+ out reliableSequencedPipeline ) ;
14021449#endif
1450+ }
14031451
1452+ #if ! UTP_TRANSPORT_2_0_ABOVE
1453+ private void SetupPipelinesForUtp1 ( NetworkDriver driver ,
1454+ out NetworkPipeline unreliableFragmentedPipeline ,
1455+ out NetworkPipeline unreliableSequencedFragmentedPipeline ,
1456+ out NetworkPipeline reliableSequencedPipeline )
1457+ {
14041458#if UNITY_EDITOR || DEVELOPMENT_BUILD
14051459 if ( DebugSimulator . PacketDelayMS > 0 || DebugSimulator . PacketDropRate > 0 )
14061460 {
14071461 unreliableFragmentedPipeline = driver . CreatePipeline (
14081462 typeof ( FragmentationPipelineStage ) ,
1409- typeof ( SimulatorPipelineStage )
1410- #if ! UTP_TRANSPORT_2_0_ABOVE
1411- , typeof ( SimulatorPipelineStageInSend )
1412- #endif
1463+ typeof ( SimulatorPipelineStage ) ,
1464+ typeof ( SimulatorPipelineStageInSend )
14131465#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
14141466 , typeof ( NetworkMetricsPipelineStage )
14151467#endif
14161468 ) ;
14171469 unreliableSequencedFragmentedPipeline = driver . CreatePipeline (
14181470 typeof ( FragmentationPipelineStage ) ,
14191471 typeof ( UnreliableSequencedPipelineStage ) ,
1420- typeof ( SimulatorPipelineStage )
1421- #if ! UTP_TRANSPORT_2_0_ABOVE
1422- , typeof ( SimulatorPipelineStageInSend )
1423- #endif
1472+ typeof ( SimulatorPipelineStage ) ,
1473+ typeof ( SimulatorPipelineStageInSend )
14241474#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
14251475 , typeof ( NetworkMetricsPipelineStage )
14261476#endif
14271477 ) ;
14281478 reliableSequencedPipeline = driver . CreatePipeline (
14291479 typeof ( ReliableSequencedPipelineStage ) ,
1430- typeof ( SimulatorPipelineStage )
1431- #if ! UTP_TRANSPORT_2_0_ABOVE
1432- , typeof ( SimulatorPipelineStageInSend )
1433- #endif
1480+ typeof ( SimulatorPipelineStage ) ,
1481+ typeof ( SimulatorPipelineStageInSend )
14341482#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
14351483 , typeof ( NetworkMetricsPipelineStage )
14361484#endif
@@ -1460,7 +1508,45 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
14601508 ) ;
14611509 }
14621510 }
1511+ #else
1512+ private void SetupPipelinesForUtp2 ( NetworkDriver driver ,
1513+ out NetworkPipeline unreliableFragmentedPipeline ,
1514+ out NetworkPipeline unreliableSequencedFragmentedPipeline ,
1515+ out NetworkPipeline reliableSequencedPipeline )
1516+ {
14631517
1518+ unreliableFragmentedPipeline = driver . CreatePipeline (
1519+ typeof ( FragmentationPipelineStage )
1520+ #if UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
1521+ , typeof ( SimulatorPipelineStage )
1522+ #endif
1523+ #if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1524+ , typeof ( NetworkMetricsPipelineStage )
1525+ #endif
1526+ ) ;
1527+
1528+ unreliableSequencedFragmentedPipeline = driver . CreatePipeline (
1529+ typeof ( FragmentationPipelineStage ) ,
1530+ typeof ( UnreliableSequencedPipelineStage )
1531+ #if UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
1532+ , typeof ( SimulatorPipelineStage )
1533+ #endif
1534+ #if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1535+ , typeof ( NetworkMetricsPipelineStage )
1536+ #endif
1537+ ) ;
1538+
1539+ reliableSequencedPipeline = driver . CreatePipeline (
1540+ typeof ( ReliableSequencedPipelineStage )
1541+ #if UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
1542+ , typeof ( SimulatorPipelineStage )
1543+ #endif
1544+ #if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1545+ , typeof ( NetworkMetricsPipelineStage )
1546+ #endif
1547+ ) ;
1548+ }
1549+ #endif
14641550 // -------------- Utility Types -------------------------------------------------------------------------------
14651551
14661552
0 commit comments