Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.60 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Net.Sockets;
  10. using System.Threading;
  11. using System.Net;
  12. using System.Windows.Forms;
  13.  
  14. namespace Ledybot
  15. {
  16. class GTSBot6
  17. {
  18.  
  19. //private System.IO.StreamWriter file = new StreamWriter(@"C:\Temp\ledylog.txt");
  20.  
  21. public enum gtsbotstates { botstart, startsearch, pressSeek, presssearch, findPokemon, trade, research, botexit, updatecomments, panic };
  22.  
  23. private TcpClient client = new TcpClient();
  24. private string consoleName = "Ledybot";
  25. private IPEndPoint serverEndPoint = null;
  26. private bool useLedySync = false;
  27.  
  28. private const int SEARCHDIRECTION_FROMBACK = 0;
  29. private const int SEARCHDIRECTION_FROMBACKFIRSTPAGEONLY = 1;
  30. private const int SEARCHDIRECTION_FROMFRONT = 2;
  31.  
  32. uint GTSPageSize;
  33. uint GTSPageIndex;
  34. uint GTSCurrentView;
  35.  
  36. uint GTSListBlock;
  37.  
  38. uint GTSBlockAll;
  39. uint GTSBlockStart;
  40. uint GTSBlockEnd;
  41.  
  42. uint GTSBlockEntrySize;
  43.  
  44. uint BoxInject;
  45.  
  46. uint BoxScreen;
  47.  
  48. uint PokemonToFind;
  49. uint PokemonToFindGender;
  50. uint PokemonToFindLevel;
  51.  
  52. private int iPokemonToFind = 0;
  53. private int iPokemonToFindGender = 0;
  54. private int iPokemonToFindLevel = 0;
  55. private int iPID = 0;
  56. private string szIP;
  57. private bool bBlacklist = false;
  58. private bool bReddit = false;
  59. private int searchDirection = 0;
  60. private string szFC = "";
  61. private string szPath;
  62. private byte[] principal = new byte[4];
  63.  
  64. public bool botstop = false;
  65.  
  66. public bool PokemonFound { get; private set; }
  67. public uint CurrentView { get; private set; }
  68. public uint PageIndex { get; private set; }
  69.  
  70. private int botState = 0;
  71. public int botresult = 0;
  72. Task<bool> waitTaskbool;
  73. private int commandtime = 250;
  74. private int delaytime = 150;
  75. private int o3dswaittime = 1000;
  76.  
  77. private int listlength = 0;
  78. private int startIndex = 100;
  79. private byte[] blockbytes = new byte[256];
  80. private byte[] block = new byte[256];
  81.  
  82. private int tradeIndex = -1;
  83. private uint addr_PageEntry = 0;
  84. private bool foundLastPage = false;
  85.  
  86. private Tuple<string, string, int, int, int, ArrayList> details;
  87. private short dex;
  88. private string szNickname;
  89. private string country;
  90. private string subregion;
  91. private int iStartIndex;
  92. private int iEndIndex;
  93. private int iDirection;
  94.  
  95. public string szTrainerName { get; private set; }
  96. public string Phrase { get; private set; }
  97. public string Message { get; private set; }
  98.  
  99. private async Task<bool> isCorrectWindow(int expectedScreen)
  100. {
  101. await Task.Delay(o3dswaittime);
  102. await Program.helper.waitNTRread(0x00);
  103. int screenID = (int)Program.helper.lastRead;
  104.  
  105. //file.WriteLine("Checkscreen: " + expectedScreen + " - " + screenID + " botstate:" + botState);
  106. //file.Flush();
  107. return expectedScreen == screenID;
  108. }
  109.  
  110. private Boolean canThisTrade(byte[] principal, string consoleName, string trainerName, string country, string region, string pokemon, string szFC, string page, string index)
  111. {
  112. NetworkStream clientStream = client.GetStream();
  113. byte[] buffer = new byte[4096];
  114. byte[] messageID = { 0x00 };
  115. string szmessage = consoleName + '\t' + trainerName + '\t' + country + '\t' + region + '\t' + pokemon + '\t' + page + "\t" + index + "\t";
  116. byte[] toSend = Encoding.UTF8.GetBytes(szmessage);
  117.  
  118. buffer = messageID.Concat(principal).Concat(toSend).ToArray();
  119. clientStream.Write(buffer, 0, buffer.Length);
  120. clientStream.Flush();
  121. byte[] message = new byte[4096];
  122. try
  123. {
  124. //blocks until a client sends a message
  125. int bytesRead = clientStream.Read(message, 0, 4096);
  126. if (message[0] == 0x02)
  127. {
  128. Program.f1.banlist.Add(szFC);
  129. }
  130. return message[0] == 0x01;
  131. }
  132. catch
  133. {
  134. return false;
  135. //a socket error has occured
  136. }
  137. }
  138.  
  139. public GTSBot6(int iP, int iPtF, int iPtFGender, int iPtFLevel, bool bBlacklist, bool bReddit, int iSearchDirection, string waittime, string consoleName, bool useLedySync, string ledySyncIp, string ledySyncPort, int game,string szIP)
  140. {
  141. this.iPokemonToFind = iPtF;
  142. this.iPokemonToFindGender = iPtFGender;
  143. this.iPokemonToFindLevel = iPtFLevel;
  144. this.iPID = iP;
  145. this.szIP = szIP;
  146. this.bBlacklist = bBlacklist;
  147. this.bReddit = bReddit;
  148. this.searchDirection = iSearchDirection;
  149. this.o3dswaittime = Int32.Parse(waittime);
  150. if (useLedySync)
  151. {
  152. this.useLedySync = useLedySync;
  153. int iPort = Int32.Parse(ledySyncPort);
  154. this.serverEndPoint = new IPEndPoint(IPAddress.Parse(ledySyncIp), iPort);
  155. client.Connect(serverEndPoint);
  156. }
  157. this.consoleName = consoleName;
  158.  
  159. if (game == 3) // Omega Rubin and Alpha Sapphire
  160. {
  161. GTSPageSize = 0x08C69D698;
  162. GTSPageIndex = 0x08C6945C;
  163. GTSCurrentView = 0x08C6D6AC;
  164.  
  165. GTSListBlock = 0x8C694F8;
  166. GTSBlockEntrySize = 0xA0;
  167.  
  168. BoxInject = 0x8C9E134;
  169.  
  170. BoxScreen = 0x1311B30;
  171.  
  172. PokemonToFind = 0x08335290;
  173. PokemonToFindLevel = 0x08335298;
  174. PokemonToFindGender = 0x08335294;
  175. }
  176. if (game == 4) // X and Y
  177. {
  178. GTSPageSize = 0x08C66080;
  179. GTSPageIndex = 0x08C61E40;
  180. GTSCurrentView = 0x08C66090;
  181.  
  182. GTSListBlock = 0x8C61EDC;
  183. GTSBlockEntrySize = 0xA0;
  184. //GTSBlockStart = 0x8C6656C;
  185. //GTSBlockEnd = 0x8C66568;
  186. //GTSBlockAll = 0x8C666A0;
  187.  
  188. BoxInject = 0x8C861C8;
  189.  
  190. PokemonToFind = 0x08334988;
  191. PokemonToFindLevel = 0x08334990;
  192. PokemonToFindGender = 0x0833498C;
  193. }
  194. }
  195.  
  196. public async Task<int> RunBot()
  197. {
  198. byte[] pokemonIndex = new byte[2];
  199. byte pokemonGender = 0x0;
  200. byte pokemonLevel = 0x0;
  201. byte[] full = BitConverter.GetBytes(iPokemonToFind);
  202. pokemonIndex[0] = full[0];
  203. pokemonIndex[1] = full[1];
  204. full = BitConverter.GetBytes(iPokemonToFindGender);
  205. pokemonGender = full[0];
  206. full = BitConverter.GetBytes(iPokemonToFindLevel);
  207. pokemonLevel = full[0];
  208. try
  209. {
  210. while (!botstop)
  211. {
  212. switch (botState)
  213. {
  214. case (int)gtsbotstates.botstart:
  215. if (bReddit)
  216. Program.f1.updateJSON();
  217.  
  218. botState = (int)gtsbotstates.pressSeek;
  219. break;
  220.  
  221. case (int)gtsbotstates.updatecomments:
  222. Program.f1.updateJSON();
  223. botState = (int)gtsbotstates.research;
  224. break;
  225.  
  226. case (int)gtsbotstates.pressSeek:
  227. await Program.helper.waitbutton(Program.PKTable.keyA);
  228. await Task.Delay(1000);
  229. botState = (int)gtsbotstates.startsearch;
  230. break;
  231.  
  232. case (int)gtsbotstates.startsearch:
  233. //Write wanted Pokemon, Level, Gender to Ram, won't Display it but works. ¯\_(ツ)_/¯
  234. Program.f1.ChangeStatus("Setting Pokemon to find");
  235. waitTaskbool = Program.helper.waitNTRwrite(PokemonToFind, pokemonIndex, iPID);
  236. waitTaskbool = Program.helper.waitNTRwrite(PokemonToFindGender, pokemonGender, iPID);
  237. waitTaskbool = Program.helper.waitNTRwrite(PokemonToFindLevel, pokemonLevel, iPID);
  238. botState = (int)gtsbotstates.presssearch;
  239. break;
  240.  
  241. case (int)gtsbotstates.presssearch:
  242. Program.f1.ChangeStatus("Pressing seek button");
  243. Program.helper.quicktouch(200, 180, commandtime);
  244.  
  245. if (searchDirection == SEARCHDIRECTION_FROMBACK)
  246. {
  247. //This is the only Solution i found so far to change the PageIndex, write Value while Loading the Frame.
  248. await Task.Delay(1000);
  249. await Program.helper.waitNTRwrite(GTSPageIndex, (uint)startIndex, iPID);
  250. }
  251.  
  252. await Task.Delay(4000);
  253. botState = (int)gtsbotstates.findPokemon;
  254. break;
  255.  
  256. case (int)gtsbotstates.findPokemon:
  257.  
  258. await Program.helper.waitNTRread(BoxScreen);
  259. if (Program.helper.lastRead.ToString() == "65794")
  260. {
  261. botState = (int)gtsbotstates.panic;
  262. break;
  263. }
  264.  
  265.  
  266.  
  267. await Program.helper.waitNTRread(GTSPageSize);
  268. uint Entries = (Program.helper.lastRead - 1);
  269. CurrentView = Entries;
  270.  
  271. if (searchDirection == SEARCHDIRECTION_FROMBACK)
  272. {
  273. // Change current Page, everytime + 100
  274. while (!foundLastPage)
  275. {
  276. startIndex += 100;
  277. await Program.helper.waitNTRwrite(GTSPageIndex, (uint)startIndex, iPID);
  278. Program.f1.ChangeStatus("Moving to last page");
  279. Program.helper.quickbuton(Program.PKTable.DpadLEFT, commandtime);
  280. await Task.Delay(commandtime + delaytime + 1000);
  281. Program.helper.quickbuton(Program.PKTable.DpadRIGHT, commandtime);
  282. await Task.Delay(commandtime + delaytime + 1000);
  283. await Program.helper.waitNTRread(GTSPageSize);
  284. Entries = (Program.helper.lastRead - 1);
  285.  
  286. if (Entries < 99 || startIndex >= 500) // Hardcoded to 5 Pages
  287. {
  288. foundLastPage = true;
  289. CurrentView = Entries;
  290. }
  291. }
  292. }
  293.  
  294. Program.f1.ChangeStatus("Looking for a Pokemon to Trade");
  295.  
  296. // Check the Trade Direction Back to Front or Front to Back
  297. if (searchDirection == SEARCHDIRECTION_FROMBACK || searchDirection == SEARCHDIRECTION_FROMBACKFIRSTPAGEONLY)
  298. {
  299. CurrentView = Entries;
  300. iStartIndex = (int)Entries;
  301. iEndIndex = 0;
  302. iDirection = -1;
  303. }
  304. else
  305. {
  306. CurrentView = 0;
  307. iStartIndex = 1;
  308. iEndIndex = (int)Entries + 1;
  309. iDirection = 1;
  310. }
  311.  
  312. //There's no Address for ALL Page Entries, so it takes alot longer to find a Pokemon. (2min from Entry 100-1)
  313.  
  314. for (int i = iStartIndex; i * iDirection < iEndIndex; i += iDirection)
  315. {
  316. //Get the Current Entry Data
  317. waitTaskbool = Program.helper.waitNTRread(GTSListBlock + (CurrentView * GTSBlockEntrySize), (uint)(256 * 100));
  318. if (await waitTaskbool)
  319. {
  320. block = Program.helper.lastArray;
  321. //Collect Data
  322. int gender = block[0x2];
  323. int level = block[0x3];
  324. // int dlevel = block[0x7];
  325. dex = BitConverter.ToInt16(block, 0x0);
  326. // int Item = BitConverter.ToInt16(block, 0x22);
  327. szNickname = Encoding.Unicode.GetString(block, 0x08, 24).Trim('\0');
  328. szTrainerName = Encoding.Unicode.GetString(block, 0x40, 24).Trim('\0');
  329. // Phrase = Encoding.Unicode.GetString(block, 0x5A, 30).Trim('\0');
  330. //int countryIndex = BitConverter.ToInt16(block, 0x48);
  331. country = "-"; // No valid Country Array found :/
  332. //Program.f1.countries.TryGetValue(countryIndex, out country);
  333. //Program.f1.getSubRegions(countryIndex);
  334. //int subRegionIndex = BitConverter.ToInt16(block, 0x236);
  335. subregion = "-"; // No valid Sub Region Array found :/
  336. //Program.f1.regions.TryGetValue(subRegionIndex, out subregion);
  337. Array.Copy(block, 0x3C, principal, 0, 4);
  338. byte check = Program.f1.calculateChecksum(principal);
  339. byte[] friendcode = new byte[8];
  340. Array.Copy(principal, 0, friendcode, 0, 4);
  341. friendcode[4] = check;
  342. long i_FC = BitConverter.ToInt64(friendcode, 0);
  343. szFC = i_FC.ToString().PadLeft(12, '0');
  344.  
  345. if (Program.f1.giveawayDetails.ContainsKey(dex))
  346. {
  347. Program.f1.giveawayDetails.TryGetValue(dex, out details);
  348.  
  349. if ((gender == 0 || gender == details.Item3) && (level == 0 || level == details.Item4))
  350. {
  351.  
  352. if (useLedySync && !Program.f1.banlist.Contains(szFC) && canThisTrade(principal, consoleName, szTrainerName, country, subregion, Program.PKTable.Species7[dex - 1], szFC, PageIndex + "", (i - 1) + ""))
  353. {
  354. //Check if Pokemon is from Gen7
  355. szPath = details.Item1;
  356. PKHeX dump = new PKHeX();
  357. dump.Data = PKHeX.decryptArray(System.IO.File.ReadAllBytes(szPath));
  358. if (dex < 721 || dump.Version < 29)
  359. {
  360. PokemonFound = true;
  361. Program.f1.ChangeStatus("Found a pokemon to trade");
  362. botState = (int)gtsbotstates.trade;
  363. break;
  364. }
  365. else
  366. {
  367. Program.f1.ChangeStatus("Looking for a Pokemon to Trade, Current Entry: " + (CurrentView) + "/" + (Entries));
  368. CurrentView--;
  369. }
  370. }
  371. else if (!useLedySync)
  372. {
  373. if ((!bReddit || Program.f1.commented.Contains(szFC)) && !details.Item6.Contains(BitConverter.ToInt32(principal, 0)) && !Program.f1.banlist.Contains(szFC))
  374. {
  375. szPath = details.Item1;
  376. //Check if Pokemon is from Gen7
  377. szPath = details.Item1;
  378. PKHeX dump = new PKHeX();
  379. dump.Data = PKHeX.decryptArray(System.IO.File.ReadAllBytes(szPath));
  380. if (dex < 721 || dump.Version < 29)
  381. {
  382. PokemonFound = true;
  383. botState = (int)gtsbotstates.trade;
  384. break;
  385. }
  386. else
  387. {
  388. Program.f1.ChangeStatus("Looking for a Pokemon to Trade, Current Entry: " + (CurrentView) + "/" + (Entries));
  389. CurrentView--;
  390. }
  391. }
  392. }
  393. }
  394. {
  395. Program.f1.ChangeStatus("Looking for a Pokemon to Trade, Current Entry: " + (CurrentView) + "/" + (Entries));
  396. CurrentView--;
  397. }
  398. }
  399. else
  400. {
  401. Program.f1.ChangeStatus("Looking for a Pokemon to Trade, Current Entry: " + (CurrentView) + "/" + (Entries));
  402. CurrentView--;
  403. }
  404. }
  405.  
  406. }
  407. // No Pokemon found, return to Seek/Deposit Screen
  408. if (!PokemonFound)
  409. {
  410. Program.f1.ChangeStatus("No Pokemon Found");
  411. botState = (int)gtsbotstates.research;
  412. break;
  413. }
  414. break;
  415.  
  416. case (int)gtsbotstates.trade:
  417. // Trade Process
  418. Program.f1.ChangeStatus("Found a pokemon to trade");
  419. //Inject Pokemon to Box1 Slot1
  420. byte[] pkmEncrypted = System.IO.File.ReadAllBytes(szPath);
  421. byte[] cloneshort = PKHeX.encryptArray(pkmEncrypted.Take(232).ToArray());
  422. string ek7 = BitConverter.ToString(cloneshort).Replace("-", ", 0x");
  423. Program.scriptHelper.write(BoxInject, cloneshort, iPID);
  424.  
  425. await Program.helper.waitNTRread(GTSPageIndex);
  426. PageIndex = (Program.helper.lastRead + 1);
  427. Program.f1.AppendListViewItem(szTrainerName, szNickname, country, subregion, Program.PKTable.Species6[dex - 1], szFC, (PageIndex / 100).ToString(), CurrentView.ToString());
  428.  
  429. //Ghetto Solution \o/
  430. //Enter current viewed Entry, write wanted current view to RAM, quit current viewed Entry
  431. Program.helper.quickbuton(Program.PKTable.keyA, 200);
  432. await Task.Delay(2200);
  433. await Program.helper.waitNTRwrite(GTSCurrentView, (uint)CurrentView, iPID);
  434. Program.helper.quickbuton(Program.PKTable.keyB, 200);
  435. await Task.Delay(500);
  436. Program.helper.quickbuton(Program.PKTable.keyB, 200);
  437. await Task.Delay(2000);
  438. Program.helper.quickbuton(Program.PKTable.keyA, 200);
  439. await Task.Delay(3000);
  440.  
  441. //Now we have the right Entry, enter current viewed Entry
  442. Program.helper.quickbuton(Program.PKTable.keyA, 200);
  443. await Task.Delay(500);
  444. Program.helper.quickbuton(Program.PKTable.keyA, 200);
  445. await Task.Delay(500);
  446. Program.helper.quickbuton(Program.PKTable.keyA, 200);
  447. Program.f1.ChangeStatus("Trading pokemon on page " + (PageIndex / 100).ToString() + " index " + CurrentView.ToString() + "");
  448. await Task.Delay(10000);
  449.  
  450. //In Case the Pokemon is already traded, go back to Seek/Deposit Screen
  451. Program.helper.quickbuton(Program.PKTable.keyB, 250);
  452. await Task.Delay(1000);
  453. Program.helper.quickbuton(Program.PKTable.keyB, 250);
  454. await Task.Delay(1000);
  455. // wait if trade is finished
  456. await Task.Delay(35000);
  457. PokemonFound = true;
  458. startIndex = 100;
  459. botState = (int)gtsbotstates.botstart;
  460. break;
  461.  
  462. case (int)gtsbotstates.research:
  463. Program.helper.quickbuton(Program.PKTable.keyB, 250);
  464. await Task.Delay(3000);
  465. Program.helper.quickbuton(Program.PKTable.keyB, 250);
  466. await Task.Delay(3000);
  467. botState = (int)gtsbotstates.pressSeek;
  468. break;
  469.  
  470. case (int)gtsbotstates.botexit:
  471. Program.f1.ChangeStatus("Stopped");
  472. botstop = true;
  473. break;
  474. case (int)gtsbotstates.panic:
  475. if (!Program.Connected)
  476. {
  477. Program.scriptHelper.connect(szIP, 8000);
  478. }
  479. Program.f1.ChangeStatus("Recovery Mode");
  480.  
  481. //In case of a Communication Error
  482. Program.helper.quickbuton(Program.PKTable.keyA, 250);
  483. await Task.Delay(5000);
  484.  
  485. // Spam B to get out of GTS
  486. for (int i = 0; i < 15; i++)
  487. {
  488. Program.helper.quickbuton(Program.PKTable.keyB, commandtime + 200);
  489. await Task.Delay(2000);
  490. }
  491.  
  492.  
  493. Program.helper.quicktouch(170, 2, 200);
  494. Program.helper.quicktouch(100, 50, 200);
  495. Program.helper.quickbuton(Program.PKTable.keyA, 250);
  496. Program.helper.quickbuton(Program.PKTable.keyA, 250);
  497. Program.helper.quickbuton(Program.PKTable.keyA, 250);
  498. await Task.Delay(10000);
  499. botState = (int)gtsbotstates.botstart;
  500. break;
  501.  
  502. default:
  503. botresult = -1;
  504. botstop = true;
  505. break;
  506. }
  507. }
  508. }
  509. catch
  510. {
  511. botState = (int)gtsbotstates.panic;
  512. }
  513. if (this.serverEndPoint != null)
  514. {
  515. client.Close();
  516. }
  517. return botresult;
  518. }
  519.  
  520. public void RequestStop()
  521. {
  522. botstop = true;
  523. }
  524.  
  525.  
  526. }
  527. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement