From 50af9e2d7e3bb704ba545bd7a5195e18fefbfa45 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 9 Mar 2011 12:27:07 -0600 Subject: [PATCH] updated to use the pointer caching callbacks --- library/python/pydfhack/creatures.py | 13 +++++- library/python/pydfhack/maps.py | 51 ++++++++++++++++++-- library/python/pydfhack/materials.py | 69 ++++++++++++++++++++++++---- library/python/pydfhack/util.py | 64 +++++++++++++++++--------- 4 files changed, 159 insertions(+), 38 deletions(-) diff --git a/library/python/pydfhack/creatures.py b/library/python/pydfhack/creatures.py index 06bc52c36..b4a51a4b5 100644 --- a/library/python/pydfhack/creatures.py +++ b/library/python/pydfhack/creatures.py @@ -4,6 +4,10 @@ import util libdfhack.Creatures_WriteLabors.argtypes = [ c_void_p, c_uint, POINTER(c_ubyte) ] +libdfhack.Creatures_ReadJob.restype = POINTER(Material) +libdfhack.Creatures_ReadInventoryIdx.restype = POINTER(c_uint) +libdfhack.Creatures_ReadInventoryPtr.restype = POINTER(c_uint) + class Creatures(object): def __init__(self, ptr): print ptr @@ -45,7 +49,14 @@ class Creatures(object): return libdfhack.Creatures_WriteLabors(self._c_ptr, c_uint(index), labors) > 0 def read_job(self, creature): - return libdfhack.Creatures_ReadJob(self._c_ptr, byref(creature)) + job_ptr = libdfhack.Creatures_ReadJob(self._c_ptr, byref(creature)) + jobs = None + + if id(job_ptr) in dftypes.pointer_dict: + jobs = dftypes.pointer_dict[id(job_ptr)][1] + del dftypes.pointer_dict[id(job_ptr)] + + return jobs @property def dwarf_race_index(self): diff --git a/library/python/pydfhack/maps.py b/library/python/pydfhack/maps.py index ea2175b6a..8fa92abe9 100644 --- a/library/python/pydfhack/maps.py +++ b/library/python/pydfhack/maps.py @@ -23,6 +23,12 @@ libdfhack.Maps_ReadSpatterVeins.argtypes = _default_argtypes libdfhack.Maps_ReadGrassVeins.argtypes = _default_argtypes libdfhack.Maps_ReadWorldConstructions.argtypes = _default_argtypes +libdfhack.Maps_ReadStandardVeins.restype = POINTER(Vein) +libdfhack.Maps_ReadFrozenVeins.restype = POINTER(FrozenLiquidVein) +libdfhack.Maps_ReadSpatterVeins.restype = POINTER(SpatterVein) +libdfhack.Maps_ReadGrassVeins.restype = POINTER(GrassVein) +libdfhack.Maps_ReadWorldConstructions.restype = POINTER(WorldConstruction) + class Maps(object): def __init__(self, ptr): self._map_ptr = ptr @@ -162,27 +168,62 @@ class Maps(object): def read_veins(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - return libdfhack.Maps_ReadStandardVeins(self._map_ptr, ux, uy, uz) + veins_ptr = libdfhack.Maps_ReadStandardVeins(self._map_ptr, ux, uy, uz) + veins = None + + if id(veins_ptr) in dftypes.pointer_dict: + veins = dftypes.pointer_dict[id(veins_ptr)][1] + del dftypes.pointer_dict[id(veins_ptr)] + + return veins def read_frozen_veins(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - return libdfhack.Maps_ReadFrozenVeins(self._map_ptr, ux, uy, uz) + veins_ptr = libdfhack.Maps_ReadFrozenVeins(self._map_ptr, ux, uy, uz) + veins = None + + if id(veins_ptr) in dftypes.pointer_dict: + veins = dftypes.pointer_dict[id(veins_ptr)][1] + del dftypes.pointer_dict[id(veins_ptr)] + + return veins def read_spatter_veins(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - return libdfhack.Maps_ReadSpatterVeins(self._map_ptr, ux, uy, uz) + veins_ptr = libdfhack.Maps_ReadSpatterVeins(self._map_ptr, ux, uy, uz) + veins = None + + if id(veins_ptr) in dftypes.pointer_dict: + veins = dftypes.pointer_dict[id(veins_ptr)][1] + del dftypes.pointer_dict[id(veins_ptr)] + + return veins def read_grass_veins(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - return libdfhack.Maps_ReadGrassVeins(self._map_ptr, ux, uy, uz) + veins_ptr = libdfhack.Maps_ReadGrassVeins(self._map_ptr, ux, uy, uz) + veins = None + + if id(veins_ptr) in dftypes.pointer_dict: + veins = dftypes.pointer_dict[id(veins_ptr)][1] + del dftypes.pointer_dict[id(veins_ptr)] + + return veins def read_world_constructions(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - return libdfhack.Maps_ReadWorldConstructions(self._map_ptr, ux, uy, uz) + veins_ptr = libdfhack.Maps_ReadWorldConstructions(self._map_ptr, ux, uy, uz) + veins = None + + if id(veins_ptr) in dftypes.pointer_dict: + veins = dftypes.pointer_dict[id(veins_ptr)][1] + del dftypes.pointer_dict[id(veins_ptr)] + + return veins @property def size(self): diff --git a/library/python/pydfhack/materials.py b/library/python/pydfhack/materials.py index 4a135f9a1..466bb68ae 100644 --- a/library/python/pydfhack/materials.py +++ b/library/python/pydfhack/materials.py @@ -1,6 +1,16 @@ from ctypes import * -from dftypes import libdfhack -from util import * +import dftypes +from dftypes import libdfhack, Matgloss, CreatureType, DescriptorColor, MatglossOther + +libdfhack.Materials_getInorganic.restype = POINTER(Matgloss) +libdfhack.Materials_getOrganic.restype = POINTER(Matgloss) +libdfhack.Materials_getTree.restype = POINTER(Matgloss) +libdfhack.Materials_getPlant.restype = POINTER(Matgloss) +libdfhack.Materials_getRace.restype = POINTER(Matgloss) +libdfhack.Materials_getRaceEx.restype = POINTER(CreatureType) +libdfhack.Materials_getColor.restype = POINTER(DescriptorColor) +libdfhack.Materials_getOther.restype = POINTER(MatglossOther) +libdfhack.Materials_getAllDesc.restype = POINTER(Matgloss) class Materials(object): def __init__(self, ptr): @@ -16,21 +26,60 @@ class Materials(object): self.other = None def _get_inorganic(self): - self.inorganic = libdfhack.Materials_getInorganic(self._mat_ptr) + inorganic = libdfhack.Materials_getInorganic(self._mat_ptr) + + if id(inorganic) in dftypes.pointer_dict: + self.inorganic = dftypes.pointer_dict[id(inorganic)][1] + del dftypes.pointer_dict[id(inorganic)] + def _get_organic(self): - self.organic = libdfhack.Materials_getOrganic(self._mat_ptr) + organic = libdfhack.Materials_getOrganic(self._mat_ptr) + + if id(organic) in dftypes.pointer_dict: + self.organic = dftypes.pointer_dict[id(organic)][1] + del dftypes.pointer_dict[id(inorganic)] + def _get_tree(self): - self.tree = libdfhack.Materials_getTree(self._mat_ptr) + tree = libdfhack.Materials_getTree(self._mat_ptr) + + if id(tree) in dftypes.pointer_dict: + self.tree = dftypes.pointer_dict[id(tree)][1] + del dftypes.pointer_dict[id(tree)] + def _get_plant(self): - self.plant = libdfhack.Materials_getPlant(self._mat_ptr) + plant = libdfhack.Materials_getPlant(self._mat_ptr) + + if id(plant) in dftypes.pointer_dict: + self.tree = dftypes.pointer_dict[id(tree)][1] + del dftypes.pointer_dict[id(tree)] + def _get_race(self): - self.race = libdfhack.Materials_getRace(self._mat_ptr) + race = libdfhack.Materials_getRace(self._mat_ptr) + + if id(race) in dftypes.pointer_dict: + self.race = dftypes.pointer_dict[id(race)][1] + del dftypes.pointer_dict[id(race)] + def _get_race_ex(self): - self.race_ex = libdfhack.Materials_getRaceEx(self._mat_ptr) + race_ex = libdfhack.Materials_getRaceEx(self._mat_ptr) + + if id(race_ex) in dftypes.pointer_dict: + self.race_ex = dftypes.pointer_dict[id(race_ex)][1] + del dftypes.pointer_dict[id(race_ex)] + def _get_color(self): - self.color = libdfhack.Materials_getColor(self._mat_ptr) + color = libdfhack.Materials_getColor(self._mat_ptr) + + if id(color) in dftypes.pointer_dict: + self.color = dftypes.pointer_dict[id(color)][1] + del dftypes.pointer_dict[id(color)] + def _get_other(self): - self.other = libdfhack.Materials_getOther(self._mat_ptr) + other = libdfhack.Materials_getOther(self._mat_ptr) + + if id(other) in dftypes.pointer_dict: + self.other = dftypes.pointer_dict[id(other)][1] + del dftypes.pointer_dict[id(other)] def _get_all(self): self._get_inorganic() diff --git a/library/python/pydfhack/util.py b/library/python/pydfhack/util.py index d0849b1d8..38365cbf5 100644 --- a/library/python/pydfhack/util.py +++ b/library/python/pydfhack/util.py @@ -1,7 +1,15 @@ from ctypes import * -uint_ptr = POINTER(c_uint) int_ptr = POINTER(c_int) +uint_ptr = POINTER(c_uint) + +short_ptr = POINTER(c_short) +ushort_ptr = POINTER(c_ushort) + +byte_ptr = POINTER(c_byte) +ubyte_ptr = POINTER(c_ubyte) + +pointer_dict = {} def _uintify(x, y, z): return (c_uint(x), c_uint(y), c_uint(z)) @@ -11,80 +19,92 @@ def _allocate_array(t_type, count): arr = arr_type() - ptr = c_void_p() - ptr = addressof(arr) - - return (arr, ptr) + return arr def _alloc_int_buffer(ptr, count): a = _allocate_array(c_int, count) + + p = cast(a, int_ptr) - ptr = addressof(a[0]) + ptr[0] = p + + pointer_dict[id(ptr[0])] = (ptr, a, p) return 1 -_int_functype = CFUNCTYPE(c_int, POINTER(c_int), c_uint) +_int_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_int)), c_uint) alloc_int_buffer = _int_functype(_alloc_int_buffer) def _alloc_uint_buffer(ptr, count): a = _allocate_array(c_uint, count) - ptr = addressof(a[0]) + ptr[0] = cast(a, uint_ptr) + + pointer_dict[id(ptr[0])] = (ptr, a, p) return 1 -_uint_functype = CFUNCTYPE(c_int, POINTER(c_uint), c_uint) +_uint_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_uint)), c_uint) alloc_uint_buffer = _uint_functype(_alloc_uint_buffer) def _alloc_short_buffer(ptr, count): a = _allocate_array(c_short, count) - ptr = addressof(a[0]) + ptr[0] = cast(a, short_ptr) + + pointer_dict[id(ptr[0])] = (ptr, a, p) return 1 -_short_functype = CFUNCTYPE(c_int, POINTER(c_short), c_uint) +_short_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_short)), c_uint) alloc_short_buffer = _short_functype(_alloc_short_buffer) def _alloc_ushort_buffer(ptr, count): a = _allocate_array(c_ushort, count) - ptr = addressof(a[0]) + ptr[0] = cast(a, ushort_ptr) + + pointer_dict[id(ptr[0])] = (ptr, a, p) return 1 -_ushort_functype = CFUNCTYPE(c_int, POINTER(c_ushort), c_uint) +_ushort_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_ushort)), c_uint) alloc_ushort_buffer = _ushort_functype(_alloc_ushort_buffer) def _alloc_byte_buffer(ptr, count): a = _allocate_array(c_byte, count) - ptr = addressof(a[0]) + ptr[0] = cast(a, byte_ptr) + + pointer_dict[id(ptr[0])] = (ptr, a, p) return 1 -_byte_functype = CFUNCTYPE(c_int, POINTER(c_byte), c_uint) +_byte_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_byte)), c_uint) alloc_byte_buffer = _byte_functype(_alloc_byte_buffer) def _alloc_ubyte_buffer(ptr, count): a = _allocate_array(c_ubyte, count) - ptr = addressof(a[0]) + ptr[0] = cast(a, ubyte_ptr) + + pointer_dict[id(ptr[0])] = (ptr, a, p) return 1 -_ubyte_functype = CFUNCTYPE(c_int, POINTER(c_ubyte), c_uint) +_ubyte_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_ubyte)), c_uint) alloc_ubyte_buffer = _ubyte_functype(_alloc_ubyte_buffer) def _alloc_char_buffer(ptr, count): c = create_string_buffer(count) + + p = cast(c, POINTER(c_char)) - if ptr is None: - ptr = c_void_p - - ptr = addressof(c) + ptr[0] = p + + pointer_dict[id(ptr[0])] = (ptr, c, p) return 1 -_char_functype = CFUNCTYPE(c_int, POINTER(c_char), c_uint) +_char_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_char)), c_uint) alloc_char_buffer = _char_functype(_alloc_char_buffer)