Skip to content

Commit

Permalink
Automatic PID detection, pk7 injection
Browse files Browse the repository at this point in the history
  • Loading branch information
imaboy321 committed Dec 6, 2016
1 parent ccf471b commit 8634079
Show file tree
Hide file tree
Showing 7 changed files with 462 additions and 152 deletions.
Binary file modified .vs/Ledybot/v14/.suo
Binary file not shown.
361 changes: 279 additions & 82 deletions Ledybot/Form1.Designer.cs

Large diffs are not rendered by default.

126 changes: 109 additions & 17 deletions Ledybot/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledybot
Expand All @@ -11,29 +14,52 @@ public partial class MainForm : Form
{
public MainForm()
{
Program.ntrClient.Connected += connectCheck;
Program.ntrClient.InfoReady += getGame;
InitializeComponent();

ofd_Injection.Title = "Select an EKX/PKX file";
ofd_Injection.Filter = "Gen 7 pokémon files|*.pk7";
string path = @Application.StartupPath;
ofd_Injection.InitialDirectory = path;
}

public int pid = 0;

private void btn_Connect_Click(object sender, EventArgs e)
{
string szIp = tb_IP.Text;
string szPort = tb_Port.Text;

int iPort = Convert.ToInt32(szPort);

if (!Program.Connected)
{
Program.Connected = Program.scriptHelper.connect(szIp, iPort);
if (Program.Connected)
{
MessageBox.Show("Connection Successful!");
}
} else
Program.scriptHelper.connect(szIp, 8000);
}
else
{
MessageBox.Show("You are already connected!");
}


}

public void getGame(object sender, EventArgs e)
{
InfoReadyEventArgs args = (InfoReadyEventArgs) e;

string log = args.info;
if(log.Contains("niji_loc"))
{
string splitlog = log.Substring(log.IndexOf(", pname: niji_loc") - 8, log.Length - log.IndexOf(", pname: niji_loc"));
pid = Convert.ToInt32("0x" + splitlog.Substring(0, 8), 16);
Program.scriptHelper.write(0x3DFFD0, BitConverter.GetBytes(0xE3A01000), pid);
MessageBox.Show("Connection Successful!");
}
}

public void connectCheck(object sender, EventArgs e)
{

Program.scriptHelper.listprocess();
}

Thread workerThread = null;
Expand All @@ -44,17 +70,17 @@ private void btn_Start_Click(object sender, EventArgs e)
if (workerThread == null && workerObject == null)
{
workerObject = new Worker();
workerObject.setValues(tb_PokemonToFind.Text, tb_Default.Text, tb_Folder.Text, tb_PID.Text, cb_Spanish.Checked, (int) nud_Dex.Value, cmb_Gender.SelectedIndex, cmb_Levels.SelectedIndex);
workerObject.setValues(tb_PokemonToFind.Text, tb_Default.Text, tb_Folder.Text, pid, cb_Spanish.Checked, (int)nud_Dex.Value, cmb_Gender.SelectedIndex, cmb_Levels.SelectedIndex);
workerThread = new Thread(workerObject.DoWork);
workerThread.Start();
}
}
}

