# Path: vendor/tcpgecko/tcpgecko.py
# def enum(**enums):
#     def __init__(self, *args):
#     def readmem(self, address, length): #Number of bytes
#     def readkern(self, address): #Only takes 4 bytes, may need to run multiple times
#     def writekern(self, address, value): #Only takes 4 bytes, may need to run multiple times
#     def pokemem(self, address, value): #Only takes 4 bytes, may need to run multiple times
#     def pokemem8(self, address, value):
#     def pokemem16(self, address, value):
#     def pokemem32(self, address, value):
#     def search32(self, address, value, size):
#     def getversion(self):
#     def writestr(self, address, string):
#     def memalign(self, size, align):
#     def freemem(self, address):
#     def memalloc(self, size, align, noprint=False):
#     def freealloc(self, address):
#     def createpath(self, path):
#     def createstr(self, string):
#     def FSInitClient(self):
#     def FSInitCmdBlock(self):
#     def FSOpenDir(self, path="/"):
#     def SAVEOpenDir(self, path="/", slot=255):
#     def FSReadDir(self):
#     def SAVEOpenFile(self, path="/", mode="r", slot=255):
#     def FSReadFile(self):
#     def get_symbol(self, rplname, symname, noprint=False, data=0):
#     def call(self, address, *args):
#     def function(self, rplname, symname, noprint=False, data=0, *args):
#     def validrange(self, address, length):
#     def validaccess(self, address, length, access):
#         def printflags(self, flags, data):
#         def printperms(self, perms):
# def hexstr0(data): #0xFFFFFFFF, uppercase hex string
#     def __init__(self, address, rpc=None, rplname=None, symname=None, noprint=False):
#     def __call__(self, *args):
# class TCPGecko:
#     class FileSystem: #TODO: Try to clean this up ????
# class ExportedSymbol(object):


from vendor.tcpgecko import tcpgecko
import time


class Memshark():
	def __init__(self, config):
		self.wii_u_ip = config.wii_u_ip
		self.tcp_gecko = None
		self.pokes = []

	def connect(self):
		if self.is_connected():
			self.tcp_gecko.s.close()
		try:
			self.tcp_gecko = tcpgecko.TCPGecko(self.wii_u_ip)
		except Exception:
			print("An error occurred when connecting to TCPGecko on the Wii U")
			print("Is TCPGecko not running on {}?".format(self.wii_u_ip))

	def disconnect(self):
		if self.is_connected():
			self.tcp_gecko.s.close()

	def poke(self, target):
		# print("--DEBUG Poking {}: {} -> {}".format(target.name, target.pretty_address, target.pretty_value))
		if not self.is_connected():
			return
		self.tcp_gecko.pokemem32(target.address,target.value)

	def is_connected(self):

# Path: vendor/tcpgecko/tcpgecko.py
# def enum(**enums):
#     def __init__(self, *args):
#     def readmem(self, address, length): #Number of bytes
#     def readkern(self, address): #Only takes 4 bytes, may need to run multiple times
#     def writekern(self, address, value): #Only takes 4 bytes, may need to run multiple times
#     def pokemem(self, address, value): #Only takes 4 bytes, may need to run multiple times
#     def pokemem8(self, address, value):
#     def pokemem16(self, address, value):
#     def pokemem32(self, address, value):
#     def search32(self, address, value, size):
#     def getversion(self):
#     def writestr(self, address, string):
#     def memalign(self, size, align):
#     def freemem(self, address):
#     def memalloc(self, size, align, noprint=False):
#     def freealloc(self, address):
#     def createpath(self, path):
#     def createstr(self, string):
#     def FSInitClient(self):
#     def FSInitCmdBlock(self):
#     def FSOpenDir(self, path="/"):
#     def SAVEOpenDir(self, path="/", slot=255):
#     def FSReadDir(self):
#     def SAVEOpenFile(self, path="/", mode="r", slot=255):
#     def FSReadFile(self):
#     def get_symbol(self, rplname, symname, noprint=False, data=0):
#     def call(self, address, *args):
#     def function(self, rplname, symname, noprint=False, data=0, *args):
#     def validrange(self, address, length):
#     def validaccess(self, address, length, access):
#         def printflags(self, flags, data):
#         def printperms(self, perms):
# def hexstr0(data): #0xFFFFFFFF, uppercase hex string
#     def __init__(self, address, rpc=None, rplname=None, symname=None, noprint=False):
#     def __call__(self, *args):
# class TCPGecko:
#     class FileSystem: #TODO: Try to clean this up ????
# class ExportedSymbol(object):


# Path: background/memshark.py
from vendor.tcpgecko import tcpgecko
import time



class Memshark():
	def __init__(self, config):
		self.wii_u_ip = config.wii_u_ip
		self.tcp_gecko = None
		self.pokes = []

	def connect(self):
		if self.is_connected():
			self.tcp_gecko.s.close()
		try:
			self.tcp_gecko = tcpgecko.TCPGecko(self.wii_u_ip)
		except Exception:
			print("An error occurred when connecting to TCPGecko on the Wii U")
			print("Is TCPGecko not running on {}?".format(self.wii_u_ip))

	def disconnect(self):
		if self.is_connected():
			self.tcp_gecko.s.close()

	def poke(self, target):
		# print("--DEBUG Poking {}: {} -> {}".format(target.name, target.pretty_address, target.pretty_value))
		if not self.is_connected():
			return
		self.tcp_gecko.pokemem32(target.address,target.value)

	def is_connected(self):

		return self.tcp_gecko != None