Skip to content

Commit

Permalink
Handle some warnings/messages
Browse files Browse the repository at this point in the history
PKHeX.Core has nullability annotated, pkm from GetDecrypted are never null
  • Loading branch information
kwsch committed Oct 31, 2020
1 parent 0e6ca95 commit 1d013b6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
2 changes: 1 addition & 1 deletion AutoLegalityMod/GUI/LiveHexUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions PKHeX.Core.AutoMod/AutoMod/APILegality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,10 @@ private static void FindNestPIDIV<T>(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
Expand Down
5 changes: 1 addition & 4 deletions PKHeX.Core.Injection/BotController/LiveHexController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;

Expand Down
40 changes: 16 additions & 24 deletions PKHeX.Core.Injection/BotController/NTRAPIFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public sealed class NTR
public int PID = -1;
private uint _currentSeq;


public NTR()
{
DataReady += HandleDataReady;
Expand All @@ -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);
Expand Down Expand Up @@ -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()
Expand All @@ -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++)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
Expand All @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -355,7 +347,6 @@ private void SendHeartbeatPacket()
}
}
}

}

private void SendEmptyPacket(uint cmd, uint arg0 = 0, uint arg1 = 0, uint arg2 = 0)
Expand Down Expand Up @@ -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
Expand All @@ -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));
Expand Down
7 changes: 3 additions & 4 deletions PKHeX.Core.Injection/BotController/NTRClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<byte>();
_lastMemoryRead = null;
return result;
}
Expand Down

0 comments on commit 1d013b6

Please sign in to comment.