Inheritance: UnityEngine.MonoBehaviour, INetworkingSerialized
示例#1
0
        /// <summary>
        /// Destroy a Simple Networked Monobehavior or any of its derivitives with the given Network ID
        /// </summary>
        /// <param name="networkId">Network ID to be destroyed</param>
        /// <returns><c>True</c> if the network behavoir was destroy, otherwise <c>False</c></returns>
        public static bool NetworkDestroy(ulong networkId)
        {
            // Make sure the object exists on the network before calling destroy
            SimpleNetworkedMonoBehavior behavior = Locate(networkId);

            if (behavior == null)
            {
                return(false);
            }

            // Destroy the object from the scene and remove it from the lookup

            GameObject.Destroy(behavior.gameObject);

            lock (networkedBehaviorsMutex)
            {
                networkedBehaviors.Remove(networkId);
            }

            if (Networking.PrimarySocket.IsServer)
            {
                Networking.PrimarySocket.ClearBufferedInstantiateFromID(networkId);
            }

            return(true);
        }
		private void PlayerSpawned(SimpleNetworkedMonoBehavior playerObject)
		{
			Debug.Log("The player object " + playerObject.name + " has spawned at " + 
				"X: " + playerObject.transform.position.x +
				"Y: " + playerObject.transform.position.y +
				"Z: " + playerObject.transform.position.z);
		}
示例#3
0
 public void AssignBehavior(SimpleNetworkedMonoBehavior targetBehavior)
 {
     lock (networkedObjectMutex)
     {
         if (!ReferenceEquals(targetBehavior, null))
         {
             NetworkedBehavior   = targetBehavior;
             NetworkedBehaviorId = targetBehavior.NetworkedId;
         }
     }
 }
示例#4
0
        /// <summary>
        /// Prepare the NetworkingStream to be used
        /// </summary>
        /// <param name="socket">The NetWorker socket to be used</param>
        /// <param name="identifierType">The type of Identifier it is going to prepare</param>
        /// <param name="networkedBehavior">NetworkedBehavior to use</param>
        /// <param name="extra">Extra parameters to prepare</param>
        /// <param name="receivers">Who shall be receiving this NetworkingStream</param>
        /// <param name="bufferedRPC">To know if this is a Buffered RPC</param>
        /// <param name="customidentifier">A custom Identifier to be passed through</param>
        /// <returns></returns>
        public NetworkingStream Prepare(NetWorker socket, IdentifierType identifierType, SimpleNetworkedMonoBehavior networkedBehavior, BMSByte extra = null, NetworkReceivers receivers = NetworkReceivers.All, bool bufferedRPC = false, uint customidentifier = 0, ulong senderId = 0)
        {
            if (ReferenceEquals(networkedBehavior, null) && (extra == null || extra.Size == 0))
            {
                throw new NetworkException(9, "Prepare was called but nothing was sent to write");
            }

            NetworkedBehaviorId = 0;
            lock (networkedObjectMutex)
            {
                NetworkedBehavior = networkedBehavior;
            }

            return(PrepareFinal(socket, identifierType, !ReferenceEquals(NetworkedBehavior, null) ? NetworkedBehavior.NetworkedId : 0, extra, receivers, bufferedRPC, customidentifier, senderId));
        }
	void PartSpawned( SimpleNetworkedMonoBehavior part )
	{
		//part.getSprite().SetFrame( blockType );
		//part.set_f32( "weight", Block::getWeight( block ) );
		GameObject partGO = part.gameObject;

		partsToAdd.Add( partGO );	
		partGO.GetComponent<Part_Info>().OwnerID = gOID;
		//b.set_u16( "playerID", playerID );
		partGO.GetComponent<Part_Info>().ShipID = -1; // don't push on ship

		part.GetComponent<Part_Info>().ShipID = 0;
		part.GetComponent<Part_Info>().PlacedTime = Time.time;

		Debug.Log("part spawned");
	}