public void AppendListViewItem(string szTrainerName, string szNickname, string szCountry, string szSubCountry)
{
if (InvokeRequired)
{
this.Invoke(new Action<string, string, string, string>(AppendListViewItem), new object[] { szTrainerName, szNickname, szCountry, szSubCountry });
this.Invoke(new Action<string, string, string, string>(AppendListViewItem), new object[] { szTrainerName, szNickname, szCountry, szSubCountry });
return;
}
string[] row = { DateTime.Now.ToString("h:mm:ss"), szTrainerName, szNickname, szCountry, szSubCountry };
Expand All @@ -66,7 +92,7 @@ public void AppendListViewItem(string szTrainerName, string szNickname, string s

private void btn_Stop_Click(object sender, EventArgs e)
{
if(workerThread != null && workerObject != null)
if (workerThread != null && workerObject != null)
{
workerObject.RequestStop();
workerThread.Join();
Expand Down Expand Up @@ -116,8 +142,6 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["IP"].Value = tb_IP.Text;
config.AppSettings.Settings["Port"].Value = tb_Port.Text;
config.AppSettings.Settings["PID"].Value = tb_PID.Text;
config.AppSettings.Settings["Deposited"].Value = tb_PokemonToFind.Text;
config.AppSettings.Settings["Dex"].Value = nud_Dex.Value.ToString();
config.AppSettings.Settings["Level"].Value = cmb_Levels.SelectedIndex.ToString();
Expand All @@ -135,8 +159,6 @@ private void MainForm_Load(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
tb_IP.Text = config.AppSettings.Settings["IP"].Value;
tb_Port.Text = config.AppSettings.Settings["Port"].Value;
tb_PID.Text = config.AppSettings.Settings["PID"].Value;
tb_PokemonToFind.Text = config.AppSettings.Settings["Deposited"].Value;
nud_Dex.Value = Int32.Parse(config.AppSettings.Settings["Dex"].Value);
cmb_Levels.SelectedIndex = Int32.Parse(config.AppSettings.Settings["Level"].Value);
Expand All @@ -145,5 +167,75 @@ private void MainForm_Load(object sender, EventArgs e)
tb_Folder.Text = config.AppSettings.Settings["Folder"].Value;
cmb_Gender.SelectedIndex = Int32.Parse(config.AppSettings.Settings["Gender"].Value);
}

private void btn_BrowseInject_Click(object sender, EventArgs e)
{
if (ofd_Injection.ShowDialog() == DialogResult.OK)
{
txt_FileInjection.Text = ofd_Injection.FileName;
ofd_Injection.InitialDirectory = Path.GetDirectoryName(ofd_Injection.FileName);
}
}

public uint boxOff = 0x330D9838;

private void btn_Inject_Click(object sender, EventArgs e)
{
byte[] pkmEncrypted = System.IO.File.ReadAllBytes(txt_FileInjection.Text);
byte[] cloneshort = PKHeX.encryptArray(pkmEncrypted.Take(232).ToArray());
uint boxIndex = Decimal.ToUInt32((nud_BoxInjection.Value - 1) * 30 + nud_SlotInjection.Value - 1);
uint count = Decimal.ToUInt32(nud_CountInjection.Value);
if (boxIndex + count > 32 * 30)
{
uint newCount = 32 * 30 - boxIndex;
count = newCount;
}

byte[] dataToWrite = new byte[count * 232];
for (int i = 0; i < count; i++)
cloneshort.CopyTo(dataToWrite, i * 232);
uint offset = boxOff + boxIndex * 232;
Program.scriptHelper.write(offset, dataToWrite, pid);
MessageBox.Show("Injection Successful!");
}

private void btn_Disconnect_Click(object sender, EventArgs e)
{
if (Program.Connected)
{
Program.ntrClient.disconnect();

if (!Program.Connected)
{
MessageBox.Show("Disconnection Successful!");
}
}
else
{
MessageBox.Show("You are already disconnected!");
}
}

private void timer1_Tick(object sender, EventArgs e)
{
try
{
Program.ntrClient.sendHeartbeatPacket();
}
catch (Exception)
{

}
}

private void nud_BoxInjection_ValueChanged(object sender, EventArgs e)
{
nud_CountInjection.Maximum = 32 * 30 - (Decimal.ToUInt32((nud_BoxInjection.Value - 1) * 30 + nud_SlotInjection.Value - 1));
}

private void nud_SlotInjection_ValueChanged(object sender, EventArgs e)
{
nud_CountInjection.Maximum = 32 * 30 - (Decimal.ToUInt32((nud_BoxInjection.Value - 1) * 30 + nud_SlotInjection.Value - 1));
}
}
}
6 changes: 6 additions & 0 deletions Ledybot/Form1.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="ofd_Injection.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>139, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Expand Down
104 changes: 56 additions & 48 deletions Ledybot/NTRClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@

namespace Ledybot
{

public class InfoReadyEventArgs : EventArgs
{
public string info;

public InfoReadyEventArgs(string info_)
{
this.info = info_;
}
}

public class NTRClient
{
public String host;
Expand All @@ -26,6 +37,20 @@ public class NTRClient
public string retVal;
public bool retDone;

public event EventHandler<InfoReadyEventArgs> InfoReady;
public event EventHandler Connected;


protected virtual void OnInfoReady(InfoReadyEventArgs e)
{
InfoReady?.Invoke(this, e);
}

protected virtual void OnConnected(EventArgs e)
{
Connected?.Invoke(this, e);
}


int readNetworkStream(NetworkStream stream, byte[] buf, int length)
{
Expand Down Expand Up @@ -97,6 +122,7 @@ void packetRecvThreadStart()
byte[] dataBuf = new byte[dataLen];
readNetworkStream(stream, dataBuf, dataBuf.Length);
string logMsg = Encoding.UTF8.GetString(dataBuf);
OnInfoReady(new InfoReadyEventArgs(logMsg));
//Console.WriteLine(logMsg);
}
lock (syncLock)
Expand Down Expand Up @@ -136,38 +162,34 @@ void handleReadMem(UInt32 seq, byte[] dataBuf)
return;
}
lastReadMemSeq = 0;

int i = 0;
int iBufferlength = dataBuf.Length;
for (i = 0; i < dataBuf.Length; i++)
string szResult = "";
if (dataBuf.Length > 4)
{
if (i % 2 == 0)
int i = 0;
int iBufferlength = dataBuf.Length;
for (i = 0; i < dataBuf.Length; i++)
{
if (dataBuf[i] == 0x00)
if (i % 2 == 0)
{
iBufferlength = i;
break;
if (dataBuf[i] == 0x00)
{
iBufferlength = i;
break;
}
}
}
}
byte[] actualBuffer = new byte[iBufferlength];
for (i = 0; i < actualBuffer.Length; i++)
{
actualBuffer[i] = dataBuf[i];
}

string szResult = "";

if(dataBuf.Length <= 4)
{
Array.Reverse(actualBuffer);
szResult = BitConverter.ToString(actualBuffer).Replace("-", string.Empty);
byte[] actualBuffer = new byte[iBufferlength];
for (i = 0; i < actualBuffer.Length; i++)
{
actualBuffer[i] = dataBuf[i];
}
szResult = Encoding.Unicode.GetString(actualBuffer).Trim('\0');
}
else
{
szResult = Encoding.Unicode.GetString(actualBuffer).Trim('\0');
Array.Reverse(dataBuf);
szResult = BitConverter.ToString(dataBuf).Replace("-", string.Empty);
}


//t.BeginInvoke((MethodInvoker)delegate () { t.Text = szResult; ; });
lock (retValLock)
Expand Down Expand Up @@ -204,7 +226,7 @@ public void setServer(String serverHost, int serverPort)
port = serverPort;
}

public Boolean connectToServer()
public void connectToServer()
{
if (tcp != null)
{
Expand All @@ -214,26 +236,20 @@ public Boolean connectToServer()
tcp.NoDelay = true;
try
{
if (tcp.ConnectAsync(host, port).Wait(1000))
{
currentSeq = 0;
netStream = tcp.GetStream();
heartbeatSendable = 1;
packetRecvThread = new Thread(new ThreadStart(packetRecvThreadStart));
packetRecvThread.Start();
Program.Connected = true;
}
else
{
Program.Connected = false;
}
tcp.Connect(host, port);
currentSeq = 0;
netStream = tcp.GetStream();
heartbeatSendable = 1;
packetRecvThread = new Thread(new ThreadStart(packetRecvThreadStart));
packetRecvThread.Start();
OnConnected(null);
Program.Connected = true;
}
catch
{
MessageBox.Show("Error connecting to the 3DS. Please make sure you have the correct IP.", "Ledybot", MessageBoxButtons.OK, MessageBoxIcon.Error);
Program.Connected = false;
}

return Program.Connected;
}

public void disconnect(bool waitPacketThread = true)
Expand Down Expand Up @@ -302,7 +318,7 @@ public void sendWriteMemPacket(UInt32 addr, UInt32 pid, byte[] buf)

public void sendHeartbeatPacket()
{
if (Program.Connected)
if (tcp != null)
{
lock (syncLock)
{
Expand All @@ -311,14 +327,6 @@ public void sendHeartbeatPacket()
heartbeatSendable = 0;
sendPacket(0, 0, null, 0);
}
else
{
timeout++;
if (timeout == 5)
{
disconnect(false);
}
}
}
}

Expand Down
Loading

0 comments on commit 8634079

Please sign in to comment.