Esempio n. 1
0
        //In this instance, console input is sent to the dispatcher. CommandModule.Execute listens for
        //strings and tries to parse them as commands, then uses the dispatcher to call their respective handlers.
        //Any text input from any source can be used for this process.
        private static async Task ReadInput(TeraClient client, Task runningTask)
        {
            while (true)
            {
                //Console.ReadLine is blocking so wrap it in Task.Run to execute asynchronously.
                var inputTask = Task.Run(() => Console.ReadLine());
                await Task.WhenAny(inputTask, runningTask);

                //Exit the loop if the bot has disconnected.
                if (runningTask.IsCompleted)
                {
                    break;
                }
                var input = inputTask.Result;
                try
                {
                    if (input != null)
                    {
                        await client.Dispatcher.Notify(input);
                    }
                }
                catch (MyTeraException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (Exception)
                {
                    client.Disconnect();
                    throw;
                }
            }
        }
Esempio n. 2
0
        private static async Task MainAsync(string[] args)
        {
            if (args.Length != 4)
            {
                Console.WriteLine("Arguments: email password realmName characterName");
                return;
            }
            var(email, password, realmName, characterName) = (args[0], args[1], args[2], args[3]);

            //Configuration for the client created below.
            var configuration = await GetOrCreateConfiguration(email, password, realmName, characterName);

            //The dispatcher is the component responsible of routing packets, system messages,
            //commands and objects in general to its registered modules. It's configured below.
            var dispatcherConfiguration = new DispatcherConfiguration
            {
                //Register all the listeners and commands in types that derive from MyModuleBase.
                ModuleTypes = new HashSet <Type>(typeof(Program).Assembly.DefinedTypes
                                                 .Where(type => type.IsSubclassOf(typeof(MyModuleBase)))),
                //Register the type to convert values that will be passed to commands.
                ConverterType = typeof(ObjectConverter)
            };
            var client = new TeraClient(configuration, dispatcherConfiguration);
            //The client will authenticate and connect to the realm and packet dispatching will start.
            var runningTask = client.Run();

            try
            {
                //Wait until the client disconnects or the input task terminates upon unexpected exception.
                await Task.WhenAll(runningTask, ReadInput(client, runningTask));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                Console.WriteLine("Press a key to end the program.");
                Console.ReadKey();
            }
        }