Skip to content

Commit 408a37f

Browse files
committed
I think I finally fixed the thread hanging stuff
1 parent 804d88b commit 408a37f

File tree

4 files changed

+88
-81
lines changed

4 files changed

+88
-81
lines changed

Controllers/LegalizeController.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,12 @@ public Legalize Legalize([FromForm] [Required] IFormFile pokemon, [FromForm] str
5555
{
5656
version = Utils.GetGameVersion(pkm).ToString();
5757
}
58-
if(!Utils.PokemonExistsInGeneration(generation, pkm.Species))
58+
if (!Utils.PokemonExistsInGeneration(generation, pkm.Species))
5959
{
6060
Response.StatusCode = 400;
6161
return null;
6262
}
63-
Legalize L = new Legalize(pkm, version);
64-
return L;
63+
return new Legalize(pkm, version);
6564
}
6665
// POST: api/LegalityCheck
6766
[Route("api/LegalityCheck")]

Helpers/AutoLegality.cs

Lines changed: 41 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55
using PKHeX.Core;
66
using PKHeX.Core.AutoMod;
77
using Microsoft.Extensions.FileSystemGlobbing.Internal.Patterns;
8+
using System.Threading.Tasks;
9+
using System.Collections.Generic;
810

911
namespace 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
}

Models/Legalize.cs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,60 @@ public class Legalize
1515
public string Species { get; }
1616
public string[] Report { get; }
1717
public bool Ran { get; }
18-
public string QR { get; }
18+
public string QR { get; }
19+
1920

2021
public Legalize(PKM pk, string version)
2122
{
22-
CancellationTokenSource cts = new CancellationTokenSource();
23-
var al = new AutoLegality(pk, version);
24-
Success = al.Successful;
25-
Report = al.Report.Split('\n');
26-
Ran = al.Ran;
27-
if (Success)
23+
CancellationTokenSource cts = new CancellationTokenSource(10000);
24+
var al = new AutoLegality(pk, version, cts);
25+
if (al.OkayToRun)
2826
{
29-
Pokemon = Convert.ToBase64String(al.GetLegalPKM().DecryptedBoxData);
30-
Species = new PokemonSummary(al.GetLegalPKM(), GameInfo.Strings).Species;
31-
try
27+
PKM pkmn;
28+
al.LegalizePokemon();
29+
while (true)
30+
{
31+
if (cts.IsCancellationRequested)
32+
{
33+
pkmn = al.getLegalPK();
34+
break;
35+
}
36+
Thread.Sleep(100);
37+
}
38+
Success = al.Successful;
39+
Report = al.Report.Split('\n');
40+
Ran = al.Ran;
41+
if (Success)
3242
{
33-
QR = Utils.GenerateQR(QRMessageUtil.GetMessage(al.GetLegalPKM()));
34-
} catch
43+
try
44+
{
45+
Pokemon = Convert.ToBase64String(pkmn.DecryptedBoxData);
46+
Species = new PokemonSummary(pkmn, GameInfo.Strings).Species;
47+
try
48+
{
49+
QR = Utils.GenerateQR(QRMessageUtil.GetMessage(pkmn));
50+
}
51+
catch
52+
{
53+
QR = "";
54+
}
55+
} catch
56+
{
57+
Pokemon = "";
58+
Species = "";
59+
Success = false;
60+
Ran = true;
61+
Report = new string[1] { "Stuck in legalization!" };
62+
}
63+
} else
3564
{
36-
QR = "";
65+
Pokemon = "";
3766
}
3867
} else
3968
{
40-
Pokemon = "";
69+
Ran = false;
70+
Success = false;
71+
Report = new string[1] { "Could not run legalization!" };
4172
}
4273
}
4374
}

deps/PKHeX.Core.AutoMod.dll

7 KB
Binary file not shown.

0 commit comments

Comments
 (0)