Skip to content

Commit

Permalink
Merge pull request #4118 from msx752/master
Browse files Browse the repository at this point in the history
[now automatic] instantly update config.json file changes
  • Loading branch information
GrimmGringo committed Aug 9, 2016
2 parents 4e8747d + 0690981 commit 4ca17da
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
22 changes: 21 additions & 1 deletion PoGo.NecroBot.Logic/Settings.cs
Expand Up @@ -20,6 +20,7 @@
using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography;
using System.Threading;

#endregion

Expand Down Expand Up @@ -787,7 +788,26 @@ public static GlobalSettings Load(string path, bool boolSkipSave = false)
try
{
//if the file exists, load the settings
var input = File.ReadAllText(configFile);
string input = "";
int count = 0;
while (true)
{
try
{
input = File.ReadAllText(configFile);
break;
}
catch (Exception exception)
{
if (count > 10)
{
//sometimes we have to wait close to config.json for access
Logger.Write("configFile: " + exception.Message, LogLevel.Error);
}
count++;
Thread.Sleep(1000);
}
};

var jsonSettings = new JsonSerializerSettings();
jsonSettings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
Expand Down
6 changes: 3 additions & 3 deletions PoGo.NecroBot.Logic/State/Session.cs
Expand Up @@ -12,7 +12,7 @@ namespace PoGo.NecroBot.Logic.State
{
public interface ISession
{
ISettings Settings { get; }
ISettings Settings { get; set; }
Inventory Inventory { get; }
Client Client { get; }
GetPlayerResponse Profile { get; set; }
Expand All @@ -35,7 +35,7 @@ public Session(ISettings settings, ILogicSettings logicSettings)
Reset(settings, LogicSettings);
}

public ISettings Settings { get; }
public ISettings Settings { get; set; }

public Inventory Inventory { get; private set; }

Expand All @@ -44,7 +44,7 @@ public Session(ISettings settings, ILogicSettings logicSettings)
public GetPlayerResponse Profile { get; set; }
public Navigation Navigation { get; private set; }

public ILogicSettings LogicSettings { get; }
public ILogicSettings LogicSettings { get; set; }

public ITranslation Translation { get; }

Expand Down
22 changes: 22 additions & 0 deletions PoGo.NecroBot.Logic/State/StateMachine.cs
Expand Up @@ -5,6 +5,8 @@
using System.Threading.Tasks;
using PoGo.NecroBot.Logic.Event;
using PokemonGo.RocketAPI.Exceptions;
using System.IO;
using PoGo.NecroBot.Logic.Logging;

#endregion

Expand All @@ -29,6 +31,24 @@ public void SetFailureState(IState state)
CancellationToken cancellationToken = default(CancellationToken))
{
var state = initialState;
var profilePath = Path.Combine(Directory.GetCurrentDirectory(), "");
var profileConfigPath = Path.Combine(profilePath, "config");

FileSystemWatcher configWatcher = new FileSystemWatcher();
configWatcher.Path = profileConfigPath;
configWatcher.Filter = "config.json";
configWatcher.NotifyFilter = NotifyFilters.LastWrite;
configWatcher.EnableRaisingEvents = true;
configWatcher.Changed += (sender, e) =>
{
if (e.ChangeType == WatcherChangeTypes.Changed)
{
session.LogicSettings = new LogicSettings(GlobalSettings.Load(""));
configWatcher.EnableRaisingEvents = !configWatcher.EnableRaisingEvents;
configWatcher.EnableRaisingEvents = !configWatcher.EnableRaisingEvents;
Logger.Write(" ##### config.json ##### ", LogLevel.Info);
}
};
do
{
try
Expand All @@ -55,6 +75,8 @@ public void SetFailureState(IState state)
state = _initialState;
}
} while (state != null);
configWatcher.EnableRaisingEvents = false;
configWatcher.Dispose();
}
}
}

1 comment on commit 4ca17da

@xafelld
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are my hero!

Please sign in to comment.