-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvanceNetworkConnection.cs
More file actions
58 lines (46 loc) · 2.35 KB
/
AdvanceNetworkConnection.cs
File metadata and controls
58 lines (46 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class AdvanceNetworkConnection : NetworkConnection
{
public override bool Send(short msgType, MessageBase msg)
{
Type messageType = msg.GetType();
//Because of the problem with child spawning we intercept normal spawns and replace it with our own if we are under the correct conditions
//This is highly illegal never do this
if (messageType.Name == "ObjectSpawnMessage")
{
NetworkInstanceId networkId = (NetworkInstanceId) messageType.GetField("netId").GetValue(msg);
GameObject foundObject = NetworkServer.FindLocalObject(networkId);
if (foundObject == null)
return base.Send(msgType, msg);
ChildIdentity childId = foundObject.GetComponent<ChildIdentity>();
if (childId == null)
return base.Send(msgType, msg);
byte[] payload = (byte[]) messageType.GetField("payload").GetValue(msg);
//Intercept the message with a child message
return base.Send(ChildSpawnMessage.MessageId, new ChildSpawnMessage(childId, payload));
}
return base.Send(msgType, msg);
}
public override bool SendByChannel(short msgType, MessageBase msg, int channelId)
{
Type messageType = msg.GetType();
//Because of the problem with child spawning we intercept normal spawns and replace it with our own if we are under the correct conditions
//This is highly illegal never do this
if (messageType.Name == "ObjectSpawnMessage")
{
NetworkInstanceId networkId = (NetworkInstanceId)messageType.GetField("netId").GetValue(msg);
GameObject foundObject = NetworkServer.FindLocalObject(networkId);
if (foundObject == null)
return base.SendByChannel(msgType, msg, channelId);
ChildIdentity childId = foundObject.GetComponent<ChildIdentity>();
if (childId == null)
return base.SendByChannel(msgType, msg, channelId);
byte[] payload = (byte[])messageType.GetField("payload").GetValue(msg);
return base.SendByChannel(ChildSpawnMessage.MessageId, new ChildSpawnMessage(childId, payload), channelId);
}
return base.SendByChannel(msgType, msg, channelId);
}
}