11using System ;
2+ using System . Text . RegularExpressions ;
3+ using System . Linq ;
24using System . Threading ;
35using PKHeX . Core ;
46using PKHeX . Core . AutoMod ;
7+ using Microsoft . Extensions . FileSystemGlobbing . Internal . Patterns ;
8+
59namespace CoreAPI . Helpers
610{
711
@@ -10,7 +14,8 @@ public class AutoLegality
1014 private static PKM legalpk ;
1115 private static LegalityAnalysis la ;
1216 private static bool Initialized ;
13- public bool Successful = true ;
17+ private readonly Random _random = new Random ( ) ;
18+ public bool Successful = false ;
1419 public bool Ran = true ;
1520 public string Report ;
1621
@@ -29,7 +34,7 @@ public static void Initalize()
2934 Legalizer . AllowBruteForce = true ;
3035 }
3136
32- public AutoLegality ( PKM pk , string ver )
37+ public AutoLegality ( PKM pk , string ver )
3338 {
3439 EnsureInitialized ( ) ;
3540 bool valid = Enum . TryParse < GameVersion > ( ver , true , out var game ) ;
@@ -39,7 +44,7 @@ public AutoLegality(PKM pk, string ver)
3944 }
4045
4146 private void ProcessALM ( PKM pkm , GameVersion ver = GameVersion . GP )
42- {
47+ {
4348 la = new LegalityAnalysis ( pkm ) ;
4449 if ( la . Valid )
4550 {
@@ -48,12 +53,12 @@ private void ProcessALM(PKM pkm, GameVersion ver = GameVersion.GP)
4853 Report = la . Report ( ) ;
4954 return ;
5055 }
51- if ( la . Report ( ) . ToLower ( ) . Contains ( "invalid move" ) ) {
56+ /* if (la.Report().ToLower().Contains("invalid move")){
5257 Ran = true; // because piepie62 and griffin wanted to make my program a liar. GG guys GG.
5358 Successful = false;
5459 Report = la.Report();
5560 return;
56- }
61+ }*/
5762 legalpk = Legalize ( pkm , ver ) ;
5863 }
5964
@@ -71,85 +76,68 @@ private SimpleTrainerInfo getInfo(PKM pk, GameVersion ver)
7176 return info ;
7277 }
7378
74- private PKM Legalize ( PKM pk , GameVersion ver )
79+ private int ChooseRandomMove ( int [ ] moves )
7580 {
76- var KeepOriginalData = true ;
77- Successful = false ;
78- SimpleTrainerInfo info = getInfo ( pk , ver ) ;
79-
80- if ( la . Report ( ) . ToLower ( ) . Contains ( "wordfilter" ) || la . Report ( ) . Contains ( "SID" ) || la . Report ( ) . Contains ( "TID" ) )
81+ var mvs = la . AllSuggestedMovesAndRelearn ( ) . Where ( move => ! moves . Contains ( move ) ) ;
82+ if ( mvs . Count ( ) == 0 )
8183 {
82- KeepOriginalData = false ;
84+ return 0 ;
8385 }
86+ return mvs . ElementAt ( Rand . RandomNum ( ) % mvs . Count ( ) ) ;
87+ }
88+ private PKM Legalize ( PKM pk , GameVersion ver )
89+ {
90+ Report = la . Report ( ) ;
91+ var sav = SaveUtil . GetBlankSAV ( ver , pk . OT_Name ) ;
92+ sav . TID = pk . TID ;
93+ sav . SID = pk . SID ;
94+ sav . Language = pk . Language ;
8495 Legalizer . AllowBruteForce = true ;
96+ Legalizer . EnableEasterEggs = false ;
8597 Legalizer . AllowAPI = true ;
86- var timeout = TimeSpan . FromSeconds ( 5 ) ;
87- var started = DateTime . UtcNow ;
88- PKM updated ;
89- var thread = new Thread ( ( ) => {
90- if ( KeepOriginalData )
91- {
92- updated = Legalizer . Legalize ( pk ) ;
93- var report = la . Report ( ) . ToLower ( ) ;
94- if ( ! report . Contains ( "handling trainer" ) )
95- {
96- if ( ! report . Contains ( "untraded" ) )
97- {
98- info . ApplyToPKM ( updated ) ;
99- if ( ! report . Contains ( "memory" ) )
100- updated . HT_Memory = pk . HT_Memory ;
101- if ( ! report . Contains ( "affection" ) )
102- updated . HT_Affection = pk . HT_Affection ;
103- if ( ! report . Contains ( "feeling" ) )
104- updated . HT_Feeling = pk . HT_Feeling ;
105- if ( ! report . Contains ( "friendship" ) )
106- updated . HT_Friendship = pk . HT_Friendship ;
107- if ( ! report . Contains ( "intensity" ) )
108- updated . HT_Intensity = pk . HT_Intensity ;
109- if ( ! report . Contains ( "trash" ) )
110- updated . HT_Trash = pk . HT_Trash ;
111- updated . HT_Gender = pk . HT_Gender ;
112- updated . HT_Name = pk . HT_Name ;
113- updated . HT_TextVar = pk . HT_TextVar ;
114- }
115- } else
116- {
117- updated . OT_Memory = pk . OT_Memory ;
118- updated . OT_Friendship = pk . OT_Friendship ;
119- updated . OT_Name = pk . OT_Name ;
120- updated . OT_Affection = pk . OT_Affection ;
121- updated . OT_Feeling = pk . OT_Feeling ;
122- updated . OT_Gender = pk . OT_Gender ;
123- updated . OT_Intensity = pk . OT_Intensity ;
124- updated . OT_TextVar = pk . OT_TextVar ;
125- updated . OT_Trash = pk . OT_Trash ;
126- }
127- } else
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 ) )
128106 {
129- updated = Legalizer . Legalize ( pk ) ;
107+ var mvs = pk . Moves ;
108+ mvs [ movePos - 1 ] = ChooseRandomMove ( pk . Moves ) ;
109+ pk . Moves = mvs ;
130110 }
131- if ( new LegalityAnalysis ( updated ) . Valid )
111+ }
112+
113+ PKM upd = sav . Legalize ( pk . Clone ( ) ) ;
114+ upd . SetTrainerData ( getInfo ( pk , ver ) ) ;
115+ la = new LegalityAnalysis ( upd ) ;
116+ if ( la . Valid )
117+ {
118+ legalpk = upd ;
119+ 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 )
132127 {
133- legalpk = updated ;
128+ legalpk = upd ;
134129 Successful = true ;
135- Report = la . Report ( ) ;
130+ } else
131+ {
132+ Console . WriteLine ( la . Report ( ) ) ;
136133 }
137- } ) ;
138- thread . Start ( ) ;
139- while ( thread . IsAlive && DateTime . UtcNow - started < timeout )
140- {
141- Thread . Sleep ( 100 ) ;
142134 }
143- if ( thread . IsAlive )
144- thread . Abort ( ) ;
145-
135+
146136 if ( Successful )
147137 {
148138 return legalpk ;
149- } else
150- {
151- Report = la . Report ( ) ;
152139 }
140+
153141 return null ;
154142 }
155143 public PKM GetLegalPKM ( )
0 commit comments