Skip to content

Commit ba84b94

Browse files
committed
I think this should fix stuff
1 parent d0ad06a commit ba84b94

6 files changed

Lines changed: 149 additions & 90 deletions

File tree

Controllers/LegalizeController.cs

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Http;
6-
using Microsoft.AspNetCore.Mvc;
7-
using CoreAPI.Models;
8-
using CoreAPI.Helpers;
9-
using System.IO;
10-
using System.ComponentModel.DataAnnotations;
11-
using PKHeX.Core;
12-
13-
namespace CoreAPI.Controllers
14-
{
15-
[ApiController]
16-
public class LegalizeController : ControllerBase
17-
{
18-
// POST: api/Legalize
19-
[Route("api/[controller]")]
20-
[HttpPost]
21-
public Legalize Legalize([FromForm] [Required] IFormFile pokemon, [FromHeader] string Version, [FromForm] string generation)
22-
{
23-
using var memoryStream = new MemoryStream();
24-
pokemon.CopyTo(memoryStream);
25-
PKM pkm;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc;
7+
using CoreAPI.Models;
8+
using CoreAPI.Helpers;
9+
using System.IO;
10+
using System.ComponentModel.DataAnnotations;
11+
using PKHeX.Core;
12+
13+
namespace CoreAPI.Controllers
14+
{
15+
[ApiController]
16+
public class LegalizeController : ControllerBase
17+
{
18+
// POST: api/Legalize
19+
[Route("api/[controller]")]
20+
[HttpPost]
21+
public Legalize Legalize([FromForm] [Required] IFormFile pokemon, [FromHeader] string Version, [FromForm] string generation)
22+
{
23+
using var memoryStream = new MemoryStream();
24+
pokemon.CopyTo(memoryStream);
25+
PKM pkm;
2626
byte[] data = memoryStream.ToArray();
2727
try
2828
{
@@ -47,23 +47,24 @@ public Legalize Legalize([FromForm] [Required] IFormFile pokemon, [FromHeader] s
4747
{
4848
Response.StatusCode = 400;
4949
return null;
50-
}
51-
52-
if (Version == "" || Version == null)
53-
{
54-
Version = Utils.GetGameVersion(pkm).ToString();
55-
}
56-
Legalize L = new Legalize(pkm, Version);
57-
return L;
58-
}
59-
// POST: api/LegalityCheck
60-
[Route("api/LegalityCheck")]
61-
[HttpPost]
62-
public string CheckLegality([FromForm] [Required] IFormFile pokemon, [FromHeader] string Version, [FromForm] string generation)
63-
{
64-
using var memoryStream = new MemoryStream();
65-
pokemon.CopyTo(memoryStream);
66-
PKM pkm;
50+
}
51+
52+
if (Version == "" || Version == null)
53+
{
54+
Version = Utils.GetGameVersion(pkm).ToString();
55+
}
56+
57+
Legalize L = new Legalize(pkm, Version);
58+
return L;
59+
}
60+
// POST: api/LegalityCheck
61+
[Route("api/LegalityCheck")]
62+
[HttpPost]
63+
public string CheckLegality([FromForm] [Required] IFormFile pokemon, [FromHeader] string Version, [FromForm] string generation)
64+
{
65+
using var memoryStream = new MemoryStream();
66+
pokemon.CopyTo(memoryStream);
67+
PKM pkm;
6768
byte[] data = memoryStream.ToArray();
6869
try
6970
{
@@ -88,14 +89,14 @@ public string CheckLegality([FromForm] [Required] IFormFile pokemon, [FromHeader
8889
{
8990
Response.StatusCode = 400;
9091
return null;
91-
}
92-
93-
if (Version == "" || Version == null)
94-
{
95-
Version = Utils.GetGameVersion(pkm).ToString();
96-
}
97-
var la = new LegalityAnalysis(pkm);
98-
return la.Report();
99-
}
100-
}
92+
}
93+
94+
if (Version == "" || Version == null)
95+
{
96+
Version = Utils.GetGameVersion(pkm).ToString();
97+
}
98+
var la = new LegalityAnalysis(pkm);
99+
return la.Report();
100+
}
101+
}
101102
}

Helpers/AutoLegality.cs

Lines changed: 93 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using PKHeX.Core;
34
using PKHeX.Core.AutoMod;
45
namespace CoreAPI.Helpers
@@ -8,77 +9,131 @@ public class AutoLegality
89
{
910
private static PKM legalpk;
1011
private static LegalityAnalysis la;
12+
private static bool Initialized;
1113
public bool Successful = true;
1214
public bool Ran = true;
1315
public string Report;
1416

15-
public AutoLegality(PKM pk, string ver)
17+
public static void EnsureInitialized()
1618
{
19+
if (Initialized)
20+
{
21+
return;
22+
}
23+
Initialized = true;
24+
Initalize();
25+
}
26+
27+
public static void Initalize()
28+
{
29+
//InitializeCoreStrings();
30+
Legalizer.AllowBruteForce = true;
31+
}
32+
33+
public AutoLegality(PKM pk, string ver)
34+
{
35+
EnsureInitialized();
1736
bool valid = Enum.TryParse<GameVersion>(ver, true, out var game);
1837
if (valid)
1938
ProcessALM(pk, game);
2039
return;
2140
}
2241

23-
private void ProcessALM(PKM pkm, GameVersion ver = GameVersion.GP)
42+
private static void InitializeCoreStrings()
2443
{
44+
var lang = Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName.Substring(0, 2);
45+
Util.SetLocalization(typeof(LegalityCheckStrings), lang);
46+
Util.SetLocalization(typeof(MessageStrings), lang);
47+
RibbonStrings.ResetDictionary(GameInfo.Strings.ribbons);
48+
49+
// Update Legality Analysis strings
50+
LegalityAnalysis.MoveStrings = GameInfo.Strings.movelist;
51+
LegalityAnalysis.SpeciesStrings = GameInfo.Strings.specieslist;
52+
}
53+
54+
private void ProcessALM(PKM pkm, GameVersion ver = GameVersion.GP)
55+
{
2556
la = new LegalityAnalysis(pkm);
2657
if (la.Valid)
2758
{
2859
legalpk = pkm;
2960
Ran = false;
3061
Report = la.Report();
62+
return;
63+
}
64+
if (la.Report().ToLower().Contains("invalid move")){
65+
Ran = true; // because piepie62 and griffin wanted to make my program a liar. GG guys GG.
66+
Successful = false;
67+
Report = la.Report();
68+
return;
3169
}
32-
else
33-
Legalizer.AllowBruteForce = false;
34-
legalpk = Legalize(pkm, ver);
70+
legalpk = Legalize(pkm, ver);
71+
}
72+
73+
private SimpleTrainerInfo getInfo(PKM pk, GameVersion ver)
74+
{
75+
SimpleTrainerInfo info = new SimpleTrainerInfo(ver);
76+
info.OT = pk.OT_Name;
77+
info.SID = pk.SID;
78+
info.TID = pk.TID;
79+
info.Language = pk.Language;
80+
info.SubRegion = pk.Region;
81+
info.Country = pk.Country;
82+
info.ConsoleRegion = pk.ConsoleRegion;
83+
info.Gender = pk.OT_Gender;
84+
return info;
3585
}
3686

3787
private PKM Legalize(PKM pk, GameVersion ver)
3888
{
39-
var OriginlTrainer = pk.OT_Name;
40-
var HandlingTrainer = pk.HT_Name;
4189
var KeepOriginalData = true;
90+
var HT_Info = pk.HT_Name;
4291
Successful = false;
92+
SimpleTrainerInfo info = getInfo(pk, ver);
93+
4394
if (la.Report().ToLower().Contains("wordfilter") || la.Report().Contains("SID") || la.Report().Contains("TID"))
4495
{
4596
KeepOriginalData = false;
46-
HandlingTrainer = "PKHeX";
4797
}
48-
var updated = Legalizer.Legalize(pk);
49-
// These are the data that PKHEX AutoMod sets by itself
50-
var NewSID = updated.SID;
51-
var NewOT = updated.OT_Name;
52-
var NewTID = updated.TID;
53-
54-
if (KeepOriginalData)
55-
{
56-
updated.TID = pk.TID;
57-
updated.SID = pk.SID;
58-
updated.OT_Name = OriginlTrainer;
59-
}
60-
if (new LegalityAnalysis(updated).Valid)
61-
{
62-
legalpk = updated;
63-
Successful = true;
64-
Report = la.Report();
65-
} else
66-
{
67-
// Let's try reverting to the old name data
98+
Legalizer.AllowBruteForce = true;
99+
Legalizer.AllowAPI = true;
100+
var timeout = TimeSpan.FromSeconds(5);
101+
var started = DateTime.UtcNow;
102+
PKM updated;
103+
var thread = new Thread(() => {
68104
if (KeepOriginalData)
69105
{
70-
updated.TID = NewTID;
71-
updated.OT_Name = NewOT;
72-
updated.SID = NewSID;
73-
// Let's trying running the LegalityAnalysis again
74-
if(new LegalityAnalysis(updated).Valid)
75-
{
76-
legalpk = updated;
77-
Successful = true;
78-
Report = la.Report();
79-
}
106+
updated = Legalizer.Legalize(pk);
107+
info.ApplyToPKM(updated);
108+
updated.HT_Affection = pk.HT_Affection;
109+
updated.HT_Feeling = pk.HT_Feeling;
110+
updated.HT_Friendship = pk.HT_Friendship;
111+
updated.HT_Gender = pk.HT_Gender;
112+
updated.HT_Intensity = pk.HT_Intensity;
113+
updated.HT_Memory = pk.HT_Memory;
114+
updated.HT_Name = pk.HT_Name;
115+
updated.HT_TextVar = pk.HT_TextVar;
116+
updated.HT_Trash = pk.HT_Trash;
117+
Console.WriteLine(pk.HT_Name);
118+
} else
119+
{
120+
updated = Legalizer.Legalize(pk);
80121
}
122+
if (new LegalityAnalysis(updated).Valid)
123+
{
124+
legalpk = updated;
125+
Successful = true;
126+
Report = la.Report();
127+
}
128+
});
129+
thread.Start();
130+
while (thread.IsAlive && DateTime.UtcNow - started < timeout)
131+
{
132+
Thread.Sleep(100);
81133
}
134+
if (thread.IsAlive)
135+
thread.Abort();
136+
82137
if (Successful)
83138
{
84139
return legalpk;

Models/Legalize.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using CoreAPI.Helpers;
67
using PKHeX.Core;
@@ -18,6 +19,7 @@ public class Legalize
1819

1920
public Legalize(PKM pk, string version)
2021
{
22+
CancellationTokenSource cts = new CancellationTokenSource();
2123
var al = new AutoLegality(pk, version);
2224
Success = al.Successful;
2325
Report = al.Report.Split('\n');

Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public Startup(IConfiguration configuration)
2020
LegalityAnalysis.MoveStrings = GameInfo.Strings.movelist;
2121
LegalityAnalysis.SpeciesStrings = GameInfo.Strings.specieslist;
2222

23+
2324
Configuration = configuration;
2425
}
2526

deps/PKHeX.Core.AutoMod.dll

8 KB
Binary file not shown.

deps/PKHeX.Core.dll

99 KB
Binary file not shown.

0 commit comments

Comments
 (0)