This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
/
GlobalSettings.cs
1161 lines (990 loc) · 54.6 KB
/
GlobalSettings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Schema.Generation;
using PoGo.NecroBot.Logic.Common;
using PoGo.NecroBot.Logic.Logging;
using PoGo.NecroBot.Logic.Service.Elevation;
using PoGo.NecroBot.Logic.State;
using PokemonGo.RocketAPI.Enums;
using PokemonGo.RocketAPI.Extensions;
using POGOProtos.Enums;
using POGOProtos.Inventory.Item;
#endregion
namespace PoGo.NecroBot.Logic.Model.Settings
{
[JsonObject(Title = " Global Settings", Description = "Set your global settings.", ItemRequired = Required.DisallowNull)]
public class GlobalSettings
{
[JsonIgnore]
public AuthSettings Auth = new AuthSettings();
[JsonIgnore]
public string GeneralConfigPath;
[JsonIgnore]
public string ProfileConfigPath;
[JsonIgnore]
public string ProfilePath;
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
[NecrobotConfig(SheetName = "ConsoleConfig", Description = "Setting up the console output")]
public ConsoleConfig ConsoleConfig = new ConsoleConfig();
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
[NecrobotConfig(SheetName = "UpdateConfig", Description = "Setting up the auto checking for every time bot start up")]
public UpdateConfig UpdateConfig = new UpdateConfig();
[NecrobotConfig(SheetName = "WebsocketsConfig", Description = "Setting up the web socket that allow bot to communicate with Visualizer.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public WebsocketsConfig WebsocketsConfig = new WebsocketsConfig();
[NecrobotConfig(SheetName = "LocationConfig", Description = "Setting up location setting for bot.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public LocationConfig LocationConfig = new LocationConfig();
[NecrobotConfig(SheetName = "TelegramConfig", Description = "Setting up Telegram API to allow control bot from Telegram .")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public TelegramConfig TelegramConfig = new TelegramConfig();
[NecrobotConfig(SheetName = "GPXConfig", Description = "Setup GPS Pathing for bot.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public GpxConfig GPXConfig = new GpxConfig();
[NecrobotConfig(SheetName = "SnipeConfig", Description = "Setting up option for snipe.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public SnipeConfig SnipeConfig = new SnipeConfig();
[NecrobotConfig(SheetName = "HumanWalkSnipeConfig", Description = "Setting up option for human walk snipe.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public HumanWalkSnipeConfig HumanWalkSnipeConfig = new HumanWalkSnipeConfig();
[NecrobotConfig(SheetName = "DataSharingConfig", Description = "Setting up data socket sharing.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public DataSharingConfig DataSharingConfig = new DataSharingConfig();
[NecrobotConfig(SheetName = "PokeStopConfig", Description = "Setting up for farming pokestop.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public PokeStopConfig PokeStopConfig = new PokeStopConfig();
[NecrobotConfig(SheetName = "GymConfig", Description = "Setting up for gym and battle.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public GymConfig GymConfig = new GymConfig();
[NecrobotConfig(SheetName = "PokemonConfig", Description = "Setting up for pokemon catching, evolve, transfer, upgrade.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public PokemonConfig PokemonConfig = new PokemonConfig();
[NecrobotConfig(SheetName = "RecycleConfig", Description = "Setting up for inventory cleanup.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public ItemRecycleConfig RecycleConfig = new ItemRecycleConfig();
[NecrobotConfig(SheetName = "CustomCatchConfig", Description = "Setting up for some custom parametter for catching.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public CustomCatchConfig CustomCatchConfig = new CustomCatchConfig();
[NecrobotConfig(SheetName = "PlayerConfig", Description = "Setting up for some custom parametter for bot perfom user action.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public PlayerConfig PlayerConfig = new PlayerConfig();
[NecrobotConfig(SheetName = "SoftBanConfig", Description = "Setting up for softban resolve.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public SoftBanConfig SoftBanConfig = new SoftBanConfig();
[NecrobotConfig(SheetName = "GoogleWalkConfig", Description = "Setup parametter for google walk such as api key, account, rules..")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public GoogleWalkConfig GoogleWalkConfig = new GoogleWalkConfig();
[NecrobotConfig(SheetName = "YoursWalkConfig", Description = "Setup parametter for YoursWalkConfig walk such as api key, account, rules..")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public YoursWalkConfig YoursWalkConfig = new YoursWalkConfig();
[NecrobotConfig(SheetName = "MapzenWalkConfig", Description = "Setup parametter for MapzenWalkConfig walk such as api key, account, rules..")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public MapzenWalkConfig MapzenWalkConfig = new MapzenWalkConfig();
[NecrobotConfig(SheetName = "ItemRecycleFilter", Description = "Set number of each item we want bot to keep when it perfom recycle for get free space")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public List<ItemRecycleFilter> ItemRecycleFilter = Settings.ItemRecycleFilter.ItemRecycleFilterDefault();
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public List<PokemonId> PokemonsNotToTransfer = TransferConfig.PokemonsNotToTransferDefault();
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public CatchSettings PokemonToCatchLocally = CatchSettings.Default();
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public List<PokemonId> PokemonsToLevelUp = LevelUpConfig.PokemonsToLevelUpDefault();
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public List<PokemonId> PokemonsToIgnore = CatchConfig.PokemonsToIgnoreDefault();
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
[NecrobotConfig(SheetName = "CaptchaConfig", Description = "Captcha config to define the way you prefer to resolve captcha")]
public CaptchaConfig CaptchaConfig = new CaptchaConfig();
/// <summary>
/// this will auto add account to auth.json with this systax by command lin
/// -i true -t abd123{0} -s 10 -e 20 -p abc1234
/// or -init true -template abd123{0} -start 10 -eend 20 -password abc1234
/// above command will add 20 account to auth.json default is ptc account. -g true will turn on google account
/// </summary>
/// <param name="isGoogle"></param>
/// <param name="template"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="password"></param>
public void GenerateAccount(bool isGoogle, string template, int start, int end, string password)
{
List<AuthConfig> allAcc = new List<AuthConfig>();
for (int i = start; i < end; i++)
{
allAcc.Add(new AuthConfig()
{
AuthType = isGoogle ? AuthType.Google : AuthType.Ptc,
Username = string.Format(template, i),
Password = password
});
}
Auth.Bots = allAcc.ToList();
string json = JsonConvert.SerializeObject(Auth, Formatting.Indented,new StringEnumConverter() { CamelCaseText = true });
File.WriteAllText("config\\auth.json", json);
if (File.Exists("accounts.db")) File.Delete("accounts.db");
}
[NecrobotConfig(SheetName = "PokemonsTransferFilter", Description = "Setting up pokemon filter rules")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public Dictionary<PokemonId, TransferFilter> PokemonsTransferFilter = TransferFilter.TransferFilterDefault();
[NecrobotConfig(SheetName = "Item Use Filters", Description = "Define logic to use item when catching Pokemon")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public Dictionary<ItemId, ItemUseFilter> ItemUseFilters = ItemUseFilter.Default();
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public List<PokemonId> PokemonToUseMasterball = CatchConfig.PokemonsToUseMasterballDefault();
[NecrobotConfig(Description = "Setting up human walk snipe filter by pokemon", SheetName = "HumanWalkSnipeFilter")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public Dictionary<PokemonId, HumanWalkSnipeFilter> HumanWalkSnipeFilters = HumanWalkSnipeFilter.Default();
[NecrobotConfig(Description = "Setting up pokemon filter for level up", SheetName = "PokemonUpgradeFilter")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public Dictionary<PokemonId, UpgradeFilter> PokemonUpgradeFilters = UpgradeFilter.Default();
[NecrobotConfig(Description = "Setting up bot to use multiple account", SheetName = "MultipleBotConfig")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public MultipleBotConfig MultipleBotConfig = MultipleBotConfig.Default();
[NecrobotConfig(Description = "Setting up notifications setting", SheetName = "NotificationConfig")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public NotificationConfig NotificationConfig = new NotificationConfig();
[NecrobotConfig(SheetName = "SnipePokemonFilter", Description = "Setup list pokemon for auto snipe")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public Dictionary<PokemonId, SnipeFilter> SnipePokemonFilter = SnipeFilter.SniperFilterDefault();
[NecrobotConfig(SheetName = "PokemonEvolveFilter", Description = "Setup list pokemon for auto evolve")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public Dictionary<PokemonId, EvolveFilter> PokemonEvolveFilter = EvolveFilter.Default();
[NecrobotConfig(SheetName = "CatchPokemonFilter", Description = "Setup list pokemon for catch")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public Dictionary<PokemonId, CatchFilter> CatchPokemonFilter = CatchFilter.Default();
[NecrobotConfig(SheetName = "BotSwitchPokemonFilter", Description = "Define the filter to switch bot in multiple account mode.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public Dictionary<PokemonId, BotSwitchPokemonFilter> BotSwitchPokemonFilters = BotSwitchPokemonFilter.Default();
[NecrobotConfig(SheetName = "UIConfig", Description = "Define all parametter to display data on UI.")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public GUIConfig UIConfig = new GUIConfig();
[NecrobotConfig(SheetName = "HumanlikeDelays", Description = "Define the delays for humanlike behaviour when catching pokemon")]
[JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
public HumanlikeDelays HumanlikeDelays = new HumanlikeDelays();
public GlobalSettings()
{
InitializePropertyDefaultValues(this);
}
public void InitializePropertyDefaultValues(object obj)
{
var fields = obj.GetType().GetFields();
foreach (var field in fields)
{
var d = field.GetCustomAttribute<DefaultValueAttribute>();
if (d != null)
field.SetValue(obj, d.Value);
}
}
public static GlobalSettings Default => new GlobalSettings();
private static JSchema _schema;
private static JSchema JsonSchema
{
get
{
if (_schema != null)
return _schema;
// JSON Schemas from .NET types
var generator = new JSchemaGenerator
{
// change contract resolver so property names are camel case
//ContractResolver = new CamelCasePropertyNamesContractResolver(),
// types with no defined ID have their type name as the ID
SchemaIdGenerationHandling = SchemaIdGenerationHandling.TypeName,
// use the default order of properties.
SchemaPropertyOrderHandling = SchemaPropertyOrderHandling.Default,
// referenced schemas are inline.
SchemaLocationHandling = SchemaLocationHandling.Inline,
// no schemas can be referenced.
SchemaReferenceHandling = SchemaReferenceHandling.None
};
// change Zone enum to generate a string property
var strEnumGen = new StringEnumGenerationProvider { CamelCaseText = true };
generator.GenerationProviders.Add(strEnumGen);
// generate json schema
var type = typeof(GlobalSettings);
var schema = generator.Generate(type);
schema.Title = type.Name;
//
_schema = schema;
return _schema;
}
}
//private JObject _jsonObject;
//public JObject JsonObject
//{
// get
// {
// if (_jsonObject == null)
// _jsonObject = JObject.FromObject(this);
// return _jsonObject;
// }
// set
// {
// _jsonObject = value;
// }
//}
//public void Load(JObject jsonObj)
//{
// try
// {
// var input = jsonObj.ToString(Formatting.None, new StringEnumConverter { CamelCaseText = true });
// var settings = new JsonSerializerSettings();
// settings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
// JsonConvert.PopulateObject(input, this, settings);
// var configFile = Path.Combine(ProfileConfigPath, "config.json");
// this.Save(configFile);
// }
// catch (JsonReaderException exception)
// {
// Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
// }
//}
public static GlobalSettings Load(string path, bool validate = false)
{
GlobalSettings settings;
var profilePath = "";
var profileConfigPath = "";
var configFile = "";
var schemaFile = "";
if (Path.IsPathRooted(path))
{
profileConfigPath = Path.GetDirectoryName(path);
configFile = path;
schemaFile = path.Replace(".json", ".schema.json");
}
else
{
profilePath = Path.Combine(Directory.GetCurrentDirectory(), path);
profileConfigPath = Path.Combine(profilePath, "config");
configFile = Path.Combine(profileConfigPath, "config.json");
schemaFile = Path.Combine(profileConfigPath, "config.schema.json");
}
var shouldExit = false;
int schemaVersionBeforeUpgrade = 0;
if (File.Exists(configFile))
{
try
{
//if the file exists, load the settings
string input;
var count = 0;
while (true)
{
try
{
input = File.ReadAllText(configFile, Encoding.UTF8);
if (!input.Contains("DeprecatedMoves"))
input = input.Replace("\"Moves\"", $"\"DeprecatedMoves\"");
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 });
jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
jsonSettings.DefaultValueHandling = DefaultValueHandling.Populate;
try
{
// validate Json using JsonSchema
if (validate)
{
JObject jsonObj = JObject.Parse(input);
// Migrate before validation.
MigrateSettings(jsonObj, configFile, schemaFile);
// Save the original schema version since we need to pass it to AuthSettings for migration.
schemaVersionBeforeUpgrade = (int)jsonObj["UpdateConfig"]["SchemaVersion"];
// After migration we need to update the schema version to the latest version.
jsonObj["UpdateConfig"]["SchemaVersion"] = UpdateConfig.CURRENT_SCHEMA_VERSION;
Logger.Write("Validating config.json...");
IList<ValidationError> errors = null;
bool valid;
try
{
valid = jsonObj.IsValid(JsonSchema, out errors);
}
catch (JSchemaException ex)
{
if (ex.Message.Contains("commercial licence") || ex.Message.Contains("free-quota"))
{
Logger.Write(
"config.json: " + ex.Message);
valid = false;
}
else
{
throw;
}
}
if (!valid)
{
if (errors != null)
foreach (var error in errors)
{
Logger.Write(
"config.json [Line: " + error.LineNumber + ", Position: " + error.LinePosition +
"]: " + error.Path + " " + error.Message, LogLevel.Error);
}
Logger.Write(
"Fix config.json and restart NecroBot or press a key to ignore and continue...",
LogLevel.Warning);
Console.ReadKey();
}
// Now we know it's valid so update input with the migrated version.
input = jsonObj.ToString();
}
settings = JsonConvert.DeserializeObject<GlobalSettings>(input, jsonSettings);
}
catch (JsonSerializationException exception)
{
Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
return null;
}
catch (JsonReaderException exception)
{
Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
return null;
}
//This makes sure that existing config files dont get null values which lead to an exception
foreach (var filter in settings.PokemonsTransferFilter.Where(x => x.Value.KeepMinOperator == null))
{
filter.Value.KeepMinOperator = "or";
}
foreach (var filter in settings.PokemonsTransferFilter.Where(x => x.Value.Moves == null))
{
filter.Value.Moves = filter.Value.DeprecatedMoves != null
? new List<List<PokemonMove>> { filter.Value.DeprecatedMoves }
: filter.Value.Moves ?? new List<List<PokemonMove>>();
}
foreach (var filter in settings.PokemonsTransferFilter.Where(x => x.Value.MovesOperator == null))
{
filter.Value.MovesOperator = "or";
}
}
catch (JsonReaderException exception)
{
Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
return null;
}
}
else
{
settings = new GlobalSettings();
shouldExit = true;
}
settings.ProfilePath = profilePath;
settings.ProfileConfigPath = profileConfigPath;
settings.GeneralConfigPath = Path.Combine(Directory.GetCurrentDirectory(), "config");
settings.Save(configFile);
settings.Auth.Load(Path.Combine(profileConfigPath, "auth.json"),
Path.Combine(profileConfigPath, "auth.schema.json"), schemaVersionBeforeUpgrade, validate);
return shouldExit ? null : settings;
}
private static void MigrateSettings(JObject settings, string configFile, string schemaFile)
{
if (settings["UpdateConfig"]?["SchemaVersion"] == null)
{
// The is the first time setup for old config.json files without the SchemaVersion.
// Just set this to 0 so that we can handle the upgrade in case 0.
settings["UpdateConfig"]["SchemaVersion"] = 0;
}
int schemaVersion = (int)settings["UpdateConfig"]["SchemaVersion"];
if (schemaVersion == UpdateConfig.CURRENT_SCHEMA_VERSION)
{
Logger.Write("Configuration is up-to-date. Schema version: " + schemaVersion);
return;
}
// Backup old config file.
long ts = DateTime.UtcNow.ToUnixTime(); // Add timestamp to avoid file conflicts
string backupPath = configFile.Replace(".json", $"-{schemaVersion}-{ts}.backup.json");
Logger.Write($"Backing up config.json to: {backupPath}", LogLevel.Info);
File.Copy(configFile, backupPath);
// Add future schema migrations below.
int version;
for (version = schemaVersion; version < UpdateConfig.CURRENT_SCHEMA_VERSION; version++)
{
Logger.Write($"Migrating configuration from schema version {version} to {version + 1}", LogLevel.Info);
switch (version)
{
case 1:
// Delete the auto complete tutorial settings.
((JObject)settings["PlayerConfig"]).Remove("AutoCompleteTutorial");
((JObject)settings["PlayerConfig"]).Remove("DesiredNickname");
((JObject)settings["PlayerConfig"]).Remove("DesiredGender");
((JObject)settings["PlayerConfig"]).Remove("DesiredStarter");
break;
case 2:
// Remove the TransferConfigAndAuthOnUpdate setting since we always transfer now.
((JObject)settings["UpdateConfig"]).Remove("TransferConfigAndAuthOnUpdate");
break;
case 6:
// Rename AustoSnipeCandy to AutoSnipeCandy
if (settings["SnipePokemonFilter"] != null)
{
foreach (var x in settings["SnipePokemonFilter"])
{
var key = ((JProperty)(x)).Name;
var filter = ((JProperty)(x)).Value;
if (filter["AustoSnipeCandy"] != null)
{
filter["AutoSnipeCandy"] = filter["AustoSnipeCandy"];
((JObject)filter).Remove("AustoSnipeCandy");
}
}
}
break;
case 7:
// We making the limits more conservative.
if (settings["PokemonConfig"] != null)
{
if ((int)(settings["PokemonConfig"]["CatchPokemonLimit"]) == 998)
settings["PokemonConfig"]["CatchPokemonLimit"] = 700;
if ((int)(settings["PokemonConfig"]["CatchPokemonLimitMinutes"]) == 1470)
settings["PokemonConfig"]["CatchPokemonLimitMinutes"] = 1200;
if ((int)(settings["PokeStopConfig"]["PokeStopLimit"]) == 1998)
settings["PokeStopConfig"]["PokeStopLimit"] = 1500;
if ((int)(settings["PokeStopConfig"]["PokeStopLimitMinutes"]) == 1470)
settings["PokeStopConfig"]["PokeStopLimitMinutes"] = 1200;
}
break;
case 8:
string oldTemplate = (string)settings["PokemonConfig"]["RenameTemplate"];
settings["PokemonConfig"]["RenameTemplate"] = oldTemplate.Replace("{0}", "{Name}").Replace("{1}", "{IV}") + "_LV{Level}";
break;
case 9:
if (settings["PlayerConfig"]["RandomizeSettingsByPercent"] == null)
{
settings["PlayerConfig"]["RandomizeSettingsByPercent"] = 5;
}
break;
case 10:
case 11:
if ((string)settings["PokemonConfig"]["DefaultBuddyPokemon"] == "dragonite" ||
(string)settings["PokemonConfig"]["DefaultBuddyPokemon"] == null ||
(string)settings["PokemonConfig"]["DefaultBuddyPokemon"] == "")
settings["PokemonConfig"]["DefaultBuddyPokemon"] = "";
else
{
// Upper case first letter.
char[] a = ((string)settings["PokemonConfig"]["DefaultBuddyPokemon"]).ToCharArray();
a[0] = char.ToUpper(a[0]);
settings["PokemonConfig"]["DefaultBuddyPokemon"] = new string(a);
}
break;
case 13:
settings["PokemonConfig"]["FavoriteOperator"] = "and";
settings["PokemonConfig"]["FavoriteMinLevel"] = 0;
break;
// Add more here.
case 14:
//migrate berries setting
if (settings["PokemonConfig"]["UseBerriesMinIv"] != null && settings["ItemUseFilters"] != null)
{
settings["ItemUseFilters"]["ItemRazzBerry"]["UseItemMinIV"] = (int)settings["PokemonConfig"]["UseBerriesMinIv"];
settings["ItemUseFilters"]["ItemRazzBerry"]["UseItemMinCP"] = settings["PokemonConfig"]["UseBerriesMinCp"];
settings["ItemUseFilters"]["ItemRazzBerry"]["CatchProbability"] = settings["PokemonConfig"]["UseBerriesBelowCatchProbability"];
settings["ItemUseFilters"]["ItemRazzBerry"]["Operator"] = settings["PokemonConfig"]["UseBerriesOperator"];
settings["ItemUseFilters"]["ItemRazzBerry"]["MaxItemsUsePerPokemon"] = settings["PokemonConfig"]["MaxBerriesToUsePerPokemon"];
//delete old
((JObject)settings["PokemonConfig"]).Remove("UseBerriesMinIv");
((JObject)settings["PokemonConfig"]).Remove("UseBerriesMinCp");
((JObject)settings["PokemonConfig"]).Remove("UseBerriesBelowCatchProbability");
((JObject)settings["PokemonConfig"]).Remove("UseBerriesOperator");
((JObject)settings["PokemonConfig"]).Remove("MaxBerriesToUsePerPokemon");
}
break;
case 15:
List<string> existing = new List<string>();
foreach (var x in settings["ItemRecycleFilter"].Children())
{
existing.Add((string)x["Key"]);
}
List<ItemId> newItems = new List<ItemId>() { ItemId.ItemDragonScale, ItemId.ItemUpGrade, ItemId.ItemKingsRock, ItemId.ItemSunStone, ItemId.ItemMetalCoat };
foreach (var item in newItems)
{
if (!existing.Any(x => x.ToLower() == item.ToString().ToLower()))
{
var itemName = item.ToString();
itemName = itemName.Replace("Item", "item");
var last = settings["ItemRecycleFilter"].Children().Last();
var newFilter = JObject.Parse(@"{""Key"":""" + itemName + @""",""Value"":100}");
last.AddAfterSelf(newFilter);
}
}
break;
case 16:
if (settings["PokemonEvolveFilter"] != null && settings["PokemonsToEvolve"] != null)
{
List<string> pokemonToEvolve = new List<string>();
foreach (var x in settings["PokemonsToEvolve"].Children())
{
var pokemonName = (string)x;
pokemonName = pokemonName[0].ToString().ToUpper() + new string(pokemonName.Skip(1).ToArray());
if (settings["PokemonEvolveFilter"][pokemonName] == null)
{
EvolveFilter ev = new EvolveFilter(0, 0, 0);
settings["PokemonEvolveFilter"][pokemonName] = JObject.Parse(JsonConvert.SerializeObject(ev));
}
}
}
break;
case 17:
if (settings["PokemonEvolveFilter"] != null && settings["PokemonsToEvolve"] != null)
{
// Repeat the migration for case 16 just in case PokemonsToEvolve has beed modified.
List<string> pokemonToEvolve = new List<string>();
foreach (var x in settings["PokemonsToEvolve"].Children())
{
var pokemonName = (string)x;
pokemonName = pokemonName[0].ToString().ToUpper() + new string(pokemonName.Skip(1).ToArray());
if (settings["PokemonEvolveFilter"][pokemonName] == null)
{
EvolveFilter ev = new EvolveFilter(0, 0, 0);
settings["PokemonEvolveFilter"][pokemonName] = JObject.Parse(JsonConvert.SerializeObject(ev));
}
}
}
// Adding new MinCandiesBeforeEvolve to all filters.
if (settings["PokemonEvolveFilter"] != null)
{
foreach (var x in settings["PokemonEvolveFilter"])
{
var filter = ((JProperty)(x)).Value;
if (filter["MinCandiesBeforeEvolve"] == null)
{
filter["MinCandiesBeforeEvolve"] = 0;
}
}
}
// But this time we are going to remove PokemonsToEvolve.
settings.Remove("PokemonsToEvolve");
break;
case 22:
if (settings["PokemonsTransferFilter"] != null)
{
foreach (var x in settings["PokemonsTransferFilter"])
{
var key = ((JProperty)(x)).Name;
var filter = ((JProperty)(x)).Value;
if (filter["KeepMaxDuplicatePokemon"] == null)
filter["KeepMaxDuplicatePokemon"] = 1000;
}
}
break;
case 23:
if (settings["PokeStopConfig"] != null)
{
settings["PokeStopConfig"]["PokeStopLimit"] = 700;
Logger.Write($"PokeStopLimit changed to {settings["PokeStopConfig"]["PokeStopLimit"]}", LogLevel.Info);
}
if (settings["PokemonConfig"] != null)
{
settings["PokemonConfig"]["CatchPokemonLimit"] = 500;
Logger.Write($"CatchPokemonLimit changed to {settings["PokemonConfig"]["CatchPokemonLimit"]}", LogLevel.Info);
}
break;
}
}
}
public void CheckProxy(ITranslation translator)
{
Auth.CheckProxy(translator);
}
public static bool PromptForSetup(ITranslation translator)
{
bool promptForSetup = PromptForBoolean(
translator,
translator.GetTranslation(TranslationString.FirstStartPrompt, "Y", "N")
);
if (!promptForSetup)
Logger.Write(translator.GetTranslation(TranslationString.FirstStartAutoGenSettings));
return promptForSetup;
}
public static Session SetupSettings(Session session, GlobalSettings settings,
IElevationService elevationService, string configPath)
{
var newSession = SetupTranslationCode(session, elevationService, session.Translation, settings);
SetupAccountType(newSession.Translation, settings);
SetupUserAccount(newSession.Translation, settings);
SetupConfig(newSession.Translation, settings);
SetupWalkingConfig(newSession.Translation, settings);
SetupTelegramConfig(newSession.Translation, settings);
SetupProxyConfig(newSession.Translation, settings);
SetupWebSocketConfig(newSession.Translation, settings);
SaveFiles(settings, configPath);
Logger.Write(session.Translation.GetTranslation(TranslationString.FirstStartSetupCompleted), LogLevel.Info);
return newSession;
}
private static Session SetupTranslationCode(Session session, IElevationService elevationService,
ITranslation translator, GlobalSettings settings)
{
if (false == PromptForBoolean(
translator,
translator.GetTranslation(TranslationString.FirstStartLanguagePrompt, "Y", "N")
)
)
return session;
string strInput = PromptForString(
translator,
translator.GetTranslation(TranslationString.FirstStartLanguageCodePrompt)
);
settings.ConsoleConfig.TranslationLanguageCode = strInput;
session = new Session(settings,
new ClientSettings(settings, elevationService),
new LogicSettings(settings),
elevationService
);
translator = session.Translation;
Logger.Write(translator.GetTranslation(TranslationString.FirstStartLanguageConfirm, strInput));
return session;
}
private static void SetupProxyConfig(ITranslation translator, GlobalSettings settings)
{
if (false == PromptForBoolean(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupProxyPrompt, "Y", "N")
)
)
return;
settings.Auth.ProxyConfig.UseProxy = true;
string strInput = PromptForString(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupProxyHostPrompt)
);
settings.Auth.ProxyConfig.UseProxyHost = strInput;
Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyHostConfirm, strInput));
strInput = PromptForString(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupProxyPortPrompt)
);
settings.Auth.ProxyConfig.UseProxyPort = strInput;
Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyPortConfirm, strInput));
if (false == PromptForBoolean(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupProxyAuthPrompt, "Y", "N")
)
)
return;
settings.Auth.ProxyConfig.UseProxyAuthentication = true;
strInput = PromptForString(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupProxyUsernamePrompt)
);
settings.Auth.ProxyConfig.UseProxyUsername = strInput;
Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyUsernameConfirm, strInput));
strInput = PromptForString(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupProxyPasswordPrompt)
);
settings.Auth.ProxyConfig.UseProxyPassword = strInput;
Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupProxyPasswordConfirm, strInput));
}
private static void SetupWalkingConfig(ITranslation translator, GlobalSettings settings)
{
if (false == PromptForBoolean(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupWalkingSpeedPrompt, "Y", "N")
)
)
return;
settings.LocationConfig.WalkingSpeedInKilometerPerHour = PromptForDouble(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupWalkingSpeedKmHPrompt)
);
Logger.Write(
translator.GetTranslation(TranslationString.FirstStartSetupWalkingSpeedKmHConfirm,
settings.LocationConfig.WalkingSpeedInKilometerPerHour.ToString()
)
);
settings.LocationConfig.UseWalkingSpeedVariant = PromptForBoolean(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupUseWalkingSpeedVariantPrompt, "Y", "N")
);
settings.LocationConfig.WalkingSpeedVariant = PromptForDouble(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupWalkingSpeedVariantPrompt)
);
Logger.Write(
translator.GetTranslation(
TranslationString.FirstStartSetupWalkingSpeedVariantConfirm,
settings.LocationConfig.WalkingSpeedVariant.ToString()
)
);
}
private static void SetupWebSocketConfig(ITranslation translator, GlobalSettings settings)
{
if (false == PromptForBoolean(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupWebSocketPrompt, "Y", "N")
)
)
return;
settings.WebsocketsConfig.UseWebsocket = true;
settings.WebsocketsConfig.WebSocketPort = PromptForInteger(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupWebSocketPortPrompt)
);
Logger.Write(
translator.GetTranslation(
TranslationString.FirstStartSetupWebSocketPortConfirm,
settings.WebsocketsConfig.WebSocketPort.ToString()
)
);
}
private static void SetupTelegramConfig(ITranslation translator, GlobalSettings settings)
{
if (false == PromptForBoolean(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupTelegramPrompt, "Y", "N")
)
)
return;
settings.TelegramConfig.UseTelegramAPI = true;
string strInput = PromptForString(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupTelegramCodePrompt)
);
settings.TelegramConfig.TelegramAPIKey = strInput;
Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupTelegramCodeConfirm, strInput));
strInput = PromptForString(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupTelegramPasswordPrompt)
);
settings.TelegramConfig.TelegramPassword = strInput;
Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupTelegramPasswordConfirm, strInput));
}
private static void SetupAccountType(ITranslation translator, GlobalSettings settings)
{
Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupAccount), LogLevel.Info);
string accountType = PromptForString(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupTypePrompt, "google", "ptc"),
new string[] { "google", "ptc" },
translator.GetTranslation(TranslationString.FirstStartSetupTypePromptError, "google", "ptc"),
false
);
switch (accountType)
{
case "google":
settings.Auth.CurrentAuthConfig.AuthType = AuthType.Google;
break;
case "ptc":
settings.Auth.CurrentAuthConfig.AuthType = AuthType.Ptc;
break;
}
Logger.Write(
translator.GetTranslation(TranslationString.FirstStartSetupTypeConfirm, accountType.ToUpper()
)
);
}
private static void SetupUserAccount(ITranslation translator, GlobalSettings settings)
{
Logger.Write("", LogLevel.Info);
var strInput = PromptForString(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupUsernamePrompt)
);
settings.Auth.CurrentAuthConfig.Username = strInput;
Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupUsernameConfirm, strInput));
Logger.Write("", LogLevel.Info);
strInput = PromptForString(
translator,
translator.GetTranslation(TranslationString.FirstStartSetupPasswordPrompt)
);
settings.Auth.CurrentAuthConfig.Password = strInput;
Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupPasswordConfirm, strInput));
Logger.Write(translator.GetTranslation(TranslationString.FirstStartAccountCompleted), LogLevel.Info);
}
private static void SetupConfig(ITranslation translator, GlobalSettings settings)
{
if (false == PromptForBoolean(
translator,
translator.GetTranslation(
TranslationString.FirstStartDefaultLocationPrompt, "Y", "N")
)
)
{
Logger.Write(translator.GetTranslation(TranslationString.FirstStartDefaultLocationSet));
return;
}
Logger.Write(translator.GetTranslation(TranslationString.FirstStartDefaultLocation), LogLevel.Info);
Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupDefaultLatLongPrompt));
while (true)
{
try
{
var strInput = Console.ReadLine();
var strSplit = strInput.Split(',');
if (strSplit.Length > 1)
{
var dblLat = double.Parse(strSplit[0].Trim(' '));
var dblLong = double.Parse(strSplit[1].Trim(' '));
settings.LocationConfig.DefaultLatitude = dblLat;
settings.LocationConfig.DefaultLongitude = dblLong;
Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupDefaultLatLongConfirm,
$"{dblLat}, {dblLong}"));
}
else
{
Logger.Write(translator.GetTranslation(TranslationString.FirstStartSetupDefaultLocationError,
$"{settings.LocationConfig.DefaultLatitude}, {settings.LocationConfig.DefaultLongitude}",
LogLevel.Error));
continue;