/// <summary>
        /// Executs the /player_list command and returns the list of players and information about each of them.
        /// </summary>
        /// <returns>List of players and their associated information</returns>
        public static PlayerInfo[] PlayerList(this StarNet.IStarNetSession session)
        {
            // Get the command response
            string[] response = session.ExecuteAdminCommand("/player_list");

            // 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("/player_list");
            }

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

            var players     = new List <PlayerInfo>();
            int currentLine = 0;

            // Loop and parse all the players.
            while (currentLine < response.Length)
            {
                players.Add(ParsePlayer(response, ref currentLine));
            }

            return(players.ToArray());
        }
        /// <summary>
        /// Executes the /player_info command for a supplied player
        /// </summary>
        /// <returns>Information about the player or null if the player didn't exist</returns>
        public static PlayerInfo PlayerInfo(this StarNet.IStarNetSession session, string playerName)
        {
            // Get the command response
            string[] response = session.ExecuteAdminCommand("/player_info " + playerName);

            // 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("/player_info " + playerName);
            }

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

            int currentLine = 0;

            // Parse and return the player
            return(ParsePlayer(response, ref currentLine));
        }
        /// <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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }