public static void NetDestroy(int netID, Packet.sendType sT = Packet.sendType.buffered, bool destroyImmediate = false, bool cullRelatedPackets = true)
        {
            NetworkObject netObj = NetworkObject.NetObjFromNetID(netID);

            if (netObj == null)
            {
                Debug.LogError("Network Object doesn't exist. ID(" + netID + ")");
                return;
            }

            if (clientID == netObj.ownerID || isServer || netObj.sharedObject)
            {
                //Destroy(netObj.gameObject);

                Packet p = new Packet(Packet.pType.gODestroy, sT, System.BitConverter.GetBytes(cullRelatedPackets));
                p.relatesToNetObjID = netID;
                p.packetOwnerID     = clientID;
                p.tag = NetworkData.instance.TagIDToTagName(NetworkData.instance.networkPrefabList[netObj.prefabDomainID].defaultPacketTagID);
                NetClient.instanceClient.SendPacket(p);

                if (destroyImmediate)
                {
                    if (!UnityPacketHandler.instance.disableBeforeDestroy)
                    {
                        //Pool the object, if it doesn't pool, destroy it.
                        if (NetworkData.instance.ResetPooledObject(netObj) == false)
                        {
                            Destroy(netObj);
                        }
                    }
                    else
                    {
                        if (NetworkData.instance.ResetPooledObject(netObj) == false)
                        {
                            UnityPacketHandler.instance.destroyQueue.Insert(0, netObj.gameObject);
                            netObj.gameObject.SetActive(false);
                        }
                    }
                }
            }
            else
            {
                Debug.Log("You don't have authority over this object.");
            }
        }
示例#2
0
        public void ExecutePacket(Packet curPacket)
        {
            if (curPacket.packetType == Packet.pType.gOInstantiate) //It gets instantiated NetTools.
            {
                NetworkObject             nObj = null;
                GameObjectInstantiateData gOID = ENSSerialization.DeserializeGOID(curPacket.packetData);

                //Debug.Log(NetTools.clientID + ", " + curPacket.packetOwnerID);
                if (NetTools.clientID != curPacket.packetOwnerID || NetworkObject.NetObjFromNetID(gOID.netObjID) == null)
                {
                    if (!curPacket.serverAuthority && NetworkData.instance.networkPrefabList[gOID.prefabDomainID].prefabList[gOID.prefabID].serverInstantiateOnly)
                    {
                        return; //If it is server only, and you aren't the server, don't do it.
                    }

                    GameObject g = NetworkData.instance.RequestPooledObject(gOID.prefabDomainID, gOID.prefabID);
                    if (g == null) //If a pooled object couldn't be found, make a new one.
                    {
                        try
                        {
                            g = Instantiate(NetworkData.instance.networkPrefabList[gOID.prefabDomainID].prefabList[gOID.prefabID].prefab, gOID.position.ToVec3(), gOID.rotation.ToQuaternion());
                        }
                        catch (System.Exception e)
                        {
                            Debug.Log("Error NetInstantiating: domainID: " + gOID.prefabDomainID + ", prefabID: " + gOID.prefabID + e);
                            return;
                        }
                    }
                    else
                    {
                        //If pooled apply required position/rotation/etc.
                        g.transform.position = gOID.position.ToVec3();
                        g.transform.rotation = gOID.rotation.ToQuaternion();
                    }
                    nObj = g.GetComponent <NetworkObject>();
                    if (nObj == null)
                    {
                        nObj = g.AddComponent <NetworkObject>();
                    }

                    foreach (NetworkField defaultField in NetworkData.instance.networkPrefabList[gOID.prefabDomainID].defaultFields)
                    {
                        nObj.fields.Add(defaultField.Clone());
                    }
                    foreach (RPC defaultRPC in NetworkData.instance.networkPrefabList[gOID.prefabDomainID].defaultRpcs)
                    {
                        nObj.rpcs.Add(defaultRPC.Clone());
                    }


                    nObj.ownerID        = curPacket.packetOwnerID;
                    nObj.prefabDomainID = gOID.prefabDomainID;
                    nObj.prefabID       = gOID.prefabID;
                    nObj.networkID      = gOID.netObjID;
                    nObj.sharedObject   = gOID.isShared;

                    nObj.Initialize();
                    //nObj.DoRpcFieldInitialization();

                    if (nObj.onNetworkStart != null)
                    {
                        nObj.onNetworkStart.Invoke();
                    }

                    if (gOID.fieldDefaults != null)
                    {
                        foreach (NetworkFieldPacket nFP in gOID.fieldDefaults)
                        {
                            nFP.networkObjID = gOID.netObjID;
                            //Debug.Log(nFP.data);
                            nObj.SetFieldLocal(nFP.fieldName, nFP.data.ToObject());
                        }
                    }
                }
            }
            else if (curPacket.packetType == Packet.pType.gODestroy)
            {
                //Debug.Log(curPacket.jsonData);
                //Debug.Log(curPacket.GetPacketData());
                //int netID = ENSSerialization.DeserializeInt(curPacket.packetData);
                NetworkObject found = NetworkObject.NetObjFromNetID(curPacket.relatesToNetObjID);
                if (found != null && (found.ownerID == curPacket.packetOwnerID || curPacket.serverAuthority || found.sharedObject))
                {
                    if (disableBeforeDestroy)
                    {
                        if (NetworkData.instance.ResetPooledObject(found) == false)
                        {
                            destroyQueue.Add(found.gameObject);
                        }
                    }
                    else
                    {
                        if (NetworkData.instance.ResetPooledObject(found) == false)
                        {
                            Destroy(found.gameObject);
                        }
                    }

                    if (NetTools.isServer && System.BitConverter.ToBoolean(curPacket.packetData, 0)) //Check NetTools.NetDestroy but basically it is cullRelatedPackets.
                    {
                        NetTools.CullPacketsByNetworkID(curPacket.relatesToNetObjID);
                    }
                }
                else if (found == null)
                {
                    Debug.LogWarning("Couldn't find NetworkObject of ID: " + curPacket.relatesToNetObjID);
                }
            }
            else if (curPacket.packetType == Packet.pType.multiPacket)
            {
                //Debug.Log("Recieved buffered packets.");
                List <byte[]> packetByteInfo = curPacket.GetPacketData <PacketListPacket>().packets;
                lock (packetQueue)
                {
                    foreach (byte[] packetByte in packetByteInfo)
                    {
                        packetQueue.Add(ENSSerialization.DeserializePacket(packetByte));
                    }
                }

                syncingBuffered = true;
            }
            else if (curPacket.packetType == Packet.pType.loginInfo)
            {
                //Debug.Log("Login Info Packet Recieved.");
                PlayerLoginData pLD = curPacket.GetPacketData <PlayerLoginData>();
                NetTools.clientID = pLD.clientID;//System.BitConverter.ToInt16(curPacket.packetData,0);
                NetClient.instanceClient.clientID       = NetTools.clientID;
                NetClient.instanceClient.serversSteamID = pLD.serverSteamID;

                if (NetTools.isServer)
                {
                    NetServer.serverInstance.myConnection = NetServer.serverInstance.GetPlayerByID(NetTools.clientID);
                }

                NetTools.onJoinServer.Invoke();

                //print("Test");
            }
            else if (curPacket.packetType == Packet.pType.netVarEdit)
            {
                NetworkFieldPacket nFP    = ENSSerialization.DeserializeNetworkFieldPacket(curPacket.packetData);
                NetworkObject      netObj = NetworkObject.NetObjFromNetID(nFP.networkObjID);

                if (netObj == null)
                {
                    return;
                }

                if (netObj.initialized == false)
                {
                    //netObj.queuedNetworkPackets.Add(curPacket);
                    return;
                }

                if ((netObj.ownerID != curPacket.packetOwnerID && !curPacket.serverAuthority && !netObj.sharedObject) || (curPacket.packetOwnerID == NetTools.clientID && nFP.immediateOnSelf))
                {
                    return;
                }


                //Debug.Log("Seting NetVarEdit.");
                try
                {
                    netObj.SetFieldLocal(nFP.fieldName, nFP.data.ToObject());
                }
                catch (System.Exception e)
                {
                    Debug.LogError(e);
                }
            }
            else if (curPacket.packetType == Packet.pType.rpc)
            {
                //Debug.Log(curPacket.jsonData);
                RPCPacketData rPD  = ENSSerialization.DeserializeRPCPacketData(curPacket.packetData);
                NetworkObject nObj = NetworkObject.NetObjFromNetID(rPD.networkObjectID);

                if (nObj == null || (nObj.rpcs[rPD.rpcIndex].serverAuthorityRequired && !curPacket.serverAuthority) || (nObj.ownerID != curPacket.packetOwnerID && !nObj.sharedObject && !curPacket.serverAuthority))
                {
                    return; //Means only server can run it.
                }
                nObj.rpcs[rPD.rpcIndex].InvokeRPC(rPD.ReturnArgs());
            }
            else if (curPacket.packetType == Packet.pType.connectionPacket)
            {
                if (!curPacket.serverAuthority && curPacket.packetOwnerID != -1)
                {
                    return; //Prevents anyone from disconnecting everyone.
                }

                ConnectionPacket cP = ENSSerialization.DeserializeConnectionPacket(curPacket.packetData);
                if (cP.immediateDisconnect)
                {
                    NetClient.instanceClient.DisconnectFromServer();
                    NetTools.onLeaveServer.Invoke(cP.reason);
                }
            }
            //else if (curPacket.packetType == Packet.pType.networkAuth && NetTools.isServer)
            //{



            //}
        }