Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions PokemonGo.Haxton.Bot/PokemonGo.Haxton.Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<HintPath>..\packages\Google.Protobuf.3.0.0-beta4\lib\net45\Google.Protobuf.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Mono.Options, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Options.4.4.0.0\lib\net4-client\Mono.Options.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MoreLinq, Version=1.4.18916.0, Culture=neutral, PublicKeyToken=384d532d7e88985d, processorArchitecture=MSIL">
<HintPath>..\packages\morelinq.1.4.0\lib\net35\MoreLinq.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -85,6 +89,7 @@
<Compile Include="Navigation\PoGoEncounter.cs" />
<Compile Include="Navigation\PoGoFort.cs" />
<Compile Include="Navigation\PoGoMap.cs" />
<Compile Include="Settings\LineArguments.cs" />
<Compile Include="Settings\LogicSettings.cs" />
<Compile Include="Settings\Settings.cs" />
<Compile Include="Utilities\PoGoStatistics.cs" />
Expand Down
119 changes: 119 additions & 0 deletions PokemonGo.Haxton.Bot/Settings/LineArguments.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
10 changes: 5 additions & 5 deletions PokemonGo.Haxton.Bot/Settings/LogicSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
//
}
Expand All @@ -55,7 +55,7 @@ private IEnumerable<KeyValuePair<double, double>> GetLocations(string usersettin
private Dictionary<ItemId, int> GetItemRecycleFilter()
{
var dict = new Dictionary<ItemId, int>();
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(' ');
Expand Down
20 changes: 11 additions & 9 deletions PokemonGo.Haxton.Bot/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
1 change: 1 addition & 0 deletions PokemonGo.Haxton.Bot/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<packages>
<package id="Google.Protobuf" version="3.0.0-beta4" targetFramework="net461" />
<package id="Google.Protobuf.Tools" version="3.0.0-beta4" targetFramework="net461" />
<package id="Mono.Options" version="4.4.0.0" targetFramework="net452" />
<package id="morelinq" version="1.4.0" targetFramework="net461" />
<package id="NLog" version="4.4.0-betaV15" targetFramework="net461" />
<package id="POGOProtos" version="1.4.0" targetFramework="net461" />
Expand Down
39 changes: 30 additions & 9 deletions PokemonGo.Haxton.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,16 @@ private static void Main(string[] args)
_.For<IPoGoLogin>().Use<PoGoLogin>().Singleton();
_.For<IPoGoFort>().Use<PoGoFort>().Singleton();

_.For<ISettings>().Use<Settings>().Singleton();
_.For<ILineArguments>().Use<LineArguments>().Singleton();
_.For<ISettings>().Use<Settings>().Ctor<string[]>().Is(args).Singleton();
_.For<ILogicSettings>().Use<LogicSettings>().Singleton();
});
currentTasks.Add(RunTask(_cancelTokenSource.Token));
while (_LoggedIn == false && !_cancelTokenSource.Token.IsCancellationRequested)
{
Thread.Sleep(100);
}
if (_LoggedIn == false)
if (_LoggedIn == false && !LineArguments.ShowHelp)
{
logger.Warn("Failed to log in, retrying in 5 seconds");
Thread.Sleep(5000);
Expand Down Expand Up @@ -121,12 +122,20 @@ private static Task RunTask(CancellationToken _token)
try
{
var login = container.GetInstance<IPoGoLogin>();
login.DoLogin();
var bot = container.GetInstance<IPoGoBot>();
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<IPoGoBot>();
task = bot.Run(_token);
task.Add(Task.Run(async () => { await UpdateConsole(_token); }, _token));
_LoggedIn = true;
Task.WaitAny(task.ToArray());
}
}
catch (AggregateException e)
{
Expand All @@ -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)
{
Expand Down