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..ad29c7f --- /dev/null +++ b/PokemonGo.Haxton.Bot/Settings/LineArguments.cs @@ -0,0 +1,119 @@ +using System; +using System.Configuration; +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); + 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)}, + { "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/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.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..2f74d59 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) { @@ -141,9 +150,21 @@ private static Task RunTask(CancellationToken _token) logger.Fatal(" Exception: {0}", v.GetType().Name); } } + catch (Exception e) + { + logger.Error(e); + } finally { - logger.Fatal("Task crashed or cancelled"); + if (LineArguments.ShowHelp) + { + ShouldRun = false; + _cancelTokenSource.Cancel(); + } + if (!_token.IsCancellationRequested) + { + logger.Fatal("Task crashed or cancelled"); + } // for the case some tasks crashed if (_token.CanBeCanceled) {