From 5203be5ce879ec3a9a55aeb80709796e24bb4550 Mon Sep 17 00:00:00 2001 From: ChrisB Date: Mon, 1 Aug 2016 15:27:37 -0700 Subject: [PATCH 1/4] Changed POGOProtos version in API to 1.4 to match the bot version --- FeroxRev/PokemonGo.RocketAPI/PokemonGo.RocketAPI.csproj | 4 ++-- FeroxRev/PokemonGo.RocketAPI/Rpc/Encounter.cs | 2 +- FeroxRev/PokemonGo.RocketAPI/packages.config | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/FeroxRev/PokemonGo.RocketAPI/PokemonGo.RocketAPI.csproj b/FeroxRev/PokemonGo.RocketAPI/PokemonGo.RocketAPI.csproj index c2eed0e..4c102ad 100644 --- a/FeroxRev/PokemonGo.RocketAPI/PokemonGo.RocketAPI.csproj +++ b/FeroxRev/PokemonGo.RocketAPI/PokemonGo.RocketAPI.csproj @@ -54,8 +54,8 @@ $(SolutionDir)\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll True - - $(SolutionDir)\packages\POGOProtos.1.3.0\lib\net45\POGOProtos.dll + + ..\..\packages\POGOProtos.1.4.0\lib\net45\POGOProtos.dll True diff --git a/FeroxRev/PokemonGo.RocketAPI/Rpc/Encounter.cs b/FeroxRev/PokemonGo.RocketAPI/Rpc/Encounter.cs index 803e927..5e62a82 100644 --- a/FeroxRev/PokemonGo.RocketAPI/Rpc/Encounter.cs +++ b/FeroxRev/PokemonGo.RocketAPI/Rpc/Encounter.cs @@ -34,7 +34,7 @@ public async Task UseCaptureItem(ulong encounterId, Item { EncounterId = encounterId, ItemId = itemId, - SpawnPointGuid = spawnPointGuid + SpawnPointId = spawnPointGuid }; return await PostProtoPayload(RequestType.UseItemCapture, message); diff --git a/FeroxRev/PokemonGo.RocketAPI/packages.config b/FeroxRev/PokemonGo.RocketAPI/packages.config index 8df2ead..28b0ffa 100644 --- a/FeroxRev/PokemonGo.RocketAPI/packages.config +++ b/FeroxRev/PokemonGo.RocketAPI/packages.config @@ -8,7 +8,7 @@ - + \ No newline at end of file From 5aa6f4466a7acc0fc0c13a2e80c391927ec4bf05 Mon Sep 17 00:00:00 2001 From: ChrisB Date: Tue, 2 Aug 2016 12:26:42 -0700 Subject: [PATCH 2/4] added command line arguments --- .../PokemonGo.Haxton.Bot.csproj | 5 + .../Settings/LineArguments.cs | 106 ++++++++++++++++++ PokemonGo.Haxton.Bot/Settings/Settings.cs | 20 ++-- PokemonGo.Haxton.Bot/packages.config | 1 + PokemonGo.Haxton.Console/Program.cs | 30 +++-- 5 files changed, 144 insertions(+), 18 deletions(-) create mode 100644 PokemonGo.Haxton.Bot/Settings/LineArguments.cs diff --git a/PokemonGo.Haxton.Bot/PokemonGo.Haxton.Bot.csproj b/PokemonGo.Haxton.Bot/PokemonGo.Haxton.Bot.csproj index cdd32f4..4819875 100644 --- a/PokemonGo.Haxton.Bot/PokemonGo.Haxton.Bot.csproj +++ b/PokemonGo.Haxton.Bot/PokemonGo.Haxton.Bot.csproj @@ -35,6 +35,10 @@ ..\packages\Google.Protobuf.3.0.0-beta4\lib\net45\Google.Protobuf.dll True + + ..\packages\Mono.Options.4.4.0.0\lib\net4-client\Mono.Options.dll + True + ..\packages\morelinq.1.4.0\lib\net35\MoreLinq.dll True @@ -85,6 +89,7 @@ + diff --git a/PokemonGo.Haxton.Bot/Settings/LineArguments.cs b/PokemonGo.Haxton.Bot/Settings/LineArguments.cs new file mode 100644 index 0000000..e76670a --- /dev/null +++ b/PokemonGo.Haxton.Bot/Settings/LineArguments.cs @@ -0,0 +1,106 @@ +using PokemonGo.RocketAPI; +using PokemonGo.RocketAPI.Enums; +using System; +using System.Threading; +using System.Configuration; +using System.Globalization; +using System.IO; +using Mono.Options; +using NLog; + +namespace PokemonGo.Haxton.Bot.Settings +{ + public interface ILineArguments + { + Configuration GetConfig(string[] args); + } + public class LineArguments : ILineArguments + { + private static readonly Logger logger = LogManager.GetCurrentClassLogger(); + private Configuration Config { get; set; } + + public LineArguments() + { + Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); + } + + public static bool ShowHelp = false; + public Configuration GetConfig(string[] args) + { + ParseArguments(args); + return Config; + } + + private void ParseArguments(string[] args) + { + var options = GetOptions(); + + try + { + options.Parse(args); + } + catch (OptionException e) + { + logger.Error(e); + ShowHelp = true; + } + + if(ShowHelp) + { + options.WriteOptionDescriptions(Console.Out); + } + } + + private OptionSet GetOptions() + { + var options = new OptionSet() + { + { "config=", "Directory of config file(see default config for formatting)", config => { + ExeConfigurationFileMap configMap = new ExeConfigurationFileMap(); + configMap.ExeConfigFilename = config; + Config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None); + } }, + { "auth=", "Type of login 'Google' or 'Ptc'", auth => { + if (auth != "Google" && auth != "Ptc") + throw new OptionException("auth arugment must be either 'Google' or 'Ptc'", "auth="); + ChangeConfigKey("AccountType", auth); + } }, + { "username=", "Username of Google or Ptc account", username => { + if (Config.AppSettings.Settings["AccountType"].Value == "Google") + ChangeConfigKey("GoogleEmail", username); + else if (Config.AppSettings.Settings["AccountType"].Value == "Ptc") + ChangeConfigKey("PtcUsername", username); + else + throw new OptionException("Invalid auth specified. Use -auth= or edit the config to set auth type", "username="); + } }, + { "password=", "Password of Google or Ptc account", password => { + if (Config.AppSettings.Settings["AccountType"].Value == "Google") + ChangeConfigKey("GooglePassword", password); + else if (Config.AppSettings.Settings["AccountType"].Value == "Ptc") + ChangeConfigKey("PtcPassword", password); + else + throw new OptionException("Invalid auth specified. Use -auth= or edit the config to set auth type", "password="); + } }, + { "latitude=", "Default latitude of Pokemon trainer", latitude => ChangeConfigKey("DefaultLatitude", latitude)}, + { "longitude=", "Default longitude of Pokemon trainer", longitude => ChangeConfigKey("DefaultLongitude", longitude)}, + { "altitude=", "Default altitude of Pokemon trainer", altitude => ChangeConfigKey("DefaultAltitude", altitude) }, + { "h|help", "Display command line argument help", h => ShowHelp = h != null} + }; + + return (options); + } + + private void ChangeConfigKey(string key, string value) + { + //this does not actually save anything to the file + var element = Config.AppSettings.Settings[key]; + if (element != null) + { + + Config.AppSettings.Settings.Remove(key); + element.Value = value; + Config.AppSettings.Settings.Add(element); + } + } + } +} diff --git a/PokemonGo.Haxton.Bot/Settings/Settings.cs b/PokemonGo.Haxton.Bot/Settings/Settings.cs index dc6def7..c7a50e0 100644 --- a/PokemonGo.Haxton.Bot/Settings/Settings.cs +++ b/PokemonGo.Haxton.Bot/Settings/Settings.cs @@ -9,18 +9,20 @@ namespace PokemonGo.Haxton.Bot.Settings { public class Settings : ISettings { - public Settings() + public Settings(LineArguments lineArguments, string[] args) { + Configuration config = lineArguments.GetConfig(args); + AuthType = - (AuthType)Enum.Parse(typeof(AuthType), ConfigurationManager.AppSettings["AccountType"]); - DefaultLatitude = Convert.ToDouble(ConfigurationManager.AppSettings["DefaultLatitude"], CultureInfo.InvariantCulture); - DefaultLongitude = Convert.ToDouble(ConfigurationManager.AppSettings["DefaultLongitude"], CultureInfo.InvariantCulture); - DefaultAltitude = Convert.ToDouble(ConfigurationManager.AppSettings["DefaultAltitude"], CultureInfo.InvariantCulture); - PtcUsername = ConfigurationManager.AppSettings["PtcUsername"]; - PtcPassword = ConfigurationManager.AppSettings["PtcPassword"]; + (AuthType)Enum.Parse(typeof(AuthType), config.AppSettings.Settings["AccountType"].Value); + DefaultLatitude = Convert.ToDouble(config.AppSettings.Settings["DefaultLatitude"].Value, CultureInfo.InvariantCulture); + DefaultLongitude = Convert.ToDouble(config.AppSettings.Settings["DefaultLongitude"].Value, CultureInfo.InvariantCulture); + DefaultAltitude = Convert.ToDouble(config.AppSettings.Settings["DefaultAltitude"].Value, CultureInfo.InvariantCulture); + PtcUsername = config.AppSettings.Settings["PtcUsername"].Value; + PtcPassword = config.AppSettings.Settings["PtcPassword"].Value; - GoogleUsername = ConfigurationManager.AppSettings["GoogleEmail"]; - GooglePassword = ConfigurationManager.AppSettings["GooglePassword"]; + GoogleUsername = config.AppSettings.Settings["GoogleEmail"].Value; + GooglePassword = config.AppSettings.Settings["GooglePassword"].Value; } public AuthType AuthType { get; } diff --git a/PokemonGo.Haxton.Bot/packages.config b/PokemonGo.Haxton.Bot/packages.config index 7835bac..d9400a9 100644 --- a/PokemonGo.Haxton.Bot/packages.config +++ b/PokemonGo.Haxton.Bot/packages.config @@ -2,6 +2,7 @@ + diff --git a/PokemonGo.Haxton.Console/Program.cs b/PokemonGo.Haxton.Console/Program.cs index 5eab472..4a9d603 100644 --- a/PokemonGo.Haxton.Console/Program.cs +++ b/PokemonGo.Haxton.Console/Program.cs @@ -65,7 +65,8 @@ private static void Main(string[] args) _.For().Use().Singleton(); _.For().Use().Singleton(); - _.For().Use().Singleton(); + _.For().Use().Singleton(); + _.For().Use().Ctor().Is(args).Singleton(); _.For().Use().Singleton(); }); currentTasks.Add(RunTask(_cancelTokenSource.Token)); @@ -73,7 +74,7 @@ private static void Main(string[] args) { Thread.Sleep(100); } - if (_LoggedIn == false) + if (_LoggedIn == false && !LineArguments.ShowHelp) { logger.Warn("Failed to log in, retrying in 5 seconds"); Thread.Sleep(5000); @@ -121,12 +122,20 @@ private static Task RunTask(CancellationToken _token) try { var login = container.GetInstance(); - login.DoLogin(); - var bot = container.GetInstance(); - task = bot.Run(_token); - task.Add(Task.Run(async () => { await UpdateConsole(_token); }, _token)); - _LoggedIn = true; - Task.WaitAny(task.ToArray()); + if (LineArguments.ShowHelp) + { + ShouldRun = false; + _cancelTokenSource.Cancel(); + } + else + { + login.DoLogin(); + var bot = container.GetInstance(); + task = bot.Run(_token); + task.Add(Task.Run(async () => { await UpdateConsole(_token); }, _token)); + _LoggedIn = true; + Task.WaitAny(task.ToArray()); + } } catch (AggregateException e) { @@ -143,7 +152,10 @@ private static Task RunTask(CancellationToken _token) } finally { - logger.Fatal("Task crashed or cancelled"); + if (!_token.IsCancellationRequested) + { + logger.Fatal("Task crashed or cancelled"); + } // for the case some tasks crashed if (_token.CanBeCanceled) { From c7d75c93b9de43f98c79b6e5e67d358a824bb9d2 Mon Sep 17 00:00:00 2001 From: ChrisB Date: Tue, 2 Aug 2016 13:12:23 -0700 Subject: [PATCH 3/4] error handling and use base directory to find files --- .../Settings/LineArguments.cs | 25 ++++++++++++++----- .../Settings/LogicSettings.cs | 10 ++++---- PokemonGo.Haxton.Console/Program.cs | 9 +++++++ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/PokemonGo.Haxton.Bot/Settings/LineArguments.cs b/PokemonGo.Haxton.Bot/Settings/LineArguments.cs index e76670a..ad29c7f 100644 --- a/PokemonGo.Haxton.Bot/Settings/LineArguments.cs +++ b/PokemonGo.Haxton.Bot/Settings/LineArguments.cs @@ -1,10 +1,5 @@ -using PokemonGo.RocketAPI; -using PokemonGo.RocketAPI.Enums; -using System; -using System.Threading; +using System; using System.Configuration; -using System.Globalization; -using System.IO; using Mono.Options; using NLog; @@ -59,27 +54,45 @@ private OptionSet GetOptions() ExeConfigurationFileMap configMap = new ExeConfigurationFileMap(); configMap.ExeConfigFilename = config; Config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None); + if (!Config.HasFile) + { + throw new OptionException("config argument could not find the specified file at " + Config.FilePath, "config="); + } } }, { "auth=", "Type of login 'Google' or 'Ptc'", auth => { if (auth != "Google" && auth != "Ptc") + { throw new OptionException("auth arugment must be either 'Google' or 'Ptc'", "auth="); + } ChangeConfigKey("AccountType", auth); } }, { "username=", "Username of Google or Ptc account", username => { if (Config.AppSettings.Settings["AccountType"].Value == "Google") + { ChangeConfigKey("GoogleEmail", username); + } else if (Config.AppSettings.Settings["AccountType"].Value == "Ptc") + { ChangeConfigKey("PtcUsername", username); + } else + { throw new OptionException("Invalid auth specified. Use -auth= or edit the config to set auth type", "username="); + } } }, { "password=", "Password of Google or Ptc account", password => { if (Config.AppSettings.Settings["AccountType"].Value == "Google") + { ChangeConfigKey("GooglePassword", password); + } else if (Config.AppSettings.Settings["AccountType"].Value == "Ptc") + { ChangeConfigKey("PtcPassword", password); + } else + { throw new OptionException("Invalid auth specified. Use -auth= or edit the config to set auth type", "password="); + } } }, { "latitude=", "Default latitude of Pokemon trainer", latitude => ChangeConfigKey("DefaultLatitude", latitude)}, { "longitude=", "Default longitude of Pokemon trainer", longitude => ChangeConfigKey("DefaultLongitude", longitude)}, diff --git a/PokemonGo.Haxton.Bot/Settings/LogicSettings.cs b/PokemonGo.Haxton.Bot/Settings/LogicSettings.cs index 168409b..667bd32 100644 --- a/PokemonGo.Haxton.Bot/Settings/LogicSettings.cs +++ b/PokemonGo.Haxton.Bot/Settings/LogicSettings.cs @@ -27,10 +27,10 @@ public LogicSettings() GpxFile = ConfigurationManager.AppSettings["GpxFile"]; UseLuckyEggsWhileEvolving = Convert.ToBoolean(ConfigurationManager.AppSettings["UseLuckyEggs"]); ItemRecycleFilter = GetItemRecycleFilter(); - PokemonsToEvolve = GetPokemon("./UserSettings/PokemonToEvolve.cfg"); - PokemonsNotToTransfer = GetPokemon("./UserSettings/PokemonToKeep.cfg"); - PokemonsNotToCatch = GetPokemon("./UserSettings/PokemonToAvoid.cfg"); - LocationsToVisit = GetLocations("./UserSettings/LocationsToCycle.cfg"); + PokemonsToEvolve = GetPokemon(AppDomain.CurrentDomain.BaseDirectory + "/UserSettings/PokemonToEvolve.cfg"); + PokemonsNotToTransfer = GetPokemon(AppDomain.CurrentDomain.BaseDirectory + "/UserSettings/PokemonToKeep.cfg"); + PokemonsNotToCatch = GetPokemon(AppDomain.CurrentDomain.BaseDirectory + "/UserSettings/PokemonToAvoid.cfg"); + LocationsToVisit = GetLocations(AppDomain.CurrentDomain.BaseDirectory + "/UserSettings/LocationsToCycle.cfg"); BurstMode = Convert.ToBoolean(ConfigurationManager.AppSettings["UseBurstMode"]); // } @@ -55,7 +55,7 @@ private IEnumerable> GetLocations(string usersettin private Dictionary GetItemRecycleFilter() { var dict = new Dictionary(); - var text = File.ReadAllLines("./UserSettings/ItemListAndCount.cfg"); + var text = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "/UserSettings/ItemListAndCount.cfg"); foreach (var line in text) { var kvp = line.Split(' '); diff --git a/PokemonGo.Haxton.Console/Program.cs b/PokemonGo.Haxton.Console/Program.cs index 4a9d603..2f74d59 100644 --- a/PokemonGo.Haxton.Console/Program.cs +++ b/PokemonGo.Haxton.Console/Program.cs @@ -150,8 +150,17 @@ private static Task RunTask(CancellationToken _token) logger.Fatal(" Exception: {0}", v.GetType().Name); } } + catch (Exception e) + { + logger.Error(e); + } finally { + if (LineArguments.ShowHelp) + { + ShouldRun = false; + _cancelTokenSource.Cancel(); + } if (!_token.IsCancellationRequested) { logger.Fatal("Task crashed or cancelled"); From afc31d3d62f59050a3c7461393fd8e3af27adeda Mon Sep 17 00:00:00 2001 From: ChrisB Date: Tue, 2 Aug 2016 13:21:23 -0700 Subject: [PATCH 4/4] This reverts commit 5203be5ce879ec3a9a55aeb80709796e24bb4550. --- FeroxRev/PokemonGo.RocketAPI/PokemonGo.RocketAPI.csproj | 4 ++-- FeroxRev/PokemonGo.RocketAPI/Rpc/Encounter.cs | 2 +- FeroxRev/PokemonGo.RocketAPI/packages.config | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/FeroxRev/PokemonGo.RocketAPI/PokemonGo.RocketAPI.csproj b/FeroxRev/PokemonGo.RocketAPI/PokemonGo.RocketAPI.csproj index 4c102ad..c2eed0e 100644 --- a/FeroxRev/PokemonGo.RocketAPI/PokemonGo.RocketAPI.csproj +++ b/FeroxRev/PokemonGo.RocketAPI/PokemonGo.RocketAPI.csproj @@ -54,8 +54,8 @@ $(SolutionDir)\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll True - - ..\..\packages\POGOProtos.1.4.0\lib\net45\POGOProtos.dll + + $(SolutionDir)\packages\POGOProtos.1.3.0\lib\net45\POGOProtos.dll True diff --git a/FeroxRev/PokemonGo.RocketAPI/Rpc/Encounter.cs b/FeroxRev/PokemonGo.RocketAPI/Rpc/Encounter.cs index 5e62a82..803e927 100644 --- a/FeroxRev/PokemonGo.RocketAPI/Rpc/Encounter.cs +++ b/FeroxRev/PokemonGo.RocketAPI/Rpc/Encounter.cs @@ -34,7 +34,7 @@ public async Task UseCaptureItem(ulong encounterId, Item { EncounterId = encounterId, ItemId = itemId, - SpawnPointId = spawnPointGuid + SpawnPointGuid = spawnPointGuid }; return await PostProtoPayload(RequestType.UseItemCapture, message); diff --git a/FeroxRev/PokemonGo.RocketAPI/packages.config b/FeroxRev/PokemonGo.RocketAPI/packages.config index 28b0ffa..8df2ead 100644 --- a/FeroxRev/PokemonGo.RocketAPI/packages.config +++ b/FeroxRev/PokemonGo.RocketAPI/packages.config @@ -8,7 +8,7 @@ - + \ No newline at end of file