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

            return(true);
        }
Ejemplo n.º 2
0
        public void NetClean()
        {
            List <uint> goNetIDs = NetIDs;

            for (int i = 0; i < goNetIDs.Count; i++)
            {
                NetworkObjects.TryRemove(goNetIDs[i], out _);
                SpawnedNetObjects.TryRemove(goNetIDs[i], out _);
            }
            Owner.OnDestroyInternal();
        }
Ejemplo n.º 3
0
 public void Destroy_CLIENT(uint netID)
 {
     lock (SpawnedNetObjects) {
         if (SpawnedNetObjects.TryGetValue(netID, out SpawnedNetObject netObj))
         {
             CleanNetObject(netObj);
         }
         else
         {
             clearBuffer.Add(netID);
         }
     }
 }
Ejemplo n.º 4
0
        public void Instantiate_CLIENT(string type, bool isOwner, uint netID, byte[] netState, byte[] data, byte[] paramsData)
        {
            NetDataReader reader = null;

            try {
                if (!spawnables.TryGetValue(type, out Type t))
                {
                    throw new Exception("No Spawnable of Type " + type + " found in dictionary.");
                }

                List <object> instantiateParameters = new List <object>();
                reader = NetworkPool.GetReader(paramsData);
                while (!reader.EndOfData)
                {
                    instantiateParameters.Add(NetData.Read(NetData.PacketBytes[reader.GetByte()], reader));
                }
                NetworkPool.ReturnReader(reader);

                // TODO: Temporary fix to prevent error where JSON deserializes floats to doubles.
                for (int i = 0; i < instantiateParameters.Count; i++)
                {
                    if (instantiateParameters[i] is double)
                    {
                        instantiateParameters[i] = Convert.ToSingle(instantiateParameters[i]);
                    }
                }

                object obj = instantiateParameters != null
                    ? Activator.CreateInstance(t, instantiateParameters.ToArray())
                    : Activator.CreateInstance(t);

                INetLogic logic = null;
                if (obj is INetLogicProxy proxy)
                {
                    logic = proxy.NetLogic;
                }
                else if (obj is INetLogic netLogic)
                {
                    logic = netLogic;
                }

                if (logic == null)
                {
                    throw new Exception("NetLogic not found.");
                }

                reader = NetworkPool.GetReader(netState);
                SetupNetLogic(logic, netID, isOwner, reader);
                NetworkPool.ReturnReader(reader);

                SpawnedNetObjects.TryAdd(logic.ID, new SpawnedNetObject(logic, logic.ID, type));

                if (logic is INetInstantiatable instantiatable)
                {
                    reader = NetworkPool.GetReader(data);
                    instantiatable.OnNetworkInstantiatedClient(type, isOwner, reader);
                    NetworkPool.ReturnReader(reader);
                }
            } catch (Exception) {
                NetworkPool.ReturnReader(reader);
                throw;
            }
        }
Ejemplo n.º 5
0
        public void Instantiate(string type, string owner = "SERVER", object[] parameters = null)
        {
            NetDataWriter writer = null;

            try {
                if (InstanceType != NetInstanceType.Server)
                {
                    throw new InvalidOperationException("Attempted to instantiate on a client. Instantiate can only be called on the server.");
                }
                if (!spawnables.TryGetValue(type, out Type t))
                {
                    throw new Exception("No Spawnable of Type " + type + " found in dictionary.");
                }

                object obj = parameters != null
                    ? Activator.CreateInstance(t, parameters)
                    : Activator.CreateInstance(t);

                INetLogic logic = null;
                if (obj is INetLogicProxy proxy)
                {
                    logic = proxy.NetLogic;
                }
                else if (obj is INetLogic netLogic)
                {
                    logic = netLogic;
                }

                if (logic == null)
                {
                    throw new Exception("NetLogic not found.");
                }

                SetupNetLogic(logic, owner == "SERVER");
                SpawnedNetObjects.TryAdd(logic.ID, new SpawnedNetObject(logic, logic.ID, type, owner));

                byte[] returned = null;
                byte[] netState = null;

                writer = NetworkPool.GetWriter();
                if (parameters != null)
                {
                    foreach (object writeObj in parameters)
                    {
                        writer.Put(NetData.PacketConverters[writeObj.GetType()]);
                        NetData.Write(writeObj.GetType(), writeObj, writer);
                    }
                }
                byte[] paramsData = writer.Length > 0 ? writer.CopyData() : null;
                NetworkPool.ReturnWriter(writer);

                if (logic is INetInstantiatable instantiatable)
                {
                    writer = NetworkPool.GetWriter();
                    instantiatable.OnNetworkInstantiatedServer(type, owner, writer);
                    NetworkPool.ReturnWriter(writer);

                    writer   = NetworkPool.GetWriter();
                    returned = instantiatable.GetBufferedData(writer);
                    NetworkPool.ReturnWriter(writer);
                }
                if (logic is INetPersistable persist)
                {
                    writer   = NetworkPool.GetWriter();
                    netState = persist.SerializeNetworkState(writer);
                    NetworkPool.ReturnWriter(writer);
                }

                foreach (string playerID in Connections.Keys)
                {
                    SendRPC(instantiateMethod, Connections[playerID], type, playerID == owner, logic.ID, netState ?? new byte[0], returned ?? new byte[0], paramsData ?? new byte[0]);
                }
            } catch (Exception e) {
                NetworkPool.ReturnWriter(writer);
                LogError(null, new Exception("OOF!", e));
            }
        }