Example #1
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;
            }
        }