Skip to content

Commit

Permalink
Merge pull request #110 from BasisVR/main
Browse files Browse the repository at this point in the history
Main
  • Loading branch information
dooly123 authored Jan 13, 2025
2 parents 33f7fbd + c47337d commit 95001a6
Show file tree
Hide file tree
Showing 172 changed files with 10,612 additions and 2,865 deletions.
5 changes: 2 additions & 3 deletions Basis Server/BasisNetworkClient/BasisNetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ public static NetPeer StartClient(string IP, int port, ReadyMessage ReadyMessage
UnsyncedEvents = true,
};
client.Start();
NetDataWriter Writer = NetDataWriterPool.GetWriter();
NetDataWriter Writer = new NetDataWriter(true,12);
//this is the only time we dont put key!
Writer.Put(BasisNetworkVersion.ServerVersion);
AuthenticationMessage.Serialize(Writer);
ReadyMessage.Serialize(Writer);
ReadyMessage.Serialize(Writer,false);
peer = client.Connect(IP, port, Writer);
NetDataWriterPool.ReturnWriter(Writer);
return peer;
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.IO.Compression;

namespace Basis.Scripts.BasisSdk.Players
{
[Serializable]
public struct AvatarNetworkLoadInformation
{
public string AvatarBundleUrl;
public string AvatarMetaUrl;
public string UnlockPassword;

/// <summary>
/// Encodes the structure to compressed byte data using custom string serialization and DeflateStream compression.
/// </summary>
public byte[] EncodeToBytes()
{
using var memoryStream = new MemoryStream();
using (var writer = new BinaryWriter(memoryStream))
{
WriteString(writer, AvatarBundleUrl);
WriteString(writer, AvatarMetaUrl);
WriteString(writer, UnlockPassword);
}

byte[] rawData = memoryStream.ToArray();

using var compressedStream = new MemoryStream();
using (var deflateStream = new DeflateStream(compressedStream, System.IO.Compression.CompressionLevel.Optimal, true))
{
deflateStream.Write(rawData, 0, rawData.Length);
}

return compressedStream.ToArray();
}

/// <summary>
/// Decodes from compressed byte data back to the structure using custom string deserialization and DeflateStream decompression.
/// </summary>
public static AvatarNetworkLoadInformation DecodeFromBytes(byte[] compressedData)
{
using var compressedStream = new MemoryStream(compressedData);
using var deflateStream = new DeflateStream(compressedStream, CompressionMode.Decompress);
using var decompressedStream = new MemoryStream();
deflateStream.CopyTo(decompressedStream);

byte[] rawData = decompressedStream.ToArray();

using var memoryStream = new MemoryStream(rawData);
using var reader = new BinaryReader(memoryStream);

return new AvatarNetworkLoadInformation
{
AvatarBundleUrl = ReadString(reader),
AvatarMetaUrl = ReadString(reader),
UnlockPassword = ReadString(reader)
};
}

/// <summary>
/// Writes a string to the BinaryWriter with its length as a ushort.
/// </summary>
private static void WriteString(BinaryWriter writer, string value)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(value ?? string.Empty);
writer.Write((ushort)bytes.Length); // Write the length as a ushort
writer.Write(bytes); // Write the string bytes
}

/// <summary>
/// Reads a string from the BinaryReader based on its length (stored as a ushort).
/// </summary>
private static string ReadString(BinaryReader reader)
{
ushort length = reader.ReadUInt16(); // Read the length
byte[] bytes = reader.ReadBytes(length); // Read the string bytes
return System.Text.Encoding.UTF8.GetString(bytes); // Convert back to a string
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Basis.Network.Core;
using Basis.Scripts.BasisSdk.Players;
using Basis.Scripts.Networking.Compression;
using BasisNetworkClientConsole;
using BasisNetworkCore;
using LiteNetLib;
using LiteNetLib.Utils;
using System.Text;
Expand All @@ -17,8 +17,18 @@ class Program
private static readonly object nameLock = new object(); // To synchronize name generation
public static NetPeer LocalPLayer;

Check warning on line 18 in Basis Server/BasisNetworkClientConsole/BasisNetworkClientConsole/Program.cs

View workflow job for this annotation

GitHub Actions / Build server on Ubuntu (Basis Server, linux)

Non-nullable field 'LocalPLayer' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 18 in Basis Server/BasisNetworkClientConsole/BasisNetworkClientConsole/Program.cs

View workflow job for this annotation

GitHub Actions / Build server on Ubuntu (Basis Server, win)

Non-nullable field 'LocalPLayer' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

public static string Ip = "localhost";//server1.basisvr.org
public static string Ip = "localhost";//server1.basisvr.org //localhost
public static int Port = 4296;

public static byte[] AvatarMessage = new byte[LocalAvatarSyncMessage.AvatarSyncSize];
public static Vector3 Position = new Vector3(0, 0, 0);
public static Quaternion Rotation = new Quaternion(0, 0, 0, 1);
public static float[] FloatArray = new float[LocalAvatarSyncMessage.StoredBones];
public static ushort[] UshortArray = new ushort[LocalAvatarSyncMessage.StoredBones];
private const ushort UShortMin = ushort.MinValue; // 0
private const ushort UShortMax = ushort.MaxValue; // 65535
private const ushort ushortRangeDifference = UShortMax - UShortMin;
public static BasisRangedUshortFloatData RotationCompression = new BasisRangedUshortFloatData(-1f, 1f, 0.001f);
public static void Main(string[] args)
{
// Set up global exception handlers
Expand All @@ -44,22 +54,33 @@ public static void Main(string[] args)
};
RM.playerMetaDataMessage.playerDisplayName = randomPlayerName;
RM.playerMetaDataMessage.playerUUID = randomUUID;

AvatarNetworkLoadInformation ANLI = new AvatarNetworkLoadInformation
{
AvatarMetaUrl = "LoadingAvatar",
AvatarBundleUrl = "LoadingAvatar",
UnlockPassword = "LoadingAvatar"
};
byte[] Bytes = ANLI.EncodeToBytes();
//0 downloading 1 local
RM.clientAvatarChangeMessage = new ClientAvatarChangeMessage
{
byteArray = new byte[0],
loadMode = 2
byteArray = Bytes,
loadMode = 1,//0 is normal
};
RM.localAvatarSyncMessage = new LocalAvatarSyncMessage
{
array = AvatarMessage,
hasAdditionalAvatarData = false,
AdditionalAvatarDatas = null,
};
AuthenticationMessage Authmessage = new AuthenticationMessage
{
bytes = Encoding.UTF8.GetBytes(Password)
};
BasisNetworkClient.AuthenticationMessage = Authmessage;
LocalPLayer = BasisNetworkClient.StartClient(Ip, Port, RM, true);
// BasisNetworkClient.listener.NetworkReceiveEvent += NetworkReceiveEvent;
// BasisNetworkClient.listener.NetworkReceiveEvent += NetworkReceiveEvent;
BNL.Log($"Connecting! Player Name: {randomPlayerName}, UUID: {randomUUID}");
}
catch (Exception ex)
Expand Down Expand Up @@ -90,11 +111,10 @@ public static void Main(string[] args)
// Keep the application running
while (true)
{
SendMovement();
SendMovement();
Thread.Sleep(33);
}
}

