/// <summary>
        /// Executes the /status command for the server and returns the reply
        /// </summary>
        /// <returns>Server status information</returns>
        public static ServerStatus Status(this StarNet.IStarNetSession session)
        {
            // Get command response
            string[] response = session.ExecuteAdminCommand("/status");

            // Make sure we got a response
            if (response == null || response.Length <= 0)
            {
                throw new InvalidOperationException("No response to command.");
            }

            var   status = new ServerStatus();
            Match match;

            // Get physics in memory
            match = RegexStatusPhysicsInMem.Match(response[0]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "PhysicsInMemory: {{physics}}; Rep: {{rep}}", response[0]);
            status.PhysicsInMemory = int.Parse(match.Groups["physicsInMem"].Value);
            status.Rep             = int.Parse(match.Groups["rep"].Value);

            // Get queued nt packages
            match = RegexStatusNetPackages.Match(response[1]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Total queued NT Packages: {{queuedPackages}}", response[1]);
            status.TotalQueuedNTPackages = int.Parse(match.Groups["queuedPackages"].Value);

            // Get segments
            match = RegexStatusSegments.Match(response[2]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Loaded !empty Segs / free: {{notEmptySegs}} / {{freeSegs}}", response[2]);
            status.NotEmptySegments = int.Parse(match.Groups["notEmptySegs"].Value);
            status.FreeSegments     = int.Parse(match.Groups["freeSegs"].Value);

            // Get loaded objects
            match = RegexStatusLoadedObjects.Match(response[3]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Loaded Objects: {{loadedObjects}}", response[3]);
            status.LoadedObjects = int.Parse(match.Groups["loadedObjects"].Value);

            // Get players
            match = RegexStatusPlayers.Match(response[4]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Players: {{currentPlayers}} / {{maxPlayers}}", response[4]);
            status.CurrentPlayers = int.Parse(match.Groups["currentPlayers"].Value);
            status.MaxPlayers     = int.Parse(match.Groups["maxPlayers"].Value);

            // Get memory
            match = RegexStatusMemory.Match(response[5]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Mem (MB)[free, taken, total]: [{{freeMemory}}, {{takenMemory}}, {{totalMemory}}]", response[5]);
            status.MemoryFree  = int.Parse(match.Groups["freeMemory"].Value);
            status.MemoryTaken = int.Parse(match.Groups["takenMemory"].Value);
            status.MemoryTotal = int.Parse(match.Groups["totalMemory"].Value);

            return(status);
        }
Beispiel #2
0
        public static SectorInfo GetSectorInfo(this StarNet.IStarNetSession session, int x, int y, int z)
        {
            // Get the command response
            string[] response = session.ExecuteAdminCommand("/sector_info " + x + " " + y + " " + z);

            // Make sure we got a response
            if (response == null || response.Length <= 0)
            {
                throw new InvalidOperationException("No response to command.");
            }

            // Check if the command failed.
            if (StarNetHelpersCommon.AdminCommandFailed.IsMatch(response[0]))
            {
                throw new AdminCommandFailedException("/sector_info " + x + " " + y + " " + z);
            }

            // Was the sector not found?
            if (RegexNoPlayerFound.IsMatch(response[0]))
            {
                return(null);
            }

            var  entities      = new List <EntityInfoBasic>();
            var  players       = new List <string>();
            var  creatures     = new List <string>();
            bool hasPlanetCore = false;

            Match match;
            int   counter = 0;

            while (true)
            {
                if ((match = RegexSectorInfoPlayer.Match(response[counter])).Success)
                {
                    players.Add(match.Groups["playerName"].Value);
                }
                else if ((match = RegexSectorInfoEntity.Match(response[counter])).Success)
                {
                    entities.Add(new EntityInfoBasic
                    {
                        Uid              = match.Groups["entityUID"].Value,
                        Type             = GetEntityTypeFromString(match.Groups["entityType"].Value),
                        LastModifiedBy   = match.Groups["lastModifier"].Value,
                        Creator          = match.Groups["spawner"].Value,
                        RealName         = match.Groups["entityName"].Value,
                        Touched          = bool.Parse(match.Groups["touched"].Value),
                        Faction          = int.Parse(match.Groups["factionId"].Value),
                        Position         = new Vector3 <float>(float.Parse(match.Groups["positionX"].Value), float.Parse(match.Groups["positionY"].Value), float.Parse(match.Groups["positionZ"].Value)),
                        MinChunkPosition = new Vector3 <int>(int.Parse(match.Groups["minPositionX"].Value), int.Parse(match.Groups["minPositionY"].Value), int.Parse(match.Groups["minPositionZ"].Value)),
                        MaxChunkPosition = new Vector3 <int>(int.Parse(match.Groups["maxPositionX"].Value), int.Parse(match.Groups["maxPositionY"].Value), int.Parse(match.Groups["maxPositionZ"].Value)),
                        CreatorId        = int.Parse(match.Groups["creatorID"].Value),
                        Sector           = new Vector3 <int>(x, y, z)
                    });
                }
                else if (RegexSectorInfoPlanetCore.IsMatch(response[counter]))
                {
                    hasPlanetCore = true;
                }
                else if ((match = RegexSectorInfoCreature.Match(response[counter])).Success)
                {
                    players.Add(match.Groups["creatureUid"].Value);
                }
                else
                {
                    break;
                }
                counter++;
            }

            match = RegexSectorInfo.Match(response[counter]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Entity [uid= ... ]", response[counter]);
            if (match.Success)
            {
                return(new SectorInfo
                {
                    Loaded = match.Groups["loaded"].Value.Equals("LOADED", StringComparison.InvariantCultureIgnoreCase),
                    Type = GetSectorTypeFromString(match.Groups["sectorType"].Value),
                    HasPlanetCore = hasPlanetCore,
                    SectorId = int.Parse(match.Groups["sectorID"].Value),
                    SectorCoordinates = new Vector3 <int>(int.Parse(match.Groups["sectorX"].Value), int.Parse(match.Groups["sectorY"].Value), int.Parse(match.Groups["sectorZ"].Value)),
                    Protected = bool.Parse(match.Groups["protected"].Value),
                    Peace = bool.Parse(match.Groups["peace"].Value),
                    Seed = long.Parse(match.Groups["seed"].Value),
                    Entities = entities.ToArray(),
                    Astronauts = players.ToArray(),
                    Creatures = creatures.ToArray()
                });
            }

            return(null);
        }
Beispiel #3
0
        // ReSharper disable once FunctionComplexityOverflow
        public static EntityInfoExtended GetEntityInfo(this StarNet.IStarNetSession session, EntityType type, string uid)
        {
            // Get the command response
            string[] response = session.ExecuteAdminCommand("/ship_info_uid \"" + StarNet.CreateFullEntityUid(type, uid) + "\"");

            // Make sure we got a response
            if (response == null || response.Length <= 0)
            {
                throw new InvalidOperationException("No response to command.");
            }

            // Check if the command failed.
            if (StarNetHelpersCommon.AdminCommandFailed.IsMatch(response[0]))
            {
                throw new AdminCommandFailedException("/ship_info_uid \"" + StarNet.CreateFullEntityUid(type, uid) + "\"");
            }

            // Was the entity not found?
            if (RegexEntityInfoNotFoundInDb.IsMatch(response[0]) && (response.Length <= 1 || RegexEntityInfoNotUnsaved.IsMatch(response[1])))
            {
                return(null);
            }

            var entityInfo = new EntityInfoExtended();

            // Get Loaded
            Match match = RegexEntityInfoLoaded.Match(response[0]);

            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Loaded: (true|false)", response[0]);
            entityInfo.Loaded = bool.Parse(match.Groups["loaded"].Value);

            // Get general information
            match = RegexSectorInfoEntity.Match(response[1]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Entity [uid= ... ]", response[1]);
            entityInfo.Uid              = match.Groups["entityUID"].Value;
            entityInfo.Type             = GetEntityTypeFromString(match.Groups["entityType"].Value);
            entityInfo.LastModifiedBy   = match.Groups["lastModifier"].Value;
            entityInfo.Creator          = match.Groups["spawner"].Value;
            entityInfo.RealName         = match.Groups["entityName"].Value;
            entityInfo.Touched          = bool.Parse(match.Groups["touched"].Value);
            entityInfo.Faction          = int.Parse(match.Groups["factionId"].Value);
            entityInfo.Position         = new Vector3 <float>(float.Parse(match.Groups["positionX"].Value), float.Parse(match.Groups["positionY"].Value), float.Parse(match.Groups["positionZ"].Value));
            entityInfo.MinChunkPosition = new Vector3 <int>(int.Parse(match.Groups["minPositionX"].Value), int.Parse(match.Groups["minPositionY"].Value), int.Parse(match.Groups["minPositionZ"].Value));
            entityInfo.MaxChunkPosition = new Vector3 <int>(int.Parse(match.Groups["maxPositionX"].Value), int.Parse(match.Groups["maxPositionY"].Value), int.Parse(match.Groups["maxPositionZ"].Value));
            entityInfo.CreatorId        = int.Parse(match.Groups["creatorID"].Value);

            // Early out for entities like shops that don't have the rest of the information.
            if (response[2].Equals("Admin command execution ended", StringComparison.InvariantCultureIgnoreCase))
            {
                return(entityInfo);
            }

            // Get attached
            match = RegexEntityInfoAttached.Match(response[2]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Attached: [...]", response[2]);
            string attached = match.Groups["attachedPlayers"].Value;

            if (!string.IsNullOrEmpty(attached))
            {
                MatchCollection matches = RegexEntityInfoAttachedParser.Matches(attached);

                entityInfo.AttachedPlayers = (from Match attachedPerson in matches select attachedPerson.Groups["playerName"].Value).ToArray();
            }
            else
            {
                entityInfo.AttachedPlayers = new string[0];
            }

            // Get docked
            match = RegexEntityInfoDocked.Match(response[3]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Attached: [...]", response[3]);
            string docked = match.Groups["dockedUIDs"].Value;

            if (!string.IsNullOrEmpty(docked))
            {
                MatchCollection matches = RegexEntityInfoDockedParser.Matches(docked);

                entityInfo.DockedShips = (from Match dockedShip in matches select dockedShip.Groups["entityName"].Value).ToArray();
            }
            else
            {
                entityInfo.DockedShips = new string[0];
            }

            // Get blocks
            match = RegexEntityInfoBlocks.Match(response[4]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Blocks: 97987", response[4]);
            entityInfo.BlockCount = int.Parse(match.Groups["blocks"].Value);

            // Get mass
            match = RegexEntityInfoMass.Match(response[5]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Mass: 9798.7", response[5]);
            entityInfo.Mass = float.Parse(match.Groups["mass"].Value);

            // Get Sector
            match = RegexEntityInfoSector.Match(response[8]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Sector: 10 => Sector[10](x, y, z)", response[8]);
            entityInfo.Sector = new Vector3 <int>(int.Parse(match.Groups["sectorX"].Value), int.Parse(match.Groups["sectorY"].Value), int.Parse(match.Groups["sectorZ"].Value));

            // Get orientation
            match = RegexEntityInfoOrientation.Match(response[15]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "Orientation: (x, y, z, w)", response[15]);
            entityInfo.Orientation = new Vector4 <float>(float.Parse(match.Groups["orientationX"].Value), float.Parse(match.Groups["orientationY"].Value), float.Parse(match.Groups["orientationZ"].Value), float.Parse(match.Groups["orientationW"].Value));

            return(entityInfo);
        }
        private static PlayerInfo ParsePlayer(string[] response, ref int currentLine)
        {
            Match match;
            var   logins = new List <PlayerLogin>();

            // Get all player logins
            while ((match = RegexPlayerLogin.Match(response[currentLine])).Success)
            {
                logins.Add(new PlayerLogin
                {
                    LoginTime    = match.Groups["time"].Value,
                    IpAddress    = IPAddress.Parse(match.Groups["ipAddress"].Value),
                    StarMadeName = match.Groups["accountName"].Value.ClearStringNull(),
                });

                // Increment the current line;
                currentLine++;
            }

            var playerInfo = new PlayerInfo {
                Logins = logins
            };

            // Check if this line is the sector (this happens if the player is in the spawn screen)
            if (response[currentLine].IndexOf("SECTOR", StringComparison.InvariantCultureIgnoreCase) < 0)
            {
                // Get the controlling position
                match = RegexPlayerControllingPosition.Match(response[currentLine++]);
                StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match,
                                                                         "[PL] CONTROLLING-POS: ({{positionX}}, {{positionY}}, {{positionZ}})", response[currentLine - 1]);
                playerInfo.Position = new Vector3 <float>(float.Parse(match.Groups["xPos"].Value),
                                                          float.Parse(match.Groups["yPos"].Value), float.Parse(match.Groups["zPos"].Value));

                // Get the controlling type and id
                match = RegexPlayerControlling.Match(response[currentLine++]);
                StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match,
                                                                         "[PL] CONTROLLING: {{ControllingType}}[{{ControllingID}}(0)]", response[currentLine - 1]);
                playerInfo.ControllingId = match.Groups["controlling"].Value;
                playerInfo.Controlling   =
                    (PlayerControlling)Enum.Parse(typeof(PlayerControlling), match.Groups["controllingType"].Value);
            }

            // Get player sector
            match = RegexPlayerSector.Match(response[currentLine++]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "[PL] SECTOR: ({{positionX}}, {{positionY}}, {{positionZ}})", response[currentLine - 1]);
            playerInfo.Sector = new Vector3 <int>(int.Parse(match.Groups["xSector"].Value),
                                                  int.Parse(match.Groups["ySector"].Value), int.Parse(match.Groups["zSector"].Value));

            // Get player faction
            match = RegexPlayerFaction.Match(response[currentLine++]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match,
                                                                     "[PL] FACTION: Faction [id={{factionId}}, name={{factionName}}, description={{factionDescription}}, size: {{factionSize}}; FP: {{factionPoints}}]",
                                                                     response[currentLine - 1]);
            if (!match.Groups["faction"].Value.IsStringNull())
            {
                playerInfo.Faction = new PlayerFaction
                {
                    Id            = int.Parse(match.Groups["factionId"].Value),
                    Name          = match.Groups["factionName"].Value,
                    Description   = match.Groups["description"].Value.ClearStringNull(),
                    Size          = int.Parse(match.Groups["size"].Value),
                    FactionPoints = int.Parse(match.Groups["factionPoints"].Value)
                };
            }

            // Get player credits
            match = RegexPlayerCredits.Match(response[currentLine++]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "[PL] CREDITS: {{Credits}}", response[currentLine - 1]);
            playerInfo.Credits = int.Parse(match.Groups["credits"].Value);

            // Get upgraded
            match = RegexPlayerUpgraded.Match(response[currentLine++]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "[PL] UPGRADED: {{Upgraded}}", response[currentLine - 1]);
            playerInfo.AccountUpgraded = bool.Parse(match.Groups["upgraded"].Value);

            // Get StarMade Name
            match = RegexPlayerAccount.Match(response[currentLine++]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "[PL] SM-NAME: {{Account}}", response[currentLine - 1]);
            playerInfo.StarMadeName = match.Groups["accountName"].Value.ClearStringNull();

            // Get player ip
            match = RegexPlayerIp.Match(response[currentLine++]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "[PL] IP: /{{Account}}", response[currentLine - 1]);
            if (!match.Groups["ipAddress"].Value.IsStringNull())
            {
                playerInfo.LastIpAddress = IPAddress.Parse(match.Groups["ipAddress"].Value);
            }

            // Get player name
            match = RegexPlayerName.Match(response[currentLine++]);
            StarNetHelpersCommon.ThrowUnexpectedResponseIfNotSuccess(match, "[PL] Name: {{name}}", response[currentLine - 1]);
            playerInfo.Name = match.Groups["playerName"].Value;

            return(playerInfo);
        }