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

Modified loadPokemonList #290

Merged
merged 4 commits into from
Jul 23, 2016
Merged
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
46 changes: 35 additions & 11 deletions PokemonGo.RocketAPI.Console/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using PokemonGo.RocketAPI.Enums;
using PokemonGo.RocketAPI.GeneratedCode;
using System.Text.RegularExpressions;

#endregion

Expand Down Expand Up @@ -72,7 +73,8 @@ public ICollection<PokemonId> PokemonsToEvolve
get
{
//Type of pokemons to evolve
_pokemonsToEvolve = _pokemonsToEvolve ?? LoadPokemonList("Configs\\ConfigPokemonsToEvolve.txt");
var defaultText = new string[] { "Zubat", "Pidgey", "Ratata" };
_pokemonsToEvolve = _pokemonsToEvolve ?? LoadPokemonList("Configs\\ConfigPokemonsToEvolve.txt", defaultText);
return _pokemonsToEvolve;
}
}
Expand All @@ -82,7 +84,8 @@ public ICollection<PokemonId> PokemonsNotToTransfer
get
{
//Type of pokemons not to transfer
_pokemonsNotToTransfer = _pokemonsNotToTransfer ?? LoadPokemonList("Configs\\ConfigPokemonsToKeep.txt");
var defaultText = new string[] { "Dragonite", "Charizard", "Zapdos", "Snorlax", "Alakhazam", "Mew", "Mewtwo" };
_pokemonsNotToTransfer = _pokemonsNotToTransfer ?? LoadPokemonList("Configs\\ConfigPokemonsToKeep.txt", defaultText);
return _pokemonsNotToTransfer;
}
}
Expand All @@ -93,38 +96,59 @@ public ICollection<PokemonId> PokemonsNotToCatch
get
{
//Type of pokemons not to catch
_pokemonsNotToCatch = _pokemonsNotToCatch ?? LoadPokemonList("Configs\\ConfigPokemonsNotToCatch.txt");
var defaultText = new string[] { "Zubat", "Pidgey", "Ratata" };
_pokemonsNotToCatch = _pokemonsNotToCatch ?? LoadPokemonList("Configs\\ConfigPokemonsNotToCatch.txt", defaultText);
return _pokemonsNotToCatch;
}
}

private static ICollection<PokemonId> LoadPokemonList(string filename)
private static ICollection<PokemonId> LoadPokemonList(string filename, string[] defaultContent)
{
ICollection<PokemonId> result = new List<PokemonId>();
Func<string, ICollection<PokemonId>> addPokemonToResult = delegate (string pokemonName) {
PokemonId pokemon;
if (Enum.TryParse<PokemonId>(pokemonName, out pokemon))
{
result.Add((PokemonId)pokemon);
}
return result;
};

DirectoryInfo di = Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Configs");

if (File.Exists(Directory.GetCurrentDirectory() + "\\" + filename))
{
Logger.Write($"Loading File: {filename}");
TextReader tr = File.OpenText(filename);

var content = string.Empty;
using (StreamReader reader = new StreamReader(filename))
{
content = reader.ReadToEnd();
reader.Close();
}

content = Regex.Replace(content, @"\\/\*(.|\n)*?\*\/", ""); //todo: supposed to remove comment blocks


StringReader tr = new StringReader(content);

var pokemonName = tr.ReadLine();
while (pokemonName != null)
{
var pokemon = Enum.Parse(typeof(PokemonId), pokemonName, true);
if (pokemon != null) result.Add((PokemonId) pokemon);
addPokemonToResult(pokemonName);
pokemonName = tr.ReadLine();
}
}
else
{
Logger.Write($"File: {filename} not found, creating new...", LogLevel.Error);
Logger.Write($"File: {filename} not found, creating new...", LogLevel.Warning);
using (var w = File.AppendText(Directory.GetCurrentDirectory() + "\\" + filename))
{
w.WriteLine(PokemonId.Mewtwo.ToString());
Array.ForEach(defaultContent, x => w.WriteLine(x));
Array.ForEach(defaultContent, x => addPokemonToResult(x));
w.Close();
}
}


return result;
}
}
Expand Down
2 changes: 1 addition & 1 deletion PokemonGo.RocketAPI.Logic/PokemonGo.RocketAPI.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Google.Protobuf">
<HintPath>..\PokemonGo.RocketAPI\bin\Debug\Google.Protobuf.dll</HintPath>
<HintPath>..\packages\Google.Protobuf.3.0.0-beta3\lib\dotnet\Google.Protobuf.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
Expand Down