Example #1
0
        /// <summary>
        /// Creates a connection object with a MockGame, including players and maps etc.
        /// </summary>
        /// <remarks>The plugin will be enabled during this process.</remarks>
        /// <returns></returns>
        protected ConnectionController CreateConnection() {
            ISandboxProtocolController protocol = new SandboxProtocolController() {
                SandboxedProtocol = new MockGame()
            };

            protocol.Setup(new ProtocolSetup() {
                Hostname = "localhost",
                Port = 9000
            });

            ConnectionController connection = new ConnectionController() {
                // This won't actually connect to anything.
                // It's just a mock so the GameState is available to be modified.
                // See MockGame for all the mock data we create.
                Protocol = protocol
            };

            // 1. When this is called you will see the constructor in the plugin executed.
            // Potato.Examples.TextCommands.Program 
            connection.Execute();

            // Note: Enable the single plugin that was loaded, otherwise it won't recieve any tunneled commands or events.

            // 2. When this is executed you will see a GenericEvent fired. Place your breakpoint in
            // Potato.Examples.TextCommands.Program.GenericEvent -> PluginsPluginEnabled
            connection.Tunnel(new Command() {
                Origin = CommandOrigin.Local,
                CommandType = CommandType.PluginsEnable,
                Scope = {
                    PluginGuid = connection.Plugins.LoadedPlugins.First().PluginGuid
                }
            });

            return connection;
        }
Example #2
0
        /// <summary>
        /// Add a connection to this instance.
        /// </summary>
        /// <param name="command"></param>
        /// <param name="parameters"></param>
        public ICommandResult PotatoAddConnection(ICommand command, Dictionary<String, ICommandParameter> parameters) {
            ICommandResult result = null;
            
            String protocolTypeProvider = parameters["gameTypeProvider"].First<String>() ?? "";
            String protocolTypeType = parameters["gameTypeType"].First<String>() ?? "";
            String hostName = parameters["hostName"].First<String>() ?? "";
            UInt16 port = parameters["port"].First<UInt16>();
            String password = parameters["password"].First<String>() ?? "";
            String additional = parameters["additional"].First<String>() ?? "";

            // As long as the current account is allowed to execute this command...
            if (this.Shared.Security.DispatchPermissionsCheck(command, command.Name).Success == true) {
                // As long as we have less than the maximum amount of connections...
                if (this.Connections.Count < this.Shared.Variables.Get(CommonVariableNames.MaximumProtocolConnections, 9000)) {
                    // As long as the connection for that specific game, hostname, and port does not exist...
                    if (this.Connections.FirstOrDefault(c => c.ConnectionModel.ProtocolType.Type == protocolTypeType && c.ConnectionModel.Hostname == hostName && c.ConnectionModel.Port == port) == null) {
                        // As long as the game type is defined...

                        var supportCheckResult = this.Protocols.Tunnel(CommandBuilder.ProtocolsCheckSupportedProtocol(protocolTypeProvider, protocolTypeType).SetOrigin(CommandOrigin.Local));

                        if (supportCheckResult.Success == true) {
                            IProtocolAssemblyMetadata meta = supportCheckResult.Now.ProtocolAssemblyMetadatas.First();
                            
                            ConnectionController connection = new ConnectionController() {
                                Potato = this
                            };

                            connection.SetupProtocol(supportCheckResult.Now.ProtocolAssemblyMetadatas.First(), supportCheckResult.Now.ProtocolTypes.First(), new ProtocolSetup() {
                                Hostname = hostName,
                                Port = port,
                                Password = password,
                                Arguments = ArgumentHelper.ToArguments(additional.Wordify()),
                                ConfigDirectory = meta.Directory.GetDirectories(Defines.ProtocolsDirectoryName, SearchOption.AllDirectories).Select(directory => directory.FullName).FirstOrDefault()
                            });

                            lock (this.Connections) {
                                this.Connections.Add(connection);
                            }

                            connection.Execute();
                            connection.AttemptConnection();

                            result = new CommandResult() {
                                Message = String.Format("Successfully added {0} connection.", protocolTypeType),
                                CommandResultType = CommandResultType.Success,
                                Success = true,
                                Now = {
                                    Connections = new List<ConnectionModel>() {
                                        connection.ConnectionModel
                                    }
                                }
                            };

                            this.Shared.Events.Log(GenericEvent.ConvertToGenericEvent(result, GenericEventType.PotatoConnectionAdded));
                        }
                        else {
                            result = new CommandResult() {
                                Message = String.Format(@"Protocol type ""{0}"" is not supported.", protocolTypeType),
                                CommandResultType = CommandResultType.DoesNotExists,
                                Success = false
                            };
                        }
                    }
                    else {
                        result = new CommandResult() {
                            Message = String.Format(@"Game type ""{0}"" with connection to {1}:{2} has already been added.", protocolTypeType, hostName, port),
                            CommandResultType = CommandResultType.AlreadyExists,
                            Success = false
                        };
                    }
                }
                else {
                    result = new CommandResult() {
                        Message = String.Format(@"Maximum number of game connections exceeded."),
                        CommandResultType = CommandResultType.LimitExceeded,
                        Success = false
                    };
                }
            }
            else {
                result = CommandResult.InsufficientPermissions;
            }

            return result;
        }