diff --git a/AutoLegalityMod/GUI/LiveHexUI.cs b/AutoLegalityMod/GUI/LiveHexUI.cs index 6028e501..8d1d413b 100644 --- a/AutoLegalityMod/GUI/LiveHexUI.cs +++ b/AutoLegalityMod/GUI/LiveHexUI.cs @@ -125,7 +125,7 @@ private void B_Connect_Click(object sender, EventArgs e) var data = Remote.Bot.ReadSlot(1, 1); var pkm = SAV.SAV.GetDecryptedPKM(data); - if (pkm?.ChecksumValid == true) + if (pkm.ChecksumValid) { ConnectionEstablished = true; currver = version; diff --git a/PKHeX.Core.AutoMod/AutoMod/APILegality.cs b/PKHeX.Core.AutoMod/AutoMod/APILegality.cs index 789cc23a..46e0dc16 100644 --- a/PKHeX.Core.AutoMod/AutoMod/APILegality.cs +++ b/PKHeX.Core.AutoMod/AutoMod/APILegality.cs @@ -648,8 +648,10 @@ private static void FindNestPIDIV(PK8 pk, T enc, bool shiny) where T : Encoun } while (++count < 10_000); if (shiny && enc is EncounterStatic8U) + { // Dynamax Adventure shinies are always XOR 1 pk.PID = (uint)(((pk.TID ^ pk.SID ^ (pk.PID & 0xFFFF) ^ 1) << 16) | (pk.PID & 0xFFFF)); + } pk.Species = iterPKM.Species; // possible evolution // can be ability capsuled diff --git a/PKHeX.Core.Injection/BotController/LiveHexController.cs b/PKHeX.Core.Injection/BotController/LiveHexController.cs index b8b64c81..4a35d842 100644 --- a/PKHeX.Core.Injection/BotController/LiveHexController.cs +++ b/PKHeX.Core.Injection/BotController/LiveHexController.cs @@ -53,8 +53,7 @@ public void ReadActiveSlot(int box, int slot) { var data = Bot.ReadSlot(box, slot); var pkm = SAV.SAV.GetDecryptedPKM(data); - if (pkm != null) - Editor.PopulateFields(pkm); + Editor.PopulateFields(pkm); } public bool ReadOffset(uint offset) @@ -63,8 +62,6 @@ public bool ReadOffset(uint offset) var pkm = SAV.SAV.GetDecryptedPKM(data); // Since data might not actually exist at the user-specified offset, double check that the pkm data is valid. - if (pkm == null) - return false; if (!pkm.ChecksumValid) return false; diff --git a/PKHeX.Core.Injection/BotController/NTRAPIFramework.cs b/PKHeX.Core.Injection/BotController/NTRAPIFramework.cs index 186c90d7..53d332ae 100644 --- a/PKHeX.Core.Injection/BotController/NTRAPIFramework.cs +++ b/PKHeX.Core.Injection/BotController/NTRAPIFramework.cs @@ -85,7 +85,6 @@ public sealed class NTR public int PID = -1; private uint _currentSeq; - public NTR() { DataReady += HandleDataReady; @@ -94,12 +93,11 @@ public NTR() _delLastLog = LastLog; } - private void LastLog(string l) => Lastlog = l; private void OnDataReady(DataReadyEventArgs e) => DataReady?.Invoke(this, e); private void OnConnected(EventArgs e) => Connected?.Invoke(this, e); private void OnInfoReady(InfoReadyEventArgs e) => InfoReady?.Invoke(this, e); - private static string ByteToHex(byte[] datBuf, int type) => datBuf.Aggregate("", (current, b) => current + (b.ToString("X2") + " ")); + private static string ByteToHex(byte[] datBuf) => datBuf.Aggregate("", (current, b) => current + (b.ToString("X2") + " ")); public void AddWaitingForData(uint newkey, DataReadyWaiting newvalue) => _waitingForData.Add(newkey, newvalue); public void ListProcess() => SendEmptyPacket(5); public uint Data(uint addr, uint size = 0x100, int pid = -1) => SendReadMemPacket(addr, size, (uint)pid); @@ -130,17 +128,15 @@ private int ReadNetworkStream(NetworkStream stream, byte[] buf, int length) private void SendHeartBeat() { var hbstarted = false; - while (true) + do { Thread.Sleep(1000); - if (IsConnected) - { - SendHeartbeatPacket(); - hbstarted = true; - } - if (hbstarted && !IsConnected) - break; + if (!IsConnected) + continue; + SendHeartbeatPacket(); + hbstarted = true; } + while (!hbstarted || IsConnected); } private void PacketRecvThreadStart() @@ -159,7 +155,7 @@ private void PacketRecvThreadStart() var magic = BitConverter.ToUInt32(buf, 0); var seq = BitConverter.ToUInt32(buf, 4); - var type = BitConverter.ToUInt32(buf, 8); + //var type = BitConverter.ToUInt32(buf, 8); var cmd = BitConverter.ToUInt32(buf, 12); var t = 12; for (var i = 0; i < args.Length; i++) @@ -227,7 +223,6 @@ private void HandleReadMem(uint seq, byte[] dataBuf) fs.Write(dataBuf, 0, dataBuf.Length); fs.Close(); Log("dump saved into " + fileName + " successfully"); - return; } else if (requestDetails.IsCallback) { @@ -239,9 +234,8 @@ private void HandleReadMem(uint seq, byte[] dataBuf) } else { - Log(ByteToHex(dataBuf, 0)); + Log(ByteToHex(dataBuf)); } - } private void HandlePacket(uint cmd, uint seq, byte[] dataBuf) @@ -261,8 +255,7 @@ private void ConnectToServer() Disconnect(); try { - _tcp = new TcpClient(); - _tcp.NoDelay = true; + _tcp = new TcpClient {NoDelay = true}; _tcp.Connect(_host, _port); _currentSeq = 0; _netStream = _tcp.GetStream(); @@ -279,7 +272,6 @@ private void ConnectToServer() { Console.WriteLine("Could not connect, make sure the IP is correct, you're running NTR and you're online in-game!"); } - } public void Disconnect(bool waitPacketThread = true) @@ -355,7 +347,6 @@ private void SendHeartbeatPacket() } } } - } private void SendEmptyPacket(uint cmd, uint arg0 = 0, uint arg1 = 0, uint arg2 = 0) @@ -385,15 +376,16 @@ private void GetGame(object sender, InfoReadyEventArgs e) var pnamestr = new[] { "kujira-1", "kujira-2", "sango-1", "sango-2", "salmon", "niji_loc", "niji_loc", "momiji", "momiji" }; string pname; string log = e.Info; - if (null == (pname = pnamestr.FirstOrDefault(log.Contains))) + if (null == (pname = Array.Find(pnamestr, log.Contains))) return; pname = ", pname:" + pname.PadLeft(9); string pidaddr = log.Substring(log.IndexOf(pname, StringComparison.Ordinal) - 10, 10); PID = Convert.ToInt32(pidaddr, 16); if (log.Contains("niji_loc")) + { Write(0x3E14C0, BitConverter.GetBytes(0xE3A01000), PID); - + } else if (log.Contains("momiji")) { Write(0x3F3424, BitConverter.GetBytes(0xE3A01000), PID); // Ultra Sun // NFC ON: E3A01001 NFC OFF: E3A01000 @@ -402,9 +394,9 @@ private void GetGame(object sender, InfoReadyEventArgs e) } private void HandleDataReady(object sender, DataReadyEventArgs e) - { // We move data processing to a separate thread. This way even if processing takes a long time, the netcode doesn't hang. - DataReadyWaiting args; - if (_waitingForData.TryGetValue(e.Seq, out args)) + { + // We move data processing to a separate thread. This way even if processing takes a long time, the netcode doesn't hang. + if (_waitingForData.TryGetValue(e.Seq, out DataReadyWaiting args)) { Array.Copy(e.Data, args.Data, Math.Min(e.Data.Length, args.Data.Length)); Thread t = new Thread(new ParameterizedThreadStart(args.Handler)); diff --git a/PKHeX.Core.Injection/BotController/NTRClient.cs b/PKHeX.Core.Injection/BotController/NTRClient.cs index 84367c7c..636f9918 100644 --- a/PKHeX.Core.Injection/BotController/NTRClient.cs +++ b/PKHeX.Core.Injection/BotController/NTRClient.cs @@ -8,7 +8,7 @@ public class NTRClient : ICommunicator private int Port = 8000; private static readonly NTR clientNTR = new NTR(); - private int timeout = 10; + private const int timeout = 10; private bool Connected; private readonly object _sync = new object(); @@ -54,15 +54,14 @@ public byte[] ReadBytes(uint offset, int length) } clientNTR.AddWaitingForData(clientNTR.Data(offset, (uint)length, clientNTR.PID), myArgs); - int readcount = 0; - for (readcount = 0; readcount < timeout * 100; readcount++) + for (int readcount = 0; readcount < timeout * 100; readcount++) { Thread.Sleep(10); if (CompareLastLog("finished")) break; } - byte[] result = _lastMemoryRead ?? new byte[]{}; + byte[] result = _lastMemoryRead ?? System.Array.Empty(); _lastMemoryRead = null; return result; }