# Path: src/zugbruecke/core/const.py
# GROUP_VOID = 1
# 
# Path: src/zugbruecke/core/data/memory.py
# def generate_pointer_from_bytes(in_bytes):
# 
# 	return ctypes.cast(ctypes.pointer((ctypes.c_ubyte * len(in_bytes)).from_buffer_copy(in_bytes)), ctypes.c_void_p)
# 
# def is_null_pointer(ctypes_pointer):
# 
# 	try:
# 		return ctypes.cast(ctypes_pointer, ctypes.c_void_p).value is None
# 	except ctypes.ArgumentError: # catch non-pointer arguments
# 		return False
# 
# def overwrite_pointer_with_bytes(ctypes_pointer, in_bytes):
# 
# 	ctypes.memmove(ctypes_pointer, ctypes.pointer((ctypes.c_ubyte * len(in_bytes)).from_buffer_copy(in_bytes)), len(in_bytes))
# 
# def serialize_pointer_into_bytes(ctypes_pointer, size_bytes):
# 
# 	return bytes(ctypes.cast(ctypes_pointer, ctypes.POINTER(ctypes.c_ubyte * size_bytes)).contents)


import ctypes
from pprint import pformat as pf
from ..const import GROUP_VOID
from .memory import (
	generate_pointer_from_bytes,
	is_null_pointer,
	overwrite_pointer_with_bytes,
	serialize_pointer_into_bytes
	)
		if memsync_d['p'][-1] == -1:
			pointer = ctypes.pointer(ctypes.c_void_p())
			path_shift = 1 # cut off 1 element from path
		else:
			pointer = ctypes.c_void_p()
			path_shift = 0

		# Search for pointer in passed arguments
		pointer_arg = self.__get_argument_by_memsync_path__(memsync_d['p'][:(-1 - path_shift)], args_tuple)

		# If we're in the top level arguments or an array ...
		if isinstance(memsync_d['p'][-1 - path_shift], int):
			# Handle deepest instance (exchange element in list/tuple) HACK
			pointer_arg[memsync_d['p'][-1 - path_shift]] = pointer
		# If we're at a field of a struct
		else:
			# Handle deepest instance
			setattr(pointer_arg.contents, memsync_d['p'][-1 - path_shift], pointer)


	def __unpack_memory_item_overwrite__(self, memory_d, memsync_d, args_tuple):

		# Swap local and remote memory addresses
		self.__swap_memory_addresses__(memory_d)

		# Adjust Unicode wchar length
		if memsync_d['w']:
			self.__adjust_wchar_length__(memory_d)

		# Overwrite the local pointers with new data

# Path: src/zugbruecke/core/const.py
# GROUP_VOID = 1
# 
# Path: src/zugbruecke/core/data/memory.py
# def generate_pointer_from_bytes(in_bytes):
# 
# 	return ctypes.cast(ctypes.pointer((ctypes.c_ubyte * len(in_bytes)).from_buffer_copy(in_bytes)), ctypes.c_void_p)
# 
# def is_null_pointer(ctypes_pointer):
# 
# 	try:
# 		return ctypes.cast(ctypes_pointer, ctypes.c_void_p).value is None
# 	except ctypes.ArgumentError: # catch non-pointer arguments
# 		return False
# 
# def overwrite_pointer_with_bytes(ctypes_pointer, in_bytes):
# 
# 	ctypes.memmove(ctypes_pointer, ctypes.pointer((ctypes.c_ubyte * len(in_bytes)).from_buffer_copy(in_bytes)), len(in_bytes))
# 
# def serialize_pointer_into_bytes(ctypes_pointer, size_bytes):
# 
# 	return bytes(ctypes.cast(ctypes_pointer, ctypes.POINTER(ctypes.c_ubyte * size_bytes)).contents)


# Path: src/zugbruecke/core/data/mem_contents.py
import ctypes
from pprint import pformat as pf
from ..const import GROUP_VOID
from .memory import (
	generate_pointer_from_bytes,
	is_null_pointer,
	overwrite_pointer_with_bytes,
	serialize_pointer_into_bytes
	)

		if memsync_d['p'][-1] == -1:
			pointer = ctypes.pointer(ctypes.c_void_p())
			path_shift = 1 # cut off 1 element from path
		else:
			pointer = ctypes.c_void_p()
			path_shift = 0

		# Search for pointer in passed arguments
		pointer_arg = self.__get_argument_by_memsync_path__(memsync_d['p'][:(-1 - path_shift)], args_tuple)

		# If we're in the top level arguments or an array ...
		if isinstance(memsync_d['p'][-1 - path_shift], int):
			# Handle deepest instance (exchange element in list/tuple) HACK
			pointer_arg[memsync_d['p'][-1 - path_shift]] = pointer
		# If we're at a field of a struct
		else:
			# Handle deepest instance
			setattr(pointer_arg.contents, memsync_d['p'][-1 - path_shift], pointer)


	def __unpack_memory_item_overwrite__(self, memory_d, memsync_d, args_tuple):

		# Swap local and remote memory addresses
		self.__swap_memory_addresses__(memory_d)

		# Adjust Unicode wchar length
		if memsync_d['w']:
			self.__adjust_wchar_length__(memory_d)

		# Overwrite the local pointers with new data

		overwrite_pointer_with_bytes(