55using PKHeX . Core ;
66using PKHeX . Core . AutoMod ;
77using Microsoft . Extensions . FileSystemGlobbing . Internal . Patterns ;
8+ using System . Threading . Tasks ;
9+ using System . Collections . Generic ;
810
911namespace CoreAPI . Helpers
1012{
1113
1214 public class AutoLegality
1315 {
16+ private PKM startingPK ;
17+ private CancellationTokenSource cts ;
18+ private GameVersion gv ;
1419 private static PKM legalpk ;
1520 private static LegalityAnalysis la ;
16- private static bool Initialized ;
17- private readonly Random _random = new Random ( ) ;
21+ private static bool Initialized = false ;
22+ public bool OkayToRun = false ;
1823 public bool Successful = false ;
1924 public bool Ran = true ;
2025 public string Report ;
@@ -25,41 +30,57 @@ public static void EnsureInitialized()
2530 {
2631 return ;
2732 }
28- Initialized = true ;
2933 Initalize ( ) ;
3034 }
3135
32- public static void Initalize ( )
36+ private static void Initalize ( )
3337 {
34- Legalizer . AllowBruteForce = true ;
38+ Legalizer . AllowBruteForce = false ;
39+ Legalizer . EnableEasterEggs = false ;
40+ Legalizer . AllowAPI = true ;
41+ APILegality . PrioritizeGame = true ;
42+ APILegality . UseTrainerData = false ;
43+ Initialized = true ;
3544 }
3645
37- public AutoLegality ( PKM pk , string ver )
46+ public AutoLegality ( PKM pk , string ver , CancellationTokenSource cancellationTokenSource )
3847 {
3948 EnsureInitialized ( ) ;
4049 bool valid = Enum . TryParse < GameVersion > ( ver , true , out var game ) ;
4150 if ( valid )
42- ProcessALM ( pk , game ) ;
51+ {
52+ OkayToRun = true ;
53+ startingPK = pk ;
54+ gv = game ;
55+ cts = cancellationTokenSource ;
56+ }
4357 return ;
4458 }
4559
46- private void ProcessALM ( PKM pkm , GameVersion ver = GameVersion . GP )
60+ public PKM LegalizePokemon ( )
61+ {
62+ return ProcessALM ( startingPK , gv ) ;
63+ }
64+
65+ private PKM ProcessALM ( PKM pkm , GameVersion ver = GameVersion . GP )
4766 {
4867 la = new LegalityAnalysis ( pkm ) ;
68+ var tcs = new TaskCompletionSource < string > ( ) ;
4969 if ( la . Valid )
5070 {
5171 legalpk = pkm ;
5272 Ran = false ;
5373 Report = la . Report ( ) ;
54- return ;
74+ return legalpk ;
5575 }
56- /*if (la.Report().ToLower().Contains("invalid move")){
57- Ran = true; // because piepie62 and griffin wanted to make my program a liar. GG guys GG.
58- Successful = false;
59- Report = la.Report();
60- return;
61- }*/
62- legalpk = Legalize ( pkm , ver ) ;
76+ Task . Run ( ( ) =>
77+ {
78+ Console . WriteLine ( String . Format ( "Legalization on Thread ({0}) has started at: {1}" , Thread . CurrentThread . ManagedThreadId , DateTime . Now . ToString ( "F" ) ) ) ;
79+ legalpk = Legalize ( pkm , ver ) ;
80+ Console . WriteLine ( String . Format ( "Legalization on Thread ({0}) has finished at: {1}" , Thread . CurrentThread . ManagedThreadId , DateTime . Now . ToString ( "F" ) ) ) ;
81+ cts . Cancel ( ) ;
82+ } , cts . Token ) ;
83+ return legalpk ;
6384 }
6485
6586 private SimpleTrainerInfo getInfo ( PKM pk , GameVersion ver )
@@ -75,72 +96,28 @@ private SimpleTrainerInfo getInfo(PKM pk, GameVersion ver)
7596 info . Gender = pk . OT_Gender ;
7697 return info ;
7798 }
78-
79- private int ChooseRandomMove ( int [ ] moves )
80- {
81- var mvs = la . AllSuggestedMovesAndRelearn ( ) . Where ( move => ! moves . Contains ( move ) ) ;
82- if ( mvs . Count ( ) == 0 )
83- {
84- return 0 ;
85- }
86- return mvs . ElementAt ( Rand . RandomNum ( ) % mvs . Count ( ) ) ;
87- }
8899 private PKM Legalize ( PKM pk , GameVersion ver )
89100 {
90101 Report = la . Report ( ) ;
91102 var sav = SaveUtil . GetBlankSAV ( ver , pk . OT_Name ) ;
92103 sav . TID = pk . TID ;
93104 sav . SID = pk . SID ;
94105 sav . Language = pk . Language ;
95- Legalizer . AllowBruteForce = true ;
96- Legalizer . EnableEasterEggs = false ;
97- Legalizer . AllowAPI = true ;
98- APILegality . PrioritizeGame = true ;
99- APILegality . UseTrainerData = false ;
100- var r = new Regex ( @"invalid move ([1-4]):" , RegexOptions . IgnoreCase ) ;
101- var matches = r . Matches ( la . Report ( ) ) ;
102- foreach ( Match match in matches )
103- {
104- int movePos ;
105- if ( int . TryParse ( match . Groups [ 1 ] . Value , out movePos ) )
106- {
107- var mvs = pk . Moves ;
108- mvs [ movePos - 1 ] = ChooseRandomMove ( pk . Moves ) ;
109- pk . Moves = mvs ;
110- }
111- }
112106
113107 PKM upd = sav . Legalize ( pk . Clone ( ) ) ;
114108 upd . SetTrainerData ( getInfo ( pk , ver ) ) ;
115109 la = new LegalityAnalysis ( upd ) ;
116- if ( la . Valid )
110+ if ( la . Valid )
117111 {
118112 legalpk = upd ;
119113 Successful = true ;
120- //Report = la.Report();
121- }
122- else
123- {
124- upd = sav . Legalize ( pk . Clone ( ) ) ;
125- la = new LegalityAnalysis ( upd ) ;
126- if ( la . Valid )
127- {
128- legalpk = upd ;
129- Successful = true ;
130- } else
131- {
132- Console . WriteLine ( la . Report ( ) ) ;
133- }
134- }
135-
136- if ( Successful )
137- {
138114 return legalpk ;
115+ //Report = la.Report();
139116 }
140-
141117 return null ;
142118 }
143- public PKM GetLegalPKM ( )
119+
120+ public PKM getLegalPK ( )
144121 {
145122 return legalpk ;
146123 }
0 commit comments