Exemplo n.º 1
0
        /// <summary>
        /// Creates an RCCommand object from the given RCPackage.
        /// </summary>
        /// <param name="cmdPackage">The RCPackage to be deserialized.</param>
        /// <returns>The created RCCommand.</returns>
        public static RCCommand FromPackage(RCPackage cmdPackage)
        {
            if (cmdPackage == null)
            {
                throw new ArgumentNullException("cmdPackage");
            }
            if (!cmdPackage.IsCommitted)
            {
                throw new ArgumentException("The incoming package is not committed!", "cmdPackage");
            }
            if (cmdPackage.PackageFormat.ID != RCCommand.COMMAND_PACKAGEFORMAT)
            {
                throw new ArgumentException("The incoming package is not a command package!", "cmdPackage");
            }

            RCNumVector targetPosition  = RCNumVector.Undefined;
            int         targetPositionX = cmdPackage.ReadInt(2);
            int         targetPositionY = cmdPackage.ReadInt(3);

            if (targetPositionX != -1 && targetPositionY != -1)
            {
                targetPosition = new RCNumVector(new RCNumber(targetPositionX), new RCNumber(targetPositionY));
            }

            return(new RCCommand(
                       cmdPackage.ReadString(0),
                       cmdPackage.ReadIntArray(1),
                       targetPosition,
                       cmdPackage.ReadInt(4),
                       cmdPackage.ReadString(5)));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a MapHeader structure from the given RCPackage.
        /// </summary>
        /// <param name="package">The RCPackage that contains the map header informations.</param>
        /// <returns>The created MapHeader structure.</returns>
        public static MapHeader FromPackage(RCPackage package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }
            if (!package.IsCommitted)
            {
                throw new ArgumentException("The header package is not committed!", "package");
            }
            if (package.PackageType != RCPackageType.CUSTOM_DATA_PACKAGE)
            {
                throw new ArgumentException("Invalid package type!", "package");
            }
            if (package.PackageFormat.ID != MapFileFormat.MAP_HEADER)
            {
                throw new ArgumentException("Invalid package format!", "package");
            }

            MapHeader header = new MapHeader();

            header.appVersion   = new Version(package.ReadInt(0), package.ReadInt(1), package.ReadInt(2), package.ReadInt(3));
            header.mapName      = package.ReadString(4);
            header.tilesetName  = package.ReadString(5);
            header.mapSize      = new RCIntVector(package.ReadShort(6), package.ReadShort(7));
            header.maxPlayers   = package.ReadByte(8);
            header.checksumList = new List <int>(package.ReadIntArray(9));

            if (header.mapName == null)
            {
                throw new MapException("Map name information is missing!");
            }
            if (header.tilesetName == null)
            {
                throw new MapException("Tileset name information is missing!");
            }
            if (header.mapSize.X <= 0 || header.mapSize.Y <= 0)
            {
                throw new MapException("Map size cannot be negative or 0!");
            }
            if (header.maxPlayers <= 0)
            {
                throw new MapException("Maximum number of players cannot be negative or 0!");
            }
            return(header);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Internal function for parsing a DSS_CTRL_START_SIMULATION package.
        /// </summary>
        /// <param name="package">The package to parse.</param>
        /// <returns>True if the package has been successfully parsed, false otherwise.</returns>
        private bool ParseStartSimPackage(RCPackage package, out int[] leftList, out int[] lostList, out bool[] opFlags)
        {
            /// Read the left-list
            leftList = package.ReadIntArray(0);
            for (int i = 0; i < leftList.Length; ++i)
            {
                if (leftList[i] < 0)
                {
                    leftList = null;
                    lostList = null;
                    opFlags  = null;
                    return(false);
                }
            }

            /// Read the lost-list
            lostList = package.ReadIntArray(1);
            for (int i = 0; i < lostList.Length; ++i)
            {
                if (lostList[i] < 0)
                {
                    leftList = null;
                    lostList = null;
                    opFlags  = null;
                    return(false);
                }
            }

            /// Read the channel-state-list
            byte[] chStateBytes = package.ReadByteArray(2);
            if (chStateBytes.Length == this.guestRoot.OpCount - 1)
            {
                opFlags    = new bool[this.guestRoot.OpCount];
                opFlags[0] = true;
                for (int i = 0; i < chStateBytes.Length; ++i)
                {
                    if (chStateBytes[i] == (byte)DssChannelState.CHANNEL_CLOSED ||
                        chStateBytes[i] == (byte)DssChannelState.CHANNEL_OPENED ||
                        chStateBytes[i] == (byte)DssChannelState.GUEST_CONNECTED)
                    {
                        opFlags[i + 1] = (chStateBytes[i] == (byte)DssChannelState.GUEST_CONNECTED);
                    }
                    else
                    {
                        /// Error
                        leftList = null;
                        lostList = null;
                        opFlags  = null;
                        return(false);
                    }
                }
            } /// end-if (chStateBytes != null && chStateBytes.Length != this.guestRoot.OpCount - 1)
            else
            {
                /// Error
                leftList = null;
                lostList = null;
                opFlags  = null;
                return(false);
            }

            /// Everything is OK, the start simulation message arrived successfully.
            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Call this function if a control package has arrived from the network and you are waiting for a setup step
        /// answer or request. This class will parse it for you automatically.
        /// </summary>
        /// <param name="package">The incoming control package.</param>
        public void IncomingPackage(RCPackage package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }
            if (!package.IsCommitted)
            {
                throw new ArgumentException("Uncommitted package!", "package");
            }
            if (package.PackageType != RCPackageType.NETWORK_CONTROL_PACKAGE)
            {
                throw new ArgumentException("Unexpected package type!", "package");
            }
            if (this.state != SetupStepState.NOT_FINISHED)
            {
                throw new DssException("This call is only allowed in NOT_FINISHED state!");
            }

            if (this.mode == DssMode.HOST_SIDE)
            {
                /// Host side parsing
                if (this.beginArrived)
                {
                    if (!DssRoot.IsInternalFormat(package.PackageFormat))
                    {
                        this.packageListTmp.Add(package);
                    }
                    else
                    {
                        if (package.PackageFormat.ID == DssRoot.DSS_CTRL_SETUP_STEP_MSG_END &&
                            package.ReadInt(0) == this.stepID)
                        {
                            /// Finished
                            this.packageList = this.packageListTmp.ToArray();
                            this.state       = SetupStepState.READY;
                        }
                        else
                        {
                            /// Error
                            this.state = SetupStepState.ERROR;
                        }
                    }
                }
                else /// end-if (this.beginArrived)
                {
                    if (package.PackageFormat.ID == DssRoot.DSS_CTRL_SETUP_STEP_AW_BEGIN &&
                        package.ReadInt(0) == this.stepID)
                    {
                        this.beginArrived = true;
                    }
                    else
                    {
                        /// Error
                        this.state = SetupStepState.ERROR;
                    }
                }
            }
            else /// end-if (this.mode == SetupStepMode.HOST_SIDE)
            {
                /// Guest side parsing
                if (this.beginArrived)
                {
                    if (!DssRoot.IsInternalFormat(package.PackageFormat))
                    {
                        this.packageListTmp.Add(package);
                    }
                    else
                    {
                        if (package.PackageFormat.ID == DssRoot.DSS_CTRL_SETUP_STEP_MSG_END &&
                            package.ReadInt(0) == this.stepID)
                        {
                            /// Finished
                            this.packageList = this.packageListTmp.ToArray();
                            this.state       = SetupStepState.READY;
                        }
                        else
                        {
                            /// Error
                            this.state = SetupStepState.ERROR;
                        }
                    }
                }
                else /// end-if (this.beginArrived)
                {
                    if (package.PackageFormat.ID == DssRoot.DSS_CTRL_SETUP_STEP_RQ_BEGIN)
                    {
                        /// Read the step ID
                        this.stepID = package.ReadInt(0);

                        /// Read the left-list
                        int[] leftList = package.ReadIntArray(1);
                        bool  err      = false;
                        for (int i = 0; i < leftList.Length; ++i)
                        {
                            if (leftList[i] < 0)
                            {
                                err = true;
                                break;
                            }
                        }
                        if (!err)
                        {
                            /// OK, save the list
                            this.leftList = leftList;
                        }
                        else
                        {
                            /// Error
                            this.state = SetupStepState.ERROR;
                            return;
                        }

                        /// Read the lost-list
                        int[] lostList = package.ReadIntArray(2);
                        err = false;
                        for (int i = 0; i < lostList.Length; ++i)
                        {
                            if (lostList[i] < 0)
                            {
                                err = true;
                                break;
                            }
                        }
                        if (!err)
                        {
                            /// OK, save the list
                            this.lostList = lostList;
                        }
                        else
                        {
                            /// Error
                            this.state = SetupStepState.ERROR;
                            return;
                        }

                        /// Read the channel-state-list
                        byte[] chStateBytes = package.ReadByteArray(3);
                        if (chStateBytes.Length != 0)
                        {
                            this.channelStateList = new DssChannelState[chStateBytes.Length];
                            for (int i = 0; i < chStateBytes.Length; ++i)
                            {
                                if (chStateBytes[i] == (byte)DssChannelState.CHANNEL_CLOSED ||
                                    chStateBytes[i] == (byte)DssChannelState.CHANNEL_OPENED ||
                                    chStateBytes[i] == (byte)DssChannelState.GUEST_CONNECTED)
                                {
                                    this.channelStateList[i] = (DssChannelState)chStateBytes[i];
                                }
                                else
                                {
                                    /// Error
                                    this.state = SetupStepState.ERROR;
                                    return;
                                }
                            }
                        } /// end-if (chStateBytes != null && chStateBytes.Length != 0)
                        else
                        {
                            /// Error
                            this.state = SetupStepState.ERROR;
                            return;
                        }

                        /// Everything is OK, the begin message arrived successfully.
                        this.beginArrived = true;
                    }
                    else /// end-if (package.PackageFormat.ID == DssRoot.DSS_CTRL_SETUP_STEP_RQ_BEGIN)
                    {
                        /// Error
                        this.state = SetupStepState.ERROR;
                    }
                }
            }
        }