converted some magic numbers to message types, added a custom message type, added editor button for scene ids

BinaryServer
Anton Franzluebbers 2022-01-30 17:04:37 -05:00
parent e837b624cc
commit d084977402
6 changed files with 135 additions and 12 deletions

View File

@ -0,0 +1,48 @@
#if UNITY_EDITOR
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace VelNet.Editor
{
public class EditorUtils : MonoBehaviour
{
[MenuItem("VelNet/Check For Duplicate NetworkIds", false, 10)]
private static void CheckDuplicateNetworkIds()
{
NetworkObject[] objs = FindObjectsOfType<NetworkObject>();
Dictionary<int, NetworkObject> ids = new Dictionary<int, NetworkObject>();
foreach (NetworkObject o in objs)
{
if (!o.isSceneObject) continue;
if (ids.ContainsKey(o.sceneNetworkId) || o.sceneNetworkId < 100)
{
if (ids.ContainsKey(o.sceneNetworkId))
{
Debug.Log($"Found duplicated id: {o.name} {ids[o.sceneNetworkId].name}", o);
}
else
{
Debug.Log($"Found duplicated id: {o.name} {o.sceneNetworkId}", o);
}
o.sceneNetworkId = 100;
while (ids.ContainsKey(o.sceneNetworkId))
{
o.sceneNetworkId += 1;
}
PrefabUtility.RecordPrefabInstancePropertyModifications(o);
EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
}
ids.Add(o.sceneNetworkId, o);
}
}
}
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8f2f5f489d44f614c96bcf8f493c787d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,18 @@
{
"name": "VelNet.Editor",
"rootNamespace": "VelNet.Editor",
"references": [
"GUID:1e55e2c4387020247a1ae212bbcbd381"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: ae0703a992a8fe347978b1cd2dd2d7a9
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -27,13 +27,14 @@ namespace VelNet
MESSAGE_SETGROUP = 6 MESSAGE_SETGROUP = 6
}; };
public enum MessageType public enum MessageType : byte
{ {
ObjectSync, ObjectSync,
TakeOwnership, TakeOwnership,
Instantiate, Instantiate,
Destroy, Destroy,
DeleteSceneObjects DeleteSceneObjects,
Custom
} }
public string host; public string host;
@ -80,6 +81,7 @@ namespace VelNet
public static Action<RoomsMessage> RoomsReceived; public static Action<RoomsMessage> RoomsReceived;
public static Action<Message> MessageReceived; public static Action<Message> MessageReceived;
public static Action<byte[]> CustomMessageReceived;
#endregion #endregion
@ -438,9 +440,17 @@ namespace VelNet
sceneObjects[i].networkId = -1 + "-" + sceneObjects[i].sceneNetworkId; sceneObjects[i].networkId = -1 + "-" + sceneObjects[i].sceneNetworkId;
sceneObjects[i].owner = masterPlayer; sceneObjects[i].owner = masterPlayer;
sceneObjects[i].isSceneObject = true; // needed for special handling when deleted sceneObjects[i].isSceneObject = true; // needed for special handling when deleted
if (objects.ContainsKey(sceneObjects[i].networkId))
{
Debug.LogError($"Duplicate NetworkID: {sceneObjects[i].networkId} {sceneObjects[i].name} {objects[sceneObjects[i].networkId]}");
}
else
{
objects.Add(sceneObjects[i].networkId, sceneObjects[i]); objects.Add(sceneObjects[i].networkId, sceneObjects[i]);
} }
} }
}
else else
{ {
masterPlayer = players[cm.masterId]; masterPlayer = players[cm.masterId];
@ -528,12 +538,12 @@ namespace VelNet
{ {
//read a byte //read a byte
byte type = (byte)stream.ReadByte(); MessageSendType type = (MessageSendType)stream.ReadByte();
switch (type) switch (type)
{ {
//login //login
case 0: case MessageSendType.MESSAGE_LOGIN:
{ {
LoginMessage m = new LoginMessage(); LoginMessage m = new LoginMessage();
m.userId = GetIntFromBytes(ReadExact(stream, 4)); //not really the sender... m.userId = GetIntFromBytes(ReadExact(stream, 4)); //not really the sender...
@ -541,7 +551,7 @@ namespace VelNet
break; break;
} }
//rooms //rooms
case 1: case MessageSendType.MESSAGE_GETROOMS:
{ {
RoomsMessage m = new RoomsMessage(); RoomsMessage m = new RoomsMessage();
m.rooms = new List<ListedRoom>(); m.rooms = new List<ListedRoom>();
@ -567,7 +577,7 @@ namespace VelNet
break; break;
} }
//joined //joined
case 2: case MessageSendType.MESSAGE_JOINROOM:
{ {
JoinMessage m = new JoinMessage(); JoinMessage m = new JoinMessage();
m.userId = GetIntFromBytes(ReadExact(stream, 4)); m.userId = GetIntFromBytes(ReadExact(stream, 4));
@ -578,7 +588,10 @@ namespace VelNet
break; break;
} }
//data //data
case 3: case MessageSendType.MESSAGE_OTHERS:
// case MessageSendType.MESSAGE_OTHERS_ORDERED:
// case MessageSendType.MESSAGE_ALL:
// case MessageSendType.MESSAGE_ALL_ORDERED:
{ {
DataMessage m = new DataMessage(); DataMessage m = new DataMessage();
m.senderId = GetIntFromBytes(ReadExact(stream, 4)); m.senderId = GetIntFromBytes(ReadExact(stream, 4));
@ -588,7 +601,7 @@ namespace VelNet
break; break;
} }
//new master //new master
case 4: case MessageSendType.MESSAGE_ALL:
{ {
ChangeMasterMessage m = new ChangeMasterMessage(); ChangeMasterMessage m = new ChangeMasterMessage();
m.masterId = GetIntFromBytes(ReadExact(stream, 4)); //sender is the new master m.masterId = GetIntFromBytes(ReadExact(stream, 4)); //sender is the new master
@ -723,7 +736,7 @@ namespace VelNet
byte[] uB = Encoding.UTF8.GetBytes(username); byte[] uB = Encoding.UTF8.GetBytes(username);
byte[] pB = Encoding.UTF8.GetBytes(password); byte[] pB = Encoding.UTF8.GetBytes(password);
writer.Write((byte)0); writer.Write((byte)MessageSendType.MESSAGE_LOGIN);
writer.Write((byte)uB.Length); writer.Write((byte)uB.Length);
writer.Write(uB); writer.Write(uB);
writer.Write((byte)pB.Length); writer.Write((byte)pB.Length);
@ -747,7 +760,7 @@ namespace VelNet
BinaryWriter writer = new BinaryWriter(stream); BinaryWriter writer = new BinaryWriter(stream);
byte[] R = Encoding.UTF8.GetBytes(roomname); byte[] R = Encoding.UTF8.GetBytes(roomname);
writer.Write((byte)2); writer.Write((byte)MessageSendType.MESSAGE_JOINROOM);
writer.Write((byte)R.Length); writer.Write((byte)R.Length);
writer.Write(R); writer.Write(R);
SendTcpMessage(stream.ToArray()); SendTcpMessage(stream.ToArray());
@ -765,6 +778,26 @@ namespace VelNet
} }
} }
public static void SendCustomMessage(byte[] message, bool include_self = false, bool reliable = true, bool ordered = false)
{
using MemoryStream mem = new MemoryStream();
using BinaryWriter writer = new BinaryWriter(mem);
writer.Write((byte)MessageType.Custom);
writer.Write(message.Length);
writer.Write(message);
SendToRoom(mem.ToArray(), include_self, reliable, ordered);
}
public static void SendCustomMessageToGroup(string group, byte[] message,bool reliable = true)
{
using MemoryStream mem = new MemoryStream();
using BinaryWriter writer = new BinaryWriter(mem);
writer.Write((byte)MessageType.Custom);
writer.Write(message.Length);
writer.Write(message);
SendToGroup(group, mem.ToArray(), reliable);
}
internal static void SendToRoom(byte[] message, bool include_self = false, bool reliable = true, bool ordered = false) internal static void SendToRoom(byte[] message, bool include_self = false, bool reliable = true, bool ordered = false)
{ {
byte sendType = (byte)MessageSendType.MESSAGE_OTHERS; byte sendType = (byte)MessageSendType.MESSAGE_OTHERS;

View File

@ -2,6 +2,7 @@ using System.Collections.Generic;
using System; using System;
using System.IO; using System.IO;
using System.Text; using System.Text;
using UnityEngine;
namespace VelNet namespace VelNet
{ {
@ -131,7 +132,12 @@ namespace VelNet
{ {
VelNetManager.NetworkDestroy(reader.ReadString()); VelNetManager.NetworkDestroy(reader.ReadString());
} }
break;
}
case VelNetManager.MessageType.Custom: // custom packets
{
int len = reader.ReadInt32();
VelNetManager.CustomMessageReceived?.Invoke(reader.ReadBytes(len));
break; break;
} }
default: default: