Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean profile/translation path logic #1083

Closed
wants to merge 2 commits into from
Closed
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: 3 additions & 2 deletions PoGo.NecroBot.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ private static void Main(string[] args)

if (settings == null)
{
Logger.Write("This is your first start and the bot will use the default config!", LogLevel.Warning);
Logger.Write("Continue? (y/n)", LogLevel.Warning);
Logger.Write("First start conditions detected! A default config.json has been generated for you.", LogLevel.Warning);
Logger.Write("This warning will not appear again. Continue with default settings? (y/n)", LogLevel.Warning);

if (!Console.ReadLine().ToUpper().Equals("Y"))
return;

settings = GlobalSettings.Load(subPath);
}

Expand Down
15 changes: 6 additions & 9 deletions PoGo.NecroBot.Logic/Common/Translations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using PoGo.NecroBot.Logic.State;

#endregion

namespace PoGo.NecroBot.Logic.Common
{
public class Translations
{
public static string ProfilePath;
public static string ConfigPath;

//Default Translations (ENGLISH)
public List<KeyValuePair<TranslationString, string>> TranslationStrings = new List
<KeyValuePair<TranslationString, string>>
Expand Down Expand Up @@ -138,12 +136,11 @@ public string GetTranslation(TranslationString translationString)
return translation != default(string) ? translation : $"Translation for {translationString} is missing";
}

public static Translations Load(string translationsLanguageCode)
public static Translations Load(ILogicSettings logicSettings)
{
ProfilePath = Directory.GetCurrentDirectory();
ConfigPath = Path.Combine(ProfilePath, "config", "translations");

var fullPath = Path.Combine(ConfigPath, "translation." + translationsLanguageCode + ".json");
string translationsLanguageCode = logicSettings.TranslationLanguageCode;
var translationPath = Path.Combine(logicSettings.GeneralConfigPath, "translations");
var fullPath = Path.Combine(translationPath, "translation." + translationsLanguageCode + ".json");

Translations translations;
if (File.Exists(fullPath))
Expand All @@ -161,7 +158,7 @@ public static Translations Load(string translationsLanguageCode)
else
{
translations = new Translations();
translations.Save(Path.Combine(ConfigPath, "translation.en.json"));
translations.Save(Path.Combine(translationPath, "translation.en.json"));
}
return translations;
}
Expand Down
3 changes: 2 additions & 1 deletion PoGo.NecroBot.Logic/ILogicSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public interface ILogicSettings
int AmountOfPokemonToDisplayOnStart { get; }
string TranslationLanguageCode { get; }
string ProfilePath { get; }
string ConfigPath { get; }
string ProfileConfigPath { get; }
string GeneralConfigPath { get; }

ICollection<KeyValuePair<ItemId, int>> ItemRecycleFilter { get; }

Expand Down
37 changes: 21 additions & 16 deletions PoGo.NecroBot.Logic/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ public class GlobalSettings

[JsonIgnore] internal AuthSettings Auth = new AuthSettings();

[JsonIgnore] public string ProfilePath;
[JsonIgnore] public string ProfileConfigPath;
[JsonIgnore] public string GeneralConfigPath;

public bool AutoUpdate = true;
public string ConfigPath;
public double DefaultAltitude = 10;
public double DefaultLatitude = 40.785091;
public double DefaultLongitude = -73.968285;
Expand Down Expand Up @@ -231,7 +234,6 @@ public class GlobalSettings
};

public bool PrioritizeIvOverCp = false;
public string ProfilePath;
public bool RenameAboveIv = false;
public bool TransferDuplicatePokemon = true;
public string TranslationLanguageCode = "en";
Expand All @@ -248,13 +250,13 @@ public static GlobalSettings Load(string path)
{
GlobalSettings settings;
var profilePath = Path.Combine(Directory.GetCurrentDirectory(), path);
var configPath = Path.Combine(profilePath, "config");
var fullPath = Path.Combine(configPath, "config.json");
var profileConfigPath = Path.Combine(profilePath, "config");
var configFile = Path.Combine(profileConfigPath, "config.json");

if (File.Exists(fullPath))
if (File.Exists(configFile))
{
//if the file exists, load the settings
var input = File.ReadAllText(fullPath);
var input = File.ReadAllText(configFile);

var jsonSettings = new JsonSerializerSettings();
jsonSettings.Converters.Add(new StringEnumConverter {CamelCaseText = true});
Expand All @@ -273,16 +275,18 @@ public static GlobalSettings Load(string path)
settings.WebSocketPort = 14251;
}
settings.ProfilePath = profilePath;
settings.ConfigPath = configPath;

if (!File.Exists(fullPath))
{
settings.Save(fullPath);
return null;
}

settings.ProfileConfigPath = profileConfigPath;
settings.GeneralConfigPath = Directory.GetCurrentDirectory();


var firstRun = !File.Exists(configFile);

settings.Save(configFile);

if (firstRun) return null;

settings.Auth.Load(Path.Combine(configPath, "auth.json"));
settings.Auth.Load(Path.Combine(profileConfigPath, "auth.json"));

return settings;
}
Expand Down Expand Up @@ -341,7 +345,8 @@ public LogicSettings(GlobalSettings settings)
}

public string ProfilePath => _settings.ProfilePath;
public string ConfigPath => _settings.ConfigPath;
public string ProfileConfigPath => _settings.ProfileConfigPath;
public string GeneralConfigPath => _settings.GeneralConfigPath;
public bool AutoUpdate => _settings.AutoUpdate;
public float KeepMinIvPercentage => _settings.KeepMinIvPercentage;
public int KeepMinCp => _settings.KeepMinCp;
Expand Down Expand Up @@ -370,4 +375,4 @@ public LogicSettings(GlobalSettings settings)
public ICollection<PokemonId> PokemonsNotToCatch => _settings.PokemonsToIgnore;
public Dictionary<PokemonId, TransferFilter> PokemonsTransferFilter => _settings.PokemonsTransferFilter;
}
}
}
4 changes: 2 additions & 2 deletions PoGo.NecroBot.Logic/State/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Session(ISettings settings, ILogicSettings logicSettings)
Settings = settings;
LogicSettings = logicSettings;
EventDispatcher = new EventDispatcher();
Translations = Translations.Load(logicSettings.TranslationLanguageCode);
Translations = Translations.Load(logicSettings);
Reset(settings, LogicSettings);
}

Expand All @@ -56,4 +56,4 @@ public void Reset(ISettings settings, ILogicSettings logicSettings)
Navigation = new Navigation(Client);
}
}
}
}