示例#6
0
 /// <summary>
 /// Assign a sender to the NetworkingStream
 /// </summary>
 /// <param name="sender">The player for this NetworkingStream</param>
 /// <param name="targetBehavior">The owning behavior of this stream</param>
 public void AssignSender(NetworkingPlayer sender, SimpleNetworkedMonoBehavior targetBehavior)
 {
     Sender = sender;
     AssignBehavior(targetBehavior);
 }
示例#7
0
        /// <summary>
        /// To consume the data of the NetWorker with a player's data
        /// </summary>
        /// <param name="socket">The NetWorker socket to be used</param>
        /// <param name="sender">The player who is sending the data</param>
        /// <param name="message">Data that is being sent</param>
        /// <returns></returns>
        public NetworkingStream Consume(NetWorker socket, NetworkingPlayer sender, BMSByte message)
        {
            lock (networkedObjectMutex)
            {
                Sender = sender;

                NetworkedBehaviorId = 0;
                ByteReadIndex       = 0;
                Bytes.Clone(message);
                FrameIndex = message[message.StartIndex() + message.Size - 1];

                ProtocolType = (Networking.ProtocolType)ObjectMapper.Map <int>(this);
                Receivers    = (NetworkReceivers)ObjectMapper.Map <int>(this);
                RealSenderId = ObjectMapper.Map <ulong>(this);

                if (ProtocolType == Networking.ProtocolType.HTTP || ProtocolType == Networking.ProtocolType.QuickUDP || ProtocolType == Networking.ProtocolType.QuickTCP)
                {
                    Ready = true;
                    return(this);
                }

                char identifier = (char)ReadByte();                // ObjectMapper.Map<char>(this);

                if (identifier == identifier_NONE)
                {
                    identifierType = IdentifierType.None;
                }
                else if (identifier == identifier_RPC)
                {
                    identifierType = IdentifierType.RPC;
                    BufferedRPC    = ReadByte() == 1;
                }
                else if (identifier == identifier_PLAYER)
                {
                    identifierType = IdentifierType.Player;
                }
                else if (identifier == identifier_NETWORKED_BEHAVIOR)
                {
                    identifierType = IdentifierType.NetworkedBehavior;
                }
                else if (identifier == identifier_DISCONNECT)
                {
                    identifierType = IdentifierType.Disconnect;
                }
                else if (identifier == identifier_CUSTOM)
                {
                    identifierType = IdentifierType.Custom;
                }

                NetworkedBehaviorId = ObjectMapper.Map <ulong>(this);

                NetworkedBehavior = SimpleNetworkedMonoBehavior.Locate(NetworkedBehaviorId);

                if (NetworkedBehaviorId > 0 && ReferenceEquals(NetworkedBehavior, null) && identifierType != IdentifierType.RPC)
                {
                    return(null);
                }

                // Remove the size of ProtocolType, identifier, NetworkId, etc.
                Bytes.RemoveStart(ByteReadIndex);

                ByteReadIndex = 0;

                if (socket.Uniqueidentifier == 0 && !socket.IsServer && identifierType == IdentifierType.Player)
                {
                    if (socket != null)
                    {
                        socket.AssignUniqueId(ObjectMapper.Map <ulong>(this));
                        Bytes.RemoveStart(sizeof(ulong));
                    }
                    else
                    {
                        Bytes.RemoveStart(sizeof(ulong));
                    }

                    if (socket != null && !socket.IsServer)
                    {
                        if (!socket.MasterServerFlag)
                        {
                            if (socket.UsingUnityEngine && ((ReferenceEquals(NetworkingManager.Instance, null) || !NetworkingManager.Instance.IsSetup || ReferenceEquals(NetworkingManager.Instance.OwningNetWorker, null))))
                            {
                                NetworkingManager.setupActions.Add(socket.GetNewPlayerUpdates);
                            }
                            else
                            {
                                socket.GetNewPlayerUpdates();
                            }
                        }
                    }
                }

                ByteReadIndex = 0;

                if (identifierType == IdentifierType.NetworkedBehavior && !ReferenceEquals(NetworkedBehavior, null))
                {
                    if (NetworkedBehavior is NetworkedMonoBehavior)
                    {
                        ((NetworkedMonoBehavior)NetworkedBehavior).PrepareDeserialize(this);
                    }
                    else
                    {
                        throw new Exception("Only NetworkedMonoBehaviors can be used for serialization and deserialization across the network, object with id " + NetworkedBehavior.NetworkedId + " is not a \"NetworkedMonoBehavior\"");
                    }
                }

                if (identifierType == IdentifierType.Custom)
                {
                    Customidentifier = ObjectMapper.Map <uint>(this);
                    Bytes.RemoveStart(sizeof(uint));
                }

                ByteReadIndex = 0;

                Ready = true;

                if (NetworkedBehaviorId > 0 && ReferenceEquals(NetworkedBehavior, null))
                {
                    if (identifierType == IdentifierType.RPC)
                    {
                        SimpleNetworkedMonoBehavior.QueueRPCForInstantiate(NetworkedBehaviorId, this);
                        QueuedRPC = true;
                        return(null);
                    }

                    return(null);
                }

                return(this);
            }
        }