private static void NetworkReceiveEvent(NetPeer peer, NetPacketReader Reader, byte channel, DeliveryMethod deliveryMethod)
{

Expand All @@ -104,13 +124,11 @@ private static void NetworkReceiveEvent(NetPeer peer, NetPacketReader Reader, by
if (BasisNetworkCommons.MovementChannel == channel)
{
ServerSideSyncPlayerMessage SSM = new ServerSideSyncPlayerMessage();
SSM.Deserialize(Reader);
SSM.Deserialize(Reader,true);
Reader.Recycle();
NetDataWriter Writer = NetDataWriterPool.GetWriter();
SSM.avatarSerialization.Serialize(Writer);
NetDataWriter Writer = new NetDataWriter(true, 202);
SSM.avatarSerialization.Serialize(Writer, true);
LocalPLayer.Send(Writer, BasisNetworkCommons.MovementChannel, deliveryMethod);

NetDataWriterPool.ReturnWriter(Writer);
}
else
{
Expand Down Expand Up @@ -138,12 +156,6 @@ private static void NetworkReceiveEvent(NetPeer peer, NetPacketReader Reader, by

}
}

public static byte[] AvatarMessage = new byte[LocalAvatarSyncMessage.AvatarSyncSize];
public static Vector3 Position = new Vector3(0,0,0);
public static Quaternion Rotation = new Quaternion(0,0,0,1);
public static float[] FloatArray = new float[LocalAvatarSyncMessage.StoredBones];
public static ushort[] UshortArray = new ushort[LocalAvatarSyncMessage.StoredBones];
public static void SendMovement()
{
if (LocalPLayer != null)
Expand All @@ -166,10 +178,6 @@ public static ushort Compress(float value, float MinValue, float MaxValue, float
float normalized = (value - MinValue) / (valueDiffence); // 0..1
return (ushort)(normalized * ushortRangeDifference);//+ UShortMin (its always zero)
}
private const ushort UShortMin = ushort.MinValue; // 0
private const ushort UShortMax = ushort.MaxValue; // 65535
private const ushort ushortRangeDifference = UShortMax - UShortMin;
public static BasisRangedUshortFloatData RotationCompression = new BasisRangedUshortFloatData(-1f, 1f, 0.001f);
public static void WriteUShortsToBytes(ushort[] values, ref byte[] bytes, ref int offset)
{
EnsureSize(ref bytes, offset + LengthUshortBytes);
Expand Down
11 changes: 9 additions & 2 deletions Basis Server/BasisNetworkCore/BasisPlayerArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ public class BasisPlayerArray

// Reusable snapshot buffer
private static NetPeer[] SnapshotBuffer = new NetPeer[1024];

/// <summary>
/// slow operation but allows us to get a copy of the players quickly later.
/// slow operation can be out of order.
/// </summary>
/// <param name="player"></param>
public static void AddPlayer(NetPeer player)
{
lock (PlayerArrayLock)
Expand All @@ -22,7 +26,10 @@ public static void AddPlayer(NetPeer player)
}
}
}

/// <summary>
/// slow operation can be out of order.
/// </summary>
/// <param name="player"></param>
public static void RemovePlayer(NetPeer player)
{
lock (PlayerArrayLock)
Expand Down
115 changes: 0 additions & 115 deletions Basis Server/BasisNetworkCore/NetDataWriterPool.cs

This file was deleted.

42 changes: 42 additions & 0 deletions Basis Server/BasisNetworkCore/Serializable/AdditionalAvatarData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using LiteNetLib.Utils;
using System;
public static partial class SerializableBasis
{
public struct AdditionalAvatarData
{
public byte messageIndex;
public byte[] array;
public void Deserialize(NetDataReader Writer)
{
int Bytes = Writer.AvailableBytes;
if (Bytes != 0)
{
messageIndex = Writer.GetByte();

byte PayloadSize = Writer.GetByte();
array = new byte[PayloadSize];
Writer.GetBytes(array, PayloadSize);
//89 * 2 = 178 + 12 + 14 = 204
//now 178 for muscles, 3*4 for position 12, 4*4 for rotation 16-2 (W is half) = 204
}
else
{
BNL.LogError($"Unable to read Remaing bytes where {Bytes}");
}
}
public void Serialize(NetDataWriter Writer)
{
if (array == null)
{
BNL.LogError("array was null!!");
}
else
{
Writer.Put(messageIndex);
byte Size = (byte)array.Length;
Writer.Put(Size);
Writer.Put(array);
}
}
}
}
Loading

0 comments on commit 95001a6

Please sign in to comment.