/// <inheritdoc/> protected override BytesStack GetSnapshot() { var bytesStack = new BytesStack(); var parameters = CachedAnimator.parameters; for (var i = 0; i < parameters.Length; i++) { var parameter = parameters[i]; switch (parameter.type) { case AnimatorControllerParameterType.Float: bytesStack.PushFloat(CachedAnimator.GetFloat(parameter.name)); bytesStack.PushString(parameter.name); break; case AnimatorControllerParameterType.Int: bytesStack.PushInt(CachedAnimator.GetInteger(parameter.name)); bytesStack.PushString(parameter.name); break; case AnimatorControllerParameterType.Bool: bytesStack.PushBool(CachedAnimator.GetBool(parameter.name)); bytesStack.PushString(parameter.name); break; } } return(bytesStack); }
private void Update() { TimeOfDayCycle(); UpdateRain(); UpdateWet(); UpdateFog(); UpdateClouds(); UpdateSunPosition(); //UpdateMoonPosition(); if (SimulatorManager.Instance.Network.IsMaster) { var masterManager = SimulatorManager.Instance.Network.Master; if (state.Fog != fog || state.Rain != rain || state.Wet != wet || state.Cloud != cloud || state.TimeOfDay != currentTimeOfDay) { state.Fog = fog; state.Rain = rain; state.Wet = wet; state.Cloud = cloud; state.TimeOfDay = currentTimeOfDay; var message = new BytesStack(masterManager.PacketsProcessor.Write(state), false); masterManager.BroadcastMessage(new Message(masterManager.Key, message, MessageType.ReliableUnordered)); } } }
public void Control(List <ControlAction> controlActions) { var fixedUpdateManager = SimulatorManager.Instance.FixedUpdateManager; if (SignalCoroutine != null) { fixedUpdateManager.StopCoroutine(SignalCoroutine); SignalCoroutine = null; } SignalCoroutine = fixedUpdateManager.StartCoroutine(SignalLoop(controlActions)); if (SimulatorManager.Instance.Network.IsMaster && controlActions.Count > 0) { //Forward control actions to clients var serializedControlActions = new BytesStack(); for (var i = 0; i < controlActions.Count; i++) { var controlAction = controlActions[i]; serializedControlActions.PushString(controlAction.Action); serializedControlActions.PushString(controlAction.Value); } BroadcastMessage(new Message(Key, serializedControlActions, MessageType.ReliableOrdered)); } }
public static void Vector3CompressionTest( float x, float y, float z, float minElementValue, float maxElementValue, int bytesPerElement) { var vector3 = new Vector3(x, y, z); var precision = (maxElementValue - minElementValue) / (1 << (bytesPerElement * 8)); var bytesStack = new BytesStack(ByteCompression.PositionRequiredBytes); bytesStack.PushCompressedVector3(vector3, minElementValue, maxElementValue, bytesPerElement); var resultVector3 = bytesStack.PopDecompressedVector3(minElementValue, maxElementValue, bytesPerElement); Assert.True( Mathf.Abs(vector3.x - resultVector3.x) <= precision, $"Compression-decompression operation of the vector3 value X result exceeds precision. Tested vector3: {vector3}, result vector3: {resultVector3}."); Assert.True( Mathf.Abs(vector3.y - resultVector3.y) <= precision, $"Compression-decompression operation of the vector3 value Y result exceeds precision. Tested vector3: {vector3}, result vector3: {resultVector3}."); Assert.True( Mathf.Abs(vector3.z - resultVector3.z) <= precision, $"Compression-decompression operation of the vector3 value Z result exceeds precision. Tested vector3: {vector3}, result vector3: {resultVector3}."); }
public void PushPeekPopBytesTests(byte[] testCase) { var bytesStack = new BytesStack(testCase.Length); //Test bytes array at once bytesStack.PushBytes(testCase); var result = bytesStack.PeekBytes(testCase.Length); Assert.True(result.SequenceEqual(testCase), "Peek of the bytes array at once returns different data than was pushed."); result = bytesStack.PopBytes(testCase.Length); Assert.True(result.SequenceEqual(testCase), "Pop of the bytes array at once returns different data than was pushed."); //Test bytes array one by one for (var i = 0; i < testCase.Length; i++) { bytesStack.PushByte(testCase[i]); } for (var i = testCase.Length - 1; i >= 0; i--) { result[i] = bytesStack.PeekByte(testCase.Length - 1 - i); } Assert.True(result.SequenceEqual(testCase), "Pop of the bytes array one by one returns different data than was pushed."); for (var i = testCase.Length - 1; i >= 0; i--) { result[i] = bytesStack.PopByte(); } Assert.True(result.SequenceEqual(testCase), "Pop of the bytes array one by one returns different data than was pushed."); }
/// <inheritdoc/> protected override bool PushSnapshot(BytesStack messageContent) { //Reverse order when writing to the stack var thisTransform = transform; var localRotation = thisTransform.localRotation; var localPosition = thisTransform.localPosition; var localScale = thisTransform.localScale; try { messageContent.PushUncompressedVector3(localScale); messageContent.PushCompressedRotation(localRotation); messageContent.PushCompressedPosition(localPosition); lastSentSnapshot.LocalRotation = localRotation; } catch (ArgumentException exception) { Debug.LogError($"{GetType().Name} could not push a snapshot. Exception: {exception.Message}."); return(false); } lastSentSnapshot.LocalPosition = localPosition; lastSentSnapshot.LocalScale = localScale; return(true); }
private BytesStack GetDespawnMessage(int orderNumber) { var bytesStack = new BytesStack(); bytesStack.PushInt(orderNumber, 2); bytesStack.PushEnum <NPCManagerCommandType>((int)NPCManagerCommandType.DespawnNPC); return(bytesStack); }
/// <summary> /// Single compress and decompress rotation test /// </summary> /// <param name="rotation">Rotation to be tested</param> private static void RotationCompressionTest(Quaternion rotation) { var bytesStack = new BytesStack(ByteCompression.RotationMaxRequiredBytes); bytesStack.PushCompressedRotation(rotation); var resultRotation = bytesStack.PopDecompressedRotation(); Assert.True(AreQuaternionsApproximate(rotation, resultRotation, ByteCompression.RotationPrecision), $"Compression-decompression operation of the rotation result exceeds rotation precision. Tested rotation: {rotation}, result rotation: {resultRotation}."); }
/// <summary> /// Constructs a /// </summary> /// <param name="prefabId"></param> /// <param name="relativePath"></param> /// <param name="newObjectName"></param> /// <returns></returns> private BytesStack GetInstantiationMessage(int prefabId, string relativePath, string newObjectName) { var bytesStack = new BytesStack(); bytesStack.PushString(newObjectName); bytesStack.PushString(relativePath); bytesStack.PushInt(prefabId, 4); bytesStack.PushInt((int)DistributedRootCommandType.InstantiateDistributedObject, CommandTypeRequiredBytes); return(bytesStack); }
private BytesStack GetSpawnMessage(string pedestrianName, int modelIndex, Vector3 position, Quaternion rotation) { var bytesStack = new BytesStack(); bytesStack.PushCompressedRotation(rotation); bytesStack.PushCompressedPosition(position); bytesStack.PushInt(modelIndex, 2); bytesStack.PushString(pedestrianName); bytesStack.PushEnum <PedestrianManagerCommandType>((int)PedestrianManagerCommandType.SpawnPedestrian); return(bytesStack); }
public void FloatCompressionTests(float value, float minValue, float maxValue, int bytesCount) { var precision = (maxValue - minValue) / (1 << (bytesCount * 8)); var bytesStack = new BytesStack(ByteCompression.PositionRequiredBytes); bytesStack.PushInt(ByteCompression.CompressFloatToInt(value, minValue, maxValue, bytesCount), bytesCount); var resultFloat = ByteCompression.DecompressFloatFromInt(bytesStack.PopInt(bytesCount), minValue, maxValue, bytesCount); Assert.True(Mathf.Abs(value - resultFloat) <= precision, $"Compression-decompression operation of the float value result exceeds precision. Tested float: {value}, result float: {resultFloat}, precision {precision}."); }
public void PushPeekPopBoolTests(bool value) { var bytesStack = new BytesStack(1); bytesStack.PushBool(value); var result = bytesStack.PeekBool(); Assert.True(value == result, "PeekBool returns different value than was pushed."); result = bytesStack.PopBool(); Assert.True(value == result, "PopBool returns different value than was pushed."); Assert.True(bytesStack.Count == 0, "BytesStack is not empty after PopBool alone bool."); }
/// <summary> /// Tests push and pop methods for a single object /// </summary> /// <param name="value">Object value to be tested</param> public void PushPeekPopObjectTest(object value) { var bytesStack = new BytesStack(1); bytesStack.PushObject(value); var result = bytesStack.PeekObject(); Assert.True(value.Equals(result), "PeekObject returns different value than was pushed."); result = bytesStack.PopObject(); Assert.True(value.Equals(result), "PopObject returns different value than was pushed."); Assert.True(bytesStack.Count == 0, "BytesStack is not empty after PopObject alone bool."); }
/// <summary> /// Tests push and pop methods for strings using given encoding /// </summary> /// <param name="value">String value to be tested</param> /// <param name="encoding">Used encoding</param> private static void PushPeekPopStringTest(string value, Encoding encoding) { var bytesStack = new BytesStack(4 + (value?.Length ?? 0)); bytesStack.PushString(value, encoding); var result = bytesStack.PeekString(encoding); Assert.True(value == result, $"PeekString returns different value than was pushed when using {encoding} encoding."); result = bytesStack.PopString(encoding); Assert.True(value == result, $"PopString returns different value than was pushed when using {encoding} encoding."); Assert.True(bytesStack.Count == 0, "BytesStack is not empty after PopString alone string."); }
public void PushPeekPopIntTests(uint value) { var bytesCount = ByteCompression.RequiredBytes(value); var bytesStack = new BytesStack(bytesCount); bytesStack.PushUint(value, bytesCount); var result = bytesStack.PeekUint(bytesCount); Assert.True(value == result, "PeekUint returns different value than was pushed."); result = bytesStack.PopUint(bytesCount); Assert.True(value == result, "PopUint returns different value than was pushed."); Assert.True(bytesStack.Count == 0, "BytesStack is not empty after PopUint alone integer."); }
public void PushPeekPopDoubleTests(double value) { var bytesStack = new BytesStack(8); bytesStack.PushDouble(value); var result = bytesStack.PeekDouble(); const float tolerance = 0.00001f; Assert.True(Math.Abs(value - result) < tolerance, "PeekDouble returns different value than was pushed."); result = bytesStack.PopDouble(); Assert.True(Math.Abs(value - result) < tolerance, "PopDouble returns different value than was pushed."); Assert.True(bytesStack.Count == 0, "BytesStack is not empty after PopDouble alone float."); }
public void PushPeekPopLongTests(long value) { var bytesCount = ByteCompression.RequiredBytes(value); var bytesStack = new BytesStack(bytesCount); bytesStack.PushLong(value, bytesCount); var result = bytesStack.PeekLong(bytesCount); Assert.True(value == result, "PeekLong returns different value than was pushed."); result = bytesStack.PopLong(bytesCount); Assert.True(value == result, "PopLong returns different value than was pushed."); Assert.True(bytesStack.Count == 0, "BytesStack is not empty after PopLong alone long."); }
/// <summary> /// Sets the parameter's float value and sends it to corresponding mocks /// </summary> /// <param name="parameterName">The parameter name</param> /// <param name="value">The new parameter value</param> public void SetFloat(string parameterName, float value) { CachedAnimator.SetFloat(parameterName, value); if (!IsInitialized) { return; } var bytesStack = new BytesStack(); bytesStack.PushFloat(value); bytesStack.PushString(parameterName); bytesStack.PushEnum <AnimatorCommandType>((int)AnimatorCommandType.SetFloatByName); SendDelta(bytesStack, MessageType.ReliableSequenced); }
/// <summary> /// Gets ids register command for the identified object in the bytes stack message /// </summary> /// <param name="commandType">Command type</param> /// <param name="identifiedObject">Identified object which is command target</param> /// <returns>Register command in bytes stack</returns> private Message GetCommandMessage(IdsRegisterCommandType commandType, IIdentifiedObject identifiedObject) { var bytesStack = new BytesStack(); var id = ResolveId(identifiedObject); if (id == null) { throw new ArgumentException("Usage of the unregistered identified object."); } bytesStack.PushInt(id.Value, BytesPerId); bytesStack.PushString(identifiedObject.Key); bytesStack.PushInt((int)commandType, BytesPerCommandType); return(new Message(Key, bytesStack, MessageType.ReliableOrdered)); }
/// <inheritdoc/> protected override BytesStack GetSnapshot() { //Reverse order when writing to the stack var bytesStack = new BytesStack(ByteCompression.PositionRequiredBytes + ByteCompression.RotationMaxRequiredBytes); var thisTransform = transform; var localRotation = thisTransform.localRotation; var localPosition = thisTransform.localPosition; bytesStack.PushCompressedRotation(localRotation); bytesStack.PushCompressedPosition(localPosition); lastSentSnapshot.LocalRotation = localRotation; lastSentSnapshot.LocalPosition = localPosition; return(bytesStack); }
public static void PositionCompressionTest(float x, float y, float z) { var position = new Vector3(x, y, z); var bytesStack = new BytesStack(ByteCompression.PositionRequiredBytes); bytesStack.PushCompressedPosition(position); var resultPosition = bytesStack.PopDecompressedPosition(); Assert.True(Mathf.Abs(position.x - resultPosition.x) <= ByteCompression.PositionPrecision, $"Compression-decompression operation of the position value X result exceeds position precision. Tested position: {position}, result position: {resultPosition}."); Assert.True(Mathf.Abs(position.y - resultPosition.y) <= ByteCompression.PositionPrecision, $"Compression-decompression operation of the position value Y result exceeds position precision. Tested position: {position}, result position: {resultPosition}."); Assert.True(Mathf.Abs(position.z - resultPosition.z) <= ByteCompression.PositionPrecision, $"Compression-decompression operation of the position value Z result exceeds position precision. Tested position: {position}, result position: {resultPosition}."); }
/// <inheritdoc/> protected override void PushSnapshot(BytesStack messageContent) { //Reverse order when writing to the stack var thisTransform = transform; var localRotation = thisTransform.localRotation; var localPosition = thisTransform.localPosition; var localScale = thisTransform.localScale; messageContent.PushUncompressedVector3(localScale); messageContent.PushCompressedRotation(localRotation); messageContent.PushCompressedPosition(localPosition); lastSentSnapshot.LocalRotation = localRotation; lastSentSnapshot.LocalPosition = localPosition; lastSentSnapshot.LocalScale = localScale; }
private BytesStack GetSpawnMessage(string vehicleId, NPCS npcData, int npcControllerSeed, Color color, Vector3 position, Quaternion rotation) { var bytesStack = new BytesStack(); var indexOfPrefab = NPCVehicles.FindIndex(npc => npc.Equals(npcData)); bytesStack.PushCompressedRotation(rotation); bytesStack.PushCompressedPosition(position); bytesStack.PushCompressedColor(color, 1); bytesStack.PushInt(npcControllerSeed); bytesStack.PushInt(indexOfPrefab, 2); bytesStack.PushString(vehicleId); bytesStack.PushEnum <NPCManagerCommandType>((int)NPCManagerCommandType.SpawnNPC); return(bytesStack); }
public void SendError(ICommand source, string message) { if (Loader.Instance.Network.IsClient) { var distributedMessage = MessagesPool.Instance.GetMessage(4 + BytesStack.GetMaxByteCount(message)); distributedMessage.AddressKey = Key; distributedMessage.Content.PushString(message); distributedMessage.Content.PushEnum <MessageType>((int)MessageType.Error); distributedMessage.Type = DistributedMessageType.ReliableOrdered; BroadcastMessage(distributedMessage); return; } SendError(message); }
/// <inheritdoc/> public void Control(List <ControlAction> controlActions) { HandleControlActions(controlActions); if (SimulatorManager.Instance.Network.IsMaster && controlActions.Count > 0) { //Forward control actions to clients var serializedControlActions = new BytesStack(); for (var i = 0; i < controlActions.Count; i++) { var controlAction = controlActions[i]; serializedControlActions.PushString(controlAction.Action); serializedControlActions.PushString(controlAction.Value); } BroadcastMessage(new Message(Key, serializedControlActions, MessageType.ReliableOrdered)); } }
public void UnicastInitialMessages(IPEndPoint endPoint) { foreach (var sensorMetaData in sensors) { if (Equals(sensorMetaData.HostEndPoint, endPoint) && !string.IsNullOrEmpty(sensorMetaData.UID)) { var message = MessagesPool.Instance.GetMessage( BytesStack.GetMaxByteCount(sensorMetaData.UID) + BytesStack.GetMaxByteCount(sensorMetaData.HierarchyPath)); message.AddressKey = Key; message.Content.PushString(sensorMetaData.UID); message.Content.PushString(sensorMetaData.HierarchyPath); message.Type = DistributedMessageType.ReliableOrdered; BroadcastMessage(message); } } }
/// <summary> /// Tests <see cref="ByteCompression.PushCompressedPosition"/> and <see cref="ByteCompression.PopDecompressedPosition"/> methods on a single position /// </summary> /// <param name="position">Position to be tested</param> private static void TestSinglePosition(Vector3 position) { var bytesStack = new BytesStack(ByteCompression.PositionRequiredBytes); bytesStack.PushCompressedPosition(position); var resultPosition = bytesStack.PopDecompressedPosition(); Assert.True( Mathf.Abs(position.x - resultPosition.x) <= ByteCompression.PositionPrecision, $"Compression-decompression operation of the position value X result exceeds position precision. Tested position: {position}, result position: {resultPosition}."); Assert.True( Mathf.Abs(position.y - resultPosition.y) <= ByteCompression.PositionPrecision, $"Compression-decompression operation of the position value Y result exceeds position precision. Tested position: {position}, result position: {resultPosition}."); Assert.True( Mathf.Abs(position.z - resultPosition.z) <= ByteCompression.PositionPrecision, $"Compression-decompression operation of the position value Z result exceeds position precision. Tested position: {position}, result position: {resultPosition}."); }
public static void UncompressedPositionTest(float x, float y, float z) { var position = new Vector3(x, y, z); var bytesStack = new BytesStack(3 * 4); bytesStack.PushUncompressedVector3(position); var resultPosition = bytesStack.PopUncompressedVector3(); Assert.True( Mathf.Abs(position.x - resultPosition.x) <= Mathf.Epsilon, $"Decoding operation of the position value X result exceeds epsilon. Tested position: {position}, result position: {resultPosition}."); Assert.True( Mathf.Abs(position.y - resultPosition.y) <= Mathf.Epsilon, $"Decoding operation of the position value Y result exceeds epsilon. Tested position: {position}, result position: {resultPosition}."); Assert.True( Mathf.Abs(position.z - resultPosition.z) <= Mathf.Epsilon, $"Decoding operation of the position value Z result exceeds epsilon. Tested position: {position}, result position: {resultPosition}."); }
public void ColorCompressionTests(float r, float g, float b, float a, int bytesPerElement) { var color = new Color(r, g, b, a); var precision = (1.0f - 0.0f) / (1 << (bytesPerElement * 8)); var bytesStack = new BytesStack(ByteCompression.PositionRequiredBytes); bytesStack.PushCompressedColor(color, bytesPerElement); var resultVector3 = bytesStack.PopDecompressedColor(bytesPerElement); Assert.True(Mathf.Abs(color.r - resultVector3.r) <= precision, $"Compression-decompression operation of the color value R result exceeds precision. Tested color: {color}, result color: {resultVector3}."); Assert.True(Mathf.Abs(color.g - resultVector3.g) <= precision, $"Compression-decompression operation of the color value G result exceeds precision. Tested color: {color}, result color: {resultVector3}."); Assert.True(Mathf.Abs(color.b - resultVector3.b) <= precision, $"Compression-decompression operation of the color value B result exceeds precision. Tested color: {color}, result color: {resultVector3}."); Assert.True(Mathf.Abs(color.a - resultVector3.a) <= precision, $"Compression-decompression operation of the color value A result exceeds precision. Tested color: {color}, result color: {resultVector3}."); }
/// <summary> /// Creates initialization message if this register assigns ids /// </summary> /// <returns>Initialization message</returns> /// <exception cref="ArgumentException">Cannot create initial message in register which does not assign ids.</exception> private Message GetInitializationMessage() { if (!assignIds) { throw new ArgumentException("Cannot create initial message in register which does not assign ids."); } var bytesStack = new BytesStack(); var id = ResolveId(this); if (id == null) { throw new ArgumentException("Usage of the unregistered identified object."); } bytesStack.PushLong(messagesManager.TimeManager.GetTimeDifference(InternalIdBindUtcTime)); bytesStack.PushInt(id.Value, BytesPerId); bytesStack.PushString(Key); bytesStack.PushInt((int)IdsRegisterCommandType.BindIdAndKey, BytesPerCommandType); return(new Message(Key, bytesStack, MessageType.ReliableOrdered)); }