1+ #Simple example on how to connect to and send commands to the sys module.
2+ #The example is for Pokemon Sword/Shield, it reads a .ek8 file from a certain file path, injects it into box1slot1
3+ #and starts a surprise trade with the given pokemon. It waits a certain amount of time (hoping the trade has completed)
4+ #before retrieving the new pokemon. Finally it extracts the pokemons .ek8 data from the game and saves it to the hard drive.
5+ #The script assumes the game is set up in a way that the character is not currently in any menus and that the cursor of the
6+ #pokebox is on box1slot1.
7+
8+ #The script isn't exactly robust, there are many ways to make it better (for example one could compare the box1slot1 data in
9+ #RAM with that of the pokemon sent to see if a trade has been found and if not back out of the menu to search for another 10
10+ #seconds or so instead of waiting a fixed 45 seconds), but it is rather meant as a showcase of the functionalites of the
11+ #sysmodule anyway.
12+
13+ #Commands:
14+ #make sure to append \r\n to the end of the command string or the switch args parser might not work
15+ #responses end with a \n (only poke has a response atm)
16+
17+ #click A/B/X/Y/LSTICK/RSTICK/L/R/ZL/ZR/PLUS/MINUS/DLEFT/DUP/DDOWN/DRIGHT/HOME/CAPTURE
18+ #press A/B/X/Y/LSTICK/RSTICK/L/R/ZL/ZR/PLUS/MINUS/DLEFT/DUP/DDOWN/DRIGHT/HOME/CAPTURE
19+ #release A/B/X/Y/LSTICK/RSTICK/L/R/ZL/ZR/PLUS/MINUS/DLEFT/DUP/DDOWN/DRIGHT/HOME/CAPTURE
20+
21+ #peek <address in hex, prefaced by 0x> <amount of bytes, dec or hex with 0x>
22+ #poke <address in hex, prefaced by 0x> <data, if in hex prefaced with 0x>
23+
24+ #setStick LEFT/RIGHT <xVal from -0x8000 to 0x7FFF> <yVal from -0x8000 to 0x7FFF
25+
26+
27+ import socket
28+ import time
29+ import binascii
30+
31+
32+ def sendCommand (s , content ):
33+ content += '\r \n ' #important for the parser on the switch side
34+ s .sendall (content .encode ())
35+
36+ s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
37+ s .connect (("192.168.178.25" , 6000 ))
38+
39+ fileIn = open ("C:/temp/toInject.ek8" , "rb" )
40+ pokemonToInject = fileIn .read (344 )
41+ pokemonToInject = str (binascii .hexlify (pokemonToInject ), "utf-8" )
42+
43+ time .sleep (2 )
44+ while True :
45+ sendCommand (s , f"poke 0x4293D8B0 0x{ pokemonToInject } " ) #read pokemon from file and inject it into box1slot1 for trade
46+
47+
48+ sendCommand (s , "click Y" )
49+ time .sleep (1 )
50+ sendCommand (s , "click DDOWN" )
51+ time .sleep (0.5 )
52+ sendCommand (s , "click A" )
53+ time .sleep (4 )
54+ sendCommand (s , "click A" )
55+ time .sleep (0.7 )
56+ sendCommand (s , "click A" )
57+ time .sleep (8 )
58+
59+ sendCommand (s , "click A" )
60+ time .sleep (0.7 )
61+ sendCommand (s , "click A" )
62+ time .sleep (0.7 )
63+ sendCommand (s , "click A" )
64+ time .sleep (0.7 )
65+
66+ time .sleep (45 ) #Time we wait for a trade
67+ sendCommand (s , "click Y" )
68+ time .sleep (0.7 )
69+ time .sleep (30 ) #probably needs to be longer for trade evolutions
70+
71+ sendCommand (s , "peek 0x4293D8B0 344" ) #get pokemon from box1slot1
72+ time .sleep (0.5 ) #give time to answer
73+ pokemonBytes = s .recv (689 )
74+ pokemonBytes = pokemonBytes [0 :- 1 ] #cut off \n at the end
75+ fileOut = open ("C:/temp/lastReceived.ek8" , "wb" )
76+ fileOut .write (binascii .unhexlify (pokemonBytes ))
77+ fileOut .close ()
0 commit comments