예제 #1
0
        internal bool CleanNetObject(SpawnedNetObject netObj)
        {
            SpawnedNetObjects.TryRemove(netObj.NetworkID, out _);
            if (netObj.NetLogic is INetInstantiatable instantiatable)
            {
                instantiatable.NetClean();
            }

            return(true);
        }
예제 #2
0
        internal void InstantiateFromBuffer(SpawnedNetObject netObj, NetPeer conn, uint netID, bool isOwner = false)
        {
            if (InstanceType != NetInstanceType.Server)
            {
                return;
            }

            byte[]        data              = null;
            byte[]        netState          = null;
            byte[]        instantiateParams = null;
            NetDataWriter writer            = null;

            try {
                if (netObj.NetLogic is INetInstantiatable instantiatable)
                {
                    writer = NetworkPool.GetWriter();
                    object[] parameters = instantiatable.InstantiateParameters;
                    foreach (object writeObj in parameters)
                    {
                        writer.Put(NetData.PacketConverters[writeObj.GetType()]);
                        NetData.Write(writeObj.GetType(), writeObj, writer);
                    }

                    instantiateParams = writer.CopyData();
                    NetworkPool.ReturnWriter(writer);

                    writer = NetworkPool.GetWriter();
                    data   = instantiatable.GetBufferedData(writer);
                    NetworkPool.ReturnWriter(writer);
                }

                if (netObj.NetLogic is INetPersistable persist)
                {
                    writer   = NetworkPool.GetWriter();
                    netState = persist.SerializeNetworkState(writer);
                    NetworkPool.ReturnWriter(writer);
                }
            } catch (Exception) {
                NetworkPool.ReturnWriter(writer);
                throw;
            }

            SendRPC(instantiateMethod, conn, netObj.SpawnableID, conn.GetUniqueID() == netObj.Owner, netObj.NetworkID, netState ?? new byte[0], data ?? new byte[0], instantiateParams ?? new byte[0]);
        }