コード例 #1
0
ファイル: INetLogic.cs プロジェクト: randomcrab/SE
        public static byte[] SerializePersistable(INetPersistable persist)
        {
            NetDataWriter writer = NetworkPool.GetWriter();

            byte[] bytes = persist.SerializeNetworkState(writer);
            NetworkPool.ReturnWriter(writer);
            return(bytes);
        }
コード例 #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]);
        }
コード例 #3
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));
            }
        }