예제 #1
0
    // sendToSelf is only for server use.
    public static void RPC(OperationView oV, string methodName, int sendToSelf, params object[] objects)
    {
        // The server doesn't send RPCs: (However the Server can send itself RPCs as if it was a client)
        if (OperationNetwork.isServer && sendToSelf != OperationNetwork.ToServer)
        {
            return;
        }

        // Demos aren't actually connected so they can't send RPCs.
        if (OperationNetwork.isDemo)
        {
            return;
        }

        // Needs to turn the data into bytes:
        byte[] data          = new byte[300];
        int    writeLocation = 3;

        for (int i = 0; i < objects.Length; i++)
        {
            if (objects[i] is float)
            {
                Buffer.BlockCopy(BitConverter.GetBytes((float)objects[i]), 0, data, writeLocation, 4);
                writeLocation += 4;
            }
            else if (objects[i] is int)
            {
                Buffer.BlockCopy(BitConverter.GetBytes((int)objects[i]), 0, data, writeLocation, 4);
                writeLocation += 4;
            }
            else if (objects[i] is bool)
            {
                data[writeLocation] = Convert.ToByte((bool)objects[i]);
                writeLocation      += 1;
            }
            else if (objects[i] is byte)
            {
                data[writeLocation] = (byte)objects[i];
                writeLocation      += 1;
            }
            else if (objects[i] is Vector3)
            {
                Buffer.BlockCopy(BitConverter.GetBytes(((Vector3)objects[i]).x), 0, data, writeLocation, 4);
                writeLocation += 4;
                Buffer.BlockCopy(BitConverter.GetBytes(((Vector3)objects[i]).y), 0, data, writeLocation, 4);
                writeLocation += 4;
                Buffer.BlockCopy(BitConverter.GetBytes(((Vector3)objects[i]).z), 0, data, writeLocation, 4);
                writeLocation += 4;
            }
            else if (objects[i] is Vector3[])
            {
                byte length = (byte)((Vector3[])objects[i]).Length;                 // Maximum length of 255
                data[writeLocation] = length;
                writeLocation      += 1;
                for (int sk = 0; sk < length; sk++)
                {
                    Buffer.BlockCopy(BitConverter.GetBytes(((Vector3[])objects[i])[sk].x), 0, data, writeLocation, 4);
                    writeLocation += 4;
                    Buffer.BlockCopy(BitConverter.GetBytes(((Vector3[])objects[i])[sk].y), 0, data, writeLocation, 4);
                    writeLocation += 4;
                    Buffer.BlockCopy(BitConverter.GetBytes(((Vector3[])objects[i])[sk].z), 0, data, writeLocation, 4);
                    writeLocation += 4;
                }
            }
            else if (objects[i] is Quaternion)
            {
                Buffer.BlockCopy(BitConverter.GetBytes(((Quaternion)objects[i]).x), 0, data, writeLocation, 4);
                writeLocation += 4;
                Buffer.BlockCopy(BitConverter.GetBytes(((Quaternion)objects[i]).y), 0, data, writeLocation, 4);
                writeLocation += 4;
                Buffer.BlockCopy(BitConverter.GetBytes(((Quaternion)objects[i]).z), 0, data, writeLocation, 4);
                writeLocation += 4;
                Buffer.BlockCopy(BitConverter.GetBytes(((Quaternion)objects[i]).w), 0, data, writeLocation, 4);
                writeLocation += 4;
            }
            else if (objects[i] is float[])
            {
                byte length = (byte)((float[])objects[i]).Length;                 // Maximum length of 255
                data[writeLocation] = length;
                writeLocation      += 1;
                Buffer.BlockCopy((float[])objects[i], 0, data, writeLocation, ((float[])objects[i]).Length * 4);
                writeLocation += ((float[])objects[i]).Length * 4;
            }
            else if (objects[i] is byte[])
            {
                if (((byte[])objects [i]).Length > 255)
                {
                    Debug.LogError("BYTE DATA OVERLOAD- Probably mirrors for playerInput hitscanData");
                }
                byte length = (byte)((byte[])objects[i]).Length;                 // Maximum length of 255
                data[writeLocation] = length;
                writeLocation      += 1;
                Buffer.BlockCopy((byte[])objects[i], 0, data, writeLocation, ((byte[])objects[i]).Length);
                writeLocation += ((byte[])objects[i]).Length;
            }
            else if (objects[i] is string)
            {
                byte[] stringBytes = Encoding.ASCII.GetBytes((string)objects[i]);
                if (stringBytes.Length > 255)
                {
                    // Cut it off:
                    byte[] extraStringBytes = stringBytes;
                    stringBytes = new byte[255];
                    Buffer.BlockCopy(extraStringBytes, 0, stringBytes, 0, 255);
                }
                data [writeLocation] = (byte)(stringBytes.Length);
                writeLocation       += 1;
                Buffer.BlockCopy(stringBytes, 0, data, writeLocation, stringBytes.Length);
                writeLocation += stringBytes.Length;
            }
            else if (objects[i] is short)
            {
                Buffer.BlockCopy(BitConverter.GetBytes((short)objects[i]), 0, data, writeLocation, 2);
                writeLocation += 2;
            }
        }
        // Trim data: (Probably should just use writeLocation in "sendData" instead)
        Array.Resize(ref data, writeLocation);

        // All OperationViews come with a SyncGameState.
        if (oV != null)
        {
            Buffer.BlockCopy(BitConverter.GetBytes(oV.GetComponent <SyncGameState> ().objectID), 0, data, 0, 2);
        }
        else
        {
            Buffer.BlockCopy(BitConverter.GetBytes((short)0), 0, data, 0, 2);
        }

        data[2] = rpcIDList[methodName];         // Which RPC call

        if (OperationNetwork.isServer)
        {
            // This is no longer how the server interacts with the players
            if (sendToSelf == OperationNetwork.ToServer)
            {
                byte[] sendData = new byte[data.Length - 3];
                Buffer.BlockCopy(data, 3, sendData, 0, data.Length - 3);
                oV.ReceiveRPC(data [2], OperationNetwork.FromServerClient, sendData);
            }
            return;
        }
        else
        {
            // Just send to server. (Client can't send anywhere else)
            OperationNetwork.sendDataToServer(data);
        }
    }