示例#8
0
        public void NetworkInstantiate(ulong ownerId, ulong startNetworkId, string name, Vector3 position, Quaternion rotation, int callbackId = 0)
        {
            lock (networkedBehaviorsMutex)
            {
                if (networkedBehaviors.ContainsKey(startNetworkId))
                {
                    return;
                }

                SimpleNetworkedMonoBehavior[] netBehaviors = null;

#if BARE_METAL
                netBehaviors = new SimpleNetworkedMonoBehavior[behaviorsAndRefCount[name].Length];

                for (int i = 0; i < netBehaviors.Length; i++)
                {
                    //if (CreateObject != null)
                    //	netBehaviors[i] = CreateObject(name + "(Clone)(Child)" + i, behaviorsAndRefCount[name][i]);

                    if (behaviorsAndRefCount[name][i] == typeof(SimpleNetworkedMonoBehavior).ToString() || behaviorsAndRefCount[name][i] == typeof(NetworkedMonoBehavior).ToString())
                    {
                        netBehaviors[i] = (SimpleNetworkedMonoBehavior)Activator.CreateInstance(typeof(SimpleNetworkedMonoBehavior).Assembly.GetType(behaviorsAndRefCount[name][i]), name, behaviorsAndRefCount[name][i]);
                    }
                    else
                    {
                        foreach (System.Reflection.Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                        {
                            if (assembly.FullName.Contains("ForgeBareMetalGame"))
                            {
                                netBehaviors[i] = (SimpleNetworkedMonoBehavior)Activator.CreateInstance(assembly.GetType(behaviorsAndRefCount[name][i]), name, behaviorsAndRefCount[name][i]);
                                break;
                            }
                        }
                    }
                }
#else
                GameObject o = Instance.PullObject((ownerId != OwningNetWorker.Me.NetworkId ? name + "(Remote)" : name), name);
                netBehaviors = GetAllSimpleMonoBehaviors(o);

                if (netBehaviors.Length == 0)
                {
                    Debug.LogError("Instantiating on the network is only for objects that derive from BaseNetworkedMonoBehavior, " +
                                   "if object does not need to be serialized consider using a RPC with GameObject.Instantiate");

                    return;
                }

                GameObject tmp = (Instantiate(o, position, rotation) as GameObject);
                netBehaviors = GetAllSimpleMonoBehaviors(tmp);
#endif

                while (ObjectCounter < startNetworkId + (ulong)netBehaviors.Length - 1)
                {
                    GenerateUniqueId();
                }

                for (int i = 0; i < netBehaviors.Length; i++)
                {
                    netBehaviors[i].Setup(OwningNetWorker, OwningNetWorker.Uniqueidentifier == ownerId, startNetworkId + (ulong)i, ownerId);
                }

#if !BARE_METAL
                if (ownerId == OwningNetWorker.Me.NetworkId)
                {
                    Networking.RunInstantiateCallback(callbackId, netBehaviors[0].GetComponent <SimpleNetworkedMonoBehavior>());
                }
#else
                if (ownerId == OwningNetWorker.Me.NetworkId)
                {
                    Networking.RunInstantiateCallback(callbackId, netBehaviors[0]);
                }
#endif
            }
        }
示例#9
0
		/// <summary>
		/// Destroy a simple networked object
		/// </summary>
		/// <param name="netBehavior">Networked behavior to destroy</param>
		public static void Destroy(SimpleNetworkedMonoBehavior netBehavior)
		{
			if (!NetworkingManager.IsOnline)
			{
				GameObject.Destroy(netBehavior.gameObject);
				return;
			}

			if (!netBehavior.IsOwner && !netBehavior.OwningNetWorker.IsServer)
				return;

			if (!ReferenceEquals(NetworkingManager.Instance, null))
				NetworkingManager.Instance.RPC("DestroyOnNetwork", netBehavior.NetworkedId);
		}
		/// <summary>
		/// Setup the Simple Networked Monobehavior stack with a NetWorker
		/// </summary>
		/// <param name="owningSocket">The NetWorker to be setup with</param>
		public static void SetupObjects(SimpleNetworkedMonoBehavior[] behaviors, NetWorker owningSocket)
		{
			if (ObjectCounter == 0)
				GenerateUniqueId();

			NetworkingManager.Instance.Setup(owningSocket, owningSocket.IsServer, 0, 0);

			// TODO:  Got through all objects in NetworkingManager stack and set them up
			foreach (SimpleNetworkedMonoBehavior behavior in behaviors)
				if (!(behavior is NetworkingManager) && behavior != null)
					behavior.Setup(owningSocket, owningSocket.IsServer, GenerateUniqueId(), 0, true);
		}
	private void ZombieSpawned(SimpleNetworkedMonoBehavior zombie)
	{
		_zombies.Add(zombie.gameObject);
	}
	private void PowerupSpawned(SimpleNetworkedMonoBehavior powerup)
	{
		_powerups.Add(powerup.gameObject);
	}
	// Callback
	void PawnSpawned(SimpleNetworkedMonoBehavior pawnSnmb)
	{
		Debug.Log("Pawn Spawned");
		pawnSnmb.transform.position = transform.position;
		RPC("PossessPawn", NetworkReceivers.AllBuffered, pawnSnmb.NetworkedId, OwnerId);
	}
示例#14
0
	// Callback only happens on the participant that calls instantiate, so we use RPC to set up bullet velocity.
	void BulletSpawned( SimpleNetworkedMonoBehavior bullet )
	{
		bullet.GetComponent<Bullet_Velocity>().RPC("setV", shootDir*bulletSpeed);
		bullet.GetComponent<Bullet_Hit>().RPC("setShooterNetworkedId", NetworkedId);
	}
示例#15
0
		private static bool ValidateNetworkedObject(string name, out SimpleNetworkedMonoBehavior netBehavior)
		{
			netBehavior = null;

			if (NetworkingManager.Instance == null)
			{
				Debug.LogError("The NetworkingManager object could not be found.");
				return false;
			}

			GameObject o = NetworkingManager.Instance.PullObject(name);

			if (o == null)
				return false;

			netBehavior = o.GetComponent<SimpleNetworkedMonoBehavior>();

			if (netBehavior == null)
			{
				Debug.LogError("Instantiating on the network is only for objects that derive from BaseNetworkedMonoBehavior, " +
					"if object does not need to be serialized consider using a RPC with GameObject.Instantiate");

				return false;
			}

			return true;
		}
示例#16
0
		/// <summary>
		/// Destroy a simple networked object
		/// </summary>
		/// <param name="netBehavior">Networked behavior to destroy</param>
		public static void Destroy(SimpleNetworkedMonoBehavior netBehavior)
		{
			if (!netBehavior.IsOwner && !netBehavior.OwningNetWorker.IsServer)
				return;

			NetworkingManager.Instance.RPC("DestroyOnNetwork", netBehavior.NetworkedId);
		}
示例#17
0
		public static bool RunInstantiateCallback(int index, SimpleNetworkedMonoBehavior spawn)
		{
			if (instantiateCallbacks.ContainsKey(index))
			{
				instantiateCallbacks[index](spawn);
				instantiateCallbacks.Remove(index);
				return true;
			}

			return false;
